From ebfa9ac960cf93594731a33c6fcabed656cfc814 Mon Sep 17 00:00:00 2001 From: Super User Date: Fri, 15 Mar 2024 13:54:31 +0100 Subject: [PATCH] fix the output status of the container --- processes/podman_processor.py | 52 +++++++++++++++++++---------------- 1 file changed, 29 insertions(+), 23 deletions(-) diff --git a/processes/podman_processor.py b/processes/podman_processor.py index 7740e34..e8f3561 100644 --- a/processes/podman_processor.py +++ b/processes/podman_processor.py @@ -5,7 +5,7 @@ class PodmanProcessor(): def connect(uri='unix:///run/podman/podman.sock'): # Connect to Podman client = PodmanClient(base_url=uri) - + if not client.ping(): raise Exception("Podman service is not running") else: @@ -18,50 +18,56 @@ def connect(uri='unix:///run/podman/podman.sock'): return client def pull_run_image(client, image_name, container_name, environment=None, mounts=None, network_mode=None, volumes=None, command=None): + # Pull the Docker image - if client.images.list(): - for image in client.images.list(): - print('image list: ', image, image.id, "\n") - if image_name not in image.labels['org.opencontainers.image.title']: - print(f"Pulling Podman image: {image_name}") - image = client.images.pull(image_name) - else: + print("image: ", client.images.list(filters={"reference": image_name})) + if not client.images.list(filters={"reference": image_name}): print(f"Pulling Podman image: {image_name}") - image = client.images.pull(image_name) + client.images.pull(image_name) - # Check if container with the same name exists, and remove it if it does - if container_name in client.containers.list(): + existing_container = client.containers.list(filters={"name": container_name}) + if existing_container: print(f"Container '{container_name}' already exists. Removing...") - client.containers.get(container_name).remove(force=True) + existing_container[0].remove(force=True) - # Run the Docker container print(f"Running Podman container: {container_name}") - container = client.containers.create( - image=image, - detach=False, + container = client.containers.run( + image=image_name, + detach=True, name=container_name, environment=environment, mounts=mounts, network_mode=network_mode, # volumes=volumes, - # command=command, + command=command, remove=False ) # Start the container container.start() + # status of the container after starting + container.reload() + print("container starting status :", container.status) + + # exit status code + exit_status = container.wait() + print("exit_status :", exit_status) + + # status of the container + container.reload() + print("container exiting status :", container.status) + + # Print container logs print(f"Container '{container.name}' logs:") for line in container.logs(stream=True): print(line.strip().decode('utf-8')) - # # Stop the container - # if container.status != 'exited': - # print(f"Container status with ID '{container.name}' is '{container.status}'") - # container.stop() - - return container + return { + "container" : container, + "container_status": container.status + } def get_secrets(file_name="processes/secret.txt"):