Compare commits

...

8 commits

Author SHA1 Message Date
Jérémy Lecour 28be7d1218 nextcloud: intall one instance at a time
* introduce many variable with sensible defaults
* generate an admin password if none is provided (default)
* execute occ commands as unprivileged user
2018-03-29 08:35:26 +02:00
Jérémy Lecour bd9f8210fe when statements should not include jinja2 templating delimiters 2018-03-28 21:18:56 +02:00
Victor LABORIE fa65c4709f nextcloud: keep Mysql password if already defined 2018-03-26 16:08:50 +02:00
Victor LABORIE 48f9213589 nextcloud: fix log directory 2018-03-26 15:19:54 +02:00
Victor LABORIE fed1ca89a7 nextcloud: split main task and add vhost configuration 2018-03-26 15:16:55 +02:00
Jérémy Lecour 4b6e2c8519 nextcloud: verify archive checksum 2018-03-26 14:39:12 +02:00
Jérémy Lecour 10946dfaec whitespaces 2018-03-26 14:38:45 +02:00
Victor LABORIE 05ce477141 Preliminary role for Nextcloud 2018-03-23 14:27:56 +01:00
10 changed files with 454 additions and 0 deletions

View file

@ -0,0 +1,19 @@
---
nextcloud_webserver: 'nginx'
nextcloud_version: "13.0.1"
nextcloud_archive_name: "nextcloud-{{ nextcloud_version }}.tar.bz2"
nextcloud_releases_baseurl: "https://download.nextcloud.com/server/releases/"
nextcloud_instance_name: "nextcloud"
nextcloud_user: "{{ nextcloud_instance_name }}"
nextcloud_domains: []
nextcloud_home: "/home/{{ nextcloud_user }}"
nextcloud_webroot: "{{ nextcloud_home }}/nextcloud"
nextcloud_data: "{{ nextcloud_webroot }}/data"
nextcloud_db_user: "{{ nextcloud_user }}"
nextcloud_db_name: "{{ nextcloud_instance_name }}"
nextcloud_admin_login: "admin"
nextcloud_admin_password: ""

4
nextcloud/meta/main.yml Normal file
View file

@ -0,0 +1,4 @@
---
dependencies:
- { role: nginx, when: nextcloud_webserver == 'nginx' }
- { role: php, php_fpm_enable: True }

View file

@ -0,0 +1,37 @@
---
- name: Retrieve Nextcloud archive
get_url:
url: "{{ nextcloud_releases_baseurl }}{{ nextcloud_archive_name }}"
dest: "{{ nextcloud_home }}/{{ nextcloud_archive_name }}"
force: no
tags:
- nextcloud
- name: Retrieve Nextcloud sha256 checksum
get_url:
url: "{{ nextcloud_releases_baseurl }}{{ nextcloud_archive_name }}.sha256"
dest: "{{ nextcloud_home }}/{{ nextcloud_archive_name }}.sha256"
force: no
tags:
- nextcloud
- name: Verify Nextcloud sha256 checksum
command: "sha256sum -c {{ nextcloud_archive_name }}.sha256"
changed_when: "False"
args:
chdir: "{{ nextcloud_home }}"
tags:
- nextcloud
- name: Extract Nextcloud archive
unarchive:
src: "{{ nextcloud_home }}/{{ nextcloud_archive_name }}"
dest: "{{ nextcloud_home }}"
creates: "{{ nextcloud_home }}/nextcloud"
remote_src: True
mode: "0750"
owner: "{{ nextcloud_user }}"
group: "{{ nextcloud_user }}"
tags:
- nextcloud

103
nextcloud/tasks/config.yml Normal file
View file

