From d2bcfec8108ebb0b0665f6c9fab0a70f34cf6827 Mon Sep 17 00:00:00 2001 From: Nick Stokoe Date: Mon, 11 Jan 2021 11:54:44 +0000 Subject: [PATCH] snackpot.yml - add docker compose config Nominally working and tested on a remote VM --- snackpot.yml | 49 +++++ templates/bin/ncadmin | 110 +++++++++++ templates/docker-compose/docker-compose.yml | 103 +++++++++++ .../docker-compose/letsencrypt-companion.env | 1 + templates/docker-compose/nextcloud.env | 1 + templates/docker-compose/postgres.env | 1 + templates/docker-compose/postgres/Dockerfile | 2 + templates/docker-compose/postgres/init.sql | 6 + templates/docker-compose/proxy/Dockerfile | 3 + .../docker-compose/proxy/uploadsize.conf | 2 + templates/docker-compose/web.env | 3 + templates/docker-compose/web/Dockerfile | 3 + templates/docker-compose/web/nginx.conf | 173 ++++++++++++++++++ 13 files changed, 457 insertions(+) create mode 100755 templates/bin/ncadmin create mode 100644 templates/docker-compose/docker-compose.yml create mode 100644 templates/docker-compose/letsencrypt-companion.env create mode 100644 templates/docker-compose/nextcloud.env create mode 100644 templates/docker-compose/postgres.env create mode 100644 templates/docker-compose/postgres/Dockerfile create mode 100644 templates/docker-compose/postgres/init.sql create mode 100644 templates/docker-compose/proxy/Dockerfile create mode 100644 templates/docker-compose/proxy/uploadsize.conf create mode 100644 templates/docker-compose/web.env create mode 100644 templates/docker-compose/web/Dockerfile create mode 100644 templates/docker-compose/web/nginx.conf diff --git a/snackpot.yml b/snackpot.yml index ba485fa..25520f4 100644 --- a/snackpot.yml +++ b/snackpot.yml @@ -1,6 +1,18 @@ --- - name: snackpot | server hosts: all + vars: + nextcloud_db_password: "{{lookup('passwordstore', 'servers/snackpot/nextcloud_db.password')}}" + postgres_password: "{{lookup('passwordstore', 'servers/snackpot/postgres_db.password')}}" + postgres_db_user: postgres + nextcloud_hostname: nc.noodlefactory.co.uk + nextcloud_base_dir: /var/www/html + nextcloud_data_dir: /var/www/data + nextcloud_db_user: nextcloud + nextcloud_db: nextcloud + letsencrypt_email: webmaster@noodlefactory.co.uk + docker_compose_base_dir: /opt/docker-compose + tasks: - include_role: name: docker_compose @@ -8,3 +20,40 @@ tags: docker_compose vars: docker_compose_version: 1.27.4 + + - name: ensure directory exists + file: + path: "{{ docker_compose_base_dir }}/{{ item.path }}" + state: directory + with_filetree: templates/docker-compose + when: item.state == "directory" + + - name: configure docker compose files + template: + dest: "{{ docker_compose_base_dir }}/{{ item.path }}" + src: "docker-compose/{{ item.path }}" + owner: root + group: root + mode: 0440 + backup: yes + notify: restart docker compose services + with_filetree: templates/docker-compose + when: item.state == "file" and not item.path.endswith("~") + + - name: ensure directory exists + file: + path: "{{ docker_compose_base_dir }}/bin" + state: directory + + - name: install binaries + template: + dest: "{{ docker_compose_base_dir }}/bin/{{ item.path }}" + src: "bin/{{ item.path }}" + owner: root + group: root + mode: 0550 + with_filetree: templates/bin + when: item.state == "file" and not item.path.endswith("~") + tags: test +# config nextcloud +# hide pg password diff --git a/templates/bin/ncadmin b/templates/bin/ncadmin new file mode 100755 index 0000000..61f2215 --- /dev/null +++ b/templates/bin/ncadmin @@ -0,0 +1,110 @@ +#!/bin/sh + +dc_dir={{ docker_compose_base_dir }} +#db_archive=nextclouddb.psql.gz +# this should be relative to the $nextcloud_dir +#file_archive=nextcloud.tgz +#config= +nextcloud_base_dir={{ nextcloud_base_dir }} +nextcloud_data_dir={{ nextcloud_data_dir }} +postgres_db_user={{ postgres_db_user }} +nextcloud_db_user={{ nextcloud_db_user }} +nextcloud_db={{ nextcloud_db }} + +DOCKER_EXE() { + ( cd $dc_dir; docker-compose exec "$@" ) +} + +ON_POSTGRES() { + DOCKER_EXE -T -u postgres postgres "$@" +} + +ON_POSTGRESi() { + DOCKER_EXE -u postgres postgres "$@" +} + +ON_NEXTCLOUD() { + DOCKER_EXE -T -u www-data nextcloud "$@" +} + +ON_NEXTCLOUDi() { + DOCKER_EXE -u www-data nextcloud "$@" +} + +PSQL() { + ON_POSTGRES /usr/local/bin/psql "$@" +} + +PGDUMP() { + ON_POSTGRES /usr/local/bin/pg_dump "$@" +} + +PSQLi() { + ON_POSTGRESi /usr/local/bin/psql "$@" +} + +PHP() { + ON_NEXTCLOUD /usr/local/bin/php "$@" +} + +TEE() { + ON_NEXTCLOUD /usr/bin/tee "$1" +} + +CAT() { + ON_NEXTCLOUD /bin/cat "$1" +} + +DUMP() { + ON_NEXTCLOUD /bin/sh -c "for d in $*; do /usr/bin/tar -C \$d -c . ; done" +} + +UNDUMP() { + ON_NEXTCLOUD /bin/sh -c "for d in $*; do /usr/bin/tar -C \$d -x ; done" +} + +_gen_config() { + local config=$nextcloud_base_dir/config/config.php + script=$( cat </dev/null && tar t >/dev/null && cat +} + +# FIXME override selected config settings +restore() { + ( UNDUMP $nextcloud_base_dir $nextcloud_data_dir + #FIXME [ -n "$config" ] && gen_config <<<'$config' | WRITE $nextcloud_base_dir/config/config.php + PSQL -U $postgres_db_user < $dc_dir/postgres/init.sql + cat | PSQL -U $postgres_db_user -d $nextcloud_db ) +} + +backup() { + ( DUMP $nextcloud_base_dir $nextcloud_data_dir + PGDUMP -U $postgres_db_user $nextcloud_db ) +} + +prune() { + docker system prune -a --volumes +} + +OCC() { + ON_NEXTCLOUD ./occ "$@" +} + +NSH() { + ON_NEXTCLOUDi sh "$@" +} + +set -vx +set -e +"$@" + diff --git a/templates/docker-compose/docker-compose.yml b/templates/docker-compose/docker-compose.yml new file mode 100644 index 0000000..b581dd4 --- /dev/null +++ b/templates/docker-compose/docker-compose.yml @@ -0,0 +1,103 @@ +--- +# Adapted from: +# https://github.com/nextcloud/docker/blob/master/.examples/docker-compose/with-nginx-proxy/postgres/fpm/docker-compose.yml + +version: '3' + +volumes: + postgres: + nextcloud_src: + nextcloud_data: + certs: + vhost.d: + html: + redis: + +networks: + proxy-tier: + +services: + + postgres: + build: ./postgres + restart: always + healthcheck: + test: ["CMD-SHELL", "pg_isready -U postgres"] + volumes: + - postgres:/var/lib/postgresql/data + env_file: + - postgres.env + # Expose postgresql's port to allow dumping to back-up +# ports: +# - "127.0.0.1:5432:5432" +# ext_file? + + redis: + restart: always + image: redis:6.0.9-alpine + healthcheck: + test: ["CMD", "redis-cli", "ping"] + volumes: + - redis:/data + + nextcloud: + image: nextcloud:17.0.2-fpm-alpine + restart: always + volumes: + - nextcloud_src:{{ nextcloud_base_dir }} + - nextcloud_data:{{ nextcloud_data_dir }} + links: + - postgres + - redis + env_file: + - nextcloud.env + environment: + - POSTGRES_HOST=postgres + - REDIS_HOST=redis + - POSTGRES_USER=nextcloud +# healthcheck: +# test: ["CMD-SHELL", "wget -q --spider --proxy=off localhost:3000/health || exit 1"] + + web: + build: ./web + restart: always + volumes: + - nextcloud_src:/var/www/html:ro + env_file: + - web.env + depends_on: + - nextcloud + networks: + - proxy-tier + - default + + proxy: + build: ./proxy + restart: always + ports: + - "80:80" + - "443:443" + labels: + com.github.jrcs.letsencrypt_nginx_proxy_companion.nginx_proxy: "true" + volumes: + - certs:/etc/nginx/certs:ro + - vhost.d:/etc/nginx/vhost.d + - html:/usr/share/nginx/html + - /var/run/docker.sock:/tmp/docker.sock:ro + networks: + - proxy-tier + + letsencrypt-companion: + image: jrcs/letsencrypt-nginx-proxy-companion:v1.13.1 + restart: always + volumes: + - certs:/etc/nginx/certs + - vhost.d:/etc/nginx/vhost.d + - html:/usr/share/nginx/html + - /var/run/docker.sock:/var/run/docker.sock:ro + networks: + - proxy-tier + depends_on: + - proxy + env_file: + - letsencrypt-companion.env diff --git a/templates/docker-compose/letsencrypt-companion.env b/templates/docker-compose/letsencrypt-companion.env new file mode 100644 index 0000000..f78913c --- /dev/null +++ b/templates/docker-compose/letsencrypt-companion.env @@ -0,0 +1 @@ +DEFAULT_EMAIL={{ letsencrypt_email }} diff --git a/templates/docker-compose/nextcloud.env b/templates/docker-compose/nextcloud.env new file mode 100644 index 0000000..99bee52 --- /dev/null +++ b/templates/docker-compose/nextcloud.env @@ -0,0 +1 @@ +POSTGRES_PASSWORD={{ nextcloud_db_password }} diff --git a/templates/docker-compose/postgres.env b/templates/docker-compose/postgres.env new file mode 100644 index 0000000..ada7fde --- /dev/null +++ b/templates/docker-compose/postgres.env @@ -0,0 +1 @@ +POSTGRES_PASSWORD={{ postgres_password }} diff --git a/templates/docker-compose/postgres/Dockerfile b/templates/docker-compose/postgres/Dockerfile new file mode 100644 index 0000000..b1dbbb1 --- /dev/null +++ b/templates/docker-compose/postgres/Dockerfile @@ -0,0 +1,2 @@ +FROM postgres:11.9-alpine +COPY --chown={{ postgres_db_user }}:{{ postgres_db_user }} init.sql /docker-entrypoint-initdb.d/ diff --git a/templates/docker-compose/postgres/init.sql b/templates/docker-compose/postgres/init.sql new file mode 100644 index 0000000..f7e1e9f --- /dev/null +++ b/templates/docker-compose/postgres/init.sql @@ -0,0 +1,6 @@ +CREATE USER {{ nextcloud_db_user }}; +ALTER USER {{ nextcloud_db_user }} WITH ENCRYPTED PASSWORD 'md5{{ (nextcloud_db_password + nextcloud_db_user) | hash("md5") }}'; +DROP DATABASE IF EXISTS {{ nextcloud_db }}; +CREATE DATABASE {{ nextcloud_db }} TEMPLATE template0 ENCODING 'UNICODE'; +ALTER DATABASE {{ nextcloud_db }} OWNER TO {{ nextcloud_db_user }}; +GRANT ALL PRIVILEGES ON DATABASE {{ nextcloud_db }} TO {{ nextcloud_db_user }}; diff --git a/templates/docker-compose/proxy/Dockerfile b/templates/docker-compose/proxy/Dockerfile new file mode 100644 index 0000000..8811b41 --- /dev/null +++ b/templates/docker-compose/proxy/Dockerfile @@ -0,0 +1,3 @@ +FROM jwilder/nginx-proxy:alpine-0.7.0 + +COPY uploadsize.conf /etc/nginx/conf.d/uploadsize.conf diff --git a/templates/docker-compose/proxy/uploadsize.conf b/templates/docker-compose/proxy/uploadsize.conf new file mode 100644 index 0000000..7e3906e --- /dev/null +++ b/templates/docker-compose/proxy/uploadsize.conf @@ -0,0 +1,2 @@ +client_max_body_size 10G; +proxy_request_buffering off; diff --git a/templates/docker-compose/web.env b/templates/docker-compose/web.env new file mode 100644 index 0000000..561c560 --- /dev/null +++ b/templates/docker-compose/web.env @@ -0,0 +1,3 @@ +VIRTUAL_HOST={{ nextcloud_hostname }} +LETSENCRYPT_HOST={{ nextcloud_hostname }} +LETSENCRYPT_EMAIL={{ letsencrypt_email }} diff --git a/templates/docker-compose/web/Dockerfile b/templates/docker-compose/web/Dockerfile new file mode 100644 index 0000000..01cf2a6 --- /dev/null +++ b/templates/docker-compose/web/Dockerfile @@ -0,0 +1,3 @@ +FROM nginx:1.19.6-alpine + +COPY nginx.conf /etc/nginx/nginx.conf diff --git a/templates/docker-compose/web/nginx.conf b/templates/docker-compose/web/nginx.conf new file mode 100644 index 0000000..6594ce9 --- /dev/null +++ b/templates/docker-compose/web/nginx.conf @@ -0,0 +1,173 @@ +worker_processes auto; + +error_log /var/log/nginx/error.log warn; +pid /var/run/nginx.pid; + + +events { + worker_connections 1024; +} + + +http { + include /etc/nginx/mime.types; + default_type application/octet-stream; + + log_format main '$remote_addr - $remote_user [$time_local] "$request" ' + '$status $body_bytes_sent "$http_referer" ' + '"$http_user_agent" "$http_x_forwarded_for"'; + + access_log /var/log/nginx/access.log main; + + sendfile on; + #tcp_nopush on; + + keepalive_timeout 65; + + set_real_ip_from 10.0.0.0/8; + set_real_ip_from 172.16.0.0/12; + set_real_ip_from 192.168.0.0/16; + real_ip_header X-Real-IP; + + #gzip on; + + upstream php-handler { + server nextcloud:9000; + } + + server { + listen 80; + + # Add headers to serve security related headers + # Before enabling Strict-Transport-Security headers please read into this + # topic first. + #add_header Strict-Transport-Security "max-age=15768000; includeSubDomains; preload;" always; + # + # 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 Referrer-Policy "no-referrer" always; + add_header X-Content-Type-Options "nosniff" always; + add_header X-Download-Options "noopen" always; + add_header X-Frame-Options "SAMEORIGIN" always; + add_header X-Permitted-Cross-Domain-Policies "none" always; + add_header X-Robots-Tag "none" always; + add_header X-XSS-Protection "1; mode=block" always; + + # Remove X-Powered-By, which is an information leak + fastcgi_hide_header X-Powered-By; + + # Path to the root of your installation + root /var/www/html; + + location = /robots.txt { + allow all; + log_not_found off; + access_log off; + } + + # The following 2 rules are only needed for the user_webfinger app. + # Uncomment it if you're planning to use this app. + #rewrite ^/.well-known/host-meta /public.php?service=host-meta last; + #rewrite ^/.well-known/host-meta.json /public.php?service=host-meta-json last; + + # The following rule is only needed for the Social app. + # Uncomment it if you're planning to use this app. + #rewrite ^/.well-known/webfinger /public.php?service=webfinger last; + + location = /.well-known/carddav { + return 301 $scheme://$host:$server_port/remote.php/dav; + } + + location = /.well-known/caldav { + return 301 $scheme://$host:$server_port/remote.php/dav; + } + + # set max upload size + client_max_body_size 10G; + 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; + + # Uncomment if your server is build with the ngx_pagespeed module + # This module is currently not supported. + #pagespeed off; + + location / { + rewrite ^ /index.php; + } + + 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\/.+|oc[ms]-provider\/.+)\.php(?:$|\/) { + fastcgi_split_path_info ^(.+?\.php)(\/.*|)$; + set $path_info $fastcgi_path_info; + try_files $fastcgi_script_name =404; + include fastcgi_params; + fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; + fastcgi_param PATH_INFO $path_info; + # fastcgi_param HTTPS on; + + # Avoid sending the security headers twice + fastcgi_param modHeadersAvailable true; + + # Enable pretty urls + fastcgi_param front_controller_active true; + fastcgi_pass php-handler; + fastcgi_intercept_errors on; + fastcgi_request_buffering off; + } + + location ~ ^\/(?:updater|oc[ms]-provider)(?:$|\/) { + try_files $uri/ =404; + index index.php; + } + + # Adding the cache control header for js, css and map files + # Make sure it is BELOW the PHP block + location ~ \.(?:css|js|woff2?|svg|gif|map)$ { + try_files $uri /index.php$request_uri; + 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;" always; + # + # 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 Referrer-Policy "no-referrer" always; + add_header X-Content-Type-Options "nosniff" always; + add_header X-Download-Options "noopen" always; + add_header X-Frame-Options "SAMEORIGIN" always; + add_header X-Permitted-Cross-Domain-Policies "none" always; + add_header X-Robots-Tag "none" always; + add_header X-XSS-Protection "1; mode=block" always; + + # Optional: Don't log access to assets + access_log off; + } + + location ~ \.(?:png|html|ttf|ico|jpg|jpeg|bcmap|mp4|webm)$ { + try_files $uri /index.php$request_uri; + # Optional: Don't log access to other assets + access_log off; + } + } +}