2016-09-14 22:30:46 +00:00
|
|
|
#!/usr/bin/env python
|
|
|
|
|
|
|
|
# (c) 2016 Red Hat, Inc.
|
|
|
|
#
|
|
|
|
# This file is part of Ansible
|
|
|
|
#
|
|
|
|
# Ansible is free software: you can redistribute it and/or modify
|
|
|
|
# it under the terms of the GNU General Public License as published by
|
|
|
|
# the Free Software Foundation, either version 3 of the License, or
|
|
|
|
# (at your option) any later version.
|
|
|
|
#
|
|
|
|
# Ansible is distributed in the hope that it will be useful,
|
|
|
|
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
# GNU General Public License for more details.
|
|
|
|
#
|
|
|
|
# You should have received a copy of the GNU General Public License
|
|
|
|
# along with Ansible. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
|
|
|
|
from __future__ import print_function
|
|
|
|
|
|
|
|
import os
|
|
|
|
import requests
|
|
|
|
|
|
|
|
from argparse import ArgumentParser
|
|
|
|
|
|
|
|
|
|
|
|
def main():
|
|
|
|
api_key = get_api_key()
|
|
|
|
|
|
|
|
parser = ArgumentParser(description='Download logs from all jobs in a Shippable run.')
|
|
|
|
|
|
|
|
parser.add_argument('run_id',
|
|
|
|
help='shippable run id.')
|
|
|
|
|
|
|
|
parser.add_argument('-v', '--verbose',
|
|
|
|
dest='verbose',
|
|
|
|
action='store_true',
|
|
|
|
help='show what is being downloaded')
|
|
|
|
|
|
|
|
parser.add_argument('-t', '--test',
|
|
|
|
dest='test',
|
|
|
|
action='store_true',
|
|
|
|
help='show what would be downloaded without downloading')
|
|
|
|
|
|
|
|
parser.add_argument('--key',
|
|
|
|
dest='api_key',
|
|
|
|
default=api_key,
|
|
|
|
required=api_key is None,
|
|
|
|
help='api key for accessing Shippable')
|
|
|
|
|
|
|
|
args = parser.parse_args()
|
|
|
|
|
|
|
|
headers = dict(
|
|
|
|
Authorization='apiToken %s' % args.api_key,
|
|
|
|
)
|
|
|
|
|
2016-11-07 17:28:13 +00:00
|
|
|
response = requests.get('https://api.shippable.com/jobs?runIds=%s' % args.run_id, headers=headers)
|
2016-09-14 22:30:46 +00:00
|
|
|
|
2016-11-07 17:28:13 +00:00
|
|
|
if response.status_code != 200:
|
|
|
|
raise Exception(response.content)
|
|
|
|
|
|
|
|
body = response.json()
|
2016-09-14 22:30:46 +00:00
|
|
|
output_dir = args.run_id
|
|
|
|
|
|
|
|
if not args.test:
|
2016-11-07 17:28:13 +00:00
|
|
|
if not os.path.exists(output_dir):
|
|
|
|
os.mkdir(output_dir)
|
2016-09-14 22:30:46 +00:00
|
|
|
|
|
|
|
for j in body:
|
|
|
|
job_id = j['id']
|
|
|
|
job_number = j['jobNumber']
|
|
|
|
path = os.path.join(output_dir, '%s.log' % job_number)
|
2016-12-13 07:27:49 +00:00
|
|
|
url = 'https://api.shippable.com/jobs/%s/consoles?download=true' % job_id
|
2016-09-14 22:30:46 +00:00
|
|
|
|
|
|
|
if args.verbose or args.test:
|
|
|
|
print('%s' % path)
|
|
|
|
|
2016-11-07 17:28:13 +00:00
|
|
|
if os.path.exists(path):
|
|
|
|
continue
|
|
|
|
|
2016-09-14 22:30:46 +00:00
|
|
|
if not args.test:
|
2016-11-07 17:28:13 +00:00
|
|
|
response = requests.get(url, headers=headers)
|
|
|
|
|
|
|
|
if response.status_code != 200:
|
|
|
|
raise Exception(response.content)
|
|
|
|
|
|
|
|
log = response.content
|
2016-09-14 22:30:46 +00:00
|
|
|
|
|
|
|
with open(path, 'w') as f:
|
|
|
|
f.write(log)
|
|
|
|
|
|
|
|
|
|
|
|
def get_api_key():
|
|
|
|
path = os.path.join(os.environ['HOME'], '.shippable.key')
|
|
|
|
|
|
|
|
try:
|
|
|
|
with open(path, 'r') as f:
|
|
|
|
return f.read().strip()
|
|
|
|
except IOError:
|
|
|
|
return None
|
|
|
|
|
|
|
|
|
|
|
|
if __name__ == '__main__':
|
|
|
|
main()
|