ansible/test/utils/shippable/tools/run.py

139 lines
3.5 KiB
Python
Raw Normal View History

#!/usr/bin/env python
# PYTHON_ARGCOMPLETE_OK
# (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/>.
"""CLI tool for starting new Shippable CI runs."""
from __future__ import print_function
# noinspection PyCompatibility
import argparse
import json
import os
import requests
try:
import argcomplete
except ImportError:
argcomplete = None
def main():
"""Main program body."""
api_key = get_api_key()
parser = argparse.ArgumentParser(description='Start a new Shippable run.')
parser.add_argument('project',
metavar='account/project',
help='Shippable account/project')
target = parser.add_mutually_exclusive_group()
target.add_argument('--branch',
help='branch name')
target.add_argument('--run',
metavar='ID',
help='Shippable run ID')
parser.add_argument('--key',
metavar='KEY',
default=api_key,
required=not api_key,
help='Shippable API key')
parser.add_argument('--env',
nargs=2,
metavar=('KEY', 'VALUE'),
action='append',
help='environment variable to pass')
if argcomplete:
argcomplete.autocomplete(parser)
args = parser.parse_args()
headers = dict(
Authorization='apiToken %s' % args.key,
)
# get project ID
data = dict(
projectFullNames=args.project,
)
url = 'https://api.shippable.com/projects'
response = requests.get(url, data, headers=headers)
if response.status_code != 200:
raise Exception(response.content)
result = response.json()
if len(result) != 1:
raise Exception(
'Received %d items instead of 1 looking for %s in:\n%s' % (
len(result),
args.project,
json.dumps(result, indent=4, sort_keys=True)))
project_id = response.json()[0]['id']
# new build
data = dict(
[stable-2.5] Backport test infra bug fixes. (#48700) * Download hello package from S3 for apt test. (cherry picked from commit 83fd82ca7e7635f6c16801c88a5fe81a377f86ca) * Fix passing of env vars to Shippable. (cherry picked from commit 9979a32e5c6d55345a8f87115ed86a94d86d8398) * Use correct interpreter for ansible-test injector. (cherry picked from commit eec21a3d12dcab755e019f7875cf357f64bd3104) * Correct ansible-test injector python behavior. Inject a symlink to the correct python into the copied injector directory instead of altering the shebang of the injector. This has the side-effect of also intercepting `python` for integration tests which simplifies cases where it needs to be directly invoked without collecting code coverage. (cherry picked from commit d6bf45cd9d62e59923df13ffb71900f7dde92497) * Fix ansible-test merge change detection. (cherry picked from commit aa7fe919d319fe45e068e94f6e2de7162d8921fe) * Fix ansible-test interpreter tracking. Track the interpreter for each copy of the injector by the interpreter path instead of the interpreter version. This avoids the possibility of mixing different interpreters with the same version. (cherry picked from commit fa53b4805bcf3573dee7d94ed6f6918d7183c92e) * Use `state: latest` for `dpkg_selections` test. We don't need to test with `upgrade: dist`, since we're not trying to test the `apt` module. We just need to make sure the hold set by the `dpkg_selections` module is working. This change will avoid updating all the packages on the system, which is slow, unnecessary, and can cause the installed python to be changed. (cherry picked from commit 136a2cca2fcea71d86ab317c476e9bb4b2dd34ac)
2018-11-15 18:32:42 +00:00
globalEnv=dict((kp[0], kp[1]) for kp in args.env or [])
)
if args.branch:
data['branchName'] = args.branch
elif args.run:
data['runId'] = args.run
url = 'https://api.shippable.com/projects/%s/newBuild' % project_id
[stable-2.5] Backport test infra bug fixes. (#48700) * Download hello package from S3 for apt test. (cherry picked from commit 83fd82ca7e7635f6c16801c88a5fe81a377f86ca) * Fix passing of env vars to Shippable. (cherry picked from commit 9979a32e5c6d55345a8f87115ed86a94d86d8398) * Use correct interpreter for ansible-test injector. (cherry picked from commit eec21a3d12dcab755e019f7875cf357f64bd3104) * Correct ansible-test injector python behavior. Inject a symlink to the correct python into the copied injector directory instead of altering the shebang of the injector. This has the side-effect of also intercepting `python` for integration tests which simplifies cases where it needs to be directly invoked without collecting code coverage. (cherry picked from commit d6bf45cd9d62e59923df13ffb71900f7dde92497) * Fix ansible-test merge change detection. (cherry picked from commit aa7fe919d319fe45e068e94f6e2de7162d8921fe) * Fix ansible-test interpreter tracking. Track the interpreter for each copy of the injector by the interpreter path instead of the interpreter version. This avoids the possibility of mixing different interpreters with the same version. (cherry picked from commit fa53b4805bcf3573dee7d94ed6f6918d7183c92e) * Use `state: latest` for `dpkg_selections` test. We don't need to test with `upgrade: dist`, since we're not trying to test the `apt` module. We just need to make sure the hold set by the `dpkg_selections` module is working. This change will avoid updating all the packages on the system, which is slow, unnecessary, and can cause the installed python to be changed. (cherry picked from commit 136a2cca2fcea71d86ab317c476e9bb4b2dd34ac)
2018-11-15 18:32:42 +00:00
response = requests.post(url, json=data, headers=headers)
if response.status_code != 200:
[stable-2.5] Backport test infra bug fixes. (#48700) * Download hello package from S3 for apt test. (cherry picked from commit 83fd82ca7e7635f6c16801c88a5fe81a377f86ca) * Fix passing of env vars to Shippable. (cherry picked from commit 9979a32e5c6d55345a8f87115ed86a94d86d8398) * Use correct interpreter for ansible-test injector. (cherry picked from commit eec21a3d12dcab755e019f7875cf357f64bd3104) * Correct ansible-test injector python behavior. Inject a symlink to the correct python into the copied injector directory instead of altering the shebang of the injector. This has the side-effect of also intercepting `python` for integration tests which simplifies cases where it needs to be directly invoked without collecting code coverage. (cherry picked from commit d6bf45cd9d62e59923df13ffb71900f7dde92497) * Fix ansible-test merge change detection. (cherry picked from commit aa7fe919d319fe45e068e94f6e2de7162d8921fe) * Fix ansible-test interpreter tracking. Track the interpreter for each copy of the injector by the interpreter path instead of the interpreter version. This avoids the possibility of mixing different interpreters with the same version. (cherry picked from commit fa53b4805bcf3573dee7d94ed6f6918d7183c92e) * Use `state: latest` for `dpkg_selections` test. We don't need to test with `upgrade: dist`, since we're not trying to test the `apt` module. We just need to make sure the hold set by the `dpkg_selections` module is working. This change will avoid updating all the packages on the system, which is slow, unnecessary, and can cause the installed python to be changed. (cherry picked from commit 136a2cca2fcea71d86ab317c476e9bb4b2dd34ac)
2018-11-15 18:32:42 +00:00
raise Exception("HTTP %s: %s\n%s" % (response.status_code, response.reason, response.content))
print(json.dumps(response.json(), indent=4, sort_keys=True))
def get_api_key():
"""
rtype: str
"""
key = os.environ.get('SHIPPABLE_KEY', None)
if key:
return key
path = os.path.join(os.environ['HOME'], '.shippable.key')
try:
with open(path, 'r') as key_fd:
return key_fd.read().strip()
except IOError:
return None
if __name__ == '__main__':
main()