From d120b0c153e3313171017baf89d1c97a5e5b51e1 Mon Sep 17 00:00:00 2001
From: Michael Hollister <michael@futo.org>
Date: Thu, 13 Jul 2023 21:16:07 -0500
Subject: [PATCH] Updated code to use identifier variable

---
 .../custom/matrix-media-repo/defaults/main.yml |  9 +++++++--
 .../matrix-media-repo/tasks/setup_install.yml  |  4 ++--
 .../tasks/setup_uninstall.yml                  | 12 ++++++------
 .../systemd/matrix-media-repo.service.j2       | 18 +++++++++---------
 4 files changed, 24 insertions(+), 19 deletions(-)

diff --git a/roles/custom/matrix-media-repo/defaults/main.yml b/roles/custom/matrix-media-repo/defaults/main.yml
index a89ea8a58..a08ad0418 100644
--- a/roles/custom/matrix-media-repo/defaults/main.yml
+++ b/roles/custom/matrix-media-repo/defaults/main.yml
@@ -7,6 +7,11 @@
 
 matrix_media_repo_enabled: false
 
+# matrix_media_repo_identifier controls the identifier of this media-repo instance, which influences:
+# - the default storage path
+# - the names of systemd services
+matrix_media_repo_identifier: matrix-media-repo
+
 matrix_media_repo_container_image_self_build: false
 matrix_media_repo_container_image_self_build_repo: "https://github.com/turt2live/matrix-media-repo.git"
 
@@ -16,7 +21,7 @@ matrix_media_repo_docker_image_name_prefix: "{{ 'localhost/' if matrix_media_rep
 matrix_media_repo_docker_image_tag: "v1.2.13"
 matrix_media_repo_docker_image_force_pull: "{{ matrix_media_repo_docker_image.endswith(':latest') }}"
 
-matrix_media_repo_base_path: "{{ matrix_base_data_path }}/media-repo"
+matrix_media_repo_base_path: "{{ matrix_base_data_path }}/{{ matrix_media_repo_identifier }}"
 matrix_media_repo_config_path: "{{ matrix_media_repo_base_path }}/config"
 matrix_media_repo_data_path: "{{ matrix_media_repo_base_path }}/data"
 matrix_media_repo_docker_src_files_path: "{{ matrix_media_repo_base_path }}/docker-src"
@@ -104,7 +109,7 @@ matrix_media_repo_federation_backoff_at: 20
 # user instead. Using the same server is fine, just not the same username and database.
 matrix_media_repo_database_username: "matrix_media_repo"
 matrix_media_repo_database_password: "your_password"
-matrix_media_repo_database_hostname: "matrix-postgres"
+matrix_media_repo_database_hostname: "{{ devture_postgres_identifier }}"
 matrix_media_repo_database_port: 5432
 matrix_media_repo_database_name: "matrix_media_repo"
 
diff --git a/roles/custom/matrix-media-repo/tasks/setup_install.yml b/roles/custom/matrix-media-repo/tasks/setup_install.yml
index 5e7399059..3bcbed966 100644
--- a/roles/custom/matrix-media-repo/tasks/setup_install.yml
+++ b/roles/custom/matrix-media-repo/tasks/setup_install.yml
@@ -81,8 +81,8 @@
     name: "{{ matrix_media_repo_container_network }}"
     driver: bridge
 
-- name: Ensure matrix-media-repo.service installed
+- name: Ensure media-repo service installed
   ansible.builtin.template:
     src: "{{ role_path }}/templates/media-repo/systemd/matrix-media-repo.service.j2"
-    dest: "{{ devture_systemd_docker_base_systemd_path }}/matrix-media-repo.service"
+    dest: "{{ devture_systemd_docker_base_systemd_path }}/{{ matrix_media_repo_identifier }}.service"
     mode: 0640
diff --git a/roles/custom/matrix-media-repo/tasks/setup_uninstall.yml b/roles/custom/matrix-media-repo/tasks/setup_uninstall.yml
index d5f08994e..449cd48b8 100644
--- a/roles/custom/matrix-media-repo/tasks/setup_uninstall.yml
+++ b/roles/custom/matrix-media-repo/tasks/setup_uninstall.yml
@@ -1,19 +1,19 @@
 ---
 
-- name: Check existence of matrix-media-repo service
+- name: Check existence of media-repo service
   ansible.builtin.stat:
-    path: "{{ devture_systemd_docker_base_systemd_path }}/matrix-media-repo.service"
+    path: "{{ devture_systemd_docker_base_systemd_path }}/{{ matrix_media_repo_identifier }}.service"
   register: matrix_media_repo_service_stat
 
 - when: matrix_media_repo_service_stat.stat.exists | bool
   block:
