commit 441ad8f89bc5c0bf3b29a4088baec19d8f63b5d8 Author: FetenDridi Date: Thu Nov 21 18:20:36 2024 +0100 second commit diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..a3807e5 --- /dev/null +++ b/.gitignore @@ -0,0 +1,2 @@ +*.pyc +venv/ diff --git a/Pulumi.yaml b/Pulumi.yaml new file mode 100644 index 0000000..8656a5a --- /dev/null +++ b/Pulumi.yaml @@ -0,0 +1,11 @@ +name: faten +description: faten +runtime: + name: python + options: + toolchain: pip + virtualenv: venv +config: + pulumi:tags: + value: + pulumi:template: python diff --git a/__main__.py b/__main__.py new file mode 100644 index 0000000..3877295 --- /dev/null +++ b/__main__.py @@ -0,0 +1,104 @@ +import pulumi +import pulumi_docker as docker +import json +import os + +# Load the configuration from JSON file +try: + with open("config.json", "r") as f: + containers_data = json.load(f) + print("Loaded configuration from config.json successfully.") +except FileNotFoundError as e: + 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 +try: + network = docker.Network("testNetwork") + print("Docker network 'testNetwork' created successfully.") +except Exception as e: + print(f"Error creating Docker network: {e}") + raise + +# Create volumes for containers +volumes = {} +for container in containers_data["containers"]: + for volume in container.get("volumes", []): + if "volume_name" in volume and volume["volume_name"] not in volumes: + try: + volumes[volume["volume_name"]] = docker.Volume(volume["volume_name"]) + print(f"Volume {volume['volume_name']} created successfully.") + except Exception as e: + print(f"Error creating volume {volume['volume_name']}: {e}") + raise + +# Create the containers +for container in containers_data["containers"]: + instances = container.get("instances", 1) + for i in range(instances): + container_name = f"{container['name']}-{i}" if instances > 1 else container["name"] + + # Configure volumes + volumes_config = [] + for volume in container.get("volumes", []): + try: + if "volume_name" in volume: + volumes_config.append(docker.ContainerVolumeArgs( + container_path=volume["container_path"], + volume_name=volumes[volume["volume_name"]].name + )) + else: + volumes_config.append(docker.ContainerVolumeArgs( + container_path=volume["container_path"], + host_path=os.path.abspath(volume["host_path"]) + )) + print(f"Volume for container {container_name} configured successfully.") + except Exception as e: + print(f"Error configuring volume 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 + try: + 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 + [ + # Mount the entrypoint.sh script into the container (if it exists) + docker.ContainerVolumeArgs( + container_path="/entrypoint.sh", # Path inside the container + host_path=os.path.abspath("entrypoint.sh") # Path on the host machine + ) + ] if "entrypoint.sh" in container.get("volumes", []) else [], + network_mode=network.name, + command=container.get("command", []) # Set the command from the JSON (if exists) + ) + 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 + urls[container_name] = f"http://localhost:{external_port}" + +pulumi.export("urls", urls) \ No newline at end of file diff --git a/config.json b/config.json new file mode 100644 index 0000000..052d8c4 --- /dev/null +++ b/config.json @@ -0,0 +1,110 @@ +{ + "containers": [ + { + "name": "db", + "image": "postgres:latest", + "envs": { + "POSTGRES_DB": "admin", + "POSTGRES_USER": "admin", + "POSTGRES_PASSWORD": "admin" + }, + "ports": [ + {"internal": 5432, "external": 5432} + ], + "volumes": [ + { + "container_path": "/var/lib/postgresql/data", + "volume_name": "postgres-data" + } + ] + }, + { + "name": "odoo-1", + "image": "odoo:latest", + "envs": { + "HOST": "db", + "USER": "admin", + "PASSWORD": "admin", + "DATABASE": "admin", + "ODOO_PASSWORD": "admin" + }, + "ports": [ + {"internal": 8069, "external": 8069}, + {"internal": 8070, "external": 8080} + ], + "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", + "image": "grafana/grafana:latest", + "envs": { + "GF_SECURITY_ADMIN_PASSWORD": "grafana_pwd", + "GF_DATASOURCES_PROMETHEUS_URL": "http://prometheus:9090" + }, + "ports": [ + {"internal": 3000, "external": 3000} + ], + "instances": 2 + }, + { + "name": "prometheus", + "image": "prom/prometheus:latest", + "envs": { + "PROMETHEUS_STORAGE_TSDB_RETENTION": "15d" + }, + "ports": [ + {"internal": 9090, "external": 9090} + ], + "volumes": [ + { + "container_path": "/prometheus", + "volume_name": "prometheus-data" + } + ] + }, + { + "name": "fluentd", + "image": "fluent/fluentd:v1.13-1", + "envs": { + "FLUENTD_ARGS": "--no-supervisor" + }, + "ports": [ + {"internal": 24224, "external": 24224} + ], + "volumes": [ + { + "container_path": "/fluentd/log", + "volume_name": "fluentd-log" + } + ] + } + ] + } \ No newline at end of file diff --git a/entrypoint.sh b/entrypoint.sh new file mode 100644 index 0000000..ca17e95 --- /dev/null +++ b/entrypoint.sh @@ -0,0 +1,5 @@ +#!/bin/bash +set -e +sleep 10 +odoo -i base +exec "$@" \ No newline at end of file diff --git a/requirements.txt b/requirements.txt new file mode 100644 index 0000000..bc4e430 --- /dev/null +++ b/requirements.txt @@ -0,0 +1 @@ +pulumi>=3.0.0,<4.0.0