From 87345121ead88edaf2ce709a19fe482032103236 Mon Sep 17 00:00:00 2001 From: "martin.fencl" Date: Tue, 3 Feb 2026 18:22:32 +0100 Subject: [PATCH] edit immich --- update_immich.yml | 136 ++++++++-------------------------------------- 1 file changed, 22 insertions(+), 114 deletions(-) diff --git a/update_immich.yml b/update_immich.yml index d265c52..f09491e 100644 --- a/update_immich.yml +++ b/update_immich.yml @@ -18,8 +18,8 @@ - docker-compose-immich.yml - docker-compose-immich.override.yml - # Remote .env handling - immich_env_backup: "{{ compose_remote_base }}/immich.env.backup" + # Persistent env file on the VM (NOT in git) + immich_env_persistent: "{{ compose_remote_base }}/env/immich.env" tasks: - name: Ensure remote base directory exists @@ -28,14 +28,23 @@ state: directory mode: "0755" - # --- Preserve .env across staging refresh (because .env is usually not in git) --- - - name: Backup existing Immich .env on remote (if present) - ansible.builtin.copy: - src: "{{ compose_remote_dir }}/.env" - dest: "{{ immich_env_backup }}" - remote_src: true - mode: "0600" - ignore_errors: true + - name: Ensure remote env directory exists + ansible.builtin.file: + path: "{{ compose_remote_base }}/env" + state: directory + mode: "0700" + + - name: Fail if persistent Immich env file is missing + ansible.builtin.stat: + path: "{{ immich_env_persistent }}" + register: immich_env_stat + + - name: Abort when Immich env is missing + ansible.builtin.fail: + msg: >- + Missing persistent env file: {{ immich_env_persistent }}. + Create it on the VM with DB_* and UPLOAD_LOCATION variables. + when: not immich_env_stat.stat.exists - name: Create local archive of docker-compose directory (controller) ansible.builtin.archive: @@ -68,113 +77,12 @@ dest: "{{ compose_remote_dir }}" remote_src: true - - name: Restore Immich .env on remote (if backup exists) + - name: Deploy Immich .env into compose directory ansible.builtin.copy: - src: "{{ immich_env_backup }}" + src: "{{ immich_env_persistent }}" dest: "{{ compose_remote_dir }}/.env" remote_src: true mode: "0600" - ignore_errors: true - - - name: Ensure Immich .env exists (reconstruct from running containers if missing) - ansible.builtin.command: - argv: - - bash - - -lc - - | - set -euo pipefail - cd "{{ compose_remote_dir }}" - - if [ -f .env ]; then - exit 0 - fi - - python3 - <<'PY' - import json - import subprocess - from pathlib import Path - - env_path = Path(".env") - - def run(cmd): - p = subprocess.run(cmd, capture_output=True, text=True) - return p.returncode, p.stdout, p.stderr - - rc, out, err = run(["bash", "-lc", "command docker inspect immich_postgres immich_server"]) - if rc != 0 or not out.strip(): - print("ERROR: .env is missing and cannot inspect running containers (immich_postgres/immich_server).", flush=True) - print("Create the .env on the VM or ensure the containers exist.", flush=True) - raise SystemExit(1) - - data = json.loads(out) - by_name = {} - for c in data: - name = (c.get("Name") or "").lstrip("/") - by_name[name] = c - - pg = by_name.get("immich_postgres") - srv = by_name.get("immich_server") - if not pg or not srv: - print("ERROR: Could not find immich_postgres and immich_server in docker inspect output.", flush=True) - raise SystemExit(1) - - def env_map(container): - m = {} - for kv in (container.get("Config", {}).get("Env") or []): - if "=" in kv: - k, v = kv.split("=", 1) - m[k] = v - return m - - def find_mount_source(container, dest): - for m in (container.get("Mounts") or []): - if m.get("Destination") == dest: - return m.get("Source") - return "" - - pg_env = env_map(pg) - db_user = pg_env.get("POSTGRES_USER", "") - db_pass = pg_env.get("POSTGRES_PASSWORD", "") - db_name = pg_env.get("POSTGRES_DB", "") - - db_data = find_mount_source(pg, "/var/lib/postgresql/data") - upload_loc = find_mount_source(srv, "/usr/src/app/upload") - - immich_version = "" - image = (srv.get("Config", {}).get("Image") or "") - if ":" in image and "@" not in image: - immich_version = image.rsplit(":", 1)[-1] - elif ":" in image and "@" in image: - immich_version = image.split("@", 1)[0].rsplit(":", 1)[-1] - - missing = [] - for k, v in [ - ("DB_USERNAME", db_user), - ("DB_PASSWORD", db_pass), - ("DB_DATABASE_NAME", db_name), - ("DB_DATA_LOCATION", db_data), - ("UPLOAD_LOCATION", upload_loc), - ]: - if not v: - missing.append(k) - - if missing: - print("ERROR: Could not reconstruct these .env values from containers: " + ", ".join(missing), flush=True) - raise SystemExit(1) - - lines = [ - f"UPLOAD_LOCATION={upload_loc}", - f"DB_DATA_LOCATION={db_data}", - f"DB_USERNAME={db_user}", - f"DB_PASSWORD={db_pass}", - f"DB_DATABASE_NAME={db_name}", - ] - if immich_version: - lines.append(f"IMMICH_VERSION={immich_version}") - - env_path.write_text("\n".join(lines) + "\n", encoding="utf-8") - print("Created .env from running containers.", flush=True) - PY - name: Pull latest Immich images community.docker.docker_compose_v2: @@ -197,7 +105,7 @@ port: "{{ immich_port }}" timeout: 120 - - name: Check Immich HTTP endpoint + - name: Check Immich API ping ansible.builtin.uri: url: "http://127.0.0.1:{{ immich_port }}/api/server/ping" status_code: 200