-    - name: Ensure matrix-media-repo is stopped
+    - name: Ensure media-repo is stopped
       ansible.builtin.systemd:
-        name: matrix-media-repo
+        name: "{{ matrix_media_repo_identifier }}"
         state: stopped
         daemon_reload: true
 
-    - name: Ensure matrix-media-repo.service doesn't exist
+    - name: Ensure media-repo service doesn't exist
       ansible.builtin.file:
-        path: "{{ devture_systemd_docker_base_systemd_path }}/matrix-media-repo.service"
+        path: "{{ devture_systemd_docker_base_systemd_path }}/{{ matrix_media_repo_identifier }}.service"
         state: absent
diff --git a/roles/custom/matrix-media-repo/templates/media-repo/systemd/matrix-media-repo.service.j2 b/roles/custom/matrix-media-repo/templates/media-repo/systemd/matrix-media-repo.service.j2
index 33b5199a6..0e73cb6ce 100644
--- a/roles/custom/matrix-media-repo/templates/media-repo/systemd/matrix-media-repo.service.j2
+++ b/roles/custom/matrix-media-repo/templates/media-repo/systemd/matrix-media-repo.service.j2
@@ -13,12 +13,12 @@ DefaultDependencies=no
 [Service]
 Type=simple
 Environment="HOME={{ devture_systemd_docker_base_systemd_unit_home_path }}"
-ExecStartPre=-{{ devture_systemd_docker_base_host_command_sh }} -c '{{ devture_systemd_docker_base_host_command_docker }} kill matrix-media-repo 2>/dev/null || true'
-ExecStartPre=-{{ devture_systemd_docker_base_host_command_sh }} -c '{{ devture_systemd_docker_base_host_command_docker }} rm matrix-media-repo 2>/dev/null || true'
+ExecStartPre=-{{ devture_systemd_docker_base_host_command_sh }} -c '{{ devture_systemd_docker_base_host_command_docker }} kill {{ matrix_media_repo_identifier }} 2>/dev/null || true'
+ExecStartPre=-{{ devture_systemd_docker_base_host_command_sh }} -c '{{ devture_systemd_docker_base_host_command_docker }} rm {{ matrix_media_repo_identifier }} 2>/dev/null || true'
 
 ExecStartPre={{ devture_systemd_docker_base_host_command_docker }} create \
 			--rm \
-			--name=matrix-media-repo \
+			--name={{ matrix_media_repo_identifier }} \
 			--log-driver=none \
 			--user={{ matrix_user_uid }}:{{ matrix_user_gid }} \
 			--cap-drop=ALL \
@@ -40,17 +40,17 @@ ExecStartPre={{ devture_systemd_docker_base_host_command_docker }} create \
 			{{ matrix_media_repo_docker_image }}
 
 {% for network in matrix_media_repo_container_additional_networks %}
-ExecStartPre={{ devture_systemd_docker_base_host_command_docker }} network connect {{ network }} matrix-media-repo
+ExecStartPre={{ devture_systemd_docker_base_host_command_docker }} network connect {{ network }} {{ matrix_media_repo_identifier }}
 {% endfor %}
 
-ExecStart={{ devture_systemd_docker_base_host_command_docker }} start --attach matrix-media-repo
+ExecStart={{ devture_systemd_docker_base_host_command_docker }} start --attach {{ matrix_media_repo_identifier }}
 
-ExecStop=-{{ devture_systemd_docker_base_host_command_sh }} -c '{{ devture_systemd_docker_base_host_command_docker }} kill matrix-media-repo 2>/dev/null || true'
-ExecStop=-{{ devture_systemd_docker_base_host_command_sh }} -c '{{ devture_systemd_docker_base_host_command_docker }} rm matrix-media-repo 2>/dev/null || true'
-ExecReload={{ devture_systemd_docker_base_host_command_docker }} exec matrix-media-repo /bin/sh -c 'kill -HUP 1'
+ExecStop=-{{ devture_systemd_docker_base_host_command_sh }} -c '{{ devture_systemd_docker_base_host_command_docker }} kill {{ matrix_media_repo_identifier }} 2>/dev/null || true'
+ExecStop=-{{ devture_systemd_docker_base_host_command_sh }} -c '{{ devture_systemd_docker_base_host_command_docker }} rm {{ matrix_media_repo_identifier }} 2>/dev/null || true'
+ExecReload={{ devture_systemd_docker_base_host_command_docker }} exec {{ matrix_media_repo_identifier }} /bin/sh -c 'kill -HUP 1'
 Restart=always
 RestartSec=30
-SyslogIdentifier=matrix-media-repo
+SyslogIdentifier={{ matrix_media_repo_identifier }}
 
 [Install]
 WantedBy=multi-user.target