From cb9cc957ef877286a7b2919d0e8723c177dec4bf Mon Sep 17 00:00:00 2001
From: Arthur BOUDREAULT <boudreaultarthur@ik.me>
Date: Wed, 3 Nov 2021 16:25:07 +0100
Subject: [PATCH] refactor: Delete useless values and replace by default values
 + add README for EN / FR

---
 README-FR.md      | 16 +++++++++++++++-
 README.md         | 16 +++++++++++++++-
 defaults/main.yml |  5 ++---
 tasks/app.yml     |  6 +++---
 tasks/apps.yml    |  2 +-
 5 files changed, 36 insertions(+), 9 deletions(-)

diff --git a/README-FR.md b/README-FR.md
index f3dcbb5..6d06594 100644
--- a/README-FR.md
+++ b/README-FR.md
@@ -95,19 +95,33 @@ ynh_apps:
       path: /
       admin: user1
       is_public: yes
+    post_install:
+      - src: "templates/file.sh.j2"
+        dest: "/tmp/script.sh"
+        type: "script"
 ```
 
 - `ynh_apps` est la liste des applications à installer.
 - `label` permet de donner un nom personnalisé à l'application sur l'interface utilisateur.
 - `link` correspond au nom de l'application Yunohost qu'on veut installer.
 
-Concernant les arguments :
+**Concernant les arguments :**
 - `domain` est indispensable. Il faut choisir un des domaines de son instance Yunohost.
 - `path` est indispensable. Il faut choisir une URL pour accéder à son application comme `domain.tld/my_app`. Utilisez juste `/` si l'application doit s'installer sur un sous-domaine.
 - `is_public` est  un argument qu'on retrouve souvent. Paramétré sur `yes`, l'application sera accessible à tout le monde, même sans authentification sur le portail SSO Yunohost. Paramétré sur `no`, l'application ne sera accessible qu'après authentification.
 
 Pour les autres arguments, il faut se référer au `manifest.json` disponible dans le dépôt de l'application Yunohost qu'on installe. Vous pouvez en apprendre plus sur cette partie [ici](https://yunohost.org/fr/packaging_apps_manifest).
 
+**Concernant la post-installation :**
+
+Il est possible de compléter l'installation des applications par l'ajout de fichiers de configuration ou de scripts que vous aurez écrit de votre côté. 
+Pour activer cette fonctionnalité, définissez la variable `post_install` qui correspond aux fichiers de post-installation de votre application. 
+Cette tâche utilisant le module template, vous pouvez tout à fait utiliser vos propres variables et les appeler dans vos fichiers de template. Pour en savoir sur ce module, cliquez [ici](https://docs.ansible.com/ansible/latest/collections/ansible/builtin/template_module.html).
+
+Par défaut le fichier va prendre comme utilisateur propriétaire le nom de l'application et comme groupe propriétaire www-data (groupe nginx). Vous pouvez les changer en précisant des valeurs différentes.
+
+Enfin, si votre fichier est un script, vous pouvez préciser son type (`type: "script"`). De cette manière, le fichier de script aura pour droits 740 et il sera exécuté après son transfert sur le serveur Yunohost puis il sera supprimé. 
+
 ## Dépendances
 
 Aucune.
diff --git a/README.md b/README.md
index 4a82b8c..0fcdd12 100644
--- a/README.md
+++ b/README.md
@@ -95,19 +95,33 @@ ynh_apps:
       path: /
       admin: user1
       is_public: yes
+    post_install:
+      - src: "templates/file.sh.j2"
+        dest: "/tmp/script.sh"
+        type: "script"
 ```
 
 - `ynh_apps` is the list of applications to install.
 - `label` allows you to give a custom name to the application on the user interface.
 - `link` is the name of the Yunohost application to install.
 
-About the arguments:
+**About the arguments:**
 - `domain` is essential. You have to choose one of the domains of your Yunohost instance.
 - `path` is required. You have to choose a URL to access your application like `domain.tld/my_app`. Just use `/` if the application is to be installed on a subdomain.
 - `is_public` argument is a common one. Set to `yes`, the application will be accessible to everyone, even without authentication to the Yunohost SSO portal. Set to `no`, the application will be accessible only after authentication.
 
 For the other arguments, you have to refer to the `manifest.json` available in the repository of the Yunohost application you install. You can learn more about this part [here](https://yunohost.org/fr/packaging_apps_manifest). 
 
+**About the post-installation:**
+
+It is possible to complete the installation of applications by adding configuration files or scripts written by yourself.
+To enable this feature, define the `post_install` variable which corresponds to the post-installation files of your applications.
+Because this task uses the template module, you can use your own variables and call them in your template files. To know more about this module, click [here](https://docs.ansible.com/ansible/latest/collections/ansible/builtin/template_module.html).
+
+By default the file will take as owner the name of the application and as owner www-data(nginx group). You can change them by specifying different values.
+
+Finally, if your file is a script, you can specify its type (`type: "script"`). This way, the script file will have 740 permissions and will be run after it is uploaded to the Yunohost server then it will be deleted. 
+
 ## Dependencies
 
 None.
diff --git a/defaults/main.yml b/defaults/main.yml
index f5b8de4..725285b 100644
--- a/defaults/main.yml
+++ b/defaults/main.yml
@@ -60,7 +60,6 @@ ynh_apps: null
   #     - src: "templates/file.sh.j2"
   #       dest: "/tmp/script.sh"
   #       type: script
-  #       owner: tinyrss
-  #       group: www-data
-  #       mode: "0770"
+  #       owner: ttrss # Only provide if different from app name
+  #       group: ttrss # Only provide if different from www-data
 
diff --git a/tasks/app.yml b/tasks/app.yml
index a6b174f..bac4b95 100644
--- a/tasks/app.yml
+++ b/tasks/app.yml
@@ -30,9 +30,9 @@
   ansible.builtin.template:
     src: "{{ item.src }}"
     dest: "{{ item.dest }}"
-    owner: "{{ item.owner }}"
-    group: "{{ item.group }}"
-    mode: "{{ item.mode }}"
+    owner: "{{ item.owner | default(ynh_app.link) }}"
+    group: "{{ item.group | default('www-data') }}"
+    mode: "{{ (item.type == 'script') | ternary('740', '640') }}"
   loop: "{{ ynh_app.post_install|default([]) }}"
   when: ynh_app.post_install
 
diff --git a/tasks/apps.yml b/tasks/apps.yml
index eeb87d0..53f19ee 100644
--- a/tasks/apps.yml
+++ b/tasks/apps.yml
@@ -27,7 +27,7 @@
   ansible.builtin.set_fact: ynh_installed_apps="{{ ynh_installed_apps_raw.stdout | from_json }}"
 
 - name: Install yunohost apps and perform post-install
-  ansible.builtin.include_tasks: app.yml 
+  ansible.builtin.include_tasks: app.yml
   loop: "{{ ynh_apps }}"
   loop_control:
     loop_var: ynh_app
-- 
GitLab