From 11a819ea2f4f2d54f718b2b5df678d397ff03043 Mon Sep 17 00:00:00 2001
From: Laur Ivan <laur.ivan@gmail.com>
Date: Mon, 19 Sep 2022 23:13:01 +0200
Subject: [PATCH] Add molecule files. Add default variables. Add empty
 templates.

---
 defaults/main.yml               | 33 +++++++++++++++
 meta/main.yml                   | 73 ++++++++++++---------------------
 molecule/default/cleanup.yml    | 20 +++++++++
 molecule/default/converge.yml   | 25 ++++++++---
 molecule/default/create.yml     | 35 ----------------
 molecule/default/molecule.yml   | 21 +++++++++-
 molecule/default/prepare.yml    | 12 ++++++
 molecule/requirements.yml       |  5 +++
 templates/docker-compose.yml.j2 |  0
 templates/env.plausible.conf.j2 |  0
 10 files changed, 137 insertions(+), 87 deletions(-)
 create mode 100644 molecule/default/cleanup.yml
 delete mode 100644 molecule/default/create.yml
 create mode 100644 molecule/default/prepare.yml
 create mode 100644 molecule/requirements.yml
 create mode 100644 templates/docker-compose.yml.j2
 create mode 100644 templates/env.plausible.conf.j2

diff --git a/defaults/main.yml b/defaults/main.yml
index cc1ceb0..51546e2 100644
--- a/defaults/main.yml
+++ b/defaults/main.yml
@@ -1,2 +1,35 @@
 ---
 # defaults file for plausible
+
+# Docker user
+plausible_uid: docker
+plausible_gid: docker
+
+# Email
+plausible_smtp_host:
+plausible_smtp_port: 587
+plausible_smtp_username:
+plausible_smtp_password:
+
+# Plausible parameters
+plausible_port: 8000
+plausible_admin_user_email: change-me
+plausible_admin_user_name: change-me
+plausible_admin_user_pwd: change-me
+plausible_base_url: change-me
+plausible_secret_key_base: change-me
+
+# SMTP
+plausible_smtp_email: "changeme@example.com"
+
+# Database
+plausible_clickhouse_version: "21.3.2.5"
+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"
\ No newline at end of file
diff --git a/meta/main.yml b/meta/main.yml
index e5b89bc..2fb14b8 100644
--- a/meta/main.yml
+++ b/meta/main.yml
@@ -1,53 +1,34 @@
 galaxy_info:
-  author: your name
+  author: Laur Ivan
   namespace: laurivan
-  description: your role description
-  company: your company (optional)
+  role_name: plausible
+  description: Plausible installation via docker
+  license: MIT
 
-  # If the issue tracker for your role is not on github, uncomment the
-  # next line and provide a value
-  # issue_tracker_url: http://example.com/issue/tracker
+  min_ansible_version: "2.4"
+  min_ansible_container_version: "2.4"
 
-  # Choose a valid license ID from https://spdx.org - some suggested licenses:
-  # - BSD-3-Clause (default)
-  # - MIT
-  # - GPL-2.0-or-later
-  # - GPL-3.0-only
-  # - Apache-2.0
-  # - CC-BY-4.0
-  license: license (GPL-2.0-or-later, MIT, etc)
+  platforms:
+    - name: Debian
+      versions:
+        - buster
+        - bullseye
+    - name: Ubuntu
+      versions:
+        - bionic
+        - focal
+        - jammy
+    - name: Alpine
+      version:
+        - all
+    - name: ArchLinux
+      versions:
+        - all
 
-  min_ansible_version: 2.1
-
-  # If this a Container Enabled role, provide the minimum Ansible Container version.
-  # min_ansible_container_version:
-
-  #
-  # Provide a list of supported platforms, and for each platform a list of versions.
-  # If you don't wish to enumerate all versions for a particular platform, use 'all'.
-  # To view available platforms and versions (or releases), visit:
-  # https://galaxy.ansible.com/api/v1/platforms/
-  #
-  # platforms:
-  # - name: Fedora
-  #   versions:
-  #   - all
-  #   - 25
-  # - name: SomePlatform
-  #   versions:
-  #   - all
-  #   - 1.0
-  #   - 7
-  #   - 99.99
-
-  galaxy_tags: []
-    # List tags for your role here, one per line. A tag is a keyword that describes
-    # and categorizes the role. Users find roles by searching for tags. Be sure to
-    # remove the '[]' above, if you add tags to this list.
-    #
-    # NOTE: A tag is limited to a single word comprised of alphanumeric characters.
-    #       Maximum 20 tags per role.
+  galaxy_tags: 
+    - docker
+    - authentik
+    - sso
+    - authentication
 
 dependencies: []
