diff --git a/awx/main/tasks.py b/awx/main/tasks.py index 7c0dd854d0..411e4b27b6 100644 --- a/awx/main/tasks.py +++ b/awx/main/tasks.py @@ -27,6 +27,7 @@ import socket import threading import concurrent.futures from base64 import b64encode +import subprocess # Django from django.conf import settings @@ -396,6 +397,24 @@ def purge_old_stdout_files(): logger.debug("Removing {}".format(os.path.join(settings.JOBOUTPUT_ROOT, f))) +@task(queue=get_local_queuename) +def cleanup_execution_environment_images(): + if settings.IS_K8S: + return + process = subprocess.run('podman images --filter="dangling=true" --format json'.split(" "), capture_output=True) + if process.returncode != 0: + logger.debug("Cleanup execution environment images: could not get list of images") + return + if len(process.stdout) > 0: + images_system = json.loads(process.stdout) + for e in images_system: + image_name = e["Id"] + logger.debug(f"Cleanup execution environment images: deleting {image_name}") + process = subprocess.run(['podman', 'rmi', image_name, '-f'], stdout=subprocess.DEVNULL) + if process.returncode != 0: + logger.debug(f"Failed to delete image {image_name}") + + @task(queue=get_local_queuename) def cluster_node_heartbeat(): logger.debug("Cluster node heartbeat task.") diff --git a/awx/settings/defaults.py b/awx/settings/defaults.py index 39426fbc43..f750d336b1 100644 --- a/awx/settings/defaults.py +++ b/awx/settings/defaults.py @@ -439,6 +439,7 @@ CELERYBEAT_SCHEDULE = { 'task_manager': {'task': 'awx.main.scheduler.tasks.run_task_manager', 'schedule': timedelta(seconds=20), 'options': {'expires': 20}}, 'k8s_reaper': {'task': 'awx.main.tasks.awx_k8s_reaper', 'schedule': timedelta(seconds=60), 'options': {'expires': 50}}, 'send_subsystem_metrics': {'task': 'awx.main.analytics.analytics_tasks.send_subsystem_metrics', 'schedule': timedelta(seconds=20)}, + 'cleanup_images': {'task': 'awx.main.tasks.cleanup_execution_environment_images', 'schedule': timedelta(hours=3)}, # 'isolated_heartbeat': set up at the end of production.py and development.py }