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
|
|
|
|
2017-05-11 07:25:02 +02:00
|
|
|
import os
|
|
|
|
import tempfile
|
2018-03-22 05:31:03 +01:00
|
|
|
import time
|
2016-11-30 06:21:53 +01:00
|
|
|
|
2019-08-06 23:43:29 +02:00
|
|
|
from .util import (
|
2016-11-30 06:21:53 +01:00
|
|
|
SubprocessError,
|
|
|
|
ApplicationError,
|
2019-05-24 22:10:33 +02:00
|
|
|
cmd_quote,
|
2019-07-23 04:24:48 +02:00
|
|
|
display,
|
2019-08-06 02:40:00 +02:00
|
|
|
ANSIBLE_TEST_DATA_ROOT,
|
2016-11-30 06:21:53 +01:00
|
|
|
)
|
|
|
|
|
2019-08-06 23:43:29 +02:00
|
|
|
from .util_common import (
|
2019-07-11 07:00:34 +02:00
|
|
|
intercept_command,
|
2020-06-10 00:40:56 +02:00
|
|
|
get_network_settings,
|
2019-07-11 07:00:34 +02:00
|
|
|
run_command,
|
|
|
|
)
|
|
|
|
|
2019-08-06 23:43:29 +02:00
|
|
|
from .core_ci import (
|
2016-11-30 06:21:53 +01:00
|
|
|
AnsibleCoreCI,
|
|
|
|
)
|
|
|
|
|
2019-08-06 23:43:29 +02:00
|
|
|
from .ansible_util import (
|
2016-11-30 06:21:53 +01:00
|
|
|
ansible_environment,
|
|
|
|
)
|
|
|
|
|
2019-08-06 23:43:29 +02:00
|
|
|
from .config import (
|
2018-11-16 23:50:01 +01:00
|
|
|
ShellConfig,
|
|
|
|
)
|
|
|
|
|
2019-08-06 23:43:29 +02:00
|
|
|
from .payload import (
|
2019-07-16 01:20:03 +02:00
|
|
|
create_payload,
|
|
|
|
)
|
|
|
|
|
2016-11-30 06:21:53 +01:00
|
|
|
|
2019-07-12 22:17:20 +02:00
|
|
|
class ManageWindowsCI:
|
2016-11-30 06:21:53 +01:00
|
|
|
"""Manage access to a Windows instance provided by Ansible Core CI."""
|
|
|
|
def __init__(self, core_ci):
|
|
|
|
"""
|
|
|
|
:type core_ci: AnsibleCoreCI
|
|
|
|
"""
|
|
|
|
self.core_ci = core_ci
|
2018-08-13 01:27:59 +02:00
|
|
|
self.ssh_args = ['-i', self.core_ci.ssh_key.key]
|
|
|
|
|
|
|
|
ssh_options = dict(
|
|
|
|
BatchMode='yes',
|
|
|
|
StrictHostKeyChecking='no',
|
|
|
|
UserKnownHostsFile='/dev/null',
|
|
|
|
ServerAliveInterval=15,
|
|
|
|
ServerAliveCountMax=4,
|
|
|
|
)
|
|
|
|
|
|
|
|
for ssh_option in sorted(ssh_options):
|
|
|
|
self.ssh_args += ['-o', '%s=%s' % (ssh_option, ssh_options[ssh_option])]
|
|
|
|
|
2019-03-28 00:40:27 +01:00
|
|
|
def setup(self, python_version):
|
|
|
|
"""Used in delegate_remote to setup the host, no action is required for Windows.
|
|
|
|
:type python_version: str
|
|
|
|
"""
|
2016-11-30 06:21:53 +01:00
|
|
|
|
|
|
|
def wait(self):
|
|
|
|
"""Wait for instance to respond to ansible ping."""
|
|
|
|
extra_vars = [
|
|
|
|
'ansible_connection=winrm',
|
|
|
|
'ansible_host=%s' % self.core_ci.connection.hostname,
|
|
|
|
'ansible_user=%s' % self.core_ci.connection.username,
|
|
|
|
'ansible_password=%s' % self.core_ci.connection.password,
|
|
|
|
'ansible_port=%s' % self.core_ci.connection.port,
|
|
|
|
'ansible_winrm_server_cert_validation=ignore',
|
|
|
|
]
|
|
|
|
|
|
|
|
name = 'windows_%s' % self.core_ci.version
|
|
|
|
|
|
|
|
env = ansible_environment(self.core_ci.args)
|
2020-03-17 00:38:47 +01:00
|
|
|
cmd = ['ansible', '-m', 'ansible.windows.win_ping', '-i', '%s,' % name, name, '-e', ' '.join(extra_vars)]
|
2016-11-30 06:21:53 +01:00
|
|
|
|
2018-03-22 05:31:03 +01:00
|
|
|
for dummy in range(1, 120):
|
2016-11-30 06:21:53 +01:00
|
|
|
try:
|
2019-08-07 20:43:42 +02:00
|
|
|
intercept_command(self.core_ci.args, cmd, 'ping', env=env, disable_coverage=True)
|
2016-11-30 06:21:53 +01:00
|
|
|
return
|
|
|
|
except SubprocessError:
|
2018-03-22 05:31:03 +01:00
|
|
|
time.sleep(10)
|
2016-11-30 06:21:53 +01:00
|
|
|
|
|
|
|
raise ApplicationError('Timeout waiting for %s/%s instance %s.' %
|
|
|
|
(self.core_ci.platform, self.core_ci.version, self.core_ci.instance_id))
|
2017-01-08 08:36:35 +01:00
|
|
|
|
2018-10-13 00:20:00 +02:00
|
|
|
def download(self, remote, local):
|
|
|
|
"""
|
|
|
|
:type remote: str
|
|
|
|
:type local: str
|
|
|
|
"""
|
|
|
|
self.scp('%s@%s:%s' % (self.core_ci.connection.username, self.core_ci.connection.hostname, remote), local)
|
|
|
|
|
|
|
|
def upload(self, local, remote):
|
|
|
|
"""
|
|
|
|
:type local: str
|
|
|
|
:type remote: str
|
|
|
|
"""
|
|
|
|
self.scp(local, '%s@%s:%s' % (self.core_ci.connection.username, self.core_ci.connection.hostname, remote))
|
|
|
|
|
|
|
|
def ssh(self, command, options=None, force_pty=True):
|
2018-08-13 01:27:59 +02:00
|
|
|
"""
|
|
|
|
:type command: str | list[str]
|
|
|
|
:type options: list[str] | None
|
2018-10-13 00:20:00 +02:00
|
|
|
:type force_pty: bool
|
2018-08-13 01:27:59 +02:00
|
|
|
"""
|
|
|
|
if not options:
|
|
|
|
options = []
|
2018-10-13 00:20:00 +02:00
|
|
|
if force_pty:
|
|
|
|
options.append('-tt')
|
2018-08-13 01:27:59 +02:00
|
|
|
|
|
|
|
if isinstance(command, list):
|
2019-05-24 22:10:33 +02:00
|
|
|
command = ' '.join(cmd_quote(c) for c in command)
|
2018-08-13 01:27:59 +02:00
|
|
|
|
|
|
|
run_command(self.core_ci.args,
|
2018-10-13 00:20:00 +02:00
|
|
|
['ssh', '-q'] + self.ssh_args +
|
2018-08-13 01:27:59 +02:00
|
|
|
options +
|
|
|
|
['-p', '22',
|
|
|
|
'%s@%s' % (self.core_ci.connection.username, self.core_ci.connection.hostname)] +
|
|
|
|
[command])
|
|
|
|
|
2018-10-13 00:20:00 +02:00
|
|
|
def scp(self, src, dst):
|
|
|
|
"""
|
|
|
|
:type src: str
|
|
|
|
:type dst: str
|
|
|
|
"""
|
|
|
|
for dummy in range(1, 10):
|
|
|
|
try:
|
|
|
|
run_command(self.core_ci.args,
|
|
|
|
['scp'] + self.ssh_args +
|
|
|
|
['-P', '22', '-q', '-r', src, dst])
|
|
|
|
return
|
|
|
|
except SubprocessError:
|
|
|
|
time.sleep(10)
|
|
|
|
|
|
|
|
raise ApplicationError('Failed transfer: %s -> %s' % (src, dst))
|
|
|
|
|
2017-01-08 08:36:35 +01:00
|
|
|
|
2019-07-12 22:17:20 +02:00
|
|
|
class ManageNetworkCI:
|
2017-01-08 08:36:35 +01:00
|
|
|
"""Manage access to a network instance provided by Ansible Core CI."""
|
|
|
|
def __init__(self, core_ci):
|
|
|
|
"""
|
|
|
|
:type core_ci: AnsibleCoreCI
|
|
|
|
"""
|
|
|
|
self.core_ci = core_ci
|
|
|
|
|
|
|
|
def wait(self):
|
|
|
|
"""Wait for instance to respond to ansible ping."""
|
2020-02-21 00:27:08 +01:00
|
|
|
settings = get_network_settings(self.core_ci.args, self.core_ci.platform, self.core_ci.version)
|
|
|
|
|
2017-01-08 08:36:35 +01:00
|
|
|
extra_vars = [
|
|
|
|
'ansible_host=%s' % self.core_ci.connection.hostname,
|
|
|
|
'ansible_port=%s' % self.core_ci.connection.port,
|
|
|
|
'ansible_ssh_private_key_file=%s' % self.core_ci.ssh_key.key,
|
2020-02-21 00:27:08 +01:00
|
|
|
] + [
|
|
|
|
'%s=%s' % (key, value) for key, value in settings.inventory_vars.items()
|
2017-01-08 08:36:35 +01:00
|
|
|
]
|
|
|
|
|
2017-07-04 00:00:16 +02:00
|
|
|
name = '%s-%s' % (self.core_ci.platform, self.core_ci.version.replace('.', '-'))
|
2017-01-08 08:36:35 +01:00
|
|
|
|
|
|
|
env = ansible_environment(self.core_ci.args)
|
2017-05-12 13:49:12 +02:00
|
|
|
cmd = [
|
|
|
|
'ansible',
|
2020-02-21 00:27:08 +01:00
|
|
|
'-m', '%s%s_command' % (settings.collection + '.' if settings.collection else '', self.core_ci.platform),
|
2017-05-12 13:49:12 +02:00
|
|
|
'-a', 'commands=?',
|
|
|
|
'-u', self.core_ci.connection.username,
|
|
|
|
'-i', '%s,' % name,
|
|
|
|
'-e', ' '.join(extra_vars),
|
|
|
|
name,
|
|
|
|
]
|
2017-01-08 08:36:35 +01:00
|
|
|
|
2018-03-22 05:31:03 +01:00
|
|
|
for dummy in range(1, 90):
|
2017-01-08 08:36:35 +01:00
|
|
|
try:
|
2019-08-07 20:43:42 +02:00
|
|
|
intercept_command(self.core_ci.args, cmd, 'ping', env=env, disable_coverage=True)
|
2017-01-08 08:36:35 +01:00
|
|
|
return
|
|
|
|
except SubprocessError:
|
2018-03-22 05:31:03 +01:00
|
|
|
time.sleep(10)
|
2017-01-08 08:36:35 +01:00
|
|
|
|
|
|
|
raise ApplicationError('Timeout waiting for %s/%s instance %s.' %
|
|
|
|
(self.core_ci.platform, self.core_ci.version, self.core_ci.instance_id))
|
2016-11-30 06:21:53 +01:00
|
|
|
|
|
|
|
|
2019-07-12 22:17:20 +02:00
|
|
|
class ManagePosixCI:
|
2016-11-30 06:21:53 +01:00
|
|
|
"""Manage access to a POSIX instance provided by Ansible Core CI."""
|
|
|
|
def __init__(self, core_ci):
|
|
|
|
"""
|
|
|
|
:type core_ci: AnsibleCoreCI
|
|
|
|
"""
|
|
|
|
self.core_ci = core_ci
|
2017-08-09 07:55:17 +02:00
|
|
|
self.ssh_args = ['-i', self.core_ci.ssh_key.key]
|
|
|
|
|
|
|
|
ssh_options = dict(
|
|
|
|
BatchMode='yes',
|
|
|
|
StrictHostKeyChecking='no',
|
|
|
|
UserKnownHostsFile='/dev/null',
|
|
|
|
ServerAliveInterval=15,
|
|
|
|
ServerAliveCountMax=4,
|
|
|
|
)
|
|
|
|
|
|
|
|
for ssh_option in sorted(ssh_options):
|
|
|
|
self.ssh_args += ['-o', '%s=%s' % (ssh_option, ssh_options[ssh_option])]
|
2016-11-30 06:21:53 +01:00
|
|
|
|
|
|
|
if self.core_ci.platform == 'freebsd':
|
2017-11-29 09:46:08 +01:00
|
|
|
if self.core_ci.provider == 'aws':
|
|
|
|
self.become = ['su', '-l', 'root', '-c']
|
|
|
|
elif self.core_ci.provider == 'azure':
|
|
|
|
self.become = ['sudo', '-in', 'sh', '-c']
|
|
|
|
else:
|
|
|
|
raise NotImplementedError('provider %s has not been implemented' % self.core_ci.provider)
|
2020-08-27 21:08:38 +02:00
|
|
|
elif self.core_ci.platform == 'macos':
|
|
|
|
self.become = ['sudo', '-in', 'PATH=/usr/local/bin:$PATH', 'sh', '-c']
|
2016-11-30 06:21:53 +01:00
|
|
|
elif self.core_ci.platform == 'osx':
|
|
|
|
self.become = ['sudo', '-in', 'PATH=/usr/local/bin:$PATH']
|
2020-04-16 01:22:17 +02:00
|
|
|
elif self.core_ci.platform == 'rhel' or self.core_ci.platform == 'centos':
|
2017-07-08 01:52:40 +02:00
|
|
|
self.become = ['sudo', '-in', 'bash', '-c']
|
2020-01-30 00:56:51 +01:00
|
|
|
elif self.core_ci.platform in ['aix', 'ibmi']:
|
|
|
|
self.become = []
|
2016-11-30 06:21:53 +01:00
|
|
|
|
2019-03-28 00:40:27 +01:00
|
|
|
def setup(self, python_version):
|
|
|
|
"""Start instance and wait for it to become ready and respond to an ansible ping.
|
|
|
|
:type python_version: str
|
2019-07-23 04:24:48 +02:00
|
|
|
:rtype: str
|
2019-03-28 00:40:27 +01:00
|
|
|
"""
|
2019-07-23 04:24:48 +02:00
|
|
|
pwd = self.wait()
|
|
|
|
|
|
|
|
display.info('Remote working directory: %s' % pwd, verbosity=1)
|
2018-11-16 23:50:01 +01:00
|
|
|
|
|
|
|
if isinstance(self.core_ci.args, ShellConfig):
|
|
|
|
if self.core_ci.args.raw:
|
2019-07-23 04:24:48 +02:00
|
|
|
return pwd
|
2018-11-16 23:50:01 +01:00
|
|
|
|
2019-03-28 00:40:27 +01:00
|
|
|
self.configure(python_version)
|
2016-11-30 06:21:53 +01:00
|
|
|
self.upload_source()
|
|
|
|
|
2019-07-23 04:24:48 +02:00
|
|
|
return pwd
|
|
|
|
|
|
|
|
def wait(self): # type: () -> str
|
2016-11-30 06:21:53 +01:00
|
|
|
"""Wait for instance to respond to SSH."""
|
2018-03-22 05:31:03 +01:00
|
|
|
for dummy in range(1, 90):
|
2016-11-30 06:21:53 +01:00
|
|
|
try:
|
2019-07-23 04:24:48 +02:00
|
|
|
stdout = self.ssh('pwd', capture=True)[0]
|
|
|
|
|
|
|
|
if self.core_ci.args.explain:
|
|
|
|
return '/pwd'
|
|
|
|
|
|
|
|
pwd = stdout.strip().splitlines()[-1]
|
|
|
|
|
|
|
|
if not pwd.startswith('/'):
|
|
|
|
raise Exception('Unexpected current working directory "%s" from "pwd" command output:\n%s' % (pwd, stdout))
|
|
|
|
|
|
|
|
return pwd
|
2016-11-30 06:21:53 +01:00
|
|
|
except SubprocessError:
|
2018-03-22 05:31:03 +01:00
|
|
|
time.sleep(10)
|
2016-11-30 06:21:53 +01:00
|
|
|
|
|
|
|
raise ApplicationError('Timeout waiting for %s/%s instance %s.' %
|
|
|
|
(self.core_ci.platform, self.core_ci.version, self.core_ci.instance_id))
|
|
|
|
|
2019-03-28 00:40:27 +01:00
|
|
|
def configure(self, python_version):
|
|
|
|
"""Configure remote host for testing.
|
|
|
|
:type python_version: str
|
|
|
|
"""
|
2019-08-06 02:40:00 +02:00
|
|
|
self.upload(os.path.join(ANSIBLE_TEST_DATA_ROOT, 'setup', 'remote.sh'), '/tmp')
|
2019-03-28 00:40:27 +01:00
|
|
|
self.ssh('chmod +x /tmp/remote.sh && /tmp/remote.sh %s %s' % (self.core_ci.platform, python_version))
|
2016-11-30 06:21:53 +01:00
|
|
|
|
|
|
|
def upload_source(self):
|
|
|
|
"""Upload and extract source."""
|
2017-05-11 07:25:02 +02:00
|
|
|
with tempfile.NamedTemporaryFile(prefix='ansible-source-', suffix='.tgz') as local_source_fd:
|
|
|
|
remote_source_dir = '/tmp'
|
|
|
|
remote_source_path = os.path.join(remote_source_dir, os.path.basename(local_source_fd.name))
|
2016-11-30 06:21:53 +01:00
|
|
|
|
2019-07-16 01:20:03 +02:00
|
|
|
create_payload(self.core_ci.args, local_source_fd.name)
|
2017-05-11 07:25:02 +02:00
|
|
|
|
|
|
|
self.upload(local_source_fd.name, remote_source_dir)
|
2020-01-30 00:56:51 +01:00
|
|
|
# AIX does not provide the GNU tar version, leading to parameters
|
|
|
|
# being different and -z not being recognized. This pattern works
|
|
|
|
# with both versions of tar.
|
|
|
|
self.ssh(
|
2020-04-21 07:04:25 +02:00
|
|
|
'rm -rf ~/ansible ~/ansible_collections && cd ~/ && gunzip --stdout %s | tar oxf - && rm %s' %
|
2020-01-30 00:56:51 +01:00
|
|
|
(remote_source_path, remote_source_path)
|
|
|
|
)
|
2016-11-30 06:21:53 +01:00
|
|
|
|
|
|
|
def download(self, remote, local):
|
|
|
|
"""
|
|
|
|
:type remote: str
|
|
|
|
:type local: str
|
|
|
|
"""
|
|
|
|
self.scp('%s@%s:%s' % (self.core_ci.connection.username, self.core_ci.connection.hostname, remote), local)
|
|
|
|
|
|
|
|
def upload(self, local, remote):
|
|
|
|
"""
|
|
|
|
:type local: str
|
|
|
|
:type remote: str
|
|
|
|
"""
|
|
|
|
self.scp(local, '%s@%s:%s' % (self.core_ci.connection.username, self.core_ci.connection.hostname, remote))
|
|
|
|
|
2019-07-23 04:24:48 +02:00
|
|
|
def ssh(self, command, options=None, capture=False):
|
2016-11-30 06:21:53 +01:00
|
|
|
"""
|
|
|
|
:type command: str | list[str]
|
2017-05-05 10:23:00 +02:00
|
|
|
:type options: list[str] | None
|
2019-07-23 04:24:48 +02:00
|
|
|
:type capture: bool
|
|
|
|
:rtype: str | None, str | None
|
2016-11-30 06:21:53 +01:00
|
|
|
"""
|
2017-05-05 10:23:00 +02:00
|
|
|
if not options:
|
|
|
|
options = []
|
|
|
|
|
2016-11-30 06:21:53 +01:00
|
|
|
if isinstance(command, list):
|
2019-05-24 22:10:33 +02:00
|
|
|
command = ' '.join(cmd_quote(c) for c in command)
|
2016-11-30 06:21:53 +01:00
|
|
|
|
2020-01-30 00:56:51 +01:00
|
|
|
command = cmd_quote(command) if self.become else command
|
2019-07-23 04:24:48 +02:00
|
|
|
return run_command(self.core_ci.args,
|
|
|
|
['ssh', '-tt', '-q'] + self.ssh_args +
|
|
|
|
options +
|
|
|
|
['-p', str(self.core_ci.connection.port),
|
|
|
|
'%s@%s' % (self.core_ci.connection.username, self.core_ci.connection.hostname)] +
|
2020-01-30 00:56:51 +01:00
|
|
|
self.become + [command], capture=capture)
|
2016-11-30 06:21:53 +01:00
|
|
|
|
|
|
|
def scp(self, src, dst):
|
|
|
|
"""
|
|
|
|
:type src: str
|
|
|
|
:type dst: str
|
|
|
|
"""
|
2018-03-24 11:43:26 +01:00
|
|
|
for dummy in range(1, 10):
|
2018-03-22 05:31:03 +01:00
|
|
|
try:
|
|
|
|
run_command(self.core_ci.args,
|
|
|
|
['scp'] + self.ssh_args +
|
|
|
|
['-P', str(self.core_ci.connection.port), '-q', '-r', src, dst])
|
|
|
|
return
|
|
|
|
except SubprocessError:
|
|
|
|
time.sleep(10)
|
|
|
|
|
|
|
|
raise ApplicationError('Failed transfer: %s -> %s' % (src, dst))
|