commit 2281e6eb41d8540e40e08a3f1f06ca9c179b86e0 Author: FetenDridi Date: Thu Nov 21 12:34:47 2024 +0100 first 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..36f1b22 --- /dev/null +++ b/Pulumi.yaml @@ -0,0 +1,11 @@ +name: pulumi4 +description: pulumi +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..a7aa857 --- /dev/null +++ b/__main__.py @@ -0,0 +1,82 @@ +import pulumi +import pulumi_docker as docker +import json + +# Load the configuration from the JSON file +with open('config.json') as f: + config_data = json.load(f) + +# Create a Docker network +network = docker.Network("testNetwork") + +# Initialize the containers list +containers = [] + + +# Initialize the URL dictionary for export +urls = {} + +# Create containers based on the configuration +for container in config_data["containers"]: + container_name = container["name"] + container_envs = [f"{key}={value}" for key, value in container.get("env", {}).items()] + + # Create the container + docker_container = docker.Container( + container_name, + name=container_name, + image=container["image"], + envs=container_envs, + ports=[docker.ContainerPortArgs( + internal=port["internal"], + external=port["external"] + ) for port in container["ports"]], + network_mode=network.name, + command=container.get("command", None) or container.get("entrypoint", None), + ##container.get("entrypoint", "/usr/local/bin/entrypoint.sh") + + volumes=[docker.ContainerVolumeArgs( + host_path=vol["host_path"], + container_path=vol["container_path"] + ) for vol in container.get("volumes", [])] + ) + containers.append(docker_container) + + # Add the URLs for the container + for port in container["ports"]: + urls[f"{container_name}_url"] = f"http://localhost:{port['external']}" + +# Scale Prometheus containers +for i in range(config_data.get("prometheus_scale", 1)): # Default to 1 if not specified + prometheus_instance = docker.Container( + f"prometheus-instance-{i}", + name=f"prometheus-{i}", + image="prom/prometheus:latest", + ports=[docker.ContainerPortArgs(internal=9090, external=9090 + i)], + network_mode=network.name + ) + containers.append(prometheus_instance) + + # Add the Prometheus URLs + urls[f"prometheus_{i}_url"] = f"http://localhost:{9090 + i}" + +# Scale Fluentd containers +fluentd_scale = config_data.get("fluentd_scale", 1) # Default to 1 if not specified + +for i in range(fluentd_scale): # This will scale based on the value from config_data + fluentd_instance = docker.Container( + f"fluentd-instance-{i}", + name=f"fluentd-{i}", + image="fluent/fluentd:v1.14-1", # Corrected image name + ports=[docker.ContainerPortArgs(internal=24224, external=24224 + i)], # Assign unique external port for each container + network_mode=network.name + ) + containers.append(fluentd_instance) + + # Add the Fluentd URLs + urls[f"fluentd_{i}_url"] = f"http://localhost:{24224 + i}" + +# Export network and container details +pulumi.export("network_name", network.name) +pulumi.export("containers", [c.name for c in containers]) +pulumi.export("urls", urls) diff --git a/config.json b/config.json new file mode 100644 index 0000000..a50f0b4 --- /dev/null +++ b/config.json @@ -0,0 +1,51 @@ +{ + "containers": [ + { + "name": "postgres", + "image": "postgres:16.5", + "env": { + "POSTGRES_USER": "odoo", + "POSTGRES_PASSWORD": "odoo", + "POSTGRES_DB": "postgres", + "POSTGRES_HOST_AUTH_METHOD": "trust" + }, + "ports": [ + {"internal": 5432, "external": 5432} + ], + "volumes": [ + {"host_path": "/local/path/postgresql/data", "container_path": "/var/lib/postgresql/data"} + ] + }, + { + "name": "odoo", + "image": "odoo:latest", + "env": { + "HOST": "postgres", + "USER": "odoo", + "PASSWORD": "odoo", + "DATABASE": "postgres", + "ODOO_PASSWORD": "admin" + }, + "ports": [ + {"internal": 8069, "external": 8069} + ], + "command": [ + "/bin/bash", "-c", "until pg_isready -h postgres -U odoo; do echo 'Waiting for PostgreSQL...'; sleep 2; done; odoo -i base" + ] + }, + { + "name": "grafana", + "image": "grafana/grafana:latest", + "env": { + "GF_SECURITY_ADMIN_PASSWORD": "grafana_pwd", + "GF_DATASOURCES_PROMETHEUS_URL": "http://prometheus:9090" + }, + "ports": [ + {"internal": 3000, "external": 3000} + ] + } + ], + "prometheus_scale": 2, + "fluentd_scale": 2 + } + \ No newline at end of file diff --git a/entrypoint.sh b/entrypoint.sh new file mode 100644 index 0000000..0ba8012 --- /dev/null +++ b/entrypoint.sh @@ -0,0 +1,6 @@ +# Wait for PostgreSQL to be ready +until pg_isready -h $HOST -U $USER; do + echo "Waiting for PostgreSQL..." + sleep 2 +done + 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