Skip to content

Commit

Permalink
fix the output status of the container
Browse files Browse the repository at this point in the history
  • Loading branch information
Super User committed Mar 15, 2024
1 parent 97262ca commit ebfa9ac
Showing 1 changed file with 29 additions and 23 deletions.
52 changes: 29 additions & 23 deletions processes/podman_processor.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -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"):

Expand Down

0 comments on commit ebfa9ac

Please sign in to comment.