projetPulumi/__main__.py
2024-11-21 12:34:47 +01:00

83 lines
2.8 KiB
Python

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)