snackpot.yml - add docker compose config

Nominally working and tested on a remote VM
This commit is contained in:
Nick Stokoe
2021-01-11 11:54:44 +00:00
parent 8637cb2af4
commit d2bcfec810
13 changed files with 457 additions and 0 deletions

View File

@@ -1,6 +1,18 @@
--- ---
- name: snackpot | server - name: snackpot | server
hosts: all 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: tasks:
- include_role: - include_role:
name: docker_compose name: docker_compose
@@ -8,3 +20,40 @@
tags: docker_compose tags: docker_compose
vars: vars:
docker_compose_version: 1.27.4 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

110
templates/bin/ncadmin Executable file
View File

@@ -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 <<EOF )
require("$config");
\$CONFIG["password"] = "password";
// FIXME more here
file_put_contents("$config.2", "<?php\\n\\\$CONFIG = ". var_export(\$CONFIG, true) .";\\n");
EOF
PHP -r "$script"
}
unpack_db() {
tar t >/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
"$@"

View File

@@ -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

View File

@@ -0,0 +1 @@
DEFAULT_EMAIL={{ letsencrypt_email }}

View File

@@ -0,0 +1 @@
POSTGRES_PASSWORD={{ nextcloud_db_password }}

View File

@@ -0,0 +1 @@
POSTGRES_PASSWORD={{ postgres_password }}

View File

@@ -0,0 +1,2 @@
FROM postgres:11.9-alpine
COPY --chown={{ postgres_db_user }}:{{ postgres_db_user }} init.sql /docker-entrypoint-initdb.d/

View File

@@ -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 }};

View File

@@ -0,0 +1,3 @@
FROM jwilder/nginx-proxy:alpine-0.7.0
COPY uploadsize.conf /etc/nginx/conf.d/uploadsize.conf

View File

@@ -0,0 +1,2 @@
client_max_body_size 10G;
proxy_request_buffering off;

View File

@@ -0,0 +1,3 @@
VIRTUAL_HOST={{ nextcloud_hostname }}
LETSENCRYPT_HOST={{ nextcloud_hostname }}
LETSENCRYPT_EMAIL={{ letsencrypt_email }}

View File

@@ -0,0 +1,3 @@
FROM nginx:1.19.6-alpine
COPY nginx.conf /etc/nginx/nginx.conf

View File

@@ -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;
}
}
}