2016-11-30 06:21:53 +01:00
|
|
|
"""Access Ansible Core CI remote services."""
|
2019-07-12 08:46:20 +02:00
|
|
|
from __future__ import (absolute_import, division, print_function)
|
|
|
|
__metaclass__ = type
|
2016-11-30 06:21:53 +01:00
|
|
|
|
|
|
|
import json
|
|
|
|
import os
|
|
|
|
import traceback
|
|
|
|
import uuid
|
|
|
|
import errno
|
|
|
|
import time
|
2019-07-23 04:24:48 +02:00
|
|
|
|
|
|
|
import lib.types as t
|
2016-11-30 06:21:53 +01:00
|
|
|
|
|
|
|
from lib.http import (
|
|
|
|
HttpClient,
|
|
|
|
HttpResponse,
|
|
|
|
HttpError,
|
|
|
|
)
|
|
|
|
|
|
|
|
from lib.util import (
|
|
|
|
ApplicationError,
|
|
|
|
make_dirs,
|
|
|
|
display,
|
|
|
|
is_shippable,
|
2019-07-23 04:24:48 +02:00
|
|
|
to_text,
|
2016-11-30 06:21:53 +01:00
|
|
|
)
|
|
|
|
|
2019-07-11 07:00:34 +02:00
|
|
|
from lib.util_common import (
|
|
|
|
run_command,
|
|
|
|
)
|
|
|
|
|
2017-07-07 01:14:44 +02:00
|
|
|
from lib.config import (
|
|
|
|
EnvironmentConfig,
|
|
|
|
)
|
|
|
|
|
2019-07-23 04:24:48 +02:00
|
|
|
from lib.data import (
|
|
|
|
data_context,
|
|
|
|
)
|
|
|
|
|
2017-01-19 01:31:34 +01:00
|
|
|
AWS_ENDPOINTS = {
|
|
|
|
'us-east-1': 'https://14blg63h2i.execute-api.us-east-1.amazonaws.com',
|
|
|
|
'us-east-2': 'https://g5xynwbk96.execute-api.us-east-2.amazonaws.com',
|
|
|
|
}
|
|
|
|
|
2016-11-30 06:21:53 +01:00
|
|
|
|
2019-07-12 22:17:20 +02:00
|
|
|
class AnsibleCoreCI:
|
2016-11-30 06:21:53 +01:00
|
|
|
"""Client for Ansible Core CI services."""
|
2017-11-29 09:46:08 +01:00
|
|
|
def __init__(self, args, platform, version, stage='prod', persist=True, load=True, name=None, provider=None):
|
2016-11-30 06:21:53 +01:00
|
|
|
"""
|
2017-01-19 01:31:34 +01:00
|
|
|
:type args: EnvironmentConfig
|
2016-11-30 06:21:53 +01:00
|
|
|
:type platform: str
|
|
|
|
:type version: str
|
|
|
|
:type stage: str
|
|
|
|
:type persist: bool
|
2017-10-26 09:21:46 +02:00
|
|
|
:type load: bool
|
2016-11-30 06:21:53 +01:00
|
|
|
:type name: str
|
|
|
|
"""
|
|
|
|
self.args = args
|
|
|
|
self.platform = platform
|
|
|
|
self.version = version
|
|
|
|
self.stage = stage
|
|
|
|
self.client = HttpClient(args)
|
|
|
|
self.connection = None
|
|
|
|
self.instance_id = None
|
2017-10-12 10:22:18 +02:00
|
|
|
self.endpoint = None
|
|
|
|
self.max_threshold = 1
|
2016-11-30 06:21:53 +01:00
|
|
|
self.name = name if name else '%s-%s' % (self.platform, self.version)
|
2017-01-19 01:31:34 +01:00
|
|
|
self.ci_key = os.path.expanduser('~/.ansible-core-ci.key')
|
2017-11-29 09:46:08 +01:00
|
|
|
self.resource = 'jobs'
|
|
|
|
|
|
|
|
# Assign each supported platform to one provider.
|
|
|
|
# This is used to determine the provider from the platform when no provider is specified.
|
|
|
|
providers = dict(
|
|
|
|
aws=(
|
|
|
|
'aws',
|
|
|
|
'windows',
|
|
|
|
'freebsd',
|
|
|
|
'vyos',
|
|
|
|
'junos',
|
|
|
|
'ios',
|
2018-03-07 23:02:31 +01:00
|
|
|
'tower',
|
2018-06-30 01:23:54 +02:00
|
|
|
'rhel',
|
2019-04-09 22:29:30 +02:00
|
|
|
'hetzner',
|
2017-11-29 09:46:08 +01:00
|
|
|
),
|
|
|
|
azure=(
|
2017-12-07 21:42:35 +01:00
|
|
|
'azure',
|
2017-11-29 09:46:08 +01:00
|
|
|
),
|
|
|
|
parallels=(
|
|
|
|
'osx',
|
|
|
|
),
|
2019-06-04 22:49:30 +02:00
|
|
|
vmware=(
|
|
|
|
'vmware'
|
|
|
|
),
|
2017-01-08 08:36:35 +01:00
|
|
|
)
|
|
|
|
|
2017-11-29 09:46:08 +01:00
|
|
|
if provider:
|
|
|
|
# override default provider selection (not all combinations are valid)
|
|
|
|
self.provider = provider
|
|
|
|
else:
|
|
|
|
for candidate in providers:
|
|
|
|
if platform in providers[candidate]:
|
|
|
|
# assign default provider based on platform
|
|
|
|
self.provider = candidate
|
|
|
|
break
|
2018-01-30 01:00:41 +01:00
|
|
|
for candidate in providers:
|
|
|
|
if '%s/%s' % (platform, version) in providers[candidate]:
|
|
|
|
# assign default provider based on platform and version
|
|
|
|
self.provider = candidate
|
|
|
|
break
|
2017-11-29 09:46:08 +01:00
|
|
|
|
2018-09-20 02:56:56 +02:00
|
|
|
self.path = os.path.expanduser('~/.ansible/test/instances/%s-%s-%s' % (self.name, self.provider, self.stage))
|
|
|
|
|
2017-11-29 09:46:08 +01:00
|
|
|
if self.provider in ('aws', 'azure'):
|
|
|
|
if self.provider != 'aws':
|
|
|
|
self.resource = self.provider
|
2017-01-08 08:36:35 +01:00
|
|
|
|
2017-01-19 01:31:34 +01:00
|
|
|
if args.remote_aws_region:
|
|
|
|
# permit command-line override of region selection
|
|
|
|
region = args.remote_aws_region
|
|
|
|
# use a dedicated CI key when overriding the region selection
|
|
|
|
self.ci_key += '.%s' % args.remote_aws_region
|
|
|
|
elif is_shippable():
|
|
|
|
# split Shippable jobs across multiple regions to maximize use of launch credits
|
2017-01-19 03:20:07 +01:00
|
|
|
if self.platform == 'windows':
|
2017-02-07 00:01:20 +01:00
|
|
|
region = 'us-east-2'
|
2017-01-19 01:31:34 +01:00
|
|
|
else:
|
|
|
|
region = 'us-east-1'
|
|
|
|
else:
|
|
|
|
# send all non-Shippable jobs to us-east-1 to reduce api key maintenance
|
|
|
|
region = 'us-east-1'
|
|
|
|
|
2018-09-20 02:56:56 +02:00
|
|
|
self.path = "%s-%s" % (self.path, region)
|
2018-10-15 07:59:52 +02:00
|
|
|
self.endpoints = (AWS_ENDPOINTS[region],)
|
2018-08-09 10:00:22 +02:00
|
|
|
self.ssh_key = SshKey(args)
|
2017-01-08 08:36:35 +01:00
|
|
|
|
|
|
|
if self.platform == 'windows':
|
|
|
|
self.port = 5986
|
|
|
|
else:
|
|
|
|
self.port = 22
|
2017-11-29 09:46:08 +01:00
|
|
|
elif self.provider == 'parallels':
|
2017-10-12 10:22:18 +02:00
|
|
|
self.endpoints = self._get_parallels_endpoints()
|
|
|
|
self.max_threshold = 6
|
2017-01-08 08:36:35 +01:00
|
|
|
|
|
|
|
self.ssh_key = SshKey(args)
|
2016-11-30 06:21:53 +01:00
|
|
|
self.port = None
|
2019-06-04 22:49:30 +02:00
|
|
|
elif self.provider == 'vmware':
|
|
|
|
self.ssh_key = SshKey(args)
|
|
|
|
self.endpoints = ['https://access.ws.testing.ansible.com']
|
|
|
|
self.max_threshold = 1
|
|
|
|
|
2016-11-30 06:21:53 +01:00
|
|
|
else:
|
|
|
|
raise ApplicationError('Unsupported platform: %s' % platform)
|
|
|
|
|
2017-10-26 09:21:46 +02:00
|
|
|
if persist and load and self._load():
|
2016-11-30 06:21:53 +01:00
|
|
|
try:
|
|
|
|
display.info('Checking existing %s/%s instance %s.' % (self.platform, self.version, self.instance_id),
|
|
|
|
verbosity=1)
|
|
|
|
|
2017-01-17 03:59:44 +01:00
|
|
|
self.connection = self.get(always_raise_on=[404])
|
2016-11-30 06:21:53 +01:00
|
|
|
|
2017-01-26 22:07:10 +01:00
|
|
|
display.info('Loaded existing %s/%s from: %s' % (self.platform, self.version, self._uri), verbosity=1)
|
2016-11-30 06:21:53 +01:00
|
|
|
except HttpError as ex:
|
|
|
|
if ex.status != 404:
|
|
|
|
raise
|
|
|
|
|
|
|
|
self._clear()
|
|
|
|
|
|
|
|
display.info('Cleared stale %s/%s instance %s.' % (self.platform, self.version, self.instance_id),
|
|
|
|
verbosity=1)
|
|
|
|
|
|
|
|
self.instance_id = None
|
2017-10-12 10:22:18 +02:00
|
|
|
self.endpoint = None
|
2017-10-26 09:21:46 +02:00
|
|
|
elif not persist:
|
2016-11-30 06:21:53 +01:00
|
|
|
self.instance_id = None
|
2017-10-12 10:22:18 +02:00
|
|
|
self.endpoint = None
|
2016-11-30 06:21:53 +01:00
|
|
|
self._clear()
|
|
|
|
|
|
|
|
if self.instance_id:
|
|
|
|
self.started = True
|
|
|
|
else:
|
|
|
|
self.started = False
|
|
|
|
self.instance_id = str(uuid.uuid4())
|
2017-10-12 10:22:18 +02:00
|
|
|
self.endpoint = None
|
|
|
|
|
2018-02-19 22:32:07 +01:00
|
|
|
display.sensitive.add(self.instance_id)
|
|
|
|
|
2017-10-12 10:22:18 +02:00
|
|
|
def _get_parallels_endpoints(self):
|
|
|
|
"""
|
|
|
|
:rtype: tuple[str]
|
|
|
|
"""
|
|
|
|
client = HttpClient(self.args, always=True)
|
|
|
|
display.info('Getting available endpoints...', verbosity=1)
|
|
|
|
sleep = 3
|
|
|
|
|
2019-07-15 22:47:16 +02:00
|
|
|
for _iteration in range(1, 10):
|
2017-10-12 10:22:18 +02:00
|
|
|
response = client.get('https://s3.amazonaws.com/ansible-ci-files/ansible-test/parallels-endpoints.txt')
|
|
|
|
|
|
|
|
if response.status_code == 200:
|
|
|
|
endpoints = tuple(response.response.splitlines())
|
|
|
|
display.info('Available endpoints (%d):\n%s' % (len(endpoints), '\n'.join(' - %s' % endpoint for endpoint in endpoints)), verbosity=1)
|
|
|
|
return endpoints
|
|
|
|
|
|
|
|
display.warning('HTTP %d error getting endpoints, trying again in %d seconds.' % (response.status_code, sleep))
|
|
|
|
time.sleep(sleep)
|
|
|
|
|
|
|
|
raise ApplicationError('Unable to get available endpoints.')
|
2016-11-30 06:21:53 +01:00
|
|
|
|
|
|
|
def start(self):
|
|
|
|
"""Start instance."""
|
2017-10-26 09:21:46 +02:00
|
|
|
if self.started:
|
|
|
|
display.info('Skipping started %s/%s instance %s.' % (self.platform, self.version, self.instance_id),
|
|
|
|
verbosity=1)
|
2018-10-15 07:59:52 +02:00
|
|
|
return None
|
2017-10-26 09:21:46 +02:00
|
|
|
|
2016-11-30 06:21:53 +01:00
|
|
|
if is_shippable():
|
2017-05-05 12:37:11 +02:00
|
|
|
return self.start_shippable()
|
|
|
|
|
|
|
|
return self.start_remote()
|
2016-11-30 06:21:53 +01:00
|
|
|
|
|
|
|
def start_remote(self):
|
|
|
|
"""Start instance for remote development/testing."""
|
2017-01-19 01:31:34 +01:00
|
|
|
with open(self.ci_key, 'r') as key_fd:
|
2016-11-30 06:21:53 +01:00
|
|
|
auth_key = key_fd.read().strip()
|
|
|
|
|
2017-05-05 10:23:00 +02:00
|
|
|
return self._start(dict(
|
2016-11-30 06:21:53 +01:00
|
|
|
remote=dict(
|
|
|
|
key=auth_key,
|
|
|
|
nonce=None,
|
|
|
|
),
|
|
|
|
))
|
|
|
|
|
|
|
|
def start_shippable(self):
|
|
|
|
"""Start instance on Shippable."""
|
2017-05-05 10:23:00 +02:00
|
|
|
return self._start(dict(
|
2016-11-30 06:21:53 +01:00
|
|
|
shippable=dict(
|
|
|
|
run_id=os.environ['SHIPPABLE_BUILD_ID'],
|
|
|
|
job_number=int(os.environ['SHIPPABLE_JOB_NUMBER']),
|
|
|
|
),
|
|
|
|
))
|
|
|
|
|
|
|
|
def stop(self):
|
|
|
|
"""Stop instance."""
|
|
|
|
if not self.started:
|
|
|
|
display.info('Skipping invalid %s/%s instance %s.' % (self.platform, self.version, self.instance_id),
|
|
|
|
verbosity=1)
|
|
|
|
return
|
|
|
|
|
|
|
|
response = self.client.delete(self._uri)
|
|
|
|
|
|
|
|
if response.status_code == 404:
|
|
|
|
self._clear()
|
|
|
|
display.info('Cleared invalid %s/%s instance %s.' % (self.platform, self.version, self.instance_id),
|
|
|
|
verbosity=1)
|
|
|
|
return
|
|
|
|
|
|
|
|
if response.status_code == 200:
|
|
|
|
self._clear()
|
|
|
|
display.info('Stopped running %s/%s instance %s.' % (self.platform, self.version, self.instance_id),
|
|
|
|
verbosity=1)
|
|
|
|
return
|
|
|
|
|
|
|
|
raise self._create_http_error(response)
|
|
|
|
|
2017-08-09 07:55:17 +02:00
|
|
|
def get(self, tries=3, sleep=15, always_raise_on=None):
|
2016-11-30 06:21:53 +01:00
|
|
|
"""
|
|
|
|
Get instance connection information.
|
2017-01-17 03:59:44 +01:00
|
|
|
:type tries: int
|
|
|
|
:type sleep: int
|
|
|
|
:type always_raise_on: list[int] | None
|
2016-11-30 06:21:53 +01:00
|
|
|
:rtype: InstanceConnection
|
|
|
|
"""
|
|
|
|
if not self.started:
|
|
|
|
display.info('Skipping invalid %s/%s instance %s.' % (self.platform, self.version, self.instance_id),
|
|
|
|
verbosity=1)
|
|
|
|
return None
|
|
|
|
|
2017-01-17 03:59:44 +01:00
|
|
|
if not always_raise_on:
|
|
|
|
always_raise_on = []
|
|
|
|
|
2016-11-30 06:21:53 +01:00
|
|
|
if self.connection and self.connection.running:
|
|
|
|
return self.connection
|
|
|
|
|
2017-01-14 02:58:36 +01:00
|
|
|
while True:
|
|
|
|
tries -= 1
|
|
|
|
response = self.client.get(self._uri)
|
|
|
|
|
|
|
|
if response.status_code == 200:
|
|
|
|
break
|
|
|
|
|
|
|
|
error = self._create_http_error(response)
|
|
|
|
|
2017-01-17 03:59:44 +01:00
|
|
|
if not tries or response.status_code in always_raise_on:
|
2017-01-14 02:58:36 +01:00
|
|
|
raise error
|
|
|
|
|
|
|
|
display.warning('%s. Trying again after %d seconds.' % (error, sleep))
|
|
|
|
time.sleep(sleep)
|
2016-11-30 06:21:53 +01:00
|
|
|
|
|
|
|
if self.args.explain:
|
|
|
|
self.connection = InstanceConnection(
|
|
|
|
running=True,
|
|
|
|
hostname='cloud.example.com',
|
|
|
|
port=self.port or 12345,
|
|
|
|
username='username',
|
|
|
|
password='password' if self.platform == 'windows' else None,
|
|
|
|
)
|
|
|
|
else:
|
|
|
|
response_json = response.json()
|
|
|
|
|
|
|
|
status = response_json['status']
|
|
|
|
con = response_json['connection']
|
|
|
|
|
|
|
|
self.connection = InstanceConnection(
|
|
|
|
running=status == 'running',
|
|
|
|
hostname=con['hostname'],
|
|
|
|
port=int(con.get('port', self.port)),
|
|
|
|
username=con['username'],
|
|
|
|
password=con.get('password'),
|
|
|
|
)
|
|
|
|
|
2018-02-19 22:32:07 +01:00
|
|
|
if self.connection.password:
|
2019-04-15 19:59:07 +02:00
|
|
|
display.sensitive.add(str(self.connection.password))
|
2018-02-19 22:32:07 +01:00
|
|
|
|
2016-11-30 06:21:53 +01:00
|
|
|
status = 'running' if self.connection.running else 'starting'
|
|
|
|
|
2017-01-26 22:07:10 +01:00
|
|
|
display.info('Status update: %s/%s on instance %s is %s.' %
|
|
|
|
(self.platform, self.version, self.instance_id, status),
|
2016-11-30 06:21:53 +01:00
|
|
|
verbosity=1)
|
|
|
|
|
|
|
|
return self.connection
|
|
|
|
|
|
|
|
def wait(self):
|
|
|
|
"""Wait for the instance to become ready."""
|
2019-07-15 22:47:16 +02:00
|
|
|
for _iteration in range(1, 90):
|
2016-11-30 06:21:53 +01:00
|
|
|
if self.get().running:
|
|
|
|
return
|
|
|
|
time.sleep(10)
|
|
|
|
|
|
|
|
raise ApplicationError('Timeout waiting for %s/%s instance %s.' %
|
|
|
|
(self.platform, self.version, self.instance_id))
|
|
|
|
|
|
|
|
@property
|
|
|
|
def _uri(self):
|
2017-11-29 09:46:08 +01:00
|
|
|
return '%s/%s/%s/%s' % (self.endpoint, self.stage, self.resource, self.instance_id)
|
2016-11-30 06:21:53 +01:00
|
|
|
|
|
|
|
def _start(self, auth):
|
|
|
|
"""Start instance."""
|
2017-05-05 10:23:00 +02:00
|
|
|
display.info('Initializing new %s/%s instance %s.' % (self.platform, self.version, self.instance_id), verbosity=1)
|
|
|
|
|
2017-01-08 08:36:35 +01:00
|
|
|
if self.platform == 'windows':
|
2017-05-18 19:37:53 +02:00
|
|
|
with open('examples/scripts/ConfigureRemotingForAnsible.ps1', 'rb') as winrm_config_fd:
|
2019-07-23 04:24:48 +02:00
|
|
|
winrm_config = to_text(winrm_config_fd.read())
|
2017-01-08 08:36:35 +01:00
|
|
|
else:
|
|
|
|
winrm_config = None
|
2017-01-11 04:44:25 +01:00
|
|
|
|
2016-11-30 06:21:53 +01:00
|
|
|
data = dict(
|
|
|
|
config=dict(
|
|
|
|
platform=self.platform,
|
|
|
|
version=self.version,
|
|
|
|
public_key=self.ssh_key.pub_contents if self.ssh_key else None,
|
|
|
|
query=False,
|
2017-01-11 04:44:25 +01:00
|
|
|
winrm_config=winrm_config,
|
2016-11-30 06:21:53 +01:00
|
|
|
)
|
|
|
|
)
|
|
|
|
|
|
|
|
data.update(dict(auth=auth))
|
|
|
|
|
|
|
|
headers = {
|
|
|
|
'Content-Type': 'application/json',
|
|
|
|
}
|
|
|
|
|
2017-10-12 10:22:18 +02:00
|
|
|
response = self._start_try_endpoints(data, headers)
|
|
|
|
|
|
|
|
self.started = True
|
|
|
|
self._save()
|
|
|
|
|
|
|
|
display.info('Started %s/%s from: %s' % (self.platform, self.version, self._uri), verbosity=1)
|
|
|
|
|
|
|
|
if self.args.explain:
|
|
|
|
return {}
|
|
|
|
|
|
|
|
return response.json()
|
|
|
|
|
|
|
|
def _start_try_endpoints(self, data, headers):
|
|
|
|
"""
|
|
|
|
:type data: dict[str, any]
|
|
|
|
:type headers: dict[str, str]
|
|
|
|
:rtype: HttpResponse
|
|
|
|
"""
|
|
|
|
threshold = 1
|
|
|
|
|
|
|
|
while threshold <= self.max_threshold:
|
|
|
|
for self.endpoint in self.endpoints:
|
|
|
|
try:
|
|
|
|
return self._start_at_threshold(data, headers, threshold)
|
|
|
|
except CoreHttpError as ex:
|
|
|
|
if ex.status == 503:
|
|
|
|
display.info('Service Unavailable: %s' % ex.remote_message, verbosity=1)
|
|
|
|
continue
|
|
|
|
display.error(ex.remote_message)
|
|
|
|
except HttpError as ex:
|
|
|
|
display.error(u'%s' % ex)
|
|
|
|
|
|
|
|
time.sleep(3)
|
|
|
|
|
|
|
|
threshold += 1
|
|
|
|
|
|
|
|
raise ApplicationError('Maximum threshold reached and all endpoints exhausted.')
|
|
|
|
|
|
|
|
def _start_at_threshold(self, data, headers, threshold):
|
|
|
|
"""
|
|
|
|
:type data: dict[str, any]
|
|
|
|
:type headers: dict[str, str]
|
|
|
|
:type threshold: int
|
|
|
|
:rtype: HttpResponse | None
|
|
|
|
"""
|
2017-08-09 07:55:17 +02:00
|
|
|
tries = 3
|
|
|
|
sleep = 15
|
2016-11-30 06:21:53 +01:00
|
|
|
|
2017-10-12 10:22:18 +02:00
|
|
|
data['threshold'] = threshold
|
|
|
|
|
|
|
|
display.info('Trying endpoint: %s (threshold %d)' % (self.endpoint, threshold), verbosity=1)
|
|
|
|
|
2017-01-13 23:59:34 +01:00
|
|
|
while True:
|
|
|
|
tries -= 1
|
|
|
|
response = self.client.put(self._uri, data=json.dumps(data), headers=headers)
|
|
|
|
|
|
|
|
if response.status_code == 200:
|
2017-10-12 10:22:18 +02:00
|
|
|
return response
|
2017-01-13 23:59:34 +01:00
|
|
|
|
|
|
|
error = self._create_http_error(response)
|
|
|
|
|
2017-10-12 10:22:18 +02:00
|
|
|
if response.status_code == 503:
|
|
|
|
raise error
|
|
|
|
|
2017-01-13 23:59:34 +01:00
|
|
|
if not tries:
|
|
|
|
raise error
|
|
|
|
|
|
|
|
display.warning('%s. Trying again after %d seconds.' % (error, sleep))
|
|
|
|
time.sleep(sleep)
|
2016-11-30 06:21:53 +01:00
|
|
|
|
|
|
|
def _clear(self):
|
|
|
|
"""Clear instance information."""
|
|
|
|
try:
|
|
|
|
self.connection = None
|
|
|
|
os.remove(self.path)
|
|
|
|
except OSError as ex:
|
|
|
|
if ex.errno != errno.ENOENT:
|
|
|
|
raise
|
|
|
|
|
|
|
|
def _load(self):
|
|
|
|
"""Load instance information."""
|
|
|
|
try:
|
|
|
|
with open(self.path, 'r') as instance_fd:
|
2017-10-12 10:22:18 +02:00
|
|
|
data = instance_fd.read()
|
2016-11-30 06:21:53 +01:00
|
|
|
except IOError as ex:
|
|
|
|
if ex.errno != errno.ENOENT:
|
|
|
|
raise
|
|
|
|
|
2017-10-12 10:22:18 +02:00
|
|
|
return False
|
|
|
|
|
|
|
|
if not data.startswith('{'):
|
|
|
|
return False # legacy format
|
|
|
|
|
|
|
|
config = json.loads(data)
|
|
|
|
|
2017-10-26 09:21:46 +02:00
|
|
|
return self.load(config)
|
|
|
|
|
|
|
|
def load(self, config):
|
|
|
|
"""
|
|
|
|
:type config: dict[str, str]
|
|
|
|
:rtype: bool
|
|
|
|
"""
|
2019-04-15 19:59:07 +02:00
|
|
|
self.instance_id = str(config['instance_id'])
|
2017-10-12 10:22:18 +02:00
|
|
|
self.endpoint = config['endpoint']
|
|
|
|
self.started = True
|
|
|
|
|
2018-02-19 22:32:07 +01:00
|
|
|
display.sensitive.add(self.instance_id)
|
|
|
|
|
2017-10-12 10:22:18 +02:00
|
|
|
return True
|
2016-11-30 06:21:53 +01:00
|
|
|
|
|
|
|
def _save(self):
|
|
|
|
"""Save instance information."""
|
|
|
|
if self.args.explain:
|
|
|
|
return
|
|
|
|
|
2017-10-26 09:21:46 +02:00
|
|
|
config = self.save()
|
|
|
|
|
2016-11-30 06:21:53 +01:00
|
|
|
make_dirs(os.path.dirname(self.path))
|
|
|
|
|
|
|
|
with open(self.path, 'w') as instance_fd:
|
2017-10-12 10:22:18 +02:00
|
|
|
instance_fd.write(json.dumps(config, indent=4, sort_keys=True))
|
2016-11-30 06:21:53 +01:00
|
|
|
|
2017-10-26 09:21:46 +02:00
|
|
|
def save(self):
|
|
|
|
"""
|
|
|
|
:rtype: dict[str, str]
|
|
|
|
"""
|
|
|
|
return dict(
|
|
|
|
platform_version='%s/%s' % (self.platform, self.version),
|
|
|
|
instance_id=self.instance_id,
|
|
|
|
endpoint=self.endpoint,
|
|
|
|
)
|
|
|
|
|
2016-11-30 06:21:53 +01:00
|
|
|
@staticmethod
|
|
|
|
def _create_http_error(response):
|
|
|
|
"""
|
|
|
|
:type response: HttpResponse
|
|
|
|
:rtype: ApplicationError
|
|
|
|
"""
|
|
|
|
response_json = response.json()
|
|
|
|
stack_trace = ''
|
|
|
|
|
|
|
|
if 'message' in response_json:
|
|
|
|
message = response_json['message']
|
|
|
|
elif 'errorMessage' in response_json:
|
|
|
|
message = response_json['errorMessage'].strip()
|
|
|
|
if 'stackTrace' in response_json:
|
2019-04-10 08:08:31 +02:00
|
|
|
traceback_lines = response_json['stackTrace']
|
|
|
|
|
|
|
|
# AWS Lambda on Python 2.7 returns a list of tuples
|
|
|
|
# AWS Lambda on Python 3.7 returns a list of strings
|
|
|
|
if traceback_lines and isinstance(traceback_lines[0], list):
|
|
|
|
traceback_lines = traceback.format_list(traceback_lines)
|
|
|
|
|
|
|
|
trace = '\n'.join([x.rstrip() for x in traceback_lines])
|
2016-11-30 06:21:53 +01:00
|
|
|
stack_trace = ('\nTraceback (from remote server):\n%s' % trace)
|
|
|
|
else:
|
|
|
|
message = str(response_json)
|
|
|
|
|
2017-10-12 10:22:18 +02:00
|
|
|
return CoreHttpError(response.status_code, message, stack_trace)
|
|
|
|
|
|
|
|
|
|
|
|
class CoreHttpError(HttpError):
|
|
|
|
"""HTTP response as an error."""
|
|
|
|
def __init__(self, status, remote_message, remote_stack_trace):
|
|
|
|
"""
|
|
|
|
:type status: int
|
|
|
|
:type remote_message: str
|
|
|
|
:type remote_stack_trace: str
|
|
|
|
"""
|
|
|
|
super(CoreHttpError, self).__init__(status, '%s%s' % (remote_message, remote_stack_trace))
|
|
|
|
|
|
|
|
self.remote_message = remote_message
|
|
|
|
self.remote_stack_trace = remote_stack_trace
|
2016-11-30 06:21:53 +01:00
|
|
|
|
|
|
|
|
2019-07-12 22:17:20 +02:00
|
|
|
class SshKey:
|
2016-11-30 06:21:53 +01:00
|
|
|
"""Container for SSH key used to connect to remote instances."""
|
2017-10-26 09:21:46 +02:00
|
|
|
KEY_NAME = 'id_rsa'
|
|
|
|
PUB_NAME = 'id_rsa.pub'
|
|
|
|
|
2016-11-30 06:21:53 +01:00
|
|
|
def __init__(self, args):
|
|
|
|
"""
|
2017-01-19 01:31:34 +01:00
|
|
|
:type args: EnvironmentConfig
|
2016-11-30 06:21:53 +01:00
|
|
|
"""
|
2019-07-23 04:24:48 +02:00
|
|
|
cache_dir = os.path.join(data_context().content.root, 'test/cache')
|
2017-10-26 09:21:46 +02:00
|
|
|
|
|
|
|
self.key = os.path.join(cache_dir, self.KEY_NAME)
|
|
|
|
self.pub = os.path.join(cache_dir, self.PUB_NAME)
|
2016-11-30 06:21:53 +01:00
|
|
|
|
2019-07-23 04:24:48 +02:00
|
|
|
key_dst = os.path.relpath(self.key, data_context().content.root)
|
|
|
|
pub_dst = os.path.relpath(self.pub, data_context().content.root)
|
|
|
|
|
2017-10-26 09:21:46 +02:00
|
|
|
if not os.path.isfile(self.key) or not os.path.isfile(self.pub):
|
|
|
|
base_dir = os.path.expanduser('~/.ansible/test/')
|
|
|
|
|
|
|
|
key = os.path.join(base_dir, self.KEY_NAME)
|
|
|
|
pub = os.path.join(base_dir, self.PUB_NAME)
|
2016-11-30 06:21:53 +01:00
|
|
|
|
|
|
|
if not args.explain:
|
2017-10-26 09:21:46 +02:00
|
|
|
make_dirs(base_dir)
|
|
|
|
|
|
|
|
if not os.path.isfile(key) or not os.path.isfile(pub):
|
2018-09-18 19:55:52 +02:00
|
|
|
run_command(args, ['ssh-keygen', '-m', 'PEM', '-q', '-t', 'rsa', '-N', '', '-f', key])
|
2016-11-30 06:21:53 +01:00
|
|
|
|
2019-07-23 04:24:48 +02:00
|
|
|
self.key = key
|
|
|
|
self.pub = pub
|
|
|
|
|
|
|
|
def ssh_key_callback(files): # type: (t.List[t.Tuple[str, str]]) -> None
|
|
|
|
"""Add the SSH keys to the payload file list."""
|
|
|
|
files.append((key, key_dst))
|
|
|
|
files.append((pub, pub_dst))
|
|
|
|
|
|
|
|
data_context().register_payload_callback(ssh_key_callback)
|
2016-11-30 06:21:53 +01:00
|
|
|
|
|
|
|
if args.explain:
|
|
|
|
self.pub_contents = None
|
|
|
|
else:
|
|
|
|
with open(self.pub, 'r') as pub_fd:
|
|
|
|
self.pub_contents = pub_fd.read().strip()
|
|
|
|
|
|
|
|
|
2019-07-12 22:17:20 +02:00
|
|
|
class InstanceConnection:
|
2016-11-30 06:21:53 +01:00
|
|
|
"""Container for remote instance status and connection details."""
|
|
|
|
def __init__(self, running, hostname, port, username, password):
|
|
|
|
"""
|
|
|
|
:type running: bool
|
|
|
|
:type hostname: str
|
|
|
|
:type port: int
|
|
|
|
:type username: str
|
|
|
|
:type password: str | None
|
|
|
|
"""
|
|
|
|
self.running = running
|
|
|
|
self.hostname = hostname
|
|
|
|
self.port = port
|
|
|
|
self.username = username
|
|
|
|
self.password = password
|
|
|
|
|
|
|
|
def __str__(self):
|
|
|
|
if self.password:
|
|
|
|
return '%s:%s [%s:%s]' % (self.hostname, self.port, self.username, self.password)
|
|
|
|
|
|
|
|
return '%s:%s [%s]' % (self.hostname, self.port, self.username)
|