From 0c0042727cff29758f6a25fcc2a8edc9b02d109e Mon Sep 17 00:00:00 2001 From: "juvenal.diaz" Date: Thu, 22 Oct 2026 09:21:01 -0600 Subject: [PATCH] feat: deploy Nextcloud Collabora office stack --- bootstrap/edge/main.tf | 60 +++++----- bootstrap/edge/templates/default.conf.tftpl | 33 ++++++ bootstrap/edge/variables.tf | 20 ++++ cmd/jeannie-server/main.go | 1 + homelab.yml | 6 + infra/office/README.md | 19 ++++ infra/office/docker-compose.yml | 67 +++++++++++ jeannie | 5 + lib/jeannie/env.sh | 8 ++ lib/jeannie/pipeline.sh | 120 +++++++++++++++++++- lib/jeannie/security.sh | 3 + 11 files changed, 315 insertions(+), 27 deletions(-) create mode 100644 infra/office/README.md create mode 100644 infra/office/docker-compose.yml diff --git a/bootstrap/edge/main.tf b/bootstrap/edge/main.tf index 3fe5c53..e43cfaa 100644 --- a/bootstrap/edge/main.tf +++ b/bootstrap/edge/main.tf @@ -23,9 +23,10 @@ locals { ) jellyfin_backend_host = var.jellyfin_backend_host != "" ? var.jellyfin_backend_host : var.gitea_backend_host jeannie_api_server_name = "api.${var.server_name}" - server_names = distinct(concat([var.server_name, local.heimdall_server_name, local.homepage_server_name, local.oidc_server_name, local.jellyfin_server_name, local.jeannie_api_server_name], var.additional_server_names)) + office_server_name = var.office_server_name + server_names = distinct(concat([var.server_name, local.heimdall_server_name, local.homepage_server_name, local.oidc_server_name, local.jellyfin_server_name, local.jeannie_api_server_name, local.office_server_name], var.additional_server_names)) matrix_server_names = var.matrix_enabled ? [var.matrix_server_name, var.element_server_name] : [] - main_server_names = [for name in local.server_names : name if !contains([local.heimdall_server_name, local.homepage_server_name, local.oidc_server_name, local.jellyfin_server_name, local.jeannie_api_server_name], name) && !contains(local.matrix_server_names, name)] + main_server_names = [for name in local.server_names : name if !contains([local.heimdall_server_name, local.homepage_server_name, local.oidc_server_name, local.jellyfin_server_name, local.jeannie_api_server_name, local.office_server_name], name) && !contains(local.matrix_server_names, name)] default_conf = templatefile("${path.module}/templates/default.conf.tftpl", { server_name = var.server_name server_names = join(" ", local.server_names) @@ -50,6 +51,10 @@ locals { jeannie_api_server_name = local.jeannie_api_server_name jeannie_api_backend_host = var.jeannie_api_backend_host jeannie_api_backend_port = tostring(var.jeannie_api_backend_port) + office_server_name = local.office_server_name + office_backend_host = var.office_backend_host + office_backend_port = tostring(var.office_backend_port) + office_tailscale_cidr = var.office_tailscale_cidr }) default_vcl = templatefile("${path.module}/templates/default.vcl.tftpl", { backend_host = var.backend_host @@ -86,30 +91,33 @@ locals { resource "null_resource" "edge_services" { triggers = { - host = var.edge_host - user = var.edge_user - install_dir = var.edge_install_dir - install_docker = tostring(var.edge_install_docker) - server_name = var.server_name - server_names = join(" ", local.server_names) - certbot_domain_args = join(" ", [for name in local.server_names : "-d ${name}"]) - backend_host = var.backend_host - backend_port = tostring(var.backend_port) - gitea_backend_host = var.gitea_backend_host - gitea_backend_port = tostring(var.gitea_backend_port) - heimdall_host = local.heimdall_backend_host - heimdall_port = tostring(var.heimdall_backend_port) - oidc_host = local.oidc_backend_host - oidc_port = tostring(var.oidc_backend_port) - jellyfin_host = local.jellyfin_backend_host - jellyfin_port = tostring(var.jellyfin_backend_port) - jeannie_api_host = var.jeannie_api_backend_host - jeannie_api_port = tostring(var.jeannie_api_backend_port) - enable_letsencrypt = tostring(var.enable_letsencrypt) - letsencrypt_email = var.letsencrypt_email - letsencrypt_staging = tostring(var.letsencrypt_staging) - certbot_version = "2" - config_hash = local.config_hash + host = var.edge_host + user = var.edge_user + install_dir = var.edge_install_dir + install_docker = tostring(var.edge_install_docker) + server_name = var.server_name + server_names = join(" ", local.server_names) + certbot_domain_args = join(" ", [for name in local.server_names : "-d ${name}"]) + backend_host = var.backend_host + backend_port = tostring(var.backend_port) + gitea_backend_host = var.gitea_backend_host + gitea_backend_port = tostring(var.gitea_backend_port) + heimdall_host = local.heimdall_backend_host + heimdall_port = tostring(var.heimdall_backend_port) + oidc_host = local.oidc_backend_host + oidc_port = tostring(var.oidc_backend_port) + jellyfin_host = local.jellyfin_backend_host + jellyfin_port = tostring(var.jellyfin_backend_port) + jeannie_api_host = var.jeannie_api_backend_host + jeannie_api_port = tostring(var.jeannie_api_backend_port) + office_host = var.office_backend_host + office_port = tostring(var.office_backend_port) + office_tailscale_cidr = var.office_tailscale_cidr + enable_letsencrypt = tostring(var.enable_letsencrypt) + letsencrypt_email = var.letsencrypt_email + letsencrypt_staging = tostring(var.letsencrypt_staging) + certbot_version = "2" + config_hash = local.config_hash } connection { diff --git a/bootstrap/edge/templates/default.conf.tftpl b/bootstrap/edge/templates/default.conf.tftpl index ce70e1c..286da4c 100644 --- a/bootstrap/edge/templates/default.conf.tftpl +++ b/bootstrap/edge/templates/default.conf.tftpl @@ -208,6 +208,39 @@ server { } } +server { + listen 443 ssl; + http2 on; + server_name ${office_server_name}; + + ssl_certificate /etc/nginx/certs/current.crt; + ssl_certificate_key /etc/nginx/certs/current.key; + ssl_protocols TLSv1.2 TLSv1.3; + ssl_ciphers HIGH:!aNULL:!MD5; + + # The office data plane is reachable only from the Tailscale CGNAT range. + allow ${office_tailscale_cidr}; + deny all; + client_max_body_size 2g; + + location / { + proxy_pass http://${office_backend_host}:${office_backend_port}; + proxy_http_version 1.1; + proxy_buffering off; + proxy_request_buffering off; + proxy_read_timeout 3600s; + proxy_send_timeout 3600s; + proxy_set_header Upgrade $http_upgrade; + proxy_set_header Connection $connection_upgrade; + proxy_set_header Host $host; + proxy_set_header X-Real-IP $remote_addr; + proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; + proxy_set_header X-Forwarded-Host $host; + proxy_set_header X-Forwarded-Proto https; + proxy_set_header X-Forwarded-Port 443; + } +} + server { listen 443 ssl; http2 on; diff --git a/bootstrap/edge/variables.tf b/bootstrap/edge/variables.tf index 5f034cd..87d245c 100644 --- a/bootstrap/edge/variables.tf +++ b/bootstrap/edge/variables.tf @@ -148,6 +148,26 @@ variable "jeannie_api_backend_port" { default = 8787 } +variable "office_server_name" { + type = string + default = "office.lab2025.duckdns.org" +} + +variable "office_backend_host" { + type = string + default = "192.168.100.73" +} + +variable "office_backend_port" { + type = number + default = 8090 +} + +variable "office_tailscale_cidr" { + type = string + default = "100.64.0.0/10" +} + # Matrix (isolated vhost + its own cert; does NOT touch shared current.crt) variable "matrix_enabled" { type = bool diff --git a/cmd/jeannie-server/main.go b/cmd/jeannie-server/main.go index 5429658..b0b4de2 100644 --- a/cmd/jeannie-server/main.go +++ b/cmd/jeannie-server/main.go @@ -48,6 +48,7 @@ var allowedCommands = map[string]bool{ "website-ollama-listen": true, "ollama-setup": true, "deploy-gitea": true, "deploy-api": true, "deploy-heimdall": true, "deploy-dex": true, "deploy-matrix": true, "deploy-ai-stack": true, + "deploy-office": true, "deploy-ai-tools": true, "deploy-omniroute": true, "deploy-hermes": true, "rpi-services": true, "bootstrap-gitea-repo": true, "backup-gitea": true, "backup": true, "drill-restore": true, "drill-gitea-restore": true, diff --git a/homelab.yml b/homelab.yml index 4cc5e21..ef5e67d 100644 --- a/homelab.yml +++ b/homelab.yml @@ -144,6 +144,12 @@ services: http_port: 5556 telemetry_port: 5558 public_url: https://auth.lab2025.duckdns.org/ + office: + host: debian + install_dir: /data/homelab-office + http_port: 8090 + collabora_port: 9980 + public_url: https://office.lab2025.duckdns.org/ jellyfin: host: debian http_port: 8096 diff --git a/infra/office/README.md b/infra/office/README.md new file mode 100644 index 0000000..2c654ba --- /dev/null +++ b/infra/office/README.md @@ -0,0 +1,19 @@ +# Online office stack + +This stack provides browser-based LibreOffice-compatible editing through +Nextcloud and Collabora Online. It stores documents in the dedicated +`/data/homelab-office` tree on Debian and uses PostgreSQL and Redis instead of +SQLite or in-container state. + +The pipeline deploys the stack independently with: + +```sh +./jeannie deploy-office +``` + +The edge route is `https://office.lab2025.duckdns.org` and is restricted to the +Tailscale CGNAT range. Dex SSO is configured by the pipeline after the first +Nextcloud boot when `DEX_OFFICE_CLIENT_SECRET` exists in the encrypted Dex +secret. Existing older Dex secret files must be regenerated or supplied with +`LAB_OFFICE_OIDC_CLIENT_SECRET`; credentials are never committed to the +repository. diff --git a/infra/office/docker-compose.yml b/infra/office/docker-compose.yml new file mode 100644 index 0000000..949a3d3 --- /dev/null +++ b/infra/office/docker-compose.yml @@ -0,0 +1,67 @@ +services: + postgres: + image: ${OFFICE_POSTGRES_IMAGE:-postgres:16-alpine} + container_name: ${OFFICE_POSTGRES_CONTAINER_NAME:-homelab-office-postgres} + restart: unless-stopped + environment: + POSTGRES_DB: ${OFFICE_POSTGRES_DB:-nextcloud} + POSTGRES_USER: ${OFFICE_POSTGRES_USER:-nextcloud} + POSTGRES_PASSWORD: ${OFFICE_POSTGRES_PASSWORD:?OFFICE_POSTGRES_PASSWORD is required} + volumes: + - ${OFFICE_POSTGRES_DATA_DIR:-/data/homelab-office/postgres}:/var/lib/postgresql/data + networks: [office] + + redis: + image: ${OFFICE_REDIS_IMAGE:-redis:7-alpine} + container_name: ${OFFICE_REDIS_CONTAINER_NAME:-homelab-office-redis} + restart: unless-stopped + command: ["redis-server", "--requirepass", "${OFFICE_REDIS_PASSWORD:?OFFICE_REDIS_PASSWORD is required}"] + volumes: + - ${OFFICE_REDIS_DATA_DIR:-/data/homelab-office/redis}:/data + networks: [office] + + nextcloud: + image: ${OFFICE_NEXTCLOUD_IMAGE:-nextcloud:31-apache} + container_name: ${OFFICE_NEXTCLOUD_CONTAINER_NAME:-homelab-office-nextcloud} + restart: unless-stopped + depends_on: + - postgres + - redis + environment: + POSTGRES_HOST: postgres + POSTGRES_DB: ${OFFICE_POSTGRES_DB:-nextcloud} + POSTGRES_USER: ${OFFICE_POSTGRES_USER:-nextcloud} + POSTGRES_PASSWORD: ${OFFICE_POSTGRES_PASSWORD:?OFFICE_POSTGRES_PASSWORD is required} + REDIS_HOST: redis + REDIS_HOST_PASSWORD: ${OFFICE_REDIS_PASSWORD:?OFFICE_REDIS_PASSWORD is required} + NEXTCLOUD_TRUSTED_DOMAINS: ${OFFICE_PUBLIC_HOST:-office.lab2025.duckdns.org} localhost + NEXTCLOUD_ADMIN_USER: ${OFFICE_ADMIN_USER:-jv} + NEXTCLOUD_ADMIN_PASSWORD: ${OFFICE_ADMIN_PASSWORD:?OFFICE_ADMIN_PASSWORD is required} + OVERWRITEHOST: ${OFFICE_PUBLIC_HOST:-office.lab2025.duckdns.org} + OVERWRITEPROTOCOL: https + OVERWRITECLIURL: https://${OFFICE_PUBLIC_HOST:-office.lab2025.duckdns.org} + ports: + - "${OFFICE_BIND_IP:-127.0.0.1}:${OFFICE_HTTP_PORT:-8090}:80" + volumes: + - ${OFFICE_NEXTCLOUD_DATA_DIR:-/data/homelab-office/nextcloud}:/var/www/html + networks: [office] + + collabora: + image: ${OFFICE_COLLABORA_IMAGE:-collabora/code:latest} + container_name: ${OFFICE_COLLABORA_CONTAINER_NAME:-homelab-office-collabora} + restart: unless-stopped + environment: + aliasgroup1: https://${OFFICE_PUBLIC_HOST:-office.lab2025.duckdns.org}:443 + dictionaries: ${OFFICE_COLLABORA_DICTIONARIES:-en_US es_MX} + extra_params: >- + --o:ssl.enable=false + --o:ssl.termination=true + --o:welcome.enable=false + --o:logging.level=warning + ports: + - "${OFFICE_BIND_IP:-127.0.0.1}:${OFFICE_COLLABORA_PORT:-9980}:9980" + networks: [office] + +networks: + office: + name: ${OFFICE_NETWORK:-homelab-office} diff --git a/jeannie b/jeannie index d26a426..f468996 100755 --- a/jeannie +++ b/jeannie @@ -56,6 +56,7 @@ Build And Bootstrap deploy-api Deploy the Temporal-backed Jeannie API on Debian. deploy-heimdall Deploy the Debian-hosted Heimdall dashboard. deploy-dex Deploy the Debian-hosted Dex SSO stack. + deploy-office Deploy Nextcloud + Collabora online office. deploy-matrix Deploy the Matrix homeserver (Synapse + Element + Postgres). deploy-ai-stack Deploy the Debian-hosted AI gateway stack. deploy-ai-tools Install/wire local AI CLIs to the AI gateway. @@ -409,6 +410,10 @@ case "${1:-}" in record_change_journal "deploy-dex" "$@" deploy_dex ;; + deploy-office) + record_change_journal "deploy-office" "$@" + deploy_office + ;; deploy-matrix) record_change_journal "deploy-matrix" "$@" deploy_matrix diff --git a/lib/jeannie/env.sh b/lib/jeannie/env.sh index 8a36082..7bdb8bc 100644 --- a/lib/jeannie/env.sh +++ b/lib/jeannie/env.sh @@ -54,6 +54,10 @@ mapping = { "services.dex.http_port": "LAB_DEX_HTTP_PORT", "services.dex.telemetry_port": "LAB_DEX_TELEMETRY_PORT", "services.dex.public_url": "LAB_DEX_PUBLIC_URL", + "services.office.install_dir": "LAB_OFFICE_INSTALL_DIR", + "services.office.http_port": "LAB_OFFICE_HTTP_PORT", + "services.office.collabora_port": "LAB_OFFICE_COLLABORA_PORT", + "services.office.public_url": "LAB_OFFICE_PUBLIC_URL", "services.jellyfin.http_port": "LAB_JELLYFIN_HTTP_PORT", "services.jellyfin.public_url": "LAB_JELLYFIN_PUBLIC_URL", "services.jeannie_api.http_port": "LAB_JEANNIE_API_BACKEND_PORT", @@ -283,6 +287,10 @@ export_homelab_inventory_tf_vars() { export_if_unset TF_VAR_gitea_auth_source_name "${LAB_GITEA_DEX_SOURCE_NAME:-dex}" export_if_unset TF_VAR_jellyfin_backend_host "${LAB_JELLYFIN_BACKEND_HOST:-${LAB_DEBIAN_TAILSCALE_IP:-}}" export_if_unset TF_VAR_jellyfin_backend_port "${LAB_JELLYFIN_HTTP_PORT:-}" + export_if_unset TF_VAR_office_server_name "${LAB_OFFICE_PUBLIC_HOST:-office.${LAB_DOMAIN:-lab2025.duckdns.org}}" + export_if_unset TF_VAR_office_backend_host "${LAB_OFFICE_BACKEND_HOST:-${LAB_DEBIAN_TAILSCALE_IP:-${LAB_DEBIAN_LAN_IP:-}}}" + export_if_unset TF_VAR_office_backend_port "${LAB_OFFICE_HTTP_PORT:-8090}" + export_if_unset TF_VAR_office_tailscale_cidr "${LAB_TAILSCALE_CGNAT_CIDR:-100.64.0.0/10}" export_if_unset TF_VAR_jeannie_api_backend_host "${LAB_JEANNIE_API_BACKEND_HOST:-${LAB_DEBIAN_TAILSCALE_IP:-${LAB_DEBIAN_LAN_IP:-}}}" export_if_unset TF_VAR_jeannie_api_backend_port "${LAB_JEANNIE_API_BACKEND_PORT:-8787}" export_if_unset TF_VAR_gitops_repo_url "${LAB_GITOPS_REPO_URL:-}" diff --git a/lib/jeannie/pipeline.sh b/lib/jeannie/pipeline.sh index 6f7baf7..457d8be 100644 --- a/lib/jeannie/pipeline.sh +++ b/lib/jeannie/pipeline.sh @@ -796,6 +796,20 @@ render_dex_config() { local user_name="$8" local user_id="$9" local user_password_hash="${10}" + local office_public_url="${11:-}" + local office_client_secret="${12:-}" + local office_client_block="" + + if [[ -n "${office_public_url}" && -n "${office_client_secret}" ]]; then + office_client_block="$(cat </dev/null sudo chown "${dex_uid}:${dex_gid}" "${install_dir}/config.yaml" sudo chown -R "${dex_uid}:${dex_gid}" "${data_dir}" @@ -946,6 +966,104 @@ ENV_EOF echo "Initial Dex user: ${DEX_USER_EMAIL}" } +deploy_office() { + local mode="${LAB_OFFICE_DEPLOY:-true}" + local install_dir="${LAB_OFFICE_INSTALL_DIR:-/data/homelab-office}" + local source_dir="${REPO_ROOT}/infra/office" + local public_host="${LAB_OFFICE_PUBLIC_HOST:-office.${LAB_DOMAIN:-lab2025.duckdns.org}}" + local bind_ip="${LAB_OFFICE_BIND_IP:-0.0.0.0}" + local http_port="${LAB_OFFICE_HTTP_PORT:-8090}" + local collabora_port="${LAB_OFFICE_COLLABORA_PORT:-9980}" + local env_file="${install_dir}/.env" + local nextcloud_data_dir="${install_dir}/nextcloud" + local postgres_data_dir="${install_dir}/postgres" + local redis_data_dir="${install_dir}/redis" + local random_value + local dex_env + local office_oidc_secret="${LAB_OFFICE_OIDC_CLIENT_SECRET:-}" + + require_debian_server "deploy-office" + if disabled_value "${mode}"; then + return 0 + fi + if [[ ! -s "${source_dir}/docker-compose.yml" ]]; then + echo "Missing office stack source files in ${source_dir}." >&2 + exit 1 + fi + + ensure_local_docker_compose "OFFICE" "${LAB_OFFICE_INSTALL_DOCKER:-false}" + repair_local_docker_iptables + if [[ -z "${office_oidc_secret}" ]]; then + dex_env="$(dex_secret_values)" || exit 1 + eval "${dex_env}" + office_oidc_secret="${DEX_OFFICE_CLIENT_SECRET:-}" + fi + if [[ -z "${office_oidc_secret}" ]]; then + echo "Office SSO secret is missing. Run ./jeannie dex-secret ensure or set LAB_OFFICE_OIDC_CLIENT_SECRET." >&2 + exit 1 + fi + sudo mkdir -p "${install_dir}" "${nextcloud_data_dir}" "${postgres_data_dir}" "${redis_data_dir}" + sudo cp "${source_dir}/docker-compose.yml" "${install_dir}/docker-compose.yml" + + if ! sudo test -s "${env_file}"; then + random_value() { + if command -v openssl >/dev/null 2>&1; then + openssl rand -hex 32 + else + python3 - <<'PY' +import secrets +print(secrets.token_hex(32)) +PY + fi + } + random_value="$(random_value)" + sudo tee "${env_file}" >/dev/null </dev/null + sudo chmod 600 "${env_file}" + fi + + ( + cd "${install_dir}" + sudo docker compose --env-file "${env_file}" pull + sudo docker compose --env-file "${env_file}" up -d --remove-orphans + sudo docker compose --env-file "${env_file}" ps + ) + + if ! curl -fsS --connect-timeout 5 "http://127.0.0.1:${http_port}/status.php" >/dev/null; then + echo "Nextcloud did not become reachable at http://127.0.0.1:${http_port}/status.php." >&2 + (cd "${install_dir}" && sudo docker compose --env-file "${env_file}" logs --tail=120) >&2 || true + exit 1 + fi + + sudo docker exec "${OFFICE_NEXTCLOUD_CONTAINER_NAME:-homelab-office-nextcloud}" \ + sh -lc 'su -s /bin/sh www-data -c "php occ app:install richdocuments 2>/dev/null || true; php occ app:enable richdocuments 2>/dev/null || true; php occ config:app:set richdocuments wopi_url --value=http://collabora:9980 2>/dev/null || true; php occ app:install user_oidc 2>/dev/null || true; php occ app:enable user_oidc 2>/dev/null || true"' || true + sudo docker exec -e "OIDC_CLIENT_SECRET=${office_oidc_secret}" \ + -e "OIDC_ISSUER=${LAB_DEX_PUBLIC_URL:-https://auth.${LAB_DOMAIN:-lab2025.duckdns.org}}" \ + "${OFFICE_NEXTCLOUD_CONTAINER_NAME:-homelab-office-nextcloud}" \ + sh -lc 'su -s /bin/sh www-data -c "php occ user_oidc:provider nextcloud --clientid=nextcloud --clientsecret=\"$OIDC_CLIENT_SECRET\" --discoveryuri=\"$OIDC_ISSUER/.well-known/openid-configuration\" --scope=openid,profile,email 2>/dev/null || true"' || true + echo "Office stack is running at https://${public_host}/ through the Tailscale-restricted edge route." + echo "Nextcloud data: ${nextcloud_data_dir}; Collabora backend: http://127.0.0.1:${collabora_port}." +} + configure_grafana_dex_secret() { local namespace="${TF_VAR_grafana_namespace:-monitoring}" local secret_name="${TF_VAR_grafana_oauth_secret_name:-grafana-dex-oauth}" diff --git a/lib/jeannie/security.sh b/lib/jeannie/security.sh index 8c4bd66..7729b3c 100644 --- a/lib/jeannie/security.sh +++ b/lib/jeannie/security.sh @@ -229,6 +229,7 @@ ensure_dex_secret_file() { local tmp_file local grafana_client_secret local gitea_client_secret + local office_client_secret local user_password local user_email="${HOMELAB_DEX_USER_EMAIL:-jv@lab2025.duckdns.org}" local user_name="${HOMELAB_DEX_USER_NAME:-jv}" @@ -247,6 +248,7 @@ ensure_dex_secret_file() { tmp_file="$(mktemp)" grafana_client_secret="$(generate_dex_secret_value)" gitea_client_secret="$(generate_dex_secret_value)" + office_client_secret="$(generate_dex_secret_value)" user_password="$(generate_dex_secret_value)" user_id="$(generate_dex_user_id)" @@ -264,6 +266,7 @@ ensure_dex_secret_file() { printf 'stringData:\n' printf ' DEX_GRAFANA_CLIENT_SECRET: "%s"\n' "${grafana_client_secret}" printf ' DEX_GITEA_CLIENT_SECRET: "%s"\n' "${gitea_client_secret}" + printf ' DEX_OFFICE_CLIENT_SECRET: "%s"\n' "${office_client_secret}" printf ' DEX_USER_EMAIL: "%s"\n' "${user_email}" printf ' DEX_USER_NAME: "%s"\n' "${user_name}" printf ' DEX_USER_ID: "%s"\n' "${user_id}"