diff --git a/awx/main/management/commands/custom_venv_associations.py b/awx/main/management/commands/custom_venv_associations.py new file mode 100644 index 0000000000..bcb0431d68 --- /dev/null +++ b/awx/main/management/commands/custom_venv_associations.py @@ -0,0 +1,37 @@ +# Copyright (c) 2021 Ansible, Inc. +# All Rights Reserved + +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 + + +class Command(BaseCommand): + """Returns the pip freeze from the path passed in the argument""" + + def add_arguments(self, parser): + parser.add_argument( + 'path', + type=str, + nargs=1, + 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.', + ) + + def handle(self, *args, **options): + # look organiztions and unified job templates (which include JTs, workflows, and Inventory updates) + super(Command, self).__init__() + 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] + 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) diff --git a/awx/main/management/commands/export_custom_venv.py b/awx/main/management/commands/export_custom_venv.py index fac39a052a..34acfc90d8 100644 --- a/awx/main/management/commands/export_custom_venv.py +++ b/awx/main/management/commands/export_custom_venv.py @@ -12,6 +12,7 @@ class Command(BaseCommand): parser.add_argument( 'path', type=str, + nargs=1, default='', help='run this with a path to a virtual environment as an argument to see the pip freeze data', ) @@ -19,7 +20,7 @@ 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')) + pip_data = get_custom_venv_pip_freeze(options.get('path')[0]) if pip_data: print(pip_data) msg = [ @@ -27,5 +28,3 @@ class Command(BaseCommand): 'awx-manage list_custom_venvs', ] print('\n'.join(msg)) - else: - print("missing argument: please include a path argument following the command.") diff --git a/awx/main/management/commands/get_custom_venv_associations.py b/awx/main/management/commands/get_custom_venv_associations.py deleted file mode 100644 index 4ae877503f..0000000000 --- a/awx/main/management/commands/get_custom_venv_associations.py +++ /dev/null @@ -1,35 +0,0 @@ -# Copyright (c) 2021 Ansible, Inc. -# All Rights Reserved - -from django.core.management.base import BaseCommand -from awx.main.models import Organization, InventorySource, UnifiedJobTemplate - - -class Command(BaseCommand): - """Returns the pip freeze from the path passed in the argument""" - - def add_arguments(self, parser): - parser.add_argument( - 'path', - type=str, - nargs='?', - default='', - help='run this with a path to a virtual environment as an argument to see the pip freeze data', - ) - - def handle(self, *args, **options): - # look organiztions and unified job templates (which include JTs, workflows, and Inventory updates) - super(Command, self).__init__() - results = {} - if options.get('path'): - # sanity check here - is path in list - path = options.get('path') - orgs = [{"name": org.name, "id": org.id} for org in Organization.objects.filter(custom_virtualenv=path)] - ujts = [{"name": org.name, "id": org.id} for org in UnifiedJobTemplate.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["Unified Job Templates"] = ujts - results["Inventory Sources"] = invsrc - print(results) - else: - print("missing argument: please include a path argument following the command.")