-
Notifications
You must be signed in to change notification settings - Fork 3.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #10090 from rebeccahhh/custom_venv_command
add a new awx-manage command `custom_venvs` add an awx-manage command that gets pip freeze data from custom_venv and outputs to command line stdout SUMMARY part of #7062 - this command is a glorified pip freeze + some extra stuff, people could navigate to each of their custom virtual environments themselves and run a pip freeze, but this allows them to not, and everyone likes their life to be easier. The extra stuff allows users to see the connections that their existing virtual envs have in awx to things like organizations, jobs, inventory updates, and projects. ISSUE TYPE Feature Pull Request COMPONENT NAME API AWX VERSION awx: 19.1.0 ADDITIONAL INFORMATION This is built off of existing code and there is a line that gets custom venv paths from the settings module, that line does not seem to be working. I have written around that but want to make a note of it. Reviewed-by: Alan Rominger <arominge@redhat.com> Reviewed-by: Rebeccah Hunter <rhunter@redhat.com> Reviewed-by: Jeff Bradberry <None> Reviewed-by: Shane McDonald <me@shanemcd.com> Reviewed-by: Elijah DeLee <kdelee@redhat.com>
- Loading branch information
Showing
9 changed files
with
178 additions
and
27 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
# 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 | ||
import yaml | ||
|
||
|
||
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.', | ||
) | ||
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) | ||
super(Command, self).__init__() | ||
results = {} | ||
path = options.get('path') | ||
if 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)] | ||
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 | ||
if not options.get('q'): | ||
msg = [ | ||
'# Virtual Environments Associations:', | ||
yaml.dump(results), | ||
'- 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(yaml.dump(results)) | ||
|
||
else: | ||
print('\n', '# Incorrect path, verify your path is from the following list:') | ||
print('\n'.join(all_venvs), '\n') |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
# Copyright (c) 2021 Ansible, Inc. | ||
# All Rights Reserved | ||
|
||
from awx.main.utils.common import get_custom_venv_pip_freeze, get_custom_venv_choices | ||
from django.core.management.base import BaseCommand | ||
|
||
|
||
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 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'): | ||
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: | ||
if not options.get('q'): | ||
msg = [ | ||
'# Virtual environment contents:', | ||
pip_data, | ||
'- 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(pip_data) | ||
|
||
else: | ||
print('\n', '# Incorrect path, verify your path is from the following list:') | ||
print('\n'.join(all_venvs)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
# Copyright (c) 2021 Ansible, Inc. | ||
# All Rights Reserved | ||
import sys | ||
|
||
from awx.main.utils.common import get_custom_venv_choices | ||
from django.core.management.base import BaseCommand | ||
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: | ||
if not options.get('q'): | ||
msg = [ | ||
'# Discovered Virtual Environments:', | ||
'\n'.join(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', | ||
'', | ||
'- 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'.join(venvs), '\n') | ||
else: | ||
msg = ["No custom virtual environments detected in:", settings.BASE_VENV_PATH] | ||
|
||
for path in settings.CUSTOM_VENV_PATHS: | ||
msg.append(path) | ||
|
||
print('\n'.join(msg), file=sys.stderr) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters