From 9f60ee49cb0ecb09504e9b2dac851ed4ec3bf0d2 Mon Sep 17 00:00:00 2001
From: Arthur BOUDREAULT <boudreaultarthur@ik.me>
Date: Wed, 27 Oct 2021 16:49:23 +0200
Subject: [PATCH] feat(discourse): add discourse config and plugin installation

---
 defaults/main.yml               | 11 ++++++++++
 handlers/main.yml               | 34 ++++++++++++++++++++++++++++
 tasks/discourse.yml             | 39 +++++++++++++++++++++++++++++++++
 tasks/main.yml                  |  5 +++++
 templates/saved_settings.yml.j2 |  7 ++++++
 5 files changed, 96 insertions(+)
 create mode 100644 handlers/main.yml
 create mode 100644 tasks/discourse.yml
 create mode 100644 templates/saved_settings.yml.j2

diff --git a/defaults/main.yml b/defaults/main.yml
index 14b42ca..205a144 100644
--- a/defaults/main.yml
+++ b/defaults/main.yml
@@ -56,3 +56,14 @@ ynh_apps: null
   #   args:
   #     domain: domain.tld
   #     path: /ttrss
+
+ynh_discourse_settings:
+  title: "Let's go!"
+  site_description: "Hello"
+  contact_email: "arthur_ynh@{{ ynh_main_domain }}"
+  invite_only: "true"
+  backup_frequency: "1"
+  maximum_backups: "60"
+
+ynh_discourse_plugins:
+  cakeday: "https://github.com/discourse/discourse-cakeday"
diff --git a/handlers/main.yml b/handlers/main.yml
new file mode 100644
index 0000000..7cd70ff
--- /dev/null
+++ b/handlers/main.yml
@@ -0,0 +1,34 @@
+---
+#-----------------------------------------------------------------------------#
+# ansible-yunohost allows to deploy Yunohost using Ansible                    #
+# Copyright 2021-2021 Lydra https://www.lydra.fr/                             #
+#                                                                             #
+# this program is free software: you can redistribute it and/or modify        #
+# it under the terms of the GNU General Public License as published by        #
+# the Free Software Foundation, either version 3 of the License, or           #
+# (at your option) any later version.                                         #
+#                                                                             #
+# this program is distributed in the hope that it will be useful,             #
+# but WITHOUT ANY WARRANTY; without even the implied warranty of              #
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the                #
+# GNU General Public License for more details.                                #
+#                                                                             #
+# You should have received a copy of the GNU General Public License           #
+# along with this program. If not, see <http://www.gnu.org/licenses/>.        #
+#                                                                             #
+#-----------------------------------------------------------------------------#
+
+- name: export new settings to Discourse instance
+  ansible.builtin.shell:
+    sudo -i -u discourse RAILS_ENV=production bin/rake \
+    site_settings:import < /var/www/discourse/config/saved_settings_test.yml
+
+- name: Install new plugin
+  ansible.builtin.command:
+    sudo -i -u discourse RAILS_ENV=production bin/rake \
+    --trace assets:precompile
+
+- name: Restart Discourse after plugin installation
+  ansible.builtin.service:
+    name: discourse
+    state: restarted
diff --git a/tasks/discourse.yml b/tasks/discourse.yml
new file mode 100644
index 0000000..a7bcb99
--- /dev/null
+++ b/tasks/discourse.yml
@@ -0,0 +1,39 @@
+---
+#-----------------------------------------------------------------------------#
+# ansible-yunohost allows to deploy Yunohost using Ansible                    #
+# Copyright 2021-2021 Lydra https://www.lydra.fr/                             #
+#                                                                             #
+# this program is free software: you can redistribute it and/or modify        #
+# it under the terms of the GNU General Public License as published by        #
+# the Free Software Foundation, either version 3 of the License, or           #
+# (at your option) any later version.                                         #
+#                                                                             #
+# this program is distributed in the hope that it will be useful,             #
+# but WITHOUT ANY WARRANTY; without even the implied warranty of              #
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the                #
+# GNU General Public License for more details.                                #
+#                                                                             #
+# You should have received a copy of the GNU General Public License           #
+# along with this program. If not, see <http://www.gnu.org/licenses/>.        #
+#                                                                             #
+#-----------------------------------------------------------------------------#
+
+- name: Transfer site_settings template file to node
+  ansible.builtin.template:
+    src: templates/saved_settings.yml.j2
+    dest: /var/www/discourse/config/saved_settings_test.yml
+    owner: discourse
+    group: www-data
+    mode: '0660'
+  notify: export new settings to Discourse instance
+  tags: discourse
+
+- name: Download plugin repo
+  ansible.builtin.git:
+    repo: "{{ ynh_discourse_plugins.cakeday }}"
+    dest: /var/www/discourse/plugins/discourse-cakeday
+    version: main
+  notify:
+    - Install new plugin
+    - Restart Discourse after plugin installation
+  tags: discourse
diff --git a/tasks/main.yml b/tasks/main.yml
index 0480221..6369a55 100644
--- a/tasks/main.yml
+++ b/tasks/main.yml
@@ -72,3 +72,8 @@
 - name: Install Yunohost apps
   ansible.builtin.include_tasks: apps.yml
   when: ynh_apps
+
+- name: configure Discourse
+  ansible.builtin.include_tasks: discourse.yml
+  when: ynh_discourse_settings or ynh_discourse_plugins
+  tags: discourse
diff --git a/templates/saved_settings.yml.j2 b/templates/saved_settings.yml.j2
new file mode 100644
index 0000000..16c03e2
--- /dev/null
+++ b/templates/saved_settings.yml.j2
@@ -0,0 +1,7 @@
+---
+title: {{ ynh_discourse_settings.title  }}
+site_description: {{ ynh_discourse_settings.site_description  }}
+contact_email: {{ ynh_discourse_settings.contact_email }}
+invite_only: {{ ynh_discourse_settings.invite_only }}
+backup_frequency: {{ ynh_discourse_settings.backup_frequency }}
+maximum_backups: {{ ynh_discourse_settings.maximum_backups }}
-- 
GitLab