-  # List your role dependencies here, one per line. Be sure to remove the '[]' above,
-  # if you add dependencies to this list.
diff --git a/molecule/default/cleanup.yml b/molecule/default/cleanup.yml
new file mode 100644
index 0000000..c6ca727
--- /dev/null
+++ b/molecule/default/cleanup.yml
@@ -0,0 +1,20 @@
+---
+- name: Clean up
+  hosts: all
+  gather_facts: true
+  tasks:
+    - name: Check if the docker-compose file exists.
+      ansible.builtin.stat:
+        path: "~/authentik/docker-compose.yml"
+      register: docker_compose_file
+    - name: Remove authentik docker-compose.
+      community.docker.docker_compose:
+        project_src: ~/authentik/
+        build: false
+        state: absent
+      when: docker_compose_file.stat.exists
+      become: false
+    - name: Remove the docker-compose file
+      ansible.builtin.file:
+        path: "~/authentik/docker-compose.yml"
+        state: absent
diff --git a/molecule/default/converge.yml b/molecule/default/converge.yml
index f420789..fd6e450 100644
--- a/molecule/default/converge.yml
+++ b/molecule/default/converge.yml
@@ -1,8 +1,23 @@
 ---
 - name: Converge
   hosts: all
-  gather_facts: false
-  tasks:
-    - name: "Include laurivan.plausible"
-      ansible.builtin.include_role:
-        name: "laurivan.plausible"
+  # 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
+      
+  roles:
+    - role: laurivan.plausible
+
diff --git a/molecule/default/create.yml b/molecule/default/create.yml
deleted file mode 100644
index 09489e3..0000000
--- a/molecule/default/create.yml
+++ /dev/null
@@ -1,35 +0,0 @@
----
-- name: Create
-  hosts: localhost
-  connection: local
-  gather_facts: false
-  no_log: "{{ molecule_no_log }}"
-  tasks:
-
-    # TODO: Developer must implement and populate 'server' variable
-
-    - when: server.changed | default(false) | bool
-      block:
-        - name: Populate instance config dict
-          ansible.builtin.set_fact:
-            instance_conf_dict: {
-              'instance': "{{ }}",
-              'address': "{{ }}",
-              'user': "{{ }}",
-              'port': "{{ }}",
-              'identity_file': "{{ }}", }
-          with_items: "{{ server.results }}"
-          register: instance_config_dict
-
-        - name: Convert instance config dict to a list
-          ansible.builtin.set_fact:
-            instance_conf: "{{ instance_config_dict.results | map(attribute='ansible_facts.instance_conf_dict') | list }}"
-
-        - name: Dump instance config
-          ansible.builtin.copy:
-            content: |
-              # Molecule managed
-
-              {{ instance_conf | to_json | from_json | to_yaml }}
-            dest: "{{ molecule_instance_config }}"
-            mode: 0600
diff --git a/molecule/default/molecule.yml b/molecule/default/molecule.yml
index 74c8557..e3697e9 100644
--- a/molecule/default/molecule.yml
+++ b/molecule/default/molecule.yml
@@ -1,11 +1,30 @@
 ---
+role_name_check: 1
 dependency:
   name: galaxy
+  options:
+    ignore-certs: true
+    ignore-errors: true
+    role-file: molecule/requirements.yml
+    requirements-file: molecule/requirements.yml
 driver:
-  name: delegated
+  name: docker
 platforms:
   - name: instance
+    image: "geerlingguy/docker-${MOLECULE_DISTRO:-centos8}-ansible:latest"
+    command: ${MOLECULE_DOCKER_COMMAND:-""}
+    volumes:
+      - /sys/fs/cgroup:/sys/fs/cgroup:ro
+      - /var/run/docker.sock:/tmp/docker_mounted.sock
+    privileged: true
+    pre_build_image: true
 provisioner:
   name: ansible
+  playbooks:
+    converge: ${MOLECULE_PLAYBOOK:-converge.yml}
 verifier:
   name: ansible
+lint: |
+  set -e
+  yamllint .
+  ansible-lint .
\ No newline at end of file
diff --git a/molecule/default/prepare.yml b/molecule/default/prepare.yml
new file mode 100644
index 0000000..07ac131
--- /dev/null
+++ b/molecule/default/prepare.yml
@@ -0,0 +1,12 @@
+---
+- name: Setup the test machine
+  hosts: instance
+  tasks:
+    - name: Create docker.sock
+      raw: touch /var/run/docker.sock
+      become: true
+      changed_when: false
+    - name: Move docker.sock from tmp
+      raw: mount --move /tmp/docker_mounted.sock /var/run/docker.sock
+      become: true
+      changed_when: false
diff --git a/molecule/requirements.yml b/molecule/requirements.yml
new file mode 100644
index 0000000..56caaf8
--- /dev/null
+++ b/molecule/requirements.yml
@@ -0,0 +1,5 @@
+---
+roles:
+  - geerlingguy.docker
+collections:
+  - community.docker
\ No newline at end of file
diff --git a/templates/docker-compose.yml.j2 b/templates/docker-compose.yml.j2
new file mode 100644
index 0000000..e69de29
diff --git a/templates/env.plausible.conf.j2 b/templates/env.plausible.conf.j2
new file mode 100644
index 0000000..e69de29