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

Use awx-plugins-shared code from awx_plugins.interfaces #15566

Merged
merged 4 commits into from
Oct 2, 2024
Merged
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
6 changes: 4 additions & 2 deletions awx/api/generics.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,9 @@
from rest_framework.renderers import StaticHTMLRenderer
from rest_framework.negotiation import DefaultContentNegotiation

# Shared code for the AWX platform
from awx_plugins.interfaces._temporary_private_licensing_api import detect_server_product_name

# django-ansible-base
from ansible_base.rest_filters.rest_framework.field_lookup_backend import FieldLookupBackend
from ansible_base.lib.utils.models import get_all_field_names
Expand All @@ -43,7 +46,6 @@
from awx.main.models.rbac import give_creator_permissions
from awx.main.access import optimize_queryset
from awx.main.utils import camelcase_to_underscore, get_search_fields, getattrd, get_object_or_400, decrypt_field, get_awx_version
from awx.main.utils.licensing import server_product_name
from awx.main.utils.proxy import is_proxy_in_headers, delete_headers_starting_with_http
from awx.main.views import ApiErrorView
from awx.api.serializers import ResourceAccessListElementSerializer, CopySerializer
Expand Down Expand Up @@ -254,7 +256,7 @@ def finalize_response(self, request, response, *args, **kwargs):
time_started = getattr(self, 'time_started', None)
if request.user.is_authenticated:
response['X-API-Product-Version'] = get_awx_version()
response['X-API-Product-Name'] = server_product_name()
response['X-API-Product-Name'] = detect_server_product_name()

response['X-API-Node'] = settings.CLUSTER_HOST_ID
if time_started:
Expand Down
8 changes: 5 additions & 3 deletions awx/main/models/credential.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,9 @@
from django.utils.timezone import now
from django.contrib.auth.models import User

# Shared code for the AWX platform
from awx_plugins.interfaces._temporary_private_container_api import get_incontainer_path

# DRF
from awx.main.utils.pglock import advisory_lock
from rest_framework.serializers import ValidationError as DRFValidationError
Expand All @@ -41,7 +44,6 @@
)
from awx.main.utils import decrypt_field, classproperty, set_environ
from awx.main.utils.safe_yaml import safe_dump
from awx.main.utils.execution_environments import to_container_path
from awx.main.validators import validate_ssh_private_key
from awx.main.models.base import CommonModelNameNotUnique, PasswordFieldsModel, PrimordialModel
from awx.main.models.mixins import ResourceMixin
Expand Down Expand Up @@ -623,7 +625,7 @@
with open(path, 'w') as f:
f.write(data)
os.chmod(path, stat.S_IRUSR | stat.S_IWUSR)
container_path = to_container_path(path, private_data_dir)
container_path = get_incontainer_path(path, private_data_dir)

Check warning on line 628 in awx/main/models/credential.py

View check run for this annotation

Codecov / codecov/patch

awx/main/models/credential.py#L628

Added line #L628 was not covered by tests

# determine if filename indicates single file or many
if file_label.find('.') == -1:
Expand Down Expand Up @@ -665,7 +667,7 @@
extra_vars = build_extra_vars(self.injectors.get('extra_vars', {}))
if extra_vars:
path = build_extra_vars_file(extra_vars, private_data_dir)
container_path = to_container_path(path, private_data_dir)
container_path = get_incontainer_path(path, private_data_dir)

Check warning on line 670 in awx/main/models/credential.py

View check run for this annotation

Codecov / codecov/patch

awx/main/models/credential.py#L670

Added line #L670 was not covered by tests
args.extend(['-e', '@%s' % container_path])


Expand Down
8 changes: 5 additions & 3 deletions awx/main/tasks/jobs.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@
# Django
from django.conf import settings

# Shared code for the AWX platform
from awx_plugins.interfaces._temporary_private_container_api import CONTAINER_ROOT, get_incontainer_path


