Skip to content

Commit

Permalink
save state
Browse files Browse the repository at this point in the history
  • Loading branch information
chrismeyersfsu committed Jan 21, 2025
1 parent e106e10 commit 8137d96
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 8 deletions.
4 changes: 2 additions & 2 deletions awx/main/tasks/receptor.py
Original file line number Diff line number Diff line change
Expand Up @@ -237,13 +237,13 @@ def run_until_complete(node, timing_data=None, **kwargs):

use_stream_tls = getattr(get_conn_type(node, receptor_ctl), 'name', None) == "STREAMTLS"
kwargs.setdefault('tlsclient', get_tls_client(config_data, use_stream_tls))
kwargs.setdefault('ttl', '20s')
# kwargs.setdefault('ttl', '20s')
kwargs.setdefault('payload', '')
if work_signing_enabled(config_data):
kwargs['signwork'] = True

transmit_start = time.time()
result = receptor_ctl.submit_work(worktype='ansible-runner', node=node, **kwargs)
result = receptor_ctl.submit_work(worktype='local', node=node, **kwargs)

unit_id = result['unitid']
run_start = time.time()
Expand Down
26 changes: 20 additions & 6 deletions awx/main/tasks/system.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
from django.utils.translation import gettext_noop
from django.core.cache import cache
from django.core.exceptions import ObjectDoesNotExist
from django.db.models.query import QuerySet

# Django-CRUM
from crum import impersonate
Expand Down Expand Up @@ -379,6 +380,22 @@ def purge_old_stdout_files():
logger.debug("Removing {}".format(os.path.join(settings.JOBOUTPUT_ROOT, f)))


class CleanupImagesAndFilesHelper:
@classmethod
def get_first_control_instance(cls) -> Instance | None:
return Instance.objects.me()
return (
Instance.objects.filter(node_type__in=['hybrid', 'control'], node_state=Instance.States.READY, enabled=True, capacity__gt=0)
.order_by('-hostname')
.first()
)

@classmethod
def get_execution_instances(cls) -> QuerySet[Instance]:
return [Instance.objects.me()]
return Instance.objects.filter(node_type='execution', node_state=Instance.States.READY, enabled=True, capacity__gt=0)


def _cleanup_images_and_files(**kwargs):
if settings.IS_K8S:
return
Expand All @@ -394,13 +411,10 @@ def _cleanup_images_and_files(**kwargs):
logger.info(f'Performed local cleanup with kwargs {kwargs}, output:\n{stdout}')

# if we are the first instance alphabetically, then run cleanup on execution nodes
checker_instance = (
Instance.objects.filter(node_type__in=['hybrid', 'control'], node_state=Instance.States.READY, enabled=True, capacity__gt=0)
.order_by('-hostname')
.first()
)
checker_instance = CleanupImagesAndFilesHelper.get_first_control_instance()

if checker_instance and this_inst.hostname == checker_instance.hostname:
for inst in Instance.objects.filter(node_type='execution', node_state=Instance.States.READY, enabled=True, capacity__gt=0):
for inst in CleanupImagesAndFilesHelper.get_execution_instances():
runner_cleanup_kwargs = inst.get_cleanup_task_kwargs(**kwargs)
if not runner_cleanup_kwargs:
continue
Expand Down
24 changes: 24 additions & 0 deletions awx/main/tests/live/tests/test_cleanup_task.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

from awx.main.tasks.receptor import _convert_args_to_cli
from awx.main.models import Instance, JobTemplate
from awx.main.tasks.system import _cleanup_images_and_files


def test_folder_cleanup_multiple_running_jobs_execution_node(request):
Expand Down Expand Up @@ -37,3 +38,26 @@ def delete_jobs():
print('ansible-runner worker ' + remote_command)

assert [os.path.exists(job_dir) for job_dir in job_dirs] == [True for i in range(3)]


def test_tagless_image():
import subprocess, json

input = """
FROM quay.io/ansible/awx-ee:latest
RUN echo "Hello, Podman!" > /tmp/hello.txt
"""
cmd = ['podman', 'build', '-f', '-'] # Create an image without a tag
result = subprocess.run(cmd, capture_output=True, input=input, text=True, check=True)

def get_podman_images():
cmd = ['podman', 'images', '--format', 'json']
return json.loads((subprocess.run(cmd, capture_output=True, text=True, check=True)).stdout)

dangling_image = next((image for image in get_podman_images() if image.get('Dangling', False)), None)
assert dangling_image, f"Test expected to find dangling image in {images}"

_cleanup_images_and_files(image_prune=True)

for image in get_podman_images():
assert image['Id'] != dangling_image['Id']

0 comments on commit 8137d96

Please sign in to comment.