2016-11-30 05:21:53 +00:00
|
|
|
"""Access Ansible Core CI remote services."""
|
|
|
|
|
|
|
|
from __future__ import absolute_import, print_function
|
|
|
|
|
2017-05-11 05:25:02 +00:00
|
|
|
import os
|
2016-11-30 05:21:53 +00:00
|
|
|
import pipes
|
2017-05-11 05:25:02 +00:00
|
|
|
import tempfile
|
2018-03-22 04:31:03 +00:00
|
|
|
import time
|
2016-11-30 05:21:53 +00:00
|
|
|
|
|
|
|
import lib.pytar
|
|
|
|
|
|
|
|
from lib.util import (
|
|
|
|
SubprocessError,
|
|
|
|
ApplicationError,
|
|
|
|
run_command,
|
2017-10-26 07:21:46 +00:00
|
|
|
intercept_command,
|
2016-11-30 05:21:53 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
from lib.core_ci import (
|
|
|
|
AnsibleCoreCI,
|
|
|
|
)
|
|
|
|
|
|
|
|
from lib.ansible_util import (
|
|
|
|
ansible_environment,
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
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-12 23:27:59 +00: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])]
|
|
|
|
|
|
|
|
def setup(self):
|
|
|
|
"""Used in delegate_remote to setup the host, no action is required for Windows."""
|
|
|
|
pass
|
2016-11-30 05:21:53 +00: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 04:31:03 +00:00
|
|
|
for dummy in range(1, 120):
|
2016-11-30 05:21:53 +00:00
|
|
|
try:
|
2017-10-26 07:21:46 +00:00
|
|
|
intercept_command(self.core_ci.args, cmd, 'ping', env=env)
|
2016-11-30 05:21:53 +00:00
|
|
|
return
|
|
|
|
except SubprocessError:
|
2018-03-22 04:31:03 +00:00
|
|
|
time.sleep(10)
|
2016-11-30 05:21:53 +00: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 07:36:35 +00:00
|
|
|
|
2018-08-12 23:27:59 +00:00
|
|
|
def ssh(self, command, options=None):
|
|
|
|
"""
|
|
|
|
:type command: str | list[str]
|
|
|
|
:type options: list[str] | None
|
|
|
|
"""
|
|
|
|
if not options:
|
|
|
|
options = []
|
|
|
|
|
|
|
|
if isinstance(command, list):
|
|
|
|
command = ' '.join(pipes.quote(c) for c in command)
|
|
|
|
|
|
|
|
run_command(self.core_ci.args,
|
|
|
|
['ssh', '-tt', '-q'] + self.ssh_args +
|
|
|
|
options +
|
|
|
|
['-p', '22',
|
|
|
|
'%s@%s' % (self.core_ci.connection.username, self.core_ci.connection.hostname)] +
|
|
|
|
[command])
|
|
|
|
|
2017-01-08 07:36:35 +00: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 20:49:49 +00:00
|
|
|
'ansible_connection=local',
|
2017-01-08 07:36:35 +00:00
|
|
|
'ansible_ssh_private_key_file=%s' % self.core_ci.ssh_key.key,
|
|
|
|
]
|
|
|
|
|
2017-07-03 22:00:16 +00:00
|
|
|
name = '%s-%s' % (self.core_ci.platform, self.core_ci.version.replace('.', '-'))
|
2017-01-08 07:36:35 +00:00
|
|
|
|
|
|
|
env = ansible_environment(self.core_ci.args)
|
2017-05-12 11:49:12 +00: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 07:36:35 +00:00
|
|
|
|
2018-03-22 04:31:03 +00:00
|
|
|
for dummy in range(1, 90):
|
2017-01-08 07:36:35 +00:00
|
|
|
try:
|
2017-10-26 07:21:46 +00:00
|
|
|
intercept_command(self.core_ci.args, cmd, 'ping', env=env)
|
2017-01-08 07:36:35 +00:00
|
|
|
return
|
|
|
|
except SubprocessError:
|
2018-03-22 04:31:03 +00:00
|
|
|
time.sleep(10)
|
2017-01-08 07:36:35 +00: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 05:21:53 +00: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 05:55:17 +00: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 05:21:53 +00:00
|
|
|
|
|
|
|
if self.core_ci.platform == 'freebsd':
|
2017-11-29 08:46:08 +00: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 05:21:53 +00:00
|
|
|
elif self.core_ci.platform == 'osx':
|
|
|
|
self.become = ['sudo', '-in', 'PATH=/usr/local/bin:$PATH']
|
2017-07-07 23:52:40 +00:00
|
|
|
elif self.core_ci.platform == 'rhel':
|
|
|
|
self.become = ['sudo', '-in', 'bash', '-c']
|
2016-11-30 05:21:53 +00:00
|
|
|
|
|
|
|
def setup(self):
|
|
|
|
"""Start instance and wait for it to become ready and respond to an ansible ping."""
|
|
|
|
self.wait()
|
|
|
|
self.configure()
|
|
|
|
self.upload_source()
|
|
|
|
|
|
|
|
def wait(self):
|
|
|
|
"""Wait for instance to respond to SSH."""
|
2018-03-22 04:31:03 +00:00
|
|
|
for dummy in range(1, 90):
|
2016-11-30 05:21:53 +00:00
|
|
|
try:
|
|
|
|
self.ssh('id')
|
|
|
|
return
|
|
|
|
except SubprocessError:
|
2018-03-22 04:31:03 +00:00
|
|
|
time.sleep(10)
|
2016-11-30 05:21:53 +00:00
|
|
|
|
|
|
|
raise ApplicationError('Timeout waiting for %s/%s instance %s.' %
|
|
|
|
(self.core_ci.platform, self.core_ci.version, self.core_ci.instance_id))
|
|
|
|
|
|
|
|
def configure(self):
|
|
|
|
"""Configure remote host for testing."""
|
|
|
|
self.upload('test/runner/setup/remote.sh', '/tmp')
|
|
|
|
self.ssh('chmod +x /tmp/remote.sh && /tmp/remote.sh %s' % self.core_ci.platform)
|
|
|
|
|
|
|
|
def upload_source(self):
|
|
|
|
"""Upload and extract source."""
|
2017-05-11 05:25:02 +00: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 05:21:53 +00:00
|
|
|
|
2017-05-11 05:25:02 +00:00
|
|
|
if not self.core_ci.args.explain:
|
2017-10-26 07:21:46 +00:00
|
|
|
lib.pytar.create_tarfile(local_source_fd.name, '.', lib.pytar.DefaultTarFilter())
|
2017-05-11 05:25:02 +00: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 05:21:53 +00: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 08:23:00 +00:00
|
|
|
def ssh(self, command, options=None):
|
2016-11-30 05:21:53 +00:00
|
|
|
"""
|
|
|
|
:type command: str | list[str]
|
2017-05-05 08:23:00 +00:00
|
|
|
:type options: list[str] | None
|
2016-11-30 05:21:53 +00:00
|
|
|
"""
|
2017-05-05 08:23:00 +00:00
|
|
|
if not options:
|
|
|
|
options = []
|
|
|
|
|
2016-11-30 05:21:53 +00:00
|
|
|
if isinstance(command, list):
|
|
|
|
command = ' '.join(pipes.quote(c) for c in command)
|
|
|
|
|
|
|
|
run_command(self.core_ci.args,
|
|
|
|
['ssh', '-tt', '-q'] + self.ssh_args +
|
2017-05-05 08:23:00 +00:00
|
|
|
options +
|
2016-11-30 05:21:53 +00:00
|
|
|
['-p', str(self.core_ci.connection.port),
|
|
|
|
'%s@%s' % (self.core_ci.connection.username, self.core_ci.connection.hostname)] +
|
|
|
|
self.become + [pipes.quote(command)])
|
|
|
|
|
|
|
|
def scp(self, src, dst):
|
|
|
|
"""
|
|
|
|
:type src: str
|
|
|
|
:type dst: str
|
|
|
|
"""
|
2018-03-24 10:43:26 +00:00
|
|
|
for dummy in range(1, 10):
|
2018-03-22 04:31:03 +00: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))
|