From 3105cc3da06e0729bef5c1cb01b61a30f6c52d89 Mon Sep 17 00:00:00 2001 From: FetenDridi Date: Thu, 28 Nov 2024 08:39:20 +0100 Subject: [PATCH] grafana --- .env | 5 ++ .gitignore | 2 + Pulumi.yaml | 11 +++++ __main__.py | 81 ++++++++++++++++++++++++++++++ backup.Dockerfile | 16 ++++++ config.json | 114 +++++++++++++++++++++++++++++++++++++++++++ entrypoint_backup.sh | 11 +++++ entrypoint_odoo.sh | 4 ++ fluent.conf | 14 ++++++ odoo.Dockerfile | 14 ++++++ odoo.conf | 7 +++ postgresql.conf | 3 ++ projetPulumi | 1 + prometheus.yml | 23 +++++++++ requirements.txt | 1 + 15 files changed, 307 insertions(+) create mode 100644 .env create mode 100644 .gitignore create mode 100644 Pulumi.yaml create mode 100644 __main__.py create mode 100644 backup.Dockerfile create mode 100644 config.json create mode 100644 entrypoint_backup.sh create mode 100644 entrypoint_odoo.sh create mode 100644 fluent.conf create mode 100644 odoo.Dockerfile create mode 100644 odoo.conf create mode 100644 postgresql.conf create mode 160000 projetPulumi create mode 100644 prometheus.yml create mode 100644 requirements.txt diff --git a/.env b/.env new file mode 100644 index 0000000..31d8b37 --- /dev/null +++ b/.env @@ -0,0 +1,5 @@ +POSTGRES_PASSWORD=openpgpwd +POSTGRES_USER=openpg +POSTGRES_DB=pulumi +ODOO_PASSWORD=admin +GRAFANA_PASSWORD=grafana_pwd 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..7c31e90 --- /dev/null +++ b/Pulumi.yaml @@ -0,0 +1,11 @@ +name: projet +description: projet pulumi docker +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..fb75224 --- /dev/null +++ b/__main__.py @@ -0,0 +1,81 @@ +import pulumi +import pulumi_docker as docker +import json +import os + +# Load the JSON configuration file +try: + with open("config.json", "r") as f: + containers_data = json.load(f) +except FileNotFoundError: + raise Exception("Error: 'config.json' file not found.") + +# Create the network +try: + network = docker.Network("testNetwork") + +except Exception as e: + pulumi.log.error(f"Failed to create network: {e}") + network = None + +# Create containers +for container in containers_data.get("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 = {} + for volume in container.get("volumes", []): + try: + if "volume_name" in volume and volume["volume_name"] not in volumes: + 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}") + volumes_config = [] + try: + if "volumes" in container: + volumes_config = [ + docker.ContainerVolumeArgs( + container_path=volume["container_path"], + volume_name=volumes[volume["volume_name"]].name + ) if "volume_name" in volume else + docker.ContainerVolumeArgs( + container_path=volume["container_path"], + host_path=os.path.abspath(volume["host_path"]) + ) + for volume in container["volumes"] + ] + except KeyError as e: + 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"], + hostname=container_name, + envs=[ + f"{key}={value}" for key, value in container.get("envs", {}).items() + ] if "envs" in container else [], + ports=[ + docker.ContainerPortArgs( + internal=port["internal"], + external=port["external"] + i + ) for port in container.get("ports", []) + ] if "ports" in container else [], + volumes=volumes_config, + network_mode=network.name if network else None, + ) + 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}") diff --git a/backup.Dockerfile b/backup.Dockerfile new file mode 100644 index 0000000..1bc4f05 --- /dev/null +++ b/backup.Dockerfile @@ -0,0 +1,16 @@ +# Use the Alpine image as the base +FROM alpine:latest as backup_custom + +# Copy the entrypoint script into the container +COPY entrypoint_backup.sh /usr/local/bin/entrypoint_backup.sh + +# Switch to root user for setup +USER root + +# Make the entrypoint script executable +RUN chmod +x /usr/local/bin/entrypoint_backup.sh + +# Set the new entrypoint +ENTRYPOINT ["/usr/local/bin/entrypoint_backup.sh"] + + diff --git a/config.json b/config.json new file mode 100644 index 0000000..a995721 --- /dev/null +++ b/config.json @@ -0,0 +1,114 @@ +{ + "containers": [ + { + "name": "admin", + "image": "postgres:latest", + "envs": { + "POSTGRES_DB": "admin", + "POSTGRES_USER": "admin", + "POSTGRES_PASSWORD": "admin" + }, + "network_mode": "testNetwork", + "ports": [{"internal": 5432, "external": 5432}], + "volumes": [ + { + "container_path": "/var/lib/postgresql/data", + "volume_name": "postgres-data" + }, + { + "container_path": "/etc/postgresql/postgresql.conf", + "host_path": "./postgresql.conf" + } + ] + }, + + { + "name": "postgres_exporter", + "image": "wrouesnel/postgres_exporter:latest", + "envs": { + "DATA_SOURCE_NAME": "postgresql://admin:admin@admin:5432/admin?sslmode=disable" + }, + "network_mode": "testNetwork", + "ports": [{"internal": 9187, "external": 9187}] + }, + + + { + "name": "odoo", + "image": "odoo_custom", + "envs": { + "HOST": "admin", + "USER": "admin", + "PASSWORD": "admin", + "DATABASE": "admin", + "ODOO_PASSWORD": "admin" + }, + "network_mode": "testNetwork", + "ports": [{"internal": 8069, "external": 8069}], + "instances": 3, + "volumes": [ + { + "host_path": "./odoo.conf", + "container_path": "/etc/odoo/odoo.conf" + } + ] + }, + { + "name": "grafana", + "image": "grafana/grafana:latest", + "envs": { + "GF_SECURITY_ADMIN_PASSWORD": "grafana_pwd", + "GF_DATASOURCES_PROMETHEUS_URL": "http://prometheus:9090" + }, + "network_mode": "testNetwork", + "ports": [{"internal": 3000, "external": 3000}], + "instances": 2 + }, + { + "name": "prometheus", + "image": "prom/prometheus:latest", + "network_mode": "testNetwork", + "ports": [{"internal": 9090, "external": 9090}], + "volumes": [ + { + "container_path": "/prometheus", + "volume_name": "prometheus-data" + }, + { + "container_path": "/etc/prometheus/prometheus.yml", + "host_path": "./prometheus.yml" + } + ] + }, + { + "name": "fluentd", + "image": "fluent/fluentd:v1.13-1", + "network_mode": "testNetwork", + "ports": [{"internal": 24224, "external": 24224}], + "volumes": [ + { + "container_path": "/fluentd/etc/fluent.conf", + "host_path": "./fluent.conf" + } + ] + }, + { + "name": "backup", + "image": "backup_custom", + "envs": { + "POSTGRES_HOST": "admin", + "POSTGRES_DB": "admin", + "POSTGRES_USER": "admin", + "POSTGRES_PASSWORD": "admin" + }, + "network_mode": "testNetwork", + "volumes": [ + { + "container_path": "/backup", + "volume_name": "backup-data" + } + ] + } + ] + } + \ No newline at end of file diff --git a/entrypoint_backup.sh b/entrypoint_backup.sh new file mode 100644 index 0000000..6ee9b8f --- /dev/null +++ b/entrypoint_backup.sh @@ -0,0 +1,11 @@ +#!/bin/sh + +# Install PostgreSQL client +apk add --no-cache postgresql-client + +# Wait until the PostgreSQL server is ready +until pg_isready -h admin -U admin; do + echo "Waiting for PostgreSQL..." + sleep 2 +done + diff --git a/entrypoint_odoo.sh b/entrypoint_odoo.sh new file mode 100644 index 0000000..28469d2 --- /dev/null +++ b/entrypoint_odoo.sh @@ -0,0 +1,4 @@ +#!/bin/sh + +sleep10 +odoo -i base diff --git a/fluent.conf b/fluent.conf new file mode 100644 index 0000000..39b2e17 --- /dev/null +++ b/fluent.conf @@ -0,0 +1,14 @@ + + @type forward + port 8069 # Odoo logs + + + + @type forward + port 3000 # Grafana logs + + + + @type file + path /fluentd/logs/collected-logs + diff --git a/odoo.Dockerfile b/odoo.Dockerfile new file mode 100644 index 0000000..26b2cb8 --- /dev/null +++ b/odoo.Dockerfile @@ -0,0 +1,14 @@ +# Use the existing Odoo image as the base +FROM odoo:latest as odoo-custom + +# Copy the entrypoint script into the container +COPY entrypoint_odoo.sh /usr/local/bin/entrypoint_odoo.sh + +USER root + +# Make the entrypoint script executable +RUN chmod +x /usr/local/bin/entrypoint_odoo.sh + +# Set the new entrypoint +ENTRYPOINT ["/usr/local/bin/entrypoint_odoo.sh"] + diff --git a/odoo.conf b/odoo.conf new file mode 100644 index 0000000..285b277 --- /dev/null +++ b/odoo.conf @@ -0,0 +1,7 @@ +[options] +db_host = admin +db_port = 5432 +db_user = admin +db_password = admin +default_productivity_apps = True +db_name = admin \ No newline at end of file diff --git a/postgresql.conf b/postgresql.conf new file mode 100644 index 0000000..81b8b7a --- /dev/null +++ b/postgresql.conf @@ -0,0 +1,3 @@ +shared_preload_libraries = 'pg_stat_statements' +pg_stat_statements.max = 10000 +pg_stat_statements.track = all diff --git a/projetPulumi b/projetPulumi new file mode 160000 index 0000000..2281e6e --- /dev/null +++ b/projetPulumi @@ -0,0 +1 @@ +Subproject commit 2281e6eb41d8540e40e08a3f1f06ca9c179b86e0 diff --git a/prometheus.yml b/prometheus.yml new file mode 100644 index 0000000..eddfb4e --- /dev/null +++ b/prometheus.yml @@ -0,0 +1,23 @@ +global: + scrape_interval: 15s # How often to scrape metrics (every 15 seconds) + +scrape_configs: + - job_name: 'postgres' # For scraping PostgreSQL + static_configs: + - targets: ['postgres:5432'] + + - job_name: 'odoo' # For scraping Odoo + static_configs: + - targets: ['odoo:8069'] + + - job_name: 'grafana' # For scraping Grafana + static_configs: + - targets: ['grafana:3000'] + + - job_name: 'postgres_exporter' + static_configs: + - targets: ['postgres_exporter:9187'] + + + + \ 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