2025-02-01 06:06:22 +05:30
|
|
|
import podman
|
2025-01-20 05:02:25 +05:30
|
|
|
|
2025-02-01 07:02:09 +05:30
|
|
|
def launch_pod_container(image, run_command, name, ports):
|
2025-02-01 06:06:22 +05:30
|
|
|
client = podman.PodmanClient(base_url="unix:///run/user/1000/podman/podman.sock")
|
2025-01-20 05:02:25 +05:30
|
|
|
try:
|
2025-02-01 06:06:22 +05:30
|
|
|
container = client.containers.create(
|
2025-01-20 05:02:25 +05:30
|
|
|
name=name,
|
2025-01-20 22:42:01 +05:30
|
|
|
image=image,
|
|
|
|
# ports=ports,
|
|
|
|
command=run_command,
|
|
|
|
detach=True,
|
|
|
|
network_mode='host'
|
2025-01-20 05:02:25 +05:30
|
|
|
)
|
2025-02-01 06:06:22 +05:30
|
|
|
container.start()
|
2025-01-20 05:02:25 +05:30
|
|
|
return f"Container launched successfully: {container.id}"
|
2025-02-01 06:06:22 +05:30
|
|
|
except podman.errors.APIError as e:
|
2025-01-20 05:02:25 +05:30
|
|
|
return f"API Error: {e}"
|
|
|
|
except Exception as e:
|
|
|
|
return f"Error: {e}"
|
|
|
|
|
2025-02-01 07:02:09 +05:30
|
|
|
def stop_pod_container(name):
|
2025-02-01 06:06:22 +05:30
|
|
|
client = podman.PodmanClient(base_url="unix:///run/user/1000/podman/podman.sock")
|
2025-01-20 05:02:25 +05:30
|
|
|
try:
|
|
|
|
container = client.containers.get(name)
|
|
|
|
container.stop()
|
|
|
|
return f"Container stopped: {name}"
|
2025-02-01 06:06:22 +05:30
|
|
|
except podman.errors.NotFound:
|
2025-01-20 05:02:25 +05:30
|
|
|
return f"Container not found: {name}"
|
|
|
|
except Exception as e:
|
|
|
|
return f"Error stopping container {name}: {e}"
|
|
|
|
|
2025-02-01 07:02:09 +05:30
|
|
|
def remove_pod_container(name):
|
2025-02-01 06:06:22 +05:30
|
|
|
client = podman.PodmanClient(base_url="unix:///run/user/1000/podman/podman.sock")
|
2025-01-20 05:02:25 +05:30
|
|
|
try:
|
|
|
|
container = client.containers.get(name)
|
|
|
|
container.remove(force=True) # Force removal if the container is running
|
|
|
|
return f"Container removed: {name}"
|
2025-02-01 06:06:22 +05:30
|
|
|
except podman.errors.NotFound:
|
2025-01-20 05:02:25 +05:30
|
|
|
return f"Container not found: {name}"
|
|
|
|
except Exception as e:
|
|
|
|
return f"Error removing container {name}: {e}"
|
|
|
|
|
|
|
|
def is_container_running(name):
|
2025-02-01 06:06:22 +05:30
|
|
|
client = podman.PodmanClient(base_url="unix:///run/user/1000/podman/podman.sock")
|
2025-01-20 05:02:25 +05:30
|
|
|
try:
|
|
|
|
container = client.containers.get(name)
|
|
|
|
return container.status == "running"
|
2025-02-01 06:06:22 +05:30
|
|
|
except podman.errors.NotFound:
|
2025-01-20 05:02:25 +05:30
|
|
|
return False
|
|
|
|
except Exception as e:
|
|
|
|
return f"Error checking container {name}: {e}"
|