@ -0,0 +1,103 @@
---
- name: Create data directory
file:
dest: "{{ nextcloud_data | mandatory }}"
state: directory
mode: "0770"
owner: "{{ nextcloud_user }}"
group: "{{ nextcloud_user }}"
tags:
- nextcloud
- name: Link config dir to global config dir
file:
src: "{{ nextcloud_home }}/config/config.php"
dest: "{{ nextcloud_webroot }}/config/config.php"
owner: "{{ nextcloud_user }}"
group: "{{ nextcloud_user }}"
state: link
force: True
tags:
- nextcloud
- block:
- name: Generate admin password
command: 'apg -n 1 -m 16 -M lcN'
register: nextcloud_admin_password_apg
check_mode: no
changed_when: False
- debug:
var: nextcloud_admin_password_apg
verbosity: 1
- set_fact:
nextcloud_admin_password: "{{ nextcloud_admin_password_apg.stdout }}"
- debug:
msg: "WARNING: your generated admin password is '{{ nextcloud_admin_password }}'."
tags:
- nextcloud
when: nextcloud_admin_password == ""
- name: Install Nextcloud
command: "php ./occ maintenance:install --database mysql --database-name {{ nextcloud_db_name | mandatory }} --database-user {{ nextcloud_db_user | mandatory }} --database-pass {{ nextcloud_db_pass | mandatory }} --admin-user {{ nextcloud_admin_login | mandatory }} --admin-pass {{ nextcloud_admin_password | mandatory }} --data-dir {{ nextcloud_data | mandatory }}"
args:
chdir: "{{ nextcloud_webroot }}"
creates: "{{ nextcloud_home }}/config/config.php"
become_user: "{{ nextcloud_user }}"
tags:
- nextcloud
- name: Configure Nextcloud Mysql password
replace:
dest: "{{ nextcloud_home }}/config/config.php"
regexp: "'dbpassword' => '([^']*)',"
replace: "'dbpassword' => '{{ nextcloud_db_pass }}',"
tags:
- nextcloud
- name: Configure Nextcloud cron
cron:
name: 'Nextcloud'
minute: "*/15"
job: "php -f {{ nextcloud_webroot }}/cron.php"
user: "{{ nextcloud_user }}"
tags:
- nextcloud
- name: Erase previously trusted domains config
command: "php ./occ config:system:set trusted_domains"
args:
chdir: "{{ nextcloud_webroot }}"
become_user: "{{ nextcloud_user }}"
tags:
- nextcloud
- name: Configure trusted domains
command: "php ./occ config:system:set trusted_domains {{ item.0 }} --value {{ item.1 }}"
args:
chdir: "{{ nextcloud_webroot }}"
with_indexed_items:
- "{{ nextcloud_domains }}"
become_user: "{{ nextcloud_user }}"
tags:
- nextcloud
#- name: Configure memcache local to APCu
# command: "php ./occ config:system:set memcache.local --value '\\OC\\Memcache\\APCu'"
# args:
# chdir: "{{ nextcloud_webroot }}"
# become_user: "{{ nextcloud_user }}"
# tags:
# - nextcloud
- name: Fix right on config.php
file:
dest: "{{ nextcloud_home }}/config/config.php"
owner: "{{ nextcloud_user }}"
group: "{{ nextcloud_user }}"
mode: "0660"
tags:
- nextcloud

36
nextcloud/tasks/main.yml Normal file
View file

@ -0,0 +1,36 @@
---
- name: Install dependencies
apt:
name: "{{ item }}"
state: present
with_items:
- bzip2
- php-ctype
- php-gd
- php-json
- php-xml
- php-mbstring
- php-zip
- php-pdo-mysql
- php-curl
- php-bz2
- php-intl
- php-mcrypt
- php-ldap
- php-imap
- php-gmp
- php-apcu
- php-redis
- python-mysqldb
tags:
- nextcloud
- include: user.yml
- include: archive.yml
- include: vhost.yml
- include: mysql.yml
- include: config.yml

62
nextcloud/tasks/mysql.yml Normal file
View file

