Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

temporary API endpoint for custom virtual env data #10007

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions awx/api/urls/custom_venvs.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
# Copyright (c) 2017 Ansible, Inc.
# All Rights Reserved.

from django.conf.urls import url

from awx.api.views import CustomVenvs


urls = [
url(r'^$', CustomVenvs.as_view(), name='custom_venvs'),
url(r'^(?P<pk>[0-9]+)/$', CustomVenvs.as_view(), name='custom_venvs_detail'),
]

__all__ = ['urls']
2 changes: 2 additions & 0 deletions awx/api/urls/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@
from .oauth2_root import urls as oauth2_root_urls
from .workflow_approval_template import urls as workflow_approval_template_urls
from .workflow_approval import urls as workflow_approval_urls
from .custom_venvs import urls as custom_venv_urls


v2_urls = [
Expand Down Expand Up @@ -132,6 +133,7 @@
url(r'^activity_stream/', include(activity_stream_urls)),
url(r'^workflow_approval_templates/', include(workflow_approval_template_urls)),
url(r'^workflow_approvals/', include(workflow_approval_urls)),
url(r'^custom_venvs/', include(custom_venv_urls)),
]


Expand Down
12 changes: 12 additions & 0 deletions awx/api/views/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,7 @@
from awx.main.utils.encryption import encrypt_value
from awx.main.utils.filters import SmartFilter
from awx.main.utils.insights import filter_insights_api_response
from awx.main.utils.common import get_custom_venv_choices, get_custom_venv_pip_freeze
from awx.main.redact import UriCleaner
from awx.api.permissions import (
JobTemplateCallbackPermission,
Expand Down Expand Up @@ -295,6 +296,7 @@ def get(self, request, format=None):
data['teams'] = {'url': reverse('api:team_list', request=request), 'total': team_list.count()}
data['credentials'] = {'url': reverse('api:credential_list', request=request), 'total': credential_list.count()}
data['job_templates'] = {'url': reverse('api:job_template_list', request=request), 'total': job_template_list.count()}
data['custom_venvs'] = {'url': reverse('api:custom_venv_list', request=request)}
return Response(data)


Expand Down Expand Up @@ -4527,3 +4529,13 @@ def post(self, request, *args, **kwargs):
return Response({"error": _("This workflow step has already been approved or denied.")}, status=status.HTTP_400_BAD_REQUEST)
obj.deny(request)
return Response(status=status.HTTP_204_NO_CONTENT)


class CustomVenvs(GenericAPIView):
def get(self, request, format=None):
custom_venv_choices = get_custom_venv_choices()
pip_freeze_data = get_custom_venv_pip_freeze(custom_venv_choices)
return Response(str(pip_freeze_data))

# def post(self, request, *args, **kwargs):
# custom_venv_choices = get_custom_venv_choices()
39 changes: 30 additions & 9 deletions awx/main/utils/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import yaml
import logging
import os
import subprocess
import re
import stat
import urllib.parse
Expand Down Expand Up @@ -890,27 +891,47 @@ def get_current_apps():
def get_custom_venv_choices(custom_paths=None):
from django.conf import settings

custom_paths = custom_paths or settings.CUSTOM_VENV_PATHS
all_venv_paths = [settings.BASE_VENV_PATH] + custom_paths
all_venv_paths = []

all_venv_paths.append(settings.BASE_VENV_PATH) # get paths from settings
if custom_paths: # get paths from API request
all_venv_paths.append(custom_paths)
for root, dir, files in os.walk('..'): # get paths on machine
if 'venv' in root and 'lib64' not in root and "python3" not in root:
all_venv_paths.append(root)

custom_venv_choices = []

for custom_venv_path in all_venv_paths:
for venv_path in all_venv_paths:
try:
if os.path.exists(custom_venv_path):
if os.path.exists(venv_path):
custom_venv_choices.extend(
[
os.path.join(custom_venv_path, x, '')
for x in os.listdir(custom_venv_path)
if x != 'awx'
and os.path.isdir(os.path.join(custom_venv_path, x))
and os.path.exists(os.path.join(custom_venv_path, x, 'bin', 'activate'))
os.path.join(venv_path, x, '')
for x in os.listdir(venv_path)
if os.path.exists(os.path.join(venv_path, x, 'activate')) and os.path.join(venv_path, x) != '../var/lib/awx/venv/awx/bin'
]
)
except Exception:
logger.exception("Encountered an error while discovering custom virtual environments.")
return custom_venv_choices


def get_custom_venv_pip_freeze(custom_venvs):
# import sdb; sdb.set_trace()
pip_data = {}
for venv_path in custom_venvs:
if '..' in venv_path:
venv_path = venv_path.replace('..', '')
try:
freeze_data = subprocess.run([f"{venv_path}/pip", "freeze"], capture_output=True)
data = freeze_data.stdout
pip_data[venv_path] = data
except Exception:
logger.exception("Encountered an error while discovering Pip Freeze data for custom virtual environments.")
return pip_data


def is_ansible_variable(key):
return key.startswith('ansible_')

Expand Down