# Runner
import ansible_runner
Expand Down Expand Up @@ -67,7 +70,6 @@
from awx.main.tasks.facts import start_fact_cache, finish_fact_cache
from awx.main.exceptions import AwxTaskError, PostRunError, ReceptorNodeNotFound
from awx.main.utils.ansible import read_ansible_config
from awx.main.utils.execution_environments import CONTAINER_ROOT, to_container_path
from awx.main.utils.safe_yaml import safe_dump, sanitize_jinja
from awx.main.utils.common import (
update_scm_url,
Expand Down Expand Up @@ -909,7 +911,7 @@
cred_files = private_data_files.get('credentials', {})
for cloud_cred in job.cloud_credentials:
if cloud_cred and cloud_cred.credential_type.namespace == 'openstack' and cred_files.get(cloud_cred, ''):
env['OS_CLIENT_CONFIG_FILE'] = to_container_path(cred_files.get(cloud_cred, ''), private_data_dir)
env['OS_CLIENT_CONFIG_FILE'] = get_incontainer_path(cred_files.get(cloud_cred, ''), private_data_dir)

Check warning on line 914 in awx/main/tasks/jobs.py

View check run for this annotation

Codecov / codecov/patch

awx/main/tasks/jobs.py#L914

Added line #L914 was not covered by tests

for network_cred in job.network_credentials:
env['ANSIBLE_NET_USERNAME'] = network_cred.get_input('username', default='')
Expand Down Expand Up @@ -1552,7 +1554,7 @@
args.append('-i')
script_params = dict(hostvars=True, towervars=True)
source_inv_path = self.write_inventory_file(input_inventory, private_data_dir, f'hosts_{input_inventory.id}', script_params)
args.append(to_container_path(source_inv_path, private_data_dir))
args.append(get_incontainer_path(source_inv_path, private_data_dir))

Check warning on line 1557 in awx/main/tasks/jobs.py

View check run for this annotation

Codecov / codecov/patch

awx/main/tasks/jobs.py#L1557

Added line #L1557 was not covered by tests
# Include any facts from input inventories so they can be used in filters
start_fact_cache(
input_inventory.hosts.only(*HOST_FACTS_FIELDS),
Expand Down
7 changes: 4 additions & 3 deletions awx/main/tests/functional/test_inventory_source_injectors.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,12 @@
import re
from collections import namedtuple

from awx_plugins.interfaces._temporary_private_container_api import get_incontainer_path

from awx.main.tasks.jobs import RunInventoryUpdate
from awx.main.models import InventorySource, Credential, CredentialType, UnifiedJob, ExecutionEnvironment
from awx.main.constants import CLOUD_PROVIDERS, STANDARD_INVENTORY_UPDATE_ENV
from awx.main.tests import data
from awx.main.utils.execution_environments import to_container_path

from django.conf import settings

Expand Down Expand Up @@ -115,7 +116,7 @@
continue # Ansible runner
abs_file_path = os.path.join(private_data_dir, filename)
file_aliases[abs_file_path] = filename
runner_path = to_container_path(abs_file_path, private_data_dir)
runner_path = get_incontainer_path(abs_file_path, private_data_dir)

Check warning on line 119 in awx/main/tests/functional/test_inventory_source_injectors.py

View check run for this annotation

Codecov / codecov/patch

awx/main/tests/functional/test_inventory_source_injectors.py#L119

Added line #L119 was not covered by tests
if runner_path in inverse_env:
referenced_paths.add(abs_file_path)
alias = 'file_reference'
Expand Down Expand Up @@ -163,7 +164,7 @@
# assert that all files laid down are used
if (
abs_file_path not in referenced_paths
and to_container_path(abs_file_path, private_data_dir) not in inventory_content
and get_incontainer_path(abs_file_path, private_data_dir) not in inventory_content
and abs_file_path not in ignore_files
):
raise AssertionError(
Expand Down
3 changes: 2 additions & 1 deletion awx/main/tests/unit/test_tasks.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@
import yaml
import jinja2

from awx_plugins.interfaces._temporary_private_container_api import CONTAINER_ROOT

from django.conf import settings

from awx.main.models import (
Expand All @@ -37,7 +39,6 @@
from awx.main.tasks import jobs, system, receptor
from awx.main.utils import encrypt_field, encrypt_value
from awx.main.utils.safe_yaml import SafeLoader
from awx.main.utils.execution_environments import CONTAINER_ROOT

from awx.main.utils.licensing import Licenser
from awx.main.constants import JOB_VARIABLE_PREFIXES
Expand Down
8 changes: 4 additions & 4 deletions awx/main/tests/unit/utils/test_execution_environments.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

import pytest

from awx.main.utils.execution_environments import to_container_path
from awx_plugins.interfaces._temporary_private_container_api import get_incontainer_path


private_data_dir = '/tmp/pdd_iso/awx_xxx'
Expand All @@ -22,7 +22,7 @@
],
)
def test_switch_paths(container_path, host_path):
assert to_container_path(host_path, private_data_dir) == container_path
assert get_incontainer_path(host_path, private_data_dir) == container_path

Check warning on line 25 in awx/main/tests/unit/utils/test_execution_environments.py

View check run for this annotation

Codecov / codecov/patch

awx/main/tests/unit/utils/test_execution_environments.py#L25

Added line #L25 was not covered by tests


def test_symlink_isolation_dir(request):
Expand All @@ -40,7 +40,7 @@

pdd = f'{dst_path}/awx_xxx'

assert to_container_path(f'{pdd}/env/tmp1234', pdd) == '/runner/env/tmp1234'
assert get_incontainer_path(f'{pdd}/env/tmp1234', pdd) == '/runner/env/tmp1234'

Check warning on line 43 in awx/main/tests/unit/utils/test_execution_environments.py

View check run for this annotation

Codecov / codecov/patch

awx/main/tests/unit/utils/test_execution_environments.py#L43

Added line #L43 was not covered by tests


@pytest.mark.parametrize(
Expand All @@ -53,4 +53,4 @@
)
def test_invalid_host_path(host_path):
with pytest.raises(RuntimeError):
to_container_path(host_path, private_data_dir)
get_incontainer_path(host_path, private_data_dir)

Check warning on line 56 in awx/main/tests/unit/utils/test_execution_environments.py

View check run for this annotation

Codecov / codecov/patch

awx/main/tests/unit/utils/test_execution_environments.py#L56

Added line #L56 was not covered by tests
23 changes: 0 additions & 23 deletions awx/main/utils/execution_environments.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
import os
import logging
from pathlib import Path

from django.conf import settings

Expand Down Expand Up @@ -51,24 +49,3 @@ def get_default_pod_spec():
],
},
}


