Handling Execptions #1 commit
This commit is contained in:
parent
6ee69c6bd9
commit
34c9d4c1a1
107
__main__.py
107
__main__.py
@ -3,47 +3,84 @@ import pulumi_docker as docker
|
||||
import json
|
||||
import os
|
||||
|
||||
# Charger le fichier JSON
|
||||
with open("config.json", "r") as f:
|
||||
containers_data = json.load(f)
|
||||
# Load the JSON configuration file
|
||||
try:
|
||||
with open("config.json", "r") as f:
|
||||
containers_data = json.load(f)
|
||||
except FileNotFoundError:
|
||||
raise Exception("Error: 'config.json' file not found.")
|
||||
|
||||
# Créer le réseau
|
||||
network = docker.Network("testNetwork")
|
||||
# Create the network
|
||||
try:
|
||||
network = docker.Network("testNetwork")
|
||||
pulumi.export("network", network.name)
|
||||
except Exception as e:
|
||||
pulumi.log.error(f"Failed to create network: {e}")
|
||||
network = None
|
||||
|
||||
# Créer les volumes
|
||||
# Create volumes
|
||||
volumes = {}
|
||||
for container in containers_data["containers"]:
|
||||
for container in containers_data.get("containers", []):
|
||||
for volume in container.get("volumes", []):
|
||||
if "volume_name" in volume and volume["volume_name"] not in volumes:
|
||||
volumes[volume["volume_name"]] = docker.Volume(volume["volume_name"])
|
||||
try:
|
||||
if "volume_name" in volume and volume["volume_name"] not in volumes:
|
||||
volumes[volume["volume_name"]] = docker.Volume(volume["volume_name"])
|
||||
except Exception as e:
|
||||
pulumi.log.error(f"Failed to create volume {volume.get('volume_name')}: {e}")
|
||||
|
||||
# Créer les conteneurs
|
||||
for container in containers_data["containers"]:
|
||||
pulumi.export("volumes", {name: vol.name for name, vol in volumes.items()})
|
||||
|
||||
# Create containers
|
||||
for container in containers_data.get("containers", []):
|
||||
instances = container.get("instances", 1)
|
||||
for i in range(instances):
|
||||
container_name = f"{container['name']}-{i}" if instances > 1 else container["name"]
|
||||
# Configurer les volumes
|
||||
volumes_config = [
|
||||
docker.ContainerVolumeArgs(
|
||||
container_path=volume["container_path"],
|
||||
volume_name=volumes[volume["volume_name"]].name
|
||||
) if "volume_name" in volume else
|
||||
docker.ContainerVolumeArgs(
|
||||
container_path=volume["container_path"],
|
||||
host_path=os.path.abspath(volume["host_path"])
|
||||
|
||||
# Configure volumes
|
||||
volumes_config = []
|
||||
try:
|
||||
if "volumes" in container:
|
||||
volumes_config = [
|
||||
docker.ContainerVolumeArgs(
|
||||
container_path=volume["container_path"],
|
||||
volume_name=volumes[volume["volume_name"]].name
|
||||
) if "volume_name" in volume else
|
||||
docker.ContainerVolumeArgs(
|
||||
container_path=volume["container_path"],
|
||||
host_path=os.path.abspath(volume["host_path"])
|
||||
)
|
||||
for volume in container["volumes"]
|
||||
]
|
||||
except KeyError as e:
|
||||
pulumi.log.warn(f"Missing key in volume configuration: {e}")
|
||||
except Exception as e:
|
||||
pulumi.log.error(f"Error configuring volumes for container {container_name}: {e}")
|
||||
|
||||
# Create the container
|
||||
try:
|
||||
container_resource = docker.Container(
|
||||
container_name,
|
||||
image=container["image"],
|
||||
envs=[
|
||||
f"{key}={value}" for key, value in container.get("envs", {}).items()
|
||||
] if "envs" in container else [],
|
||||
ports=[
|
||||
docker.ContainerPortArgs(
|
||||
internal=port["internal"],
|
||||
external=port["external"] + i
|
||||
) for port in container.get("ports", [])
|
||||
] if "ports" in container else [],
|
||||
volumes=volumes_config,
|
||||
network_mode=network.name if network else None,
|
||||
command=container.get("command", []),
|
||||
)
|
||||
for volume in container.get("volumes", [])
|
||||
]
|
||||
# Créer le conteneur
|
||||
docker.Container(container_name,
|
||||
image=container["image"],
|
||||
envs=[f"{key}={value}" for key, value in container.get("envs", {}).items()],
|
||||
ports=[
|
||||
docker.ContainerPortArgs(
|
||||
internal=port["internal"],
|
||||
external=port["external"] + i
|
||||
) for port in container.get("ports", [])
|
||||
],
|
||||
volumes=volumes_config,
|
||||
network_mode=network.name,
|
||||
command=container.get("command", []))
|
||||
ports = container.get("ports", [])
|
||||
if ports:
|
||||
for port in ports:
|
||||
external_port = port["external"] + i
|
||||
pulumi.export(
|
||||
f"{container_name}_url",
|
||||
f"http://localhost:{external_port}"
|
||||
)
|
||||
except Exception as e:
|
||||
pulumi.log.error(f"Failed to create container {container_name}: {e}")
|
||||
|
@ -1,10 +1,4 @@
|
||||
#!/bin/bash
|
||||
|
||||
set -e
|
||||
|
||||
|
||||
sleep 10
|
||||
odoo -i base
|
||||
|
||||
|
||||
exec "$@"
|
@ -8,6 +8,7 @@
|
||||
port 3000 # Grafana logs
|
||||
</source>
|
||||
|
||||
<match **>
|
||||
@type stdout
|
||||
<match *>
|
||||
@type file
|
||||
path /fluentd/logs/collected-logs
|
||||
</match>
|
||||
|
Loading…
Reference in New Issue
Block a user