2016-11-30 05:21:53 +00:00
|
|
|
"""Miscellaneous utility functions and classes specific to ansible cli tools."""
|
2019-07-12 06:46:20 +00:00
|
|
|
from __future__ import (absolute_import, division, print_function)
|
|
|
|
__metaclass__ = type
|
2016-11-30 05:21:53 +00:00
|
|
|
|
2019-06-05 05:08:23 +00:00
|
|
|
import json
|
2016-11-30 05:21:53 +00:00
|
|
|
import os
|
|
|
|
|
2019-02-18 21:28:25 +00:00
|
|
|
from lib.constants import (
|
|
|
|
SOFT_RLIMIT_NOFILE,
|
|
|
|
)
|
|
|
|
|
2017-10-04 18:36:53 +00:00
|
|
|
from lib.util import (
|
|
|
|
common_environment,
|
2019-06-05 05:08:23 +00:00
|
|
|
display,
|
|
|
|
find_python,
|
2017-10-04 18:36:53 +00:00
|
|
|
ApplicationError,
|
2019-07-23 02:24:48 +00:00
|
|
|
ANSIBLE_ROOT,
|
2017-10-04 18:36:53 +00:00
|
|
|
)
|
|
|
|
|
2019-07-11 05:00:34 +00:00
|
|
|
from lib.util_common import (
|
|
|
|
run_command,
|
|
|
|
)
|
|
|
|
|
2017-10-04 18:36:53 +00:00
|
|
|
from lib.config import (
|
|
|
|
IntegrationConfig,
|
2019-06-05 05:08:23 +00:00
|
|
|
EnvironmentConfig,
|
2017-10-04 18:36:53 +00:00
|
|
|
)
|
2016-11-30 05:21:53 +00:00
|
|
|
|
2019-07-23 02:24:48 +00:00
|
|
|
from lib.data import (
|
|
|
|
data_context,
|
|
|
|
)
|
2016-11-30 05:21:53 +00:00
|
|
|
|
2019-06-05 05:08:23 +00:00
|
|
|
CHECK_YAML_VERSIONS = {}
|
|
|
|
|
|
|
|
|
2019-01-10 22:43:21 +00:00
|
|
|
def ansible_environment(args, color=True, ansible_config=None):
|
2016-11-30 05:21:53 +00:00
|
|
|
"""
|
|
|
|
:type args: CommonConfig
|
2017-03-08 19:45:38 +00:00
|
|
|
:type color: bool
|
2019-01-10 22:43:21 +00:00
|
|
|
:type ansible_config: str | None
|
2016-11-30 05:21:53 +00:00
|
|
|
:rtype: dict[str, str]
|
|
|
|
"""
|
|
|
|
env = common_environment()
|
|
|
|
path = env['PATH']
|
|
|
|
|
2019-07-23 02:24:48 +00:00
|
|
|
ansible_path = os.path.join(ANSIBLE_ROOT, 'bin')
|
2016-11-30 05:21:53 +00:00
|
|
|
|
2018-09-21 18:38:22 +00:00
|
|
|
if not path.startswith(ansible_path + os.path.pathsep):
|
|
|
|
path = ansible_path + os.path.pathsep + path
|
2016-11-30 05:21:53 +00:00
|
|
|
|
2019-01-10 22:43:21 +00:00
|
|
|
if ansible_config:
|
|
|
|
pass
|
|
|
|
elif isinstance(args, IntegrationConfig):
|
2019-07-23 02:24:48 +00:00
|
|
|
ansible_config = os.path.join(ANSIBLE_ROOT, 'test/integration/%s.cfg' % args.command)
|
2017-10-04 18:36:53 +00:00
|
|
|
else:
|
2019-07-23 02:24:48 +00:00
|
|
|
ansible_config = os.path.join(ANSIBLE_ROOT, 'test/%s/ansible.cfg' % args.command)
|
2017-10-04 18:36:53 +00:00
|
|
|
|
2019-01-10 22:43:21 +00:00
|
|
|
if not args.explain and not os.path.exists(ansible_config):
|
2017-10-04 18:36:53 +00:00
|
|
|
raise ApplicationError('Configuration not found: %s' % ansible_config)
|
|
|
|
|
2016-11-30 05:21:53 +00:00
|
|
|
ansible = dict(
|
2019-02-18 21:28:25 +00:00
|
|
|
ANSIBLE_PYTHON_MODULE_RLIMIT_NOFILE=str(SOFT_RLIMIT_NOFILE),
|
2017-03-08 19:45:38 +00:00
|
|
|
ANSIBLE_FORCE_COLOR='%s' % 'true' if args.color and color else 'false',
|
2016-11-30 05:21:53 +00:00
|
|
|
ANSIBLE_DEPRECATION_WARNINGS='false',
|
2017-01-08 07:36:35 +00:00
|
|
|
ANSIBLE_HOST_KEY_CHECKING='false',
|
2019-01-08 22:03:15 +00:00
|
|
|
ANSIBLE_RETRY_FILES_ENABLED='false',
|
2017-10-04 18:36:53 +00:00
|
|
|
ANSIBLE_CONFIG=os.path.abspath(ansible_config),
|
2018-09-21 19:55:04 +00:00
|
|
|
ANSIBLE_LIBRARY='/dev/null',
|
2019-07-23 02:24:48 +00:00
|
|
|
PYTHONPATH=os.path.join(ANSIBLE_ROOT, 'lib'),
|
2016-11-30 05:21:53 +00:00
|
|
|
PAGER='/bin/cat',
|
|
|
|
PATH=path,
|
|
|
|
)
|
|
|
|
|
|
|
|
env.update(ansible)
|
|
|
|
|
2017-02-01 19:15:08 +00:00
|
|
|
if args.debug:
|
2017-10-26 07:21:46 +00:00
|
|
|
env.update(dict(
|
|
|
|
ANSIBLE_DEBUG='true',
|
|
|
|
ANSIBLE_LOG_PATH=os.path.abspath('test/results/logs/debug.log'),
|
|
|
|
))
|
2017-02-01 19:15:08 +00:00
|
|
|
|
2019-07-23 02:24:48 +00:00
|
|
|
if data_context().content.collection:
|
|
|
|
env.update(dict(
|
|
|
|
ANSIBLE_COLLECTIONS_PATHS=data_context().content.collection.root,
|
|
|
|
))
|
|
|
|
|
2016-11-30 05:21:53 +00:00
|
|
|
return env
|
2019-06-05 05:08:23 +00: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-23 02:24:48 +00:00
|
|
|
stdout, _dummy = run_command(args, [python, os.path.join(ANSIBLE_ROOT, 'test/runner/yamlcheck.py')], capture=True)
|
2019-06-05 05:08:23 +00: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)
|