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