@ -0,0 +1,62 @@
---
- name: Get actual Mysql password
shell: "grep password {{ nextcloud_home }}/.my.cnf | awk '{ print $3 }'"
register: nextcloud_db_pass_grep
check_mode: no
changed_when: False
failed_when: False
tags:
- nextcloud
- name: Generate Mysql password
command: 'apg -n 1 -m 16 -M lcN'
register: nextcloud_db_pass_apg
check_mode: no
changed_when: False
tags:
- nextcloud
- name: Set Mysql password
set_fact:
nextcloud_db_pass: "{{ nextcloud_db_pass_grep.stdout | default(nextcloud_db_pass_apg.stdout, True) }}"
tags:
- nextcloud
- debug:
var: nextcloud_db_pass
verbosity: 1
- name: Create Mysql database
mysql_db:
name: "{{ nextcloud_db_name }}"
config_file: "/root/.my.cnf"
state: present
tags:
- nextcloud
- name: Create Mysql user
mysql_user:
name: "{{ nextcloud_db_user }}"
password: '{{ nextcloud_db_pass }}'
priv: "{{ nextcloud_db_name }}.*:ALL"
config_file: "/root/.my.cnf"
update_password: always
state: present
tags:
- nextcloud
- name: Store credentials in my.cnf
ini_file:
dest: "{{ nextcloud_home }}/.my.cnf"
owner: "{{ nextcloud_user }}"
group: "{{ nextcloud_user }}"
mode: "0600"
section: client
option: "{{ item.option }}"
value: "{{ item.value }}"
with_items:
- { option: "user", value: "{{ nextcloud_db_user }}" }
- { option: "database", value: "{{ nextcloud_db_name }}" }
- { option: "password", value: "{{ nextcloud_db_pass }}" }
tags:
- nextcloud

32
nextcloud/tasks/user.yml Normal file
View file

@ -0,0 +1,32 @@
---
- name: Create Nextcloud groups
group:
name: "{{ nextcloud_instance_name | mandatory }}"
state: present
tags:
- nextcloud
- name: Create Nextcloud users
user:
name: "{{ nextcloud_user | mandatory }}"
group: "{{ nextcloud_user }}"
home: "{{ nextcloud_home | mandatory }}"
shell: '/bin/bash'
createhome: True
state: present
tags:
- nextcloud
- name: Create top-level directories
file:
dest: "{{ item }}"
state: directory
mode: "0770"
owner: "{{ nextcloud_user }}"
group: "{{ nextcloud_user }}"
with_items:
- "{{ nextcloud_home }}/log"
- "{{ nextcloud_home }}/config"
- "{{ nextcloud_home }}/tmp"
tags:
- nextcloud

29
nextcloud/tasks/vhost.yml Normal file
View file

@ -0,0 +1,29 @@
---
- block:
- name: Copy Nginx vhost
template:
src: nginx.conf.j2
dest: "/etc/nginx/sites-available/{{ nextcloud_instance_name }}.conf"
mode: "0640"
notify: reload nginx
tags:
- nextcloud
- name: Enable Nginx vhost
file:
src: "/etc/nginx/sites-available/{{ nextcloud_instance_name }}.conf"
dest: "/etc/nginx/sites-enabled/{{ nextcloud_instance_name }}.conf"
state: link
notify: reload nginx
tags:
- nextcloud
when: nextcloud_webserver == 'nginx'
- name: Copy PHP-FPM pool
template:
src: php-fpm.conf.j2
dest: "/etc/php/7.0/fpm/pool.d/{{ nextcloud_instance_name }}.conf"
mode: "0640"
notify: restart php-fpm
tags:
- nextcloud

View file

