2019-07-16 01:20:03 +02:00
|
|
|
"""Payload management for sending Ansible files and test content to other systems (VMs, containers)."""
|
|
|
|
from __future__ import (absolute_import, division, print_function)
|
|
|
|
__metaclass__ = type
|
|
|
|
|
2019-08-09 02:21:38 +02:00
|
|
|
import atexit
|
2019-07-23 04:24:48 +02:00
|
|
|
import os
|
2019-08-21 21:12:52 +02:00
|
|
|
import stat
|
2019-07-23 04:24:48 +02:00
|
|
|
import tarfile
|
2019-08-09 02:21:38 +02:00
|
|
|
import tempfile
|
2019-07-23 04:24:48 +02:00
|
|
|
import time
|
|
|
|
|
2019-08-09 02:21:38 +02:00
|
|
|
from . import types as t
|
|
|
|
|
2019-08-06 23:43:29 +02:00
|
|
|
from .config import (
|
2019-07-23 04:24:48 +02:00
|
|
|
IntegrationConfig,
|
|
|
|
ShellConfig,
|
|
|
|
)
|
|
|
|
|
2019-08-06 23:43:29 +02:00
|
|
|
from .util import (
|
2019-07-23 04:24:48 +02:00
|
|
|
display,
|
2019-08-09 08:51:03 +02:00
|
|
|
ANSIBLE_SOURCE_ROOT,
|
2019-08-09 02:21:38 +02:00
|
|
|
remove_tree,
|
|
|
|
is_subdir,
|
2019-07-16 01:20:03 +02:00
|
|
|
)
|
|
|
|
|
2019-08-06 23:43:29 +02:00
|
|
|
from .data import (
|
2019-07-23 04:24:48 +02:00
|
|
|
data_context,
|
2019-07-16 01:20:03 +02:00
|
|
|
)
|
|
|
|
|
2019-08-06 23:43:29 +02:00
|
|
|
from .util_common import (
|
2019-07-23 04:24:48 +02:00
|
|
|
CommonConfig,
|
|
|
|
)
|
|
|
|
|
|
|
|
# improve performance by disabling uid/gid lookups
|
|
|
|
tarfile.pwd = None
|
|
|
|
tarfile.grp = None
|
|
|
|
|
2019-08-09 02:21:38 +02:00
|
|
|
# this bin symlink map must exactly match the contents of the bin directory
|
|
|
|
# it is necessary for payload creation to reconstruct the bin directory when running ansible-test from an installed version of ansible
|
|
|
|
ANSIBLE_BIN_SYMLINK_MAP = {
|
|
|
|
'ansible': '../lib/ansible/cli/scripts/ansible_cli_stub.py',
|
|
|
|
'ansible-config': 'ansible',
|
|
|
|
'ansible-connection': '../lib/ansible/cli/scripts/ansible_connection_cli_stub.py',
|
|
|
|
'ansible-console': 'ansible',
|
|
|
|
'ansible-doc': 'ansible',
|
|
|
|
'ansible-galaxy': 'ansible',
|
|
|
|
'ansible-inventory': 'ansible',
|
|
|
|
'ansible-playbook': 'ansible',
|
|
|
|
'ansible-pull': 'ansible',
|
|
|
|
'ansible-test': '../test/lib/ansible_test/_data/cli/ansible_test_cli_stub.py',
|
|
|
|
'ansible-vault': 'ansible',
|
|
|
|
}
|
|
|
|
|
2019-07-16 01:20:03 +02:00
|
|
|
|
|
|
|
def create_payload(args, dst_path): # type: (CommonConfig, str) -> None
|
|
|
|
"""Create a payload for delegation."""
|
|
|
|
if args.explain:
|
|
|
|
return
|
|
|
|
|
2019-08-09 02:21:38 +02:00
|
|
|
files = list(data_context().ansible_source)
|
2019-08-21 21:12:52 +02:00
|
|
|
filters = {}
|
|
|
|
|
|
|
|
def make_executable(tar_info): # type: (tarfile.TarInfo) -> t.Optional[tarfile.TarInfo]
|
|
|
|
"""Make the given file executable."""
|
|
|
|
tar_info.mode |= stat.S_IXUSR | stat.S_IXOTH | stat.S_IXGRP
|
|
|
|
return tar_info
|
2019-08-09 02:21:38 +02:00
|
|
|
|
2019-08-09 08:51:03 +02:00
|
|
|
if not ANSIBLE_SOURCE_ROOT:
|
2019-08-09 02:21:38 +02:00
|
|
|
# reconstruct the bin directory which is not available when running from an ansible install
|
|
|
|
files.extend(create_temporary_bin_files(args))
|
2019-08-21 21:12:52 +02:00
|
|
|
filters.update(dict((path[3:], make_executable) for path in ANSIBLE_BIN_SYMLINK_MAP.values() if path.startswith('../')))
|
2019-07-23 04:24:48 +02:00
|
|
|
|
|
|
|
if not data_context().content.is_ansible:
|
2019-08-09 02:21:38 +02:00
|
|
|
# exclude unnecessary files when not testing ansible itself
|
2019-07-23 04:24:48 +02:00
|
|
|
files = [f for f in files if
|
2019-08-09 02:21:38 +02:00
|
|
|
is_subdir(f[1], 'bin/') or
|
|
|
|
is_subdir(f[1], 'lib/ansible/') or
|
|
|
|
(is_subdir(f[1], 'test/lib/ansible_test/') and not is_subdir(f[1], 'test/lib/ansible_test/tests/'))]
|
2019-07-23 04:24:48 +02:00
|
|
|
|
|
|
|
if not isinstance(args, (ShellConfig, IntegrationConfig)):
|
2019-08-09 02:21:38 +02:00
|
|
|
# exclude built-in ansible modules when they are not needed
|
|
|
|
files = [f for f in files if not is_subdir(f[1], 'lib/ansible/modules/') or f[1] == 'lib/ansible/modules/__init__.py']
|
|
|
|
|
2019-08-27 09:59:55 +02:00
|
|
|
collection_layouts = data_context().create_collection_layouts()
|
|
|
|
|
|
|
|
for layout in collection_layouts:
|
|
|
|
# include files from each collection in the same collection root as the content being tested
|
|
|
|
files.extend((os.path.join(layout.root, path), os.path.join(layout.collection.directory, path)) for path in layout.all_files())
|
2019-08-09 02:21:38 +02:00
|
|
|
|
2019-07-23 04:24:48 +02:00
|
|
|
for callback in data_context().payload_callbacks:
|
|
|
|
callback(files)
|
|
|
|
|
2019-08-09 02:21:38 +02:00
|
|
|
# maintain predictable file order
|
2019-08-21 21:12:37 +02:00
|
|
|
files = sorted(set(files))
|
2019-08-09 02:21:38 +02:00
|
|
|
|
2019-07-23 04:24:48 +02:00
|
|
|
display.info('Creating a payload archive containing %d files...' % len(files), verbosity=1)
|
|
|
|
|
|
|
|
start = time.time()
|
|
|
|
|
2020-01-30 03:21:49 +01:00
|
|
|
with tarfile.TarFile.open(dst_path, mode='w:gz', compresslevel=4, format=tarfile.GNU_FORMAT) as tar:
|
2019-07-23 04:24:48 +02:00
|
|
|
for src, dst in files:
|
|
|
|
display.info('%s -> %s' % (src, dst), verbosity=4)
|
2019-08-21 21:12:52 +02:00
|
|
|
tar.add(src, dst, filter=filters.get(dst))
|
2019-07-23 04:24:48 +02:00
|
|
|
|
|
|
|
duration = time.time() - start
|
|
|
|
payload_size_bytes = os.path.getsize(dst_path)
|
2019-07-16 01:20:03 +02:00
|
|
|
|
2019-07-23 04:24:48 +02:00
|
|
|
display.info('Created a %d byte payload archive containing %d files in %d seconds.' % (payload_size_bytes, len(files), duration), verbosity=1)
|
2019-08-09 02:21:38 +02:00
|
|
|
|
|
|
|
|
|
|
|
def create_temporary_bin_files(args): # type: (CommonConfig) -> t.Tuple[t.Tuple[str, str], ...]
|
|
|
|
"""Create a temporary ansible bin directory populated using the symlink map."""
|
|
|
|
if args.explain:
|
|
|
|
temp_path = '/tmp/ansible-tmp-bin'
|
|
|
|
else:
|
|
|
|
temp_path = tempfile.mkdtemp(prefix='ansible', suffix='bin')
|
|
|
|
atexit.register(remove_tree, temp_path)
|
|
|
|
|
|
|
|
for name, dest in ANSIBLE_BIN_SYMLINK_MAP.items():
|
|
|
|
path = os.path.join(temp_path, name)
|
2019-08-16 21:47:21 +02:00
|
|
|
os.symlink(dest, path)
|
2019-08-09 02:21:38 +02:00
|
|
|
|
|
|
|
return tuple((os.path.join(temp_path, name), os.path.join('bin', name)) for name in sorted(ANSIBLE_BIN_SYMLINK_MAP))
|