# this is the root of the private data dir as seen from inside
# of the container running a job
CONTAINER_ROOT = '/runner'


def to_container_path(path, private_data_dir):
"""Given a path inside of the host machine filesystem,
this returns the expected path which would be observed by the job running
inside of the EE container.
This only handles the volume mount from private_data_dir to /runner
"""
if not os.path.isabs(private_data_dir):
raise RuntimeError('The private_data_dir path must be absolute')
# due to how tempfile.mkstemp works, we are probably passed a resolved path, but unresolved private_data_dir
resolved_path = Path(path).resolve()
resolved_pdd = Path(private_data_dir).resolve()
if resolved_pdd != resolved_path and resolved_pdd not in resolved_path.parents:
raise RuntimeError(f'Cannot convert path {resolved_path} unless it is a subdir of {resolved_pdd}')
return str(resolved_path).replace(str(resolved_pdd), CONTAINER_ROOT, 1)
14 changes: 6 additions & 8 deletions awx/main/utils/licensing.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
import collections
import copy
import io
import os
import json
import logging
import re
Expand All @@ -35,6 +34,9 @@
from django.conf import settings
from django.utils.translation import gettext_lazy as _

# Shared code for the AWX platform
from awx_plugins.interfaces._temporary_private_licensing_api import detect_server_product_name

from awx.main.constants import SUBSCRIPTION_USAGE_MODEL_UNIQUE_HOSTS

MAX_INSTANCES = 9999999
Expand Down Expand Up @@ -480,13 +482,9 @@
from awx.main.utils.licensing import Licenser, OpenLicense

try:
if os.path.exists('/var/lib/awx/.tower_version'):
return Licenser(*args, **kwargs)
else:
if detect_server_product_name() == 'AWX':
return OpenLicense()
else:
return Licenser(*args, **kwargs)

Check warning on line 488 in awx/main/utils/licensing.py

View check run for this annotation

Codecov / codecov/patch

awx/main/utils/licensing.py#L488

Added line #L488 was not covered by tests
except Exception as e:
raise ValueError(_('Error importing License: %s') % e)


def server_product_name():
return 'AWX' if isinstance(get_licenser(), OpenLicense) else 'Red Hat Ansible Automation Platform'
5 changes: 3 additions & 2 deletions awx/ui/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@
from django.utils.translation import gettext_lazy as _
from django.views.generic.base import TemplateView

from awx.main.utils.licensing import server_product_name
# Shared code for the AWX platform
from awx_plugins.interfaces._temporary_private_licensing_api import detect_server_product_name


class IndexView(TemplateView):
Expand All @@ -14,7 +15,7 @@

def get_context_data(self, **kwargs):
context = super().get_context_data(**kwargs)
product_name = server_product_name()
product_name = detect_server_product_name()

Check warning on line 18 in awx/ui/urls.py

View check run for this annotation

Codecov / codecov/patch

awx/ui/urls.py#L18

Added line #L18 was not covered by tests
context['title'] = _('%s Upgrading' % product_name)
context['image_alt'] = _('Logo')
context['aria_spinner'] = _('Loading')
Expand Down
Loading
Loading