From 550ab82f63f119da465fb1a666287c1fe2e996f1 Mon Sep 17 00:00:00 2001 From: Rebeccah Date: Wed, 2 Jun 2021 13:54:23 -0400 Subject: [PATCH] add the conditionals for incorrect paths and helpful info for if a user does that and remove unused import --- awx/api/views/root.py | 2 +- .../commands/custom_venv_associations.py | 32 +++++++++------ .../management/commands/export_custom_venv.py | 39 ++++++++++++------- .../management/commands/list_custom_venvs.py | 12 +++--- 4 files changed, 50 insertions(+), 35 deletions(-) diff --git a/awx/api/views/root.py b/awx/api/views/root.py index 558d512010..e464ca86bd 100644 --- a/awx/api/views/root.py +++ b/awx/api/views/root.py @@ -24,7 +24,7 @@ from awx.api.generics import APIView from awx.conf.registry import settings_registry from awx.main.analytics import all_collectors from awx.main.ha import is_ha_environment -from awx.main.utils import get_awx_version, get_custom_venv_choices +from awx.main.utils import get_awx_version from awx.main.utils.licensing import validate_entitlement_manifest from awx.api.versioning import reverse, drf_reverse from awx.main.constants import PRIVILEGE_ESCALATION_METHODS diff --git a/awx/main/management/commands/custom_venv_associations.py b/awx/main/management/commands/custom_venv_associations.py index e1f749b798..e80c97aad0 100644 --- a/awx/main/management/commands/custom_venv_associations.py +++ b/awx/main/management/commands/custom_venv_associations.py @@ -26,7 +26,8 @@ class Command(BaseCommand): results = {} path = options.get('path') if path: - if path[0] in get_custom_venv_choices(): # verify this is a valid path + all_venvs = get_custom_venv_choices() + if path[0] in all_venvs: # verify this is a valid path path = path[0] orgs = [{"name": org.name, "id": org.id} for org in Organization.objects.filter(custom_virtualenv=path)] jts = [{"name": jt.name, "id": jt.id} for jt in JobTemplate.objects.filter(custom_virtualenv=path)] @@ -38,14 +39,21 @@ class Command(BaseCommand): results["inventory_sources"] = invsrc print('\n', '# {}'.format("Virtual environments associations:")) print(yaml.dump(results)) - if not options.get('q'): - msg = [ - '', - 'To list all (now deprecated) custom virtual environments run:', - 'awx-manage list_custom_venvs', - '', - 'To export the contents of a (deprecated) virtual environment, ' 'run the following command while supplying the path as an argument:', - 'awx-manage export_custom_venv /path/to/venv', - '', - ] - print('\n'.join(msg)) + + if not options.get('q'): + msg = [ + '', + '- To list all (now deprecated) custom virtual environments run:', + 'awx-manage list_custom_venvs', + '', + '- To export the contents of a (deprecated) virtual environment, ' 'run the following command while supplying the path as an argument:', + 'awx-manage export_custom_venv /path/to/venv', + '', + '- Run these commands with `-q` to remove tool tips.', + '', + ] + print('\n'.join(msg)) + + else: + print('\n', '# Incorrect path, verify your path is from the following list:') + print('\n'.join(all_venvs), '\n') diff --git a/awx/main/management/commands/export_custom_venv.py b/awx/main/management/commands/export_custom_venv.py index 246bf151ef..f267baa6be 100644 --- a/awx/main/management/commands/export_custom_venv.py +++ b/awx/main/management/commands/export_custom_venv.py @@ -1,7 +1,7 @@ # Copyright (c) 2021 Ansible, Inc. # All Rights Reserved -from awx.main.utils.common import get_custom_venv_pip_freeze +from awx.main.utils.common import get_custom_venv_pip_freeze, get_custom_venv_choices from django.core.management.base import BaseCommand @@ -21,17 +21,26 @@ class Command(BaseCommand): def handle(self, *args, **options): super(Command, self).__init__() if options.get('path'): - pip_data = get_custom_venv_pip_freeze(options.get('path')[0]) - if pip_data: - print('\n', '# {}'.format("Virtual environment contents:")) - print(pip_data) - if not options.get('q'): - msg = [ - 'To list all (now deprecated) custom virtual environments run:', - 'awx-manage list_custom_venvs', - '', - 'To view the connections a (deprecated) virtual environment had in the database, run the following command while supplying the path as an argument:', - 'awx-manage custom_venv_associations /path/to/venv', - '', - ] - print('\n'.join(msg)) + path = options.get('path') + all_venvs = get_custom_venv_choices() + if path[0] in all_venvs: + pip_data = get_custom_venv_pip_freeze(options.get('path')[0]) + if pip_data: + print('\n', '# {}'.format("Virtual environment contents:")) + print(pip_data) + if not options.get('q'): + msg = [ + '- To list all (now deprecated) custom virtual environments run:', + 'awx-manage list_custom_venvs', + '', + '- To view the connections a (deprecated) virtual environment had in the database, run the following command while supplying the path as an argument:', + 'awx-manage custom_venv_associations /path/to/venv', + '', + '- Run these commands with `-q` to remove tool tips.', + '', + ] + print('\n'.join(msg)) + + else: + print('\n', '# Incorrect path, verify your path is from the following list:') + print('\n'.join(all_venvs)) diff --git a/awx/main/management/commands/list_custom_venvs.py b/awx/main/management/commands/list_custom_venvs.py index 6262d2300a..03534881b9 100644 --- a/awx/main/management/commands/list_custom_venvs.py +++ b/awx/main/management/commands/list_custom_venvs.py @@ -18,21 +18,19 @@ class Command(BaseCommand): venvs = get_custom_venv_choices() if venvs: print('\n', '# {}'.format("Discovered virtual environments:")) - for venv in venvs: - print(venv) + print('\n'.join(venvs), '\n') if not options.get('q'): msg = [ - '', - 'To export the contents of a (deprecated) virtual environment, ' 'run the following command while supplying the path as an argument:', + '- To export the contents of a (deprecated) virtual environment, ' 'run the following command while supplying the path as an argument:', 'awx-manage export_custom_venv /path/to/venv', '', - 'To view the connections a (deprecated) virtual environment had in the database, run the following command while supplying the path as an argument:', + '- To view the connections a (deprecated) virtual environment had in the database, run the following command while supplying the path as an argument:', 'awx-manage custom_venv_associations /path/to/venv', '', + '- Run these commands with `-q` to remove tool tips.', + '', ] print('\n'.join(msg)) - else: - print('\n') else: msg = ["No custom virtual environments detected in:", settings.BASE_VENV_PATH]