second commit
This commit is contained in:
parent
441ad8f89b
commit
f84e163629
108
__main__.py
108
__main__.py
@ -3,102 +3,82 @@ import pulumi_docker as docker
|
|||||||
import json
|
import json
|
||||||
import os
|
import os
|
||||||
|
|
||||||
# Load the configuration from JSON file
|
# Load the JSON configuration file
|
||||||
try:
|
try:
|
||||||
with open("config.json", "r") as f:
|
with open("config.json", "r") as f:
|
||||||
containers_data = json.load(f)
|
containers_data = json.load(f)
|
||||||
print("Loaded configuration from config.json successfully.")
|
except FileNotFoundError:
|
||||||
except FileNotFoundError as e:
|
raise Exception("Error: 'config.json' file not found.")
|
||||||
print(f"Error: {e}. The file 'config.json' was not found.")
|
|
||||||
raise
|
|
||||||
except json.JSONDecodeError as e:
|
|
||||||
print(f"Error: {e}. There was an issue parsing 'config.json'.")
|
|
||||||
raise
|
|
||||||
except Exception as e:
|
|
||||||
print(f"Unexpected error while loading config.json: {e}")
|
|
||||||
raise
|
|
||||||
|
|
||||||
# Create the Docker network
|
# Create the network
|
||||||
try:
|
try:
|
||||||
network = docker.Network("testNetwork")
|
network = docker.Network("testNetwork")
|
||||||
print("Docker network 'testNetwork' created successfully.")
|
pulumi.export("network", network.name)
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
print(f"Error creating Docker network: {e}")
|
pulumi.log.error(f"Failed to create network: {e}")
|
||||||
raise
|
network = None
|
||||||
|
|
||||||
# Create volumes for containers
|
# 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", []):
|
||||||
if "volume_name" in volume and volume["volume_name"] not in volumes:
|
|
||||||
try:
|
try:
|
||||||
|
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"])
|
||||||
print(f"Volume {volume['volume_name']} created successfully.")
|
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
print(f"Error creating volume {volume['volume_name']}: {e}")
|
pulumi.log.error(f"Failed to create volume {volume.get('volume_name')}: {e}")
|
||||||
raise
|
|
||||||
|
|
||||||
# Create the containers
|
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"]
|
||||||
|
|
||||||
# Configure volumes
|
# Configure volumes
|
||||||
volumes_config = []
|
volumes_config = []
|
||||||
for volume in container.get("volumes", []):
|
|
||||||
try:
|
try:
|
||||||
if "volume_name" in volume:
|
if "volumes" in container:
|
||||||
volumes_config.append(docker.ContainerVolumeArgs(
|
volumes_config = [
|
||||||
|
docker.ContainerVolumeArgs(
|
||||||
container_path=volume["container_path"],
|
container_path=volume["container_path"],
|
||||||
volume_name=volumes[volume["volume_name"]].name
|
volume_name=volumes[volume["volume_name"]].name
|
||||||
))
|
) if "volume_name" in volume else
|
||||||
else:
|
docker.ContainerVolumeArgs(
|
||||||
volumes_config.append(docker.ContainerVolumeArgs(
|
|
||||||
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"])
|
||||||
))
|
)
|
||||||
print(f"Volume for container {container_name} configured successfully.")
|
for volume in container["volumes"]
|
||||||
|
]
|
||||||
|
except KeyError as e:
|
||||||
|
pulumi.log.warn(f"Missing key in volume configuration: {e}")
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
print(f"Error configuring volume for container {container_name}: {e}")
|
pulumi.log.error(f"Error configuring volumes for container {container_name}: {e}")
|
||||||
raise
|
|
||||||
|
|
||||||
# Set entrypoint script (if any)
|
|
||||||
entrypoint_script = "/entrypoint.sh" # Path where entrypoint.sh will be mounted
|
|
||||||
|
|
||||||
# Create the container
|
# Create the container
|
||||||
try:
|
try:
|
||||||
docker.Container(
|
container_resource = docker.Container(
|
||||||
container_name,
|
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=[docker.ContainerPortArgs(
|
ports=[
|
||||||
|
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", [])
|
||||||
volumes=volumes_config + [
|
] if "ports" in container else [],
|
||||||
# Mount the entrypoint.sh script into the container (if it exists)
|
volumes=volumes_config,
|
||||||
docker.ContainerVolumeArgs(
|
network_mode=network.name if network else None,
|
||||||
container_path="/entrypoint.sh", # Path inside the container
|
command=container.get("command", []),
|
||||||
host_path=os.path.abspath("entrypoint.sh") # Path on the host machine
|
|
||||||
)
|
)
|
||||||
] if "entrypoint.sh" in container.get("volumes", []) else [],
|
ports = container.get("ports", [])
|
||||||
network_mode=network.name,
|
if ports:
|
||||||
command=container.get("command", []) # Set the command from the JSON (if exists)
|
for port in ports:
|
||||||
)
|
|
||||||
print(f"Container {container_name} created successfully.")
|
|
||||||
except Exception as e:
|
|
||||||
print(f"Error creating container {container_name}: {e}")
|
|
||||||
raise
|
|
||||||
|
|
||||||
# Export the container URLs
|
|
||||||
urls = {}
|
|
||||||
for container in containers_data["containers"]:
|
|
||||||
for i in range(container.get("instances", 1)):
|
|
||||||
container_name = f"{container['name']}-{i}" if container.get("instances", 1) > 1 else container["name"]
|
|
||||||
for port in container.get("ports", []):
|
|
||||||
if "external" in port:
|
|
||||||
external_port = port["external"] + i
|
external_port = port["external"] + i
|
||||||
urls[container_name] = f"http://localhost:{external_port}"
|
pulumi.export(
|
||||||
|
f"{container_name}_url",
|
||||||
pulumi.export("urls", urls)
|
f"http://localhost:{external_port}"
|
||||||
|
)
|
||||||
|
except Exception as e:
|
||||||
|
pulumi.log.error(f"Failed to create container {container_name}: {e}")
|
||||||
|
84
config.json
84
config.json
@ -8,9 +8,7 @@
|
|||||||
"POSTGRES_USER": "admin",
|
"POSTGRES_USER": "admin",
|
||||||
"POSTGRES_PASSWORD": "admin"
|
"POSTGRES_PASSWORD": "admin"
|
||||||
},
|
},
|
||||||
"ports": [
|
"ports": [{"internal": 5432, "external": 5432}],
|
||||||
{"internal": 5432, "external": 5432}
|
|
||||||
],
|
|
||||||
"volumes": [
|
"volumes": [
|
||||||
{
|
{
|
||||||
"container_path": "/var/lib/postgresql/data",
|
"container_path": "/var/lib/postgresql/data",
|
||||||
@ -19,7 +17,7 @@
|
|||||||
]
|
]
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"name": "odoo-1",
|
"name": "odoo",
|
||||||
"image": "odoo:latest",
|
"image": "odoo:latest",
|
||||||
"envs": {
|
"envs": {
|
||||||
"HOST": "db",
|
"HOST": "db",
|
||||||
@ -28,39 +26,9 @@
|
|||||||
"DATABASE": "admin",
|
"DATABASE": "admin",
|
||||||
"ODOO_PASSWORD": "admin"
|
"ODOO_PASSWORD": "admin"
|
||||||
},
|
},
|
||||||
"ports": [
|
"ports": [{"internal": 8069, "external": 8069}],
|
||||||
{"internal": 8069, "external": 8069},
|
"instances": 2,
|
||||||
{"internal": 8070, "external": 8080}
|
"command": ["/bin/bash", "-c", "sleep 10 && /opt/odoo/odoo-bin -i base"]
|
||||||
],
|
|
||||||
"instances": 1,
|
|
||||||
"volumes": [
|
|
||||||
{
|
|
||||||
"container_path": "/entrypoint.sh",
|
|
||||||
"host_path": "entrypoint.sh"
|
|
||||||
}
|
|
||||||
]
|
|
||||||
},
|
|
||||||
{
|
|
||||||
"name": "odoo-2",
|
|
||||||
"image": "odoo:latest",
|
|
||||||
"envs": {
|
|
||||||
"HOST": "db",
|
|
||||||
"USER": "admin",
|
|
||||||
"PASSWORD": "admin",
|
|
||||||
"DATABASE": "admin",
|
|
||||||
"ODOO_PASSWORD": "admin"
|
|
||||||
},
|
|
||||||
"ports": [
|
|
||||||
{"internal": 8069, "external": 8072},
|
|
||||||
{"internal": 8071, "external": 8081}
|
|
||||||
],
|
|
||||||
"instances": 1,
|
|
||||||
"volumes": [
|
|
||||||
{
|
|
||||||
"container_path": "/entrypoint.sh",
|
|
||||||
"host_path": "entrypoint.sh"
|
|
||||||
}
|
|
||||||
]
|
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"name": "grafana",
|
"name": "grafana",
|
||||||
@ -69,40 +37,52 @@
|
|||||||
"GF_SECURITY_ADMIN_PASSWORD": "grafana_pwd",
|
"GF_SECURITY_ADMIN_PASSWORD": "grafana_pwd",
|
||||||
"GF_DATASOURCES_PROMETHEUS_URL": "http://prometheus:9090"
|
"GF_DATASOURCES_PROMETHEUS_URL": "http://prometheus:9090"
|
||||||
},
|
},
|
||||||
"ports": [
|
"ports": [{"internal": 3000, "external": 3000}],
|
||||||
{"internal": 3000, "external": 3000}
|
|
||||||
],
|
|
||||||
"instances": 2
|
"instances": 2
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"name": "prometheus",
|
"name": "prometheus",
|
||||||
"image": "prom/prometheus:latest",
|
"image": "prom/prometheus:latest",
|
||||||
"envs": {
|
"ports": [{"internal": 9090, "external": 9090}],
|
||||||
"PROMETHEUS_STORAGE_TSDB_RETENTION": "15d"
|
|
||||||
},
|
|
||||||
"ports": [
|
|
||||||
{"internal": 9090, "external": 9090}
|
|
||||||
],
|
|
||||||
"volumes": [
|
"volumes": [
|
||||||
{
|
{
|
||||||
"container_path": "/prometheus",
|
"container_path": "/prometheus",
|
||||||
"volume_name": "prometheus-data"
|
"volume_name": "prometheus-data"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"container_path": "/etc/prometheus/prometheus.yml",
|
||||||
|
"host_path": "./prometheus.yml"
|
||||||
}
|
}
|
||||||
]
|
]
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"name": "fluentd",
|
"name": "fluentd",
|
||||||
"image": "fluent/fluentd:v1.13-1",
|
"image": "fluent/fluentd:v1.13-1",
|
||||||
"envs": {
|
"ports": [{"internal": 24224, "external": 24224}],
|
||||||
"FLUENTD_ARGS": "--no-supervisor"
|
"volumes": [
|
||||||
|
{
|
||||||
|
"container_path": "/fluentd/etc/fluent.conf",
|
||||||
|
"host_path": "./fluent.conf"
|
||||||
|
}
|
||||||
|
]
|
||||||
},
|
},
|
||||||
"ports": [
|
{
|
||||||
{"internal": 24224, "external": 24224}
|
"name": "backup",
|
||||||
|
"image": "alpine:latest",
|
||||||
|
"envs": {
|
||||||
|
"POSTGRES_HOST": "db",
|
||||||
|
"POSTGRES_DB": "admin",
|
||||||
|
"POSTGRES_USER": "admin",
|
||||||
|
"POSTGRES_PASSWORD": "admin"
|
||||||
|
},
|
||||||
|
"command": [
|
||||||
|
"/bin/sh", "-c",
|
||||||
|
"apk add --no-cache postgresql-client && sleep 10"
|
||||||
],
|
],
|
||||||
"volumes": [
|
"volumes": [
|
||||||
{
|
{
|
||||||
"container_path": "/fluentd/log",
|
"container_path": "/backup",
|
||||||
"volume_name": "fluentd-log"
|
"volume_name": "backup-data"
|
||||||
}
|
}
|
||||||
]
|
]
|
||||||
}
|
}
|
||||||
|
@ -1,5 +1,10 @@
|
|||||||
#!/bin/bash
|
#!/bin/bash
|
||||||
set -e
|
set -e
|
||||||
|
|
||||||
|
until pg_isready -h db -U admin; do
|
||||||
|
echo "Waiting for PostgreSQL to be ready..."
|
||||||
sleep 10
|
sleep 10
|
||||||
|
done
|
||||||
|
|
||||||
odoo -i base
|
odoo -i base
|
||||||
exec "$@"
|
exec "$@"
|
24
fluent.conf
Normal file
24
fluent.conf
Normal file
@ -0,0 +1,24 @@
|
|||||||
|
<source>
|
||||||
|
@type forward
|
||||||
|
port 24224 # Fluentd default forward port
|
||||||
|
bind 0.0.0.0
|
||||||
|
</source>
|
||||||
|
|
||||||
|
<source>
|
||||||
|
@type forward
|
||||||
|
port 8069 # Odoo logs
|
||||||
|
bind 0.0.0.0
|
||||||
|
</source>
|
||||||
|
|
||||||
|
<source>
|
||||||
|
@type forward
|
||||||
|
port 3000 # Grafana logs
|
||||||
|
bind 0.0.0.0
|
||||||
|
</source>
|
||||||
|
|
||||||
|
|
||||||
|
<match **>
|
||||||
|
@type stdout
|
||||||
|
</match>
|
||||||
|
|
||||||
|
|
20
prometheus.yml
Normal file
20
prometheus.yml
Normal file
@ -0,0 +1,20 @@
|
|||||||
|
global:
|
||||||
|
scrape_interval: 15s
|
||||||
|
|
||||||
|
scrape_configs:
|
||||||
|
- job_name: 'postgres'
|
||||||
|
static_configs:
|
||||||
|
- targets: ['db:5432']
|
||||||
|
|
||||||
|
- job_name: 'odoo'
|
||||||
|
static_configs:
|
||||||
|
- targets: ['odoo-0:8069', 'odoo-1:8069']
|
||||||
|
|
||||||
|
- job_name: 'grafana'
|
||||||
|
static_configs:
|
||||||
|
- targets: ['grafana:3000']
|
||||||
|
|
||||||
|
- job_name: 'fluentd'
|
||||||
|
static_configs:
|
||||||
|
- targets: ['fluentd:24224']
|
||||||
|
|
Loading…
Reference in New Issue
Block a user