From 347f54fd79611efaf2a1ffe145b6bda6bab6729f Mon Sep 17 00:00:00 2001 From: Laur Ivan Date: Thu, 22 Sep 2022 09:43:03 +0200 Subject: [PATCH] Fix files and templates. Check if the GeoIP db exists and configure accordingly. Move preparation tasks to prepare.yml. Fix destroy.yml. --- defaults/main.yml | 10 ++--- ...se-config.xml.j2 => clickhouse-config.xml} | 0 ...nfig.xml.j2 => clickhouse-user-config.xml} | 0 handlers/main.yml | 6 +++ molecule/default/converge.yml | 17 ++----- molecule/default/destroy.yml | 4 ++ molecule/default/prepare.yml | 14 ++++++ tasks/main.yml | 44 +++++++++++++------ templates/docker-compose.yml.j2 | 20 ++++++--- templates/env.mail.conf.j2 | 4 +- templates/env.plausible.conf.j2 | 17 +++++++ 11 files changed, 95 insertions(+), 41 deletions(-) rename files/{clickhouse-config.xml.j2 => clickhouse-config.xml} (100%) rename files/{clickhouse-user-config.xml.j2 => clickhouse-user-config.xml} (100%) diff --git a/defaults/main.yml b/defaults/main.yml index 580cc72..d1b7a7b 100644 --- a/defaults/main.yml +++ b/defaults/main.yml @@ -24,8 +24,8 @@ plausible_version: "latest" plausible_pg_password: "change_me" # Volumes -plausible_base: "/mnt/plausible" -plausible_config: "{{ plausible_base }}/config" -plausible_db_data: "{{ plausible_base }}/db_data" -plausible_event_data: "{{ plausible_base }}/event_data" -plausible_geoip_database: "{{ plausible_base }}/geoip_data" +plausible_volume_base: "/mnt/plausible" +plausible_volume_config: "{{ plausible_volume_base }}/config" +plausible_volume_db: "{{ plausible_volume_base }}/db_data" +plausible_volume_events: "{{ plausible_volume_base }}/event_data" +plausible_volume_geoip: "{{ plausible_volume_base }}/geoip_data" diff --git a/files/clickhouse-config.xml.j2 b/files/clickhouse-config.xml similarity index 100% rename from files/clickhouse-config.xml.j2 rename to files/clickhouse-config.xml diff --git a/files/clickhouse-user-config.xml.j2 b/files/clickhouse-user-config.xml similarity index 100% rename from files/clickhouse-user-config.xml.j2 rename to files/clickhouse-user-config.xml diff --git a/handlers/main.yml b/handlers/main.yml index 4b234c7..4254508 100644 --- a/handlers/main.yml +++ b/handlers/main.yml @@ -1,2 +1,8 @@ --- # handlers file for plausible +- name: Restart plausible + community.docker.docker_compose: + project_src: ~/plausible/ + build: false + restarted: true + become: false diff --git a/molecule/default/converge.yml b/molecule/default/converge.yml index fd6e450..d4038a6 100644 --- a/molecule/default/converge.yml +++ b/molecule/default/converge.yml @@ -3,20 +3,9 @@ hosts: all # gather_facts: false pre_tasks: - - name: Update apt cache. - apt: update_cache=yes cache_valid_time=600 - when: ansible_os_family == 'Debian' - - name: Install python requests - pip: - name: - - requests - - docker - - docker-compose - - name: Install docker - vars: - docker_service_manage: false - include_role: - name: geerlingguy.docker + - name: "Include necessary variables" + ansible.builtin.include_vars: + file: "../../.env.yml" roles: - role: laurivan.plausible diff --git a/molecule/default/destroy.yml b/molecule/default/destroy.yml index dd6e220..b408e02 100644 --- a/molecule/default/destroy.yml +++ b/molecule/default/destroy.yml @@ -6,6 +6,10 @@ no_log: "{{ molecule_no_log }}" tasks: # Developer must implement. + - name: Remove the docker image + community.docker.docker_container: + name: instance + state: absent # Mandatory configuration for Molecule to function. diff --git a/molecule/default/prepare.yml b/molecule/default/prepare.yml index 07ac131..cf724d9 100644 --- a/molecule/default/prepare.yml +++ b/molecule/default/prepare.yml @@ -10,3 +10,17 @@ raw: mount --move /tmp/docker_mounted.sock /var/run/docker.sock become: true changed_when: false + - name: Update apt cache. + apt: update_cache=yes cache_valid_time=600 + when: ansible_os_family == 'Debian' + - name: Install python requests + pip: + name: + - requests + - docker + - docker-compose + - name: Install docker + vars: + docker_service_manage: false + include_role: + name: geerlingguy.docker diff --git a/tasks/main.yml b/tasks/main.yml index 5e162ec..29f23c6 100644 --- a/tasks/main.yml +++ b/tasks/main.yml @@ -1,18 +1,20 @@ --- # tasks file for plausible +# Check if geoip volume exists +- name: Check if geoip volume exists + ansible.builtin.stat: + path: "{{ plausible_volume_geoip }}/GeoLite2-Country.mmdb" + register: plausible_geoip_stat + - name: Set up directories ansible.builtin.file: state: directory path: "{{ item }}" mode: 0755 with_items: - - "{{ plausible_volume_media }}" - - "{{ plausible_volume_certs }}" - - "{{ plausible_volume_geoip }}" - - "{{ plausible_volume_templates }}" - "{{ plausible_volume_config }}" - - "{{ plausible_volume_redis }}" + - "{{ plausible_volume_events }}" - "~/plausible" tags: - configuration @@ -28,7 +30,29 @@ - "{{ plausible_volume_db }}" tags: - configuration - become: false + become: true + +- name: Write plausible configuration (1) + ansible.builtin.template: + src: "{{ item }}.j2" + dest: "{{ plausible_volume_config }}/{{ item }}" + mode: '0640' + loop: + - "env.plausible.conf" + - "env.mail.conf" + tags: + - configuration + +- name: Write plausible configuration (2) + ansible.builtin.copy: + src: "{{ item }}" + dest: "{{ plausible_volume_config }}/{{ item }}" + mode: '0640' + loop: + - "clickhouse-config.xml" + - "clickhouse-user-config.xml" + tags: + - configuration - name: Copy plausible docker-compose template. ansible.builtin.template: @@ -38,14 +62,6 @@ become: false notify: Restart plausible -- name: Copy plausible configuration. - ansible.builtin.template: - src: templates/env.plausible.conf.j2 - dest: "{{ plausible_volume_config }}/env.plausible.conf" - mode: '0640' - become: false - notify: Restart plausible - - name: Ensure plausible is running. community.docker.docker_compose: project_src: ~/plausible/ diff --git a/templates/docker-compose.yml.j2 b/templates/docker-compose.yml.j2 index ede9870..0df4e6e 100644 --- a/templates/docker-compose.yml.j2 +++ b/templates/docker-compose.yml.j2 @@ -3,6 +3,8 @@ services: mail: image: bytemark/smtp restart: always + env_file: + - {{ plausible_volume_config }}/env.mail.conf networks: - plausible @@ -10,9 +12,9 @@ services: image: postgres:12 restart: always volumes: - - {{ plausible_db_data }}:/var/lib/postgresql/data + - {{ plausible_volume_db }}:/var/lib/postgresql/data environment: - - POSTGRES_PASSWORD=postgres + - POSTGRES_PASSWORD={{ plausible_pg_password }} networks: - plausible @@ -20,9 +22,9 @@ services: image: clickhouse/clickhouse-server:22.6-alpine restart: always volumes: - - {{ plausible_event_data }}:/var/lib/clickhouse - - {{ plausible_config }}/clickhouse-config.xml:/etc/clickhouse-server/config.d/logging.xml:ro - - {{ plausible_config }}/clickhouse-user-config.xml:/etc/clickhouse-server/users.d/logging.xml:ro + - {{ plausible_volume_events }}:/var/lib/clickhouse + - {{ plausible_volume_config }}/clickhouse-config.xml:/etc/clickhouse-server/config.d/logging.xml:ro + - {{ plausible_volume_config }}/clickhouse-user-config.xml:/etc/clickhouse-server/users.d/logging.xml:ro ulimits: nofile: soft: 262144 @@ -41,9 +43,13 @@ services: ports: - {{ plausible_port }}:8000 env_file: - - {{ plausible_config }}/env.plausible.conf + - {{ plausible_volume_config }}/env.plausible.conf +{% if plausible_geoip_stat.stat.exists %} volumes: - - {{ plausible_geoip_database }}:/geoip:ro + - {{ plausible_volume_geoip }}/GeoLite2-Country.mmdb:/geoip:ro +{% else %} + # No GeoIP data volume +{% endif%} networks: - plausible diff --git a/templates/env.mail.conf.j2 b/templates/env.mail.conf.j2 index a9d9109..c70e022 100644 --- a/templates/env.mail.conf.j2 +++ b/templates/env.mail.conf.j2 @@ -1,4 +1,6 @@ +{%if plausible_smtp_host %} RELAY_HOST={{ plausible_smtp_host }} RELAY_PORT={{ plausible_smtp_port }} RELAY_USERNAME={{ plausible_smtp_username }} -RELAY_PASSWORD={{ plausible_smtp_password }} \ No newline at end of file +RELAY_PASSWORD={{ plausible_smtp_password }} +{% endif %} \ No newline at end of file diff --git a/templates/env.plausible.conf.j2 b/templates/env.plausible.conf.j2 index e69de29..cf4792f 100644 --- a/templates/env.plausible.conf.j2 +++ b/templates/env.plausible.conf.j2 @@ -0,0 +1,17 @@ +ADMIN_USER_EMAIL={{ plausible_admin_user_email }} +ADMIN_USER_NAME={{ plausible_admin_user_name }} +ADMIN_USER_PWD={{ plausible_admin_user_pwd }} +BASE_URL={{ plausible_base_url }} +SECRET_KEY_BASE={{ plausible_secret_key_base }} + +{% if plausible_geoip_stat.stat.exists %} +GEOLITE2_COUNTRY_DB={{ plausible_volume_geoip }}/GeoLite2-Country.mmdb +{% endif %} + +# DB and events +DATABASE_URL=postgres://postgres:{{ plausible_pg_password }}@plausible_db:5432/plausible_db +CLICKHOUSE_DATABASE_URL=http://plausible_events_db:8123/plausible_events_db + +#SMTP +SMTP_HOST_ADDR=plausible_mail +MAILER_EMAIL={{ plausible_smtp_email }}