diff --git a/playbooks/02c-services-deploy.yml b/playbooks/02c-services-deploy.yml index 9e5b002..f3cd64d 100644 --- a/playbooks/02c-services-deploy.yml +++ b/playbooks/02c-services-deploy.yml @@ -6,6 +6,9 @@ versions: database: postgres: "15.0" + cloud: + nextcloud: "25-fpm" + tasks: - import_tasks: tasks/services/c-deploy/00-hosts.yml - include_tasks: tasks/services/c-deploy/01-service-deploy.yml diff --git a/playbooks/filesystem/common/var/lib/_hostname/home/pod-_rproxy/.config/pod-_rproxy/nginx-conf.d/cloud.wojciechkozlowski.eu.conf b/playbooks/filesystem/common/var/lib/_hostname/home/pod-_rproxy/.config/pod-_rproxy/nginx-conf.d/cloud.wojciechkozlowski.eu.conf new file mode 100644 index 0000000..7631881 --- /dev/null +++ b/playbooks/filesystem/common/var/lib/_hostname/home/pod-_rproxy/.config/pod-_rproxy/nginx-conf.d/cloud.wojciechkozlowski.eu.conf @@ -0,0 +1,52 @@ +server { + listen 80; + server_name cloud.wojciechkozlowski.eu; + + location ^~ /.well-known { + allow all; + root /var/www/html; + } + + location / { + return 301 https://$server_name$request_uri; + } +} + +server { + listen 443 ssl; + server_name cloud.wojciechkozlowski.eu; + + ssl_certificate /etc/letsencrypt/live/cloud.wojciechkozlowski.eu/fullchain.pem; + ssl_certificate_key /etc/letsencrypt/live/cloud.wojciechkozlowski.eu/privkey.pem; + ssl_trusted_certificate /etc/letsencrypt/live/cloud.wojciechkozlowski.eu/chain.pem; + + # Redirect rules copied from + # https://docs.nextcloud.com/server/latest/admin_manual/installation/nginx.html. + location ^~ /.well-known { + location = /.well-known/webfinger { + return 301 /index.php$uri; + } + location = /.well-known/nodeinfo { + return 301 /index.php$uri; + } + location = /.well-known/carddav { + return 301 /remote.php/dav/; + } + location = /.well-known/caldav { + return 301 /remote.php/dav/; + } + } + + location / { + proxy_set_header X-Real-IP $remote_addr; + proxy_set_header X-Forwarded-For $remote_addr; + proxy_set_header Host $host; + proxy_pass http://pod-cloud; + } + + error_page 500 502 503 504 /50x.html; + location = /50x.html { + root /usr/share/nginx/html; + } + +} diff --git a/playbooks/filesystem/yggdrasil/var/lib/yggdrasil/home/pod-cloud/.config/pod-cloud/nginx.conf b/playbooks/filesystem/yggdrasil/var/lib/yggdrasil/home/pod-cloud/.config/pod-cloud/nginx.conf new file mode 100644 index 0000000..1738c5f --- /dev/null +++ b/playbooks/filesystem/yggdrasil/var/lib/yggdrasil/home/pod-cloud/.config/pod-cloud/nginx.conf @@ -0,0 +1,189 @@ +# Base configuration taken from +# https://github.com/nextcloud/docker/blob/master/.examples/docker-compose/with-nginx-proxy/postgres/fpm/web/nginx.conf +# with updates from https://docs.nextcloud.com/server/latest/admin_manual/installation/nginx.html + +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; + + # Prevent nginx HTTP Server Detection + server_tokens off; + + keepalive_timeout 65; + + #gzip on; + + upstream php-handler { + server localhost:9000; + } + + server { + listen 80; + + # HSTS settings + # 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 Strict-Transport-Security "max-age=15768000; includeSubDomains; preload;" always; + + # set max upload size and increase upload timeout: + client_max_body_size 512M; + client_body_timeout 300s; + 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/wasm 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; + + # Pagespeed is not supported by Nextcloud, so if your server is built + # with the `ngx_pagespeed` module, uncomment this line to disable it. + #pagespeed off; + + # The settings allows you to optimize the HTTP2 bandwitdth. + # See https://blog.cloudflare.com/delivering-http-2-upload-speed-improvements/ + # for tunning hints + client_body_buffer_size 512k; + + # HTTP response headers borrowed from Nextcloud `.htaccess` + 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; + + # Specify how to handle directories -- specifying `/index.php$request_uri` + # here as the fallback means that Nginx always exhibits the desired behaviour + # when a client requests a path that corresponds to a directory that exists + # on the server. In particular, if that directory contains an index.php file, + # that file is correctly served; if it doesn't, then the request is passed to + # the front-end controller. This consistent behaviour means that we don't need + # to specify custom rules for certain paths (e.g. images and other assets, + # `/updater`, `/ocm-provider`, `/ocs-provider`), and thus + # `try_files $uri $uri/ /index.php$request_uri` + # always provides the desired behaviour. + index index.php index.html /index.php$request_uri; + + # Rule borrowed from `.htaccess` to handle Microsoft DAV clients + location = / { + if ( $http_user_agent ~ ^DavClnt ) { + return 302 /remote.php/webdav/$is_args$args; + } + } + + location = /robots.txt { + allow all; + log_not_found off; + access_log off; + } + + # Make a regex exception for `/.well-known` so that clients can still + # access it despite the existence of the regex rule + # `location ~ /(\.|autotest|...)` which would otherwise handle requests + # for `/.well-known`. + location ^~ /.well-known { + # The rules in this block are an adaptation of the rules + # in `.htaccess` that concern `/.well-known`. + + location = /.well-known/carddav { return 301 /remote.php/dav/; } + location = /.well-known/caldav { return 301 /remote.php/dav/; } + + location /.well-known/acme-challenge { try_files $uri $uri/ =404; } + location /.well-known/pki-validation { try_files $uri $uri/ =404; } + + # Let Nextcloud's API for `/.well-known` URIs handle all other + # requests by passing them to the front-end controller. + return 301 /index.php$request_uri; + } + + # Rules borrowed from `.htaccess` to hide certain paths from clients + location ~ ^/(?:build|tests|config|lib|3rdparty|templates|data)(?:$|/) { return 404; } + location ~ ^/(?:\.|autotest|occ|issue|indie|db_|console) { return 404; } + + # Ensure this block, which passes PHP files to the PHP process, is above the blocks + # which handle static assets (as seen below). If this block is not declared first, + # then Nginx will encounter an infinite rewriting loop when it prepends `/index.php` + # to the URI, resulting in a HTTP 500 error response. + location ~ \.php(?:$|/) { + # Required for legacy support + rewrite ^/(?!index|remote|public|cron|core\/ajax\/update|status|ocs\/v[12]|updater\/.+|oc[ms]-provider\/.+|.+\/richdocumentscode\/proxy) /index.php$request_uri; + + 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; + + fastcgi_param modHeadersAvailable true; # Avoid sending the security headers twice + fastcgi_param front_controller_active true; # Enable pretty urls + fastcgi_pass php-handler; + + fastcgi_intercept_errors on; + fastcgi_request_buffering off; + + fastcgi_max_temp_file_size 0; + } + + location ~ \.(?:css|js|svg|gif)$ { + try_files $uri /index.php$request_uri; + expires 6M; # Cache-Control policy borrowed from `.htaccess` + access_log off; # Optional: Don't log access to assets + + location ~ \.wasm$ { + default_type application/wasm; + } + } + + location ~ \.woff2?$ { + try_files $uri /index.php$request_uri; + expires 7d; # Cache-Control policy borrowed from `.htaccess` + access_log off; # Optional: Don't log access to assets + } + + # Rule borrowed from `.htaccess` + location /remote { + return 301 /remote.php$request_uri; + } + + location / { + try_files $uri $uri/ /index.php$request_uri; + } + } +} diff --git a/playbooks/filesystem/yggdrasil/var/lib/yggdrasil/home/pod-cloud/.config/systemd/user/container-cloud-cron.service.j2 b/playbooks/filesystem/yggdrasil/var/lib/yggdrasil/home/pod-cloud/.config/systemd/user/container-cloud-cron.service.j2 new file mode 100644 index 0000000..2195ceb --- /dev/null +++ b/playbooks/filesystem/yggdrasil/var/lib/yggdrasil/home/pod-cloud/.config/systemd/user/container-cloud-cron.service.j2 @@ -0,0 +1,25 @@ +[Unit] +Description=Podman container-cloud-cron.service +Documentation=man:podman-generate-systemd(1) +OnFailure=status-mail@%n.service + +[Service] +Environment=PODMAN_SYSTEMD_UNIT=%n +TimeoutStopSec=70 +ExecStartPre=/bin/rm -f %t/container-cloud-cron.pid %t/container-cloud-cron.ctr-id +ExecStart=/usr/bin/podman run \ + --conmon-pidfile %t/container-cloud-cron.pid \ + --cidfile %t/container-cloud-cron.ctr-id \ + --cgroups=no-conmon \ + --pod-id-file %t/pod-cloud.pod-id \ + --replace \ + --add-host=pod-database:{{ services['database'].address }} \ + -v /var/lib/yggdrasil/valkyrie-resolv.conf:/etc/resolv.conf:ro \ + -v /var/lib/yggdrasil/data/pod-cloud/nextcloud/_data:/var/www/html \ + -v /var/lib/yggdrasil/data/pod-cloud/data/_data:/var/www/html/data \ + --name=pod-cloud-cron \ + --user=www-data \ + docker.io/library/nextcloud:{{ versions.cloud.nextcloud }} \ + php -f /var/www/html/cron.php +ExecStopPost=/usr/bin/podman rm --ignore -f --cidfile %t/container-cloud-cron.ctr-id +Type=oneshot diff --git a/playbooks/filesystem/yggdrasil/var/lib/yggdrasil/home/pod-cloud/.config/systemd/user/container-cloud-cron.timer.j2 b/playbooks/filesystem/yggdrasil/var/lib/yggdrasil/home/pod-cloud/.config/systemd/user/container-cloud-cron.timer.j2 new file mode 100644 index 0000000..e8a6824 --- /dev/null +++ b/playbooks/filesystem/yggdrasil/var/lib/yggdrasil/home/pod-cloud/.config/systemd/user/container-cloud-cron.timer.j2 @@ -0,0 +1,11 @@ +[Unit] +Description=Run nextcloud cron job +BindsTo=pod-cloud.service +After=pod-cloud.service + +[Timer] +OnActiveSec=5min +OnUnitActiveSec=5min + +[Install] +WantedBy=pod-cloud.service diff --git a/playbooks/filesystem/yggdrasil/var/lib/yggdrasil/home/pod-cloud/.config/systemd/user/container-cloud-nextcloud.service.j2 b/playbooks/filesystem/yggdrasil/var/lib/yggdrasil/home/pod-cloud/.config/systemd/user/container-cloud-nextcloud.service.j2 new file mode 100644 index 0000000..c86bbd3 --- /dev/null +++ b/playbooks/filesystem/yggdrasil/var/lib/yggdrasil/home/pod-cloud/.config/systemd/user/container-cloud-nextcloud.service.j2 @@ -0,0 +1,35 @@ +[Unit] +Description=Podman container-cloud-nextcloud.service +Documentation=man:podman-generate-systemd(1) +Wants=network.target +After=network-online.target +BindsTo=pod-cloud.service +After=pod-cloud.service +OnFailure=status-mail@%n.service + +[Service] +Environment=PODMAN_SYSTEMD_UNIT=%n +Restart=on-failure +TimeoutStopSec=70 +ExecStartPre=/bin/rm -f %t/container-cloud-nextcloud.pid %t/container-cloud-nextcloud.ctr-id +ExecStart=/usr/bin/podman run \ + --conmon-pidfile %t/container-cloud-nextcloud.pid \ + --cidfile %t/container-cloud-nextcloud.ctr-id \ + --cgroups=no-conmon \ + --pod-id-file %t/pod-cloud.pod-id \ + --replace \ + --label "io.containers.autoupdate=image" \ + -dt \ + --add-host=pod-database:{{ services['database'].address }} \ + -v /var/lib/yggdrasil/valkyrie-resolv.conf:/etc/resolv.conf:ro \ + -v /var/lib/yggdrasil/data/pod-cloud/nextcloud/_data:/var/www/html \ + -v /var/lib/yggdrasil/data/pod-cloud/data/_data:/var/www/html/data \ + --name=pod-cloud-nextcloud \ + docker.io/library/nextcloud:{{ versions.cloud.nextcloud }} +ExecStop=/usr/bin/podman stop --ignore --cidfile %t/container-cloud-nextcloud.ctr-id -t 10 +ExecStopPost=/usr/bin/podman rm --ignore -f --cidfile %t/container-cloud-nextcloud.ctr-id +PIDFile=%t/container-cloud-nextcloud.pid +Type=forking + +[Install] +WantedBy=multi-user.target default.target diff --git a/playbooks/filesystem/yggdrasil/var/lib/yggdrasil/home/pod-cloud/.config/systemd/user/container-cloud-nginx.service.j2 b/playbooks/filesystem/yggdrasil/var/lib/yggdrasil/home/pod-cloud/.config/systemd/user/container-cloud-nginx.service.j2 new file mode 100644 index 0000000..e74170e --- /dev/null +++ b/playbooks/filesystem/yggdrasil/var/lib/yggdrasil/home/pod-cloud/.config/systemd/user/container-cloud-nginx.service.j2 @@ -0,0 +1,35 @@ +[Unit] +Description=Podman container-cloud-nginx.service +Documentation=man:podman-generate-systemd(1) +Wants=network.target +After=network-online.target +BindsTo=pod-cloud.service +After=pod-cloud.service +OnFailure=status-mail@%n.service + +[Service] +Environment=PODMAN_SYSTEMD_UNIT=%n +Restart=on-failure +TimeoutStopSec=70 +ExecStartPre=/bin/rm -f %t/container-cloud-nginx.pid %t/container-cloud-nginx.ctr-id +ExecStart=/usr/bin/podman run \ + --conmon-pidfile %t/container-cloud-nginx.pid \ + --cidfile %t/container-cloud-nginx.ctr-id \ + --cgroups=no-conmon \ + --pod-id-file %t/pod-cloud.pod-id \ + --replace \ + --label "io.containers.autoupdate=image" \ + -dt \ + -v /var/lib/yggdrasil/valkyrie-resolv.conf:/etc/resolv.conf:ro \ + -v ./.config/pod-cloud/nginx.conf:/etc/nginx/nginx.conf:ro \ + -v /var/lib/yggdrasil/data/pod-cloud/nextcloud/_data:/var/www/html \ + -v /var/lib/yggdrasil/data/pod-cloud/data/_data:/var/www/html/data \ + --name=pod-cloud-nginx \ + docker.io/library/nginx +ExecStop=/usr/bin/podman stop --ignore --cidfile %t/container-cloud-nginx.ctr-id -t 10 +ExecStopPost=/usr/bin/podman rm --ignore -f --cidfile %t/container-cloud-nginx.ctr-id +PIDFile=%t/container-cloud-nginx.pid +Type=forking + +[Install] +WantedBy=multi-user.target default.target diff --git a/playbooks/filesystem/yggdrasil/var/lib/yggdrasil/home/pod-cloud/.config/systemd/user/pod-cloud.service.j2 b/playbooks/filesystem/yggdrasil/var/lib/yggdrasil/home/pod-cloud/.config/systemd/user/pod-cloud.service.j2 new file mode 100644 index 0000000..6bc6c2e --- /dev/null +++ b/playbooks/filesystem/yggdrasil/var/lib/yggdrasil/home/pod-cloud/.config/systemd/user/pod-cloud.service.j2 @@ -0,0 +1,24 @@ +[Unit] +Description=Podman pod-cloud.service +Documentation=man:podman-generate-systemd(1) +Wants=network.target +After=network-online.target +Requires=container-cloud-nginx.service container-cloud-nextcloud.service +Before=container-cloud-nginx.service container-cloud-nextcloud.service +OnFailure=status-mail@%n.service + +[Service] +Environment=PODMAN_SYSTEMD_UNIT=%n +Restart=on-failure +TimeoutStopSec=70 +ExecStartPre=/bin/rm -f %t/pod-cloud.pid %t/pod-cloud.pod-id +ExecStartPre=/usr/bin/podman pod create --infra-conmon-pidfile %t/pod-cloud.pid --pod-id-file %t/pod-cloud.pod-id --name=cloud --network=none --replace +ExecStart=/usr/bin/podman pod start --pod-id-file %t/pod-cloud.pod-id +ExecStartPost=/usr/bin/sh -c 'podman inspect --format "{% raw %}{{ .State.Pid }}{% endraw %}" $(podman inspect --format "{% raw %}{{ .InfraContainerID }}{% endraw %}" cloud) > /var/lib/{{ ansible_hostname }}/containers/pod-cloud/pidfile' +ExecStop=/usr/bin/podman pod stop --ignore --pod-id-file %t/pod-cloud.pod-id -t 10 +ExecStopPost=/usr/bin/podman pod rm --ignore -f --pod-id-file %t/pod-cloud.pod-id +PIDFile=%t/pod-cloud.pid +Type=forking + +[Install] +WantedBy=multi-user.target default.target diff --git a/playbooks/tasks/services/b-user/service-user/01-user.d/data/cloud.yml b/playbooks/tasks/services/b-user/service-user/01-user.d/data/cloud.yml new file mode 100644 index 0000000..c9c6b15 --- /dev/null +++ b/playbooks/tasks/services/b-user/service-user/01-user.d/data/cloud.yml @@ -0,0 +1,39 @@ +- name: Create volume data directory for user {{ service_user_name }} + file: + path: "/var/lib/{{ ansible_hostname }}/data/{{ service_user_name }}" + state: directory + owner: "{{ service_user_name }}" + group: "{{ service_user_name }}" + mode: 0755 + +- name: Create data directory for user {{ service_user_name }} + file: + path: "/var/lib/{{ ansible_hostname }}/data/{{ service_user_name }}/nextcloud" + state: directory + owner: "{{ service_user_name }}" + group: "{{ service_user_name }}" + mode: 0755 + +- name: Create data mount directory for user {{ service_user_name }} + file: + path: "/var/lib/{{ ansible_hostname }}/data/{{ service_user_name }}/nextcloud/_data" + state: directory + owner: "{{ service_user_name }}" + group: "{{ service_user_name }}" + mode: 0755 + +- name: Create data directory for user {{ service_user_name }} + file: + path: "/var/lib/{{ ansible_hostname }}/data/{{ service_user_name }}/data" + state: directory + owner: "{{ service_user_name }}" + group: "{{ service_user_name }}" + mode: 0755 + +- name: Create data mount directory for user {{ service_user_name }} + file: + path: "/var/lib/{{ ansible_hostname }}/data/{{ service_user_name }}/data/_data" + state: directory + owner: "{{ service_user_name }}" + group: "{{ service_user_name }}" + mode: 0755 diff --git a/playbooks/tasks/services/b-user/service-user/01-zfs-datasets.d/cloud.yml b/playbooks/tasks/services/b-user/service-user/01-zfs-datasets.d/cloud.yml new file mode 100644 index 0000000..9d844d0 --- /dev/null +++ b/playbooks/tasks/services/b-user/service-user/01-zfs-datasets.d/cloud.yml @@ -0,0 +1,16 @@ +- name: Create data dataset for user {{ service_user_name }} + zfs: + name: rpool/var/lib/{{ ansible_hostname }}/data/{{ service_user_name }} + state: present + extra_zfs_properties: + canmount: "off" + +- name: Create app dataset for user {{ service_user_name }} + zfs: + name: rpool/var/lib/{{ ansible_hostname }}/data/{{ service_user_name }}/nextcloud + state: present + +- name: Create data dataset for user {{ service_user_name }} + zfs: + name: rpool/var/lib/{{ ansible_hostname }}/data/{{ service_user_name }}/data + state: present diff --git a/playbooks/tasks/services/c-deploy/service-deploy/service.d/cloud.yml b/playbooks/tasks/services/c-deploy/service-deploy/service.d/cloud.yml new file mode 100644 index 0000000..222fdb8 --- /dev/null +++ b/playbooks/tasks/services/c-deploy/service-deploy/service.d/cloud.yml @@ -0,0 +1,16 @@ +- block: + + - name: Enable container-cloud-cron timer + systemd: + name: container-cloud-cron.timer + enabled: yes + scope: user + register: container_cloud_cron_timer + + - name: Record changes + set_fact: + service_changed: true + when: + container_cloud_cron_timer is changed + + become_user: "{{ service_user_name }}"