2016-11-30 06:21:53 +01:00
|
|
|
"""Miscellaneous utility functions and classes specific to ansible cli tools."""
|
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
|
|
|
|
2019-06-05 07:08:23 +02:00
|
|
|
import json
|
2016-11-30 06:21:53 +01:00
|
|
|
import os
|
|
|
|
|
2019-02-18 22:28:25 +01:00
|
|
|
from lib.constants import (
|
|
|
|
SOFT_RLIMIT_NOFILE,
|
|
|
|
)
|
|
|
|
|
2017-10-04 20:36:53 +02:00
|
|
|
from lib.util import (
|
|
|
|
common_environment,
|
2019-06-05 07:08:23 +02:00
|
|
|
display,
|
|
|
|
find_python,
|
2017-10-04 20:36:53 +02:00
|
|
|
ApplicationError,
|
2019-07-10 02:31:04 +02:00
|
|
|
INSTALL_ROOT,
|
2017-10-04 20:36:53 +02:00
|
|
|
)
|
|
|
|
|
2019-07-11 07:00:34 +02:00
|
|
|
from lib.util_common import (
|
|
|
|
run_command,
|
|
|
|
)
|
|
|
|
|
2017-10-04 20:36:53 +02:00
|
|
|
from lib.config import (
|
|
|
|
IntegrationConfig,
|
2019-06-05 07:08:23 +02:00
|
|
|
EnvironmentConfig,
|
2017-10-04 20:36:53 +02:00
|
|
|
)
|
2016-11-30 06:21:53 +01:00
|
|
|
|
|
|
|
|
2019-06-05 07:08:23 +02:00
|
|
|
CHECK_YAML_VERSIONS = {}
|
|
|
|
|
|
|
|
|
2019-01-10 23:43:21 +01:00
|
|
|
def ansible_environment(args, color=True, ansible_config=None):
|
2016-11-30 06:21:53 +01:00
|
|
|
"""
|
|
|
|
:type args: CommonConfig
|
2017-03-08 20:45:38 +01:00
|
|
|
:type color: bool
|
2019-01-10 23:43:21 +01:00
|
|
|
:type ansible_config: str | None
|
2016-11-30 06:21:53 +01:00
|
|
|
:rtype: dict[str, str]
|
|
|
|
"""
|
|
|
|
env = common_environment()
|
|
|
|
path = env['PATH']
|
|
|
|
|
2019-07-10 02:31:04 +02:00
|
|
|
ansible_path = os.path.join(INSTALL_ROOT, 'bin')
|
2016-11-30 06:21:53 +01:00
|
|
|
|
2018-09-21 20:38:22 +02:00
|
|
|
if not path.startswith(ansible_path + os.path.pathsep):
|
|
|
|
path = ansible_path + os.path.pathsep + path
|
2016-11-30 06:21:53 +01:00
|
|
|
|
2019-01-10 23:43:21 +01:00
|
|
|
if ansible_config:
|
|
|
|
pass
|
|
|
|
elif isinstance(args, IntegrationConfig):
|
2019-07-10 02:31:04 +02:00
|
|
|
ansible_config = os.path.join(INSTALL_ROOT, 'test/integration/%s.cfg' % args.command)
|
2017-10-04 20:36:53 +02:00
|
|
|
else:
|
2019-07-10 02:31:04 +02:00
|
|
|
ansible_config = os.path.join(INSTALL_ROOT, 'test/%s/ansible.cfg' % args.command)
|
2017-10-04 20:36:53 +02:00
|
|
|
|
2019-01-10 23:43:21 +01:00
|
|
|
if not args.explain and not os.path.exists(ansible_config):
|
2017-10-04 20:36:53 +02:00
|
|
|
raise ApplicationError('Configuration not found: %s' % ansible_config)
|
|
|
|
|
2016-11-30 06:21:53 +01:00
|
|
|
ansible = dict(
|
2019-02-18 22:28:25 +01:00
|
|
|
ANSIBLE_PYTHON_MODULE_RLIMIT_NOFILE=str(SOFT_RLIMIT_NOFILE),
|
2017-03-08 20:45:38 +01:00
|
|
|
ANSIBLE_FORCE_COLOR='%s' % 'true' if args.color and color else 'false',
|
2016-11-30 06:21:53 +01:00
|
|
|
ANSIBLE_DEPRECATION_WARNINGS='false',
|
2017-01-08 08:36:35 +01:00
|
|
|
ANSIBLE_HOST_KEY_CHECKING='false',
|
2019-01-08 23:03:15 +01:00
|
|
|
ANSIBLE_RETRY_FILES_ENABLED='false',
|
2017-10-04 20:36:53 +02:00
|
|
|
ANSIBLE_CONFIG=os.path.abspath(ansible_config),
|
2018-09-21 21:55:04 +02:00
|
|
|
ANSIBLE_LIBRARY='/dev/null',
|
2019-07-10 02:31:04 +02:00
|
|
|
PYTHONPATH=os.path.join(INSTALL_ROOT, 'lib'),
|
2016-11-30 06:21:53 +01:00
|
|
|
PAGER='/bin/cat',
|
|
|
|
PATH=path,
|
|
|
|
)
|
|
|
|
|
|
|
|
env.update(ansible)
|
|
|
|
|
2017-02-01 20:15:08 +01:00
|
|
|
if args.debug:
|
2017-10-26 09:21:46 +02:00
|
|
|
env.update(dict(
|
|
|
|
ANSIBLE_DEBUG='true',
|
|
|
|
ANSIBLE_LOG_PATH=os.path.abspath('test/results/logs/debug.log'),
|
|
|
|
))
|
2017-02-01 20:15:08 +01:00
|
|
|
|
2016-11-30 06:21:53 +01:00
|
|
|
return env
|
2019-06-05 07:08:23 +02:00
|
|
|
|
|
|
|
|
|
|
|
def check_pyyaml(args, version):
|
|
|
|
"""
|
|
|
|
:type args: EnvironmentConfig
|
|
|
|
:type version: str
|
|
|
|
"""
|
|
|
|
if version in CHECK_YAML_VERSIONS:
|
|
|
|
return
|
|
|
|
|
|
|
|
python = find_python(version)
|
2019-07-10 02:31:04 +02:00
|
|
|
stdout, _dummy = run_command(args, [python, os.path.join(INSTALL_ROOT, 'test/runner/yamlcheck.py')], capture=True)
|
2019-06-05 07:08:23 +02:00
|
|
|
|
|
|
|
if args.explain:
|
|
|
|
return
|
|
|
|
|
|
|
|
CHECK_YAML_VERSIONS[version] = result = json.loads(stdout)
|
|
|
|
|
|
|
|
yaml = result['yaml']
|
|
|
|
cloader = result['cloader']
|
|
|
|
|
|
|
|
if not yaml:
|
|
|
|
display.warning('PyYAML is not installed for interpreter: %s' % python)
|
|
|
|
elif not cloader:
|
|
|
|
display.warning('PyYAML will be slow due to installation without libyaml support for interpreter: %s' % python)
|