diff options
| author | leiyashuai <leiyashuai@inspur.com> | 2022-09-12 15:56:04 +0800 |
|---|---|---|
| committer | Mark Goddard <mark@stackhpc.com> | 2024-08-21 08:52:12 +0000 |
| commit | b3a66ef3d4f360b4dce14e9051dc32861ef44018 (patch) | |
| tree | 5f2eab26b7711905334cf8a6609e4b7ab8a3b6a9 /ansible/library | |
| parent | 4615f247fcf50bcdb5f7329f0bd88b58dac57302 (diff) | |
| download | kolla-ansible-b3a66ef3d4f360b4dce14e9051dc32861ef44018.tar.gz kolla-ansible-b3a66ef3d4f360b4dce14e9051dc32861ef44018.zip | |
Remove "distutils" library
Library "distutils" is deprecated in Python 3.10:
https://peps.python.org/pep-0632/
The versions previously referenced using StrictVersion should be old
enough that they will not be used in a Dalmatian deployment:
- Ansible 2.11
- Docker API 1.42, included since Docker engine 23.0.0
Change-Id: Ie315004715a1cb5a91dd54bc64b0a8fd0af650ec
Diffstat (limited to 'ansible/library')
| -rw-r--r-- | ansible/library/kolla_toolbox.py | 130 |
1 files changed, 42 insertions, 88 deletions
diff --git a/ansible/library/kolla_toolbox.py b/ansible/library/kolla_toolbox.py index acfc8a15a..b3bc82dc4 100644 --- a/ansible/library/kolla_toolbox.py +++ b/ansible/library/kolla_toolbox.py @@ -12,11 +12,9 @@ # See the License for the specific language governing permissions and # limitations under the License. -from distutils.version import StrictVersion import json import re -from ansible.module_utils.ansible_release import __version__ as ansible_version from ansible.module_utils.basic import AnsibleModule from ast import literal_eval @@ -117,17 +115,14 @@ def gen_commandline(params): if params.get('module_name'): command.extend(['-m', params.get('module_name')]) if params.get('module_args'): - if StrictVersion(ansible_version) < StrictVersion('2.11.0'): - module_args = params.get('module_args') - else: - try: - module_args = literal_eval(params.get('module_args')) - except SyntaxError: - if not isinstance(params.get('module_args'), str): - raise - - # account for string arguments - module_args = split(params.get('module_args')) + try: + module_args = literal_eval(params.get('module_args')) + except SyntaxError: + if not isinstance(params.get('module_args'), str): + raise + + # account for string arguments + module_args = split(params.get('module_args')) if isinstance(module_args, dict): module_args = ' '.join("{}='{}'".format(key, value) for key, value in module_args.items()) @@ -147,11 +142,6 @@ def get_docker_client(): return docker.APIClient -def docker_supports_environment_in_exec(client): - docker_version = StrictVersion(client.api_version) - return docker_version >= StrictVersion('1.25') - - def use_docker(module): client = get_docker_client()( version=module.params.get('api_version'), @@ -167,79 +157,43 @@ def use_docker(module): if 'user' in module.params: kwargs['user'] = module.params['user'] - # NOTE(mgoddard): Docker 1.12 has API version 1.24, and was installed by - # kolla-ansible bootstrap-servers on Rocky and earlier releases. This API - # version does not have support for specifying environment variables for - # exec jobs, which is necessary to use the Ansible JSON output formatter. - # While we continue to support this version of Docker, fall back to the old - # regex-based method for API version 1.24 and earlier. - # TODO(mgoddard): Remove this conditional (keep the if) when we require - # Docker API version 1.25+. - if docker_supports_environment_in_exec(client): - # Use the JSON output formatter, so that we can parse it. - environment = {"ANSIBLE_STDOUT_CALLBACK": "json", - "ANSIBLE_LOAD_CALLBACK_PLUGINS": "True"} - job = client.exec_create(kolla_toolbox, command_line, - environment=environment, **kwargs) - json_output = client.exec_start(job) + # Use the JSON output formatter, so that we can parse it. + environment = {"ANSIBLE_STDOUT_CALLBACK": "json", + "ANSIBLE_LOAD_CALLBACK_PLUGINS": "True"} + job = client.exec_create(kolla_toolbox, command_line, + environment=environment, **kwargs) + json_output = client.exec_start(job) - try: - output = json.loads(json_output) - except Exception: - module.fail_json( - msg='Can not parse the inner module output: %s' % json_output) - - # Expected format is the following: - # { - # "plays": [ - # { - # "tasks": [ - # { - # "hosts": { - # "localhost": { - # <module result> - # } - # } - # } - # ] - # { - # ] - # } - try: - ret = output['plays'][0]['tasks'][0]['hosts']['localhost'] - except (KeyError, IndexError): - module.fail_json( - msg='Ansible JSON output has unexpected format: %s' % output) + try: + output = json.loads(json_output) + except Exception: + module.fail_json( + msg='Can not parse the inner module output: %s' % json_output) - # Remove Ansible's internal variables from returned fields. - ret.pop('_ansible_no_log', None) - else: - job = client.exec_create(kolla_toolbox, command_line, **kwargs) - output = client.exec_start(job) - - for exp in [JSON_REG, NON_JSON_REG]: - m = exp.match(output) - if m: - inner_output = m.groupdict().get('stdout') - status = m.groupdict().get('status') - break - else: - module.fail_json( - msg='Can not parse the inner module output: %s' % output) - - ret = dict() - try: - ret = json.loads(inner_output) - except ValueError: - # Some modules (e.g. command) do not produce a JSON output. - # Instead, check the status, and assume changed on success. - ret['stdout'] = inner_output - if status != "SUCCESS": - ret['failed'] = True - else: - # No way to know whether changed - assume yes. - ret['changed'] = True + # Expected format is the following: + # { + # "plays": [ + # { + # "tasks": [ + # { + # "hosts": { + # "localhost": { + # <module result> + # } + # } + # } + # ] + # { + # ] + # } + try: + ret = output['plays'][0]['tasks'][0]['hosts']['localhost'] + except (KeyError, IndexError): + module.fail_json( + msg='Ansible JSON output has unexpected format: %s' % output) + # Remove Ansible's internal variables from returned fields. + ret.pop('_ansible_no_log', None) return ret |
