2016-11-30 06:21:53 +01:00
|
|
|
"""Access Ansible Core CI remote services."""
|
|
|
|
|
|
|
|
from __future__ import absolute_import, print_function
|
|
|
|
|
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
|
|
|
|
|
|
|
import lib.pytar
|
|
|
|
|
|
|
|
from lib.util import (
|
|
|
|
SubprocessError,
|
|
|
|
ApplicationError,
|
|
|
|
run_command,
|
2017-10-26 09:21:46 +02:00
|
|
|
intercept_command,
|
2019-05-24 22:10:33 +02:00
|
|
|
cmd_quote,
|
2016-11-30 06:21:53 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
from lib.core_ci import (
|
|
|
|
AnsibleCoreCI,
|
|
|
|
)
|
|
|
|
|
|
|
|
from lib.ansible_util import (
|
|
|
|
ansible_environment,
|
|
|
|
)
|
|
|
|
|
2018-11-16 23:50:01 +01:00
|
|
|
from lib.config import (
|
|
|
|
ShellConfig,
|
|
|
|
)
|
|
|
|
|
2016-11-30 06:21:53 +01:00
|
|
|
|
|
|
|
class ManageWindowsCI(object):
|
|
|
|
"""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
|
|
|
|
"""
|
2018-08-13 01:27:59 +02:00
|
|
|
pass
|
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)
|
|
|
|
cmd = ['ansible', '-m', 'win_ping', '-i', '%s,' % name, name, '-e', ' '.join(extra_vars)]
|
|
|
|
|
2018-03-22 05:31:03 +01:00
|
|
|
for dummy in range(1, 120):
|
2016-11-30 06:21:53 +01:00
|
|
|
try:
|
2017-10-26 09:21:46 +02:00
|
|
|
intercept_command(self.core_ci.args, cmd, 'ping', env=env)
|
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
|
|
|
|
|
|
|
class ManageNetworkCI(object):
|
|
|
|
"""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."""
|
|
|
|
extra_vars = [
|
|
|
|
'ansible_host=%s' % self.core_ci.connection.hostname,
|
|
|
|
'ansible_port=%s' % self.core_ci.connection.port,
|
2017-12-19 21:49:49 +01:00
|
|
|
'ansible_connection=local',
|
2017-01-08 08:36:35 +01:00
|
|
|
'ansible_ssh_private_key_file=%s' % self.core_ci.ssh_key.key,
|
|
|
|
]
|
|
|
|
|
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',
|
|
|
|
'-m', '%s_command' % self.core_ci.platform,
|
|
|
|
'-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:
|
2017-10-26 09:21:46 +02:00
|
|
|
intercept_command(self.core_ci.args, cmd, 'ping', env=env)
|
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
|
|
|
|
|
|
|
|
|
|
|
class ManagePosixCI(object):
|
|
|
|
"""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)
|
2016-11-30 06:21:53 +01:00
|
|
|
elif self.core_ci.platform == 'osx':
|
|
|
|
self.become = ['sudo', '-in', 'PATH=/usr/local/bin:$PATH']
|
2017-07-08 01:52:40 +02:00
|
|
|
elif self.core_ci.platform == 'rhel':
|
|
|
|
self.become = ['sudo', '-in', 'bash', '-c']
|
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
|
|
|
|
"""
|
2016-11-30 06:21:53 +01:00
|
|
|
self.wait()
|
2018-11-16 23:50:01 +01:00
|
|
|
|
|
|
|
if isinstance(self.core_ci.args, ShellConfig):
|
|
|
|
if self.core_ci.args.raw:
|
|
|
|
return
|
|
|
|
|
2019-03-28 00:40:27 +01:00
|
|
|
self.configure(python_version)
|
2016-11-30 06:21:53 +01:00
|
|
|
self.upload_source()
|
|
|
|
|
|
|
|
def wait(self):
|
|
|
|
"""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:
|
|
|
|
self.ssh('id')
|
|
|
|
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))
|
|
|
|
|
2019-03-28 00:40:27 +01:00
|
|
|
def configure(self, python_version):
|
|
|
|
"""Configure remote host for testing.
|
|
|
|
:type python_version: str
|
|
|
|
"""
|
2016-11-30 06:21:53 +01:00
|
|
|
self.upload('test/runner/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
|
|
|
|
2017-05-11 07:25:02 +02:00
|
|
|
if not self.core_ci.args.explain:
|
2017-10-26 09:21:46 +02:00
|
|
|
lib.pytar.create_tarfile(local_source_fd.name, '.', lib.pytar.DefaultTarFilter())
|
2017-05-11 07:25:02 +02:00
|
|
|
|
|
|
|
self.upload(local_source_fd.name, remote_source_dir)
|
|
|
|
self.ssh('rm -rf ~/ansible && mkdir ~/ansible && cd ~/ansible && tar oxzf %s' % 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))
|
|
|
|
|
2017-05-05 10:23:00 +02:00
|
|
|
def ssh(self, command, options=None):
|
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
|
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
|
|
|
|
|
|
|
run_command(self.core_ci.args,
|
|
|
|
['ssh', '-tt', '-q'] + self.ssh_args +
|
2017-05-05 10:23:00 +02:00
|
|
|
options +
|
2016-11-30 06:21:53 +01:00
|
|
|
['-p', str(self.core_ci.connection.port),
|
|
|
|
'%s@%s' % (self.core_ci.connection.username, self.core_ci.connection.hostname)] +
|
2019-05-24 22:10:33 +02:00
|
|
|
self.become + [cmd_quote(command)])
|
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))
|