From 91afd991677d6ae71e3ae9c6336b4067c889e6f6 Mon Sep 17 00:00:00 2001 From: vincent Date: Tue, 8 Sep 2020 19:27:15 +0200 Subject: [PATCH] add postgres compatibility --- defaults/main.yml | 7 +++++-- meta/main.yml | 1 - tasks/database_mysql.yml | 27 ++++++++++++++++++++++++ tasks/database_postgres.yml | 29 +++++++++++++++++++++++++ tasks/main.yml | 42 +++++++++++++------------------------ vars/main.yml | 16 +++++++++++++- 6 files changed, 91 insertions(+), 31 deletions(-) create mode 100644 tasks/database_mysql.yml create mode 100644 tasks/database_postgres.yml diff --git a/defaults/main.yml b/defaults/main.yml index 2dea285..e1e490b 100644 --- a/defaults/main.yml +++ b/defaults/main.yml @@ -4,14 +4,17 @@ nextcloud_db_name: nextcloud nextcloud_SQl_target_file: nextcloud_db_user: nextcloud nextcloud_db_password: +nextcloud_dbhost: localhost +nextcloud_dbport: +nextcloud_DB_type: postgres # mysql or postgres nextcloud_admin_user: admin nextcloud_admin_password: nextcloud_datadirectory: /var/nextcloud nextcloud_web_root: /usr/share/webapps/nextcloud nextcloud_trusted_domains: - localhost -nextcloud_dbhost: localhost -nextcloud_dbport: + + nextcloud_config_options: # additional options to set in config.php - option: overwrite.cli.url diff --git a/meta/main.yml b/meta/main.yml index 367655b..441fb5d 100644 --- a/meta/main.yml +++ b/meta/main.yml @@ -48,7 +48,6 @@ galaxy_info: dependencies: - {role: nginx,become: yes} - - {role: mariadb,become: yes} - php - cronie - fail2ban diff --git a/tasks/database_mysql.yml b/tasks/database_mysql.yml new file mode 100644 index 0000000..c9c7eeb --- /dev/null +++ b/tasks/database_mysql.yml @@ -0,0 +1,27 @@ + +- name: "Ensure database is present" + become: yes + mysql_db: + name: "{{ nextcloud_db_name }}" + collation: utf8mb4_unicode_ci + encoding: utf8mb4 + state: present + register: nextcloud_database_creation + +- name: import DATA in database in case of creation + become: yes + mysql_db: + name: "{{ nextcloud_db_name }}" + state: import + target: "{{gitea_SQl_target_file}}" + register: nextcloud_database_import + when: nextcloud_database_creation.changed == true and nextcloud_SQl_target_file is defined + +- name: "Ensure db user is present" + become: yes + mysql_user: + name: "{{ nextcloud_db_user }}" + host: localhost + password: "{{ nextcloud_db_password }}" + priv: "{{ nextcloud_db_name }}.*:ALL" + state: present diff --git a/tasks/database_postgres.yml b/tasks/database_postgres.yml new file mode 100644 index 0000000..328f903 --- /dev/null +++ b/tasks/database_postgres.yml @@ -0,0 +1,29 @@ +- name: "Ensure db user is present" + become: yes + become_user: postgres + postgresql_user: + name: "{{ nextcloud_db_user }}" + password: "{{ nextcloud_db_password }}" + state: present + +- name: "Ensure database is present" + become: yes + become_user: postgres + postgresql_db: + name: "{{ nextcloud_db_name }}" + lc_collate: fr_FR.UTF-8 + encoding: utf8 + owner: "{{ nextcloud_db_user }}" + state: present + register: nextcloud_database_creation + + +- name: import DATA in database in case of creation + become: yes + become_user: postgres + postgresql_db: + name: "{{ nextcloud_db_name }}" + state: restore + target: "{{ nextcloud_SQl_target_file }}" + register: nextcloud_database_import + when: nextcloud_database_creation.changed == true and nextcloud_SQl_target_file is defined \ No newline at end of file diff --git a/tasks/main.yml b/tasks/main.yml index 923b5d8..926f52b 100644 --- a/tasks/main.yml +++ b/tasks/main.yml @@ -1,31 +1,8 @@ --- # tasks file for nextcloud -- name: "Ensure database is present" - become: yes - mysql_db: - name: "{{ nextcloud_db_name }}" - collation: utf8mb4_unicode_ci - encoding: utf8mb4 - state: present - register: nextcloud_database_creation - -- name: import DATA in database in case of creation - become: yes - mysql_db: - name: "{{ nextcloud_db_name }}" - state: import - target: "{{gitea_SQl_target_file}}" - when: nextcloud_database_creation.changed == true and nextcloud_SQl_target_file is defined - -- name: "Ensure db user is present" - become: yes - mysql_user: - name: "{{ nextcloud_db_user }}" - host: localhost - password: "{{ nextcloud_db_password }}" - priv: "{{ nextcloud_db_name }}.*:ALL" - state: present +- name: select specific Database tasks + include_tasks: "database_{{nextcloud_DB_type}}.yml" - name: ensure pacman hook folder exist become: true @@ -77,15 +54,16 @@ -- name: installation - ensure nextcloud installation is finished +- name: installation - ensure nextcloud config command: > - php {{ nextcloud_web_root }}/occ maintenance:install --database "mysql" --database-name "{{ nextcloud_db_name}}" + php {{ nextcloud_web_root }}/occ maintenance:install --database "{{ 'pgsql' if nextcloud_DB_type == 'postgres' else nextcloud_DB_type }}" --database-name "{{ nextcloud_db_name}}" --database-user "{{ nextcloud_db_user }}" --database-pass "{{ nextcloud_db_password }}" --admin-user "{{ nextcloud_admin_user }}" --admin-pass "{{ nextcloud_admin_password }}" --data-dir "{{ nextcloud_datadirectory }}" become: true become_user: http changed_when: true when: installed_mode is changed or nextcloud_config_exist.stat.exists == false + - name: installation - ensure trusted domains are set command: 'php {{ nextcloud_web_root }}/occ config:system:set trusted_domains {{ item.0 }} --value "{{ item.1 }}"' @@ -95,6 +73,16 @@ with_indexed_items: - '{{ nextcloud_trusted_domains }}' +- name: ensure correct database parameter in config.php + become: true + lineinfile: + path: '{{ nextcloud_web_root }}/config/config.php' + regexp: '^\s*''{{ item.option }}''' + line: ' ''{{ item.option }}'' => {{ item.value }},' + insertafter: '\$CONFIG' + with_items: '{{ nextcloud_db_options }}' + when: nextcloud_config_options is defined + - name: ensure additional options are set in config.php if defined become: true lineinfile: diff --git a/vars/main.yml b/vars/main.yml index b52587a..ead83f1 100644 --- a/vars/main.yml +++ b/vars/main.yml @@ -1,2 +1,16 @@ --- -# vars file for nextcloud \ No newline at end of file +# vars file for nextcloud + +nextcloud_db_options: + - option: dbtype + value: "{{ 'pgsql' if nextcloud_DB_type == 'postgres' else nextcloud_DB_type }}" + - option: dbname + value: "{{nextcloud_db_name}}" + - option: dbuser + value: "{{nextcloud_db_user}}" + - option: dbpassword + value: "{{nextcloud_db_password}}" + - option: dbhost + value: "{{nextcloud_dbhost}}" + - option: dbport + value: "{{nextcloud_dbport}}" \ No newline at end of file