From cece7ff741234d4c3995d78ea1a6697485c8c221 Mon Sep 17 00:00:00 2001 From: Rebeccah Date: Wed, 2 Jun 2021 12:05:20 -0400 Subject: [PATCH] add a -q flag to make scripting easier and general improvements for readability --- awx/api/views/root.py | 2 +- .../commands/custom_venv_associations.py | 28 ++++++++++++++----- .../management/commands/export_custom_venv.py | 17 +++++++---- .../management/commands/list_custom_venvs.py | 24 +++++++++++----- 4 files changed, 51 insertions(+), 20 deletions(-) diff --git a/awx/api/views/root.py b/awx/api/views/root.py index 393440fe5c..558d512010 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, to_python_boolean, get_custom_venv_choices +from awx.main.utils import get_awx_version, get_custom_venv_choices 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 bcb0431d68..e1f749b798 100644 --- a/awx/main/management/commands/custom_venv_associations.py +++ b/awx/main/management/commands/custom_venv_associations.py @@ -4,6 +4,7 @@ from django.core.management.base import BaseCommand from awx.main.utils.common import get_custom_venv_choices from awx.main.models import Organization, InventorySource, JobTemplate, Project +import yaml class Command(BaseCommand): @@ -17,6 +18,7 @@ class Command(BaseCommand): default='', help='run this with a path to a virtual environment as an argument to see the associated Job Templates, Organizations, Projects, and Inventory Sources.', ) + parser.add_argument('-q', action='store_true', help='run with -q to output only the results of the query.') def handle(self, *args, **options): # look organiztions and unified job templates (which include JTs, workflows, and Inventory updates) @@ -24,14 +26,26 @@ class Command(BaseCommand): results = {} path = options.get('path') if path: - if path in get_custom_venv_choices(): # verify this is a valid path - path = options.get('path')[0] + if path[0] in get_custom_venv_choices(): # 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)] proj = [{"name": proj.name, "id": proj.id} for proj in Project.objects.filter(custom_virtualenv=path)] invsrc = [{"name": inv.name, "id": inv.id} for inv in InventorySource.objects.filter(custom_virtualenv=path)] - results["Organizations"] = orgs - results["Job Templates"] = jts - results["Projects"] = proj - results["Inventory Sources"] = invsrc - print(results) + results["organizations"] = orgs + results["job_templates"] = jts + results["projects"] = proj + 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)) diff --git a/awx/main/management/commands/export_custom_venv.py b/awx/main/management/commands/export_custom_venv.py index 34acfc90d8..246bf151ef 100644 --- a/awx/main/management/commands/export_custom_venv.py +++ b/awx/main/management/commands/export_custom_venv.py @@ -16,15 +16,22 @@ class Command(BaseCommand): default='', help='run this with a path to a virtual environment as an argument to see the pip freeze data', ) + parser.add_argument('-q', action='store_true', help='run with -q to output only the results of the query.') 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) - msg = [ - 'To list all (now deprecated) custom virtual environments run:', - 'awx-manage list_custom_venvs', - ] - 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 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)) diff --git a/awx/main/management/commands/list_custom_venvs.py b/awx/main/management/commands/list_custom_venvs.py index b621ea3a3b..6262d2300a 100644 --- a/awx/main/management/commands/list_custom_venvs.py +++ b/awx/main/management/commands/list_custom_venvs.py @@ -10,20 +10,30 @@ from django.conf import settings class Command(BaseCommand): """Returns a list of custom venv paths from the path passed in the argument""" + def add_arguments(self, parser): + parser.add_argument('-q', action='store_true', help='run with -q to output only the results of the query.') + def handle(self, *args, **options): super(Command, self).__init__() venvs = get_custom_venv_choices() if venvs: - print('# {}'.format("Discovered virtual environments:")) + print('\n', '# {}'.format("Discovered virtual environments:")) for venv in venvs: print(venv) + 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:', + '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:', + 'awx-manage custom_venv_associations /path/to/venv', + '', + ] + print('\n'.join(msg)) + else: + print('\n') - msg = [ - '', - '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)) else: msg = ["No custom virtual environments detected in:", settings.BASE_VENV_PATH]