@ -0,0 +1,112 @@
server {
listen 80;
listen [::]:80;
listen 443 ssl http2;
listen [::]:443 ssl http2;
server_name {{ nextcloud_domains | join(' ') }};
access_log {{ nextcloud_home }}/log/access.log;
error_log {{ nextcloud_home }}/log/error.log;
include /etc/nginx/ssl/{{ nextcloud_instance_name }}.conf;
add_header X-Content-Type-Options nosniff;
add_header X-XSS-Protection "1; mode=block";
add_header X-Robots-Tag none;
add_header X-Download-Options noopen;
add_header X-Permitted-Cross-Domain-Policies none;
root {{ nextcloud_webroot }};
include /etc/nginx/snippets/letsencrypt.conf;
location = /robots.txt {
allow all;
log_not_found off;
access_log off;
}
rewrite ^/.well-known/host-meta /public.php?service=host-meta last;
rewrite ^/.well-known/host-meta.json /public.php?service=host-meta-json last;
location = /.well-known/carddav {
return 301 $scheme://$host/remote.php/dav;
}
location = /.well-known/caldav {
return 301 $scheme://$host/remote.php/dav;
}
# set max upload size
client_max_body_size 512M;
fastcgi_buffers 64 4K;
# Enable gzip but do not remove ETag headers
gzip on;
gzip_vary on;
gzip_comp_level 4;
gzip_min_length 256;
gzip_proxied expired no-cache no-store private no_last_modified no_etag auth;
gzip_types application/atom+xml application/javascript application/json application/ld+json application/manifest+json application/rss+xml application/vnd.geo+json application/vnd.ms-fontobject application/x-font-ttf application/x-web-app-manifest+json application/xhtml+xml application/xml font/opentype image/bmp image/svg+xml image/x-icon text/cache-manifest text/css text/plain text/vcard text/vnd.rim.location.xloc text/vtt text/x-component text/x-cross-domain-policy;
location / {
rewrite ^ /index.php$uri;
}
location ~ ^/(?:build|tests|config|lib|3rdparty|templates|data)/ {
deny all;
}
location ~ ^/(?:\.|autotest|occ|issue|indie|db_|console) {
deny all;
}
location ~ ^/(?:index|remote|public|cron|core/ajax/update|status|ocs/v[12]|updater/.+|ocs-provider/.+)\.php(?:$|/) {
fastcgi_split_path_info ^(.+\.php)(/.*)$;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_param PATH_INFO $fastcgi_path_info;
fastcgi_param HTTPS on;
#Avoid sending the security headers twice
fastcgi_param modHeadersAvailable true;
fastcgi_param front_controller_active true;
fastcgi_pass unix:///var/run/php/php7.0-fpm-{{ nextcloud_instance_name }}.sock;
fastcgi_intercept_errors on;
fastcgi_request_buffering off;
}
location ~ ^/(?:updater|ocs-provider)(?:$|/) {
try_files $uri/ =404;
index index.php;
}
# Adding the cache control header for js and css files
# Make sure it is BELOW the PHP block
location ~ \.(?:css|js|woff|svg|gif)$ {
try_files $uri /index.php$uri$is_args$args;
add_header Cache-Control "public, max-age=15778463";
# Add headers to serve security related headers (It is intended to
# have those duplicated to the ones above)
# Before enabling Strict-Transport-Security headers please read into
# this topic first.
# add_header Strict-Transport-Security "max-age=15768000; includeSubDomains; preload;";
#
# WARNING: Only add the preload option once you read about
# the consequences in https://hstspreload.org/. This option
# will add the domain to a hardcoded list that is shipped
# in all major browsers and getting removed from this list
# could take several months.
add_header X-Content-Type-Options nosniff;
add_header X-XSS-Protection "1; mode=block";
add_header X-Robots-Tag none;
add_header X-Download-Options noopen;
add_header X-Permitted-Cross-Domain-Policies none;
# Optional: Don't log access to assets
access_log off;
}
location ~ \.(?:png|html|ttf|ico|jpg|jpeg)$ {
try_files $uri /index.php$uri$is_args$args;
# Optional: Don't log access to other assets
access_log off;
}
}

View file

@ -0,0 +1,20 @@
[{{ nextcloud_instance_name }}]
user = {{ nextcloud_user }}
group = {{ nextcloud_user }}
listen = /run/php/php7.0-fpm-{{ nextcloud_instance_name }}.sock
listen.owner = {{ nextcloud_user }}
listen.group = {{ nextcloud_user }}
pm = ondemand
pm.max_children = 100
pm.process_idle_timeout = 10s
pm.status_path = /fpm_status
request_terminate_timeout = 60s
chdir = {{ nextcloud_webroot }}
env[HOSTNAME] = $HOSTNAME
env[PATH] = /usr/local/bin:/usr/bin:/bin
env[TMP] = {{ nextcloud_home }}/tmp
env[TMPDIR] = {{ nextcloud_home }}/tmp
env[TEMP] = {{ nextcloud_home }}/tmp