diff --git a/docker-compose/docker-compose-homarr.yml b/docker-compose/docker-compose-homarr.yml new file mode 100644 index 0000000..a547c97 --- /dev/null +++ b/docker-compose/docker-compose-homarr.yml @@ -0,0 +1,13 @@ +services: + homarr: + container_name: homarr + image: ghcr.io/homarr-labs/homarr:latest + restart: unless-stopped + volumes: + - /var/run/docker.sock:/var/run/docker.sock # Optional, only if you want docker integration + - /data/compose/homarr/appdata:/appdata + environment: + - SECRET_ENCRYPTION_KEY=4fb16028fa1788d9a24fa93a323aa4a278524bed177c8c38454f4c4068c1b9b6 + ports: + - '7575:7575' + \ No newline at end of file diff --git a/old/update_homarr.yml b/old/update_homarr.yml new file mode 100644 index 0000000..daba4d1 --- /dev/null +++ b/old/update_homarr.yml @@ -0,0 +1,194 @@ +# update_homarr.yml + +- name: Update Homarr on VM via Proxmox + hosts: linux_servers + gather_facts: false + become: true + become_user: root + become_method: sudo + + vars: + # VM connection (provided by Semaphore env vars) + vm_ip: "{{ lookup('env', 'VM_IP') }}" + vm_user: "{{ lookup('env', 'VM_USER') }}" + vm_pass: "{{ lookup('env', 'VM_PASS') }}" + use_sudo: false + + # --- Debug mode (controlled via Semaphore variable) --- + DEBUG: "{{ lookup('env', 'DEBUG') | default(0) | int }}" + RETRIES: "{{ lookup('env', 'RETRIES') | default(25) | int }}" + + # Homarr specifics + homarr_project: "homarr" + homarr_compose_file: "/data/compose/homarr/docker-compose-homarr.yml" + homarr_service: "homarr" + homarr_image: "ghcr.io/homarr-labs/homarr:latest" + homarr_port: 7575 + + # Optional external URL for controller-side readiness check (e.g., https://homarr.example.com) + # If empty/undefined, controller check is skipped and we only probe from the VM. + homarr_url: "{{ lookup('env', 'HOMARR_URL') | default('', true) }}" + + # Fixed container name used in your compose (avoid conflicts with any leftover container) + homarr_container_name: "homarr" + + # Retry policy (same pattern as Kuma): 25x with 2s delay + homarr_retries: "{{ RETRIES }}" + homarr_delay: 2 + + # Docker command prefix (consistent behavior) + docker_prefix: "unalias docker 2>/dev/null || true; DOCKER_CLI_HINTS=0; command docker" + + # Commands to run on the target VM (quiet outputs) + homarr_commands: + - "{{ docker_prefix }} pull -q {{ homarr_image }} >/dev/null" + - "{{ docker_prefix }} compose -p {{ homarr_project }} -f {{ homarr_compose_file }} pull {{ homarr_service }} >/dev/null" + # remove conflicting container name before compose up (silently) + - "{{ docker_prefix }} rm -f {{ homarr_container_name }} >/dev/null 2>&1 || true" + - "{{ docker_prefix }} compose -p {{ homarr_project }} -f {{ homarr_compose_file }} up -d --no-deps --force-recreate {{ homarr_service }} >/dev/null" + + tasks: + - name: Ensure sshpass is installed (for password-based SSH) # English comments + ansible.builtin.apt: + name: sshpass + state: present + update_cache: yes + + - name: Run Homarr update commands on VM (via SSH) # use SSHPASS env, hide item label + ansible.builtin.command: + argv: + - sshpass + - -e # read password from SSHPASS environment + - ssh + - -o + - StrictHostKeyChecking=no + - -o + - ConnectTimeout=15 + - "{{ vm_user }}@{{ vm_ip }}" + - bash + - -lc + - "{{ ('sudo ' if use_sudo else '') + item }}" + environment: + SSHPASS: "{{ vm_pass }}" # supply password via environment + loop: "{{ homarr_commands }}" + loop_control: + index_var: idx # capture loop index + label: "cmd-{{ idx }}" # avoid printing full command in (item=...) line + register: homarr_cmds + changed_when: false + no_log: "{{ DEBUG == 0 }}" # hide outputs and env when not debugging + + - name: Show outputs for each Homarr command + ansible.builtin.debug: + msg: | + CMD: {{ item.item }} + RC: {{ item.rc }} + STDOUT: + {{ (item.stdout | default('')).strip() }} + STDERR: + {{ (item.stderr | default('')).strip() }} + loop: "{{ homarr_cmds.results }}" + when: DEBUG == 1 + + - name: Fail play if any Homarr command failed # also hide item label + ansible.builtin.assert: + that: "item.rc == 0" + fail_msg: "Homarr update failed on VM: {{ item.item }} (rc={{ item.rc }})" + success_msg: "All Homarr update commands succeeded." + loop: "{{ homarr_cmds.results }}" + loop_control: + index_var: idx + label: "cmd-{{ idx }}" + + # ------------------------- + # Readiness checks (controller first, then VM fallback) + # ------------------------- + + - name: Homarr | Wait for homepage (controller first, with retries) + ansible.builtin.uri: + url: "{{ (homarr_url | regex_replace('/$','')) + '/' }}" + method: GET + return_content: true + # Validate TLS only when using https:// + validate_certs: "{{ (homarr_url | default('')) is match('^https://') }}" + status_code: 200 + register: homarr_controller + delegate_to: localhost + run_once: true + when: homarr_url is defined and (homarr_url | length) > 0 + retries: "{{ homarr_retries }}" + delay: "{{ homarr_delay }}" + until: homarr_controller.status == 200 + failed_when: false + changed_when: false + + - name: Homarr | VM-side fetch (HTML via Python, with retries) # use SSHPASS env here too + ansible.builtin.command: + argv: + - sshpass + - -e + - ssh + - -o + - StrictHostKeyChecking=no + - -o + - ConnectTimeout=15 + - "{{ vm_user }}@{{ vm_ip }}" + - bash + - -lc + - | + python3 - <<'PY' + # Fetch Homarr homepage from localhost and print HTML to stdout + import urllib.request, sys + try: + with urllib.request.urlopen("http://127.0.0.1:{{ homarr_port }}/", timeout=15) as r: + sys.stdout.write(r.read().decode(errors='ignore')) + except Exception: + pass + PY + environment: + SSHPASS: "{{ vm_pass }}" + register: homarr_vm + changed_when: false + failed_when: false + when: homarr_controller.status | default(0) != 200 or homarr_controller.content is not defined + retries: "{{ homarr_retries }}" + delay: "{{ homarr_delay }}" + until: (homarr_vm.stdout | default('') | trim | length) > 0 and ('Homarr' in (homarr_vm.stdout | default(''))) + no_log: "{{ DEBUG == 0 }}" + + - name: Homarr | Choose homepage HTML (controller wins, else VM) # safe guard against empty result + ansible.builtin.set_fact: + homarr_home_html: >- + {{ + ( + homarr_controller.content + if (homarr_controller is defined) + and ((homarr_controller.status|default(0))==200) + and (homarr_controller.content is defined) + else + (homarr_vm.stdout | default('') | trim) + ) + }} + when: + - (homarr_controller is defined and (homarr_controller.status|default(0))==200 and (homarr_controller.content is defined)) + or ((homarr_vm.stdout | default('') | trim | length) > 0) + + - name: Homarr | Print concise summary + ansible.builtin.debug: + msg: >- + Homarr homepage {{ 'reachable' if (homarr_home_html is defined) else 'NOT reachable' }}. + Source={{ 'controller' if ((homarr_controller is defined) and (homarr_controller.status|default(0))==200 and (homarr_controller.content is defined)) else 'vm' if (homarr_vm.stdout|default('')|trim|length>0) else 'n/a' }}; + length={{ (homarr_home_html | default('')) | length }}; + contains('Homarr')={{ (homarr_home_html is defined) and ('Homarr' in homarr_home_html) }} + when: DEBUG == 1 + + - name: Homarr | Homepage unavailable (after retries) + ansible.builtin.debug: + msg: "Homarr web není dostupný ani po pokusech." + when: homarr_home_html is not defined and DEBUG == 1 + + # Optional detailed dump (short excerpt only) + - name: Homarr | HTML excerpt (debug) + ansible.builtin.debug: + msg: "{{ (homarr_home_html | default(''))[:500] }}" + when: homarr_home_html is defined and DEBUG == 1 diff --git a/update_homarr.yml b/update_homarr.yml index daba4d1..535eb0e 100644 --- a/update_homarr.yml +++ b/update_homarr.yml @@ -1,194 +1,40 @@ -# update_homarr.yml +# update_homarr2.yml -- name: Update Homarr on VM via Proxmox - hosts: linux_servers +- name: Update Homarr + hosts: pve2_vm gather_facts: false - become: true - become_user: root - become_method: sudo vars: - # VM connection (provided by Semaphore env vars) - vm_ip: "{{ lookup('env', 'VM_IP') }}" - vm_user: "{{ lookup('env', 'VM_USER') }}" - vm_pass: "{{ lookup('env', 'VM_PASS') }}" - use_sudo: false - - # --- Debug mode (controlled via Semaphore variable) --- - DEBUG: "{{ lookup('env', 'DEBUG') | default(0) | int }}" - RETRIES: "{{ lookup('env', 'RETRIES') | default(25) | int }}" - - # Homarr specifics - homarr_project: "homarr" - homarr_compose_file: "/data/compose/homarr/docker-compose-homarr.yml" - homarr_service: "homarr" - homarr_image: "ghcr.io/homarr-labs/homarr:latest" + homarr_project: homarr + homarr_compose_file: "{{ playbook_dir }}/docker-compose/docker-compose-homarr.yml" + homarr_service: homarr homarr_port: 7575 - # Optional external URL for controller-side readiness check (e.g., https://homarr.example.com) - # If empty/undefined, controller check is skipped and we only probe from the VM. - homarr_url: "{{ lookup('env', 'HOMARR_URL') | default('', true) }}" - - # Fixed container name used in your compose (avoid conflicts with any leftover container) - homarr_container_name: "homarr" - - # Retry policy (same pattern as Kuma): 25x with 2s delay - homarr_retries: "{{ RETRIES }}" - homarr_delay: 2 - - # Docker command prefix (consistent behavior) - docker_prefix: "unalias docker 2>/dev/null || true; DOCKER_CLI_HINTS=0; command docker" - - # Commands to run on the target VM (quiet outputs) - homarr_commands: - - "{{ docker_prefix }} pull -q {{ homarr_image }} >/dev/null" - - "{{ docker_prefix }} compose -p {{ homarr_project }} -f {{ homarr_compose_file }} pull {{ homarr_service }} >/dev/null" - # remove conflicting container name before compose up (silently) - - "{{ docker_prefix }} rm -f {{ homarr_container_name }} >/dev/null 2>&1 || true" - - "{{ docker_prefix }} compose -p {{ homarr_project }} -f {{ homarr_compose_file }} up -d --no-deps --force-recreate {{ homarr_service }} >/dev/null" - tasks: - - name: Ensure sshpass is installed (for password-based SSH) # English comments - ansible.builtin.apt: - name: sshpass + - name: Pull latest Homarr image + community.docker.docker_compose_v2: + project_src: "{{ homarr_compose_file | dirname }}" + files: + - "{{ homarr_compose_file | basename }}" + pull: always + + - name: Recreate Homarr service + community.docker.docker_compose_v2: + project_src: "{{ homarr_compose_file | dirname }}" + files: + - "{{ homarr_compose_file | basename }}" + services: + - "{{ homarr_service }}" state: present - update_cache: yes + recreate: always - - name: Run Homarr update commands on VM (via SSH) # use SSHPASS env, hide item label - ansible.builtin.command: - argv: - - sshpass - - -e # read password from SSHPASS environment - - ssh - - -o - - StrictHostKeyChecking=no - - -o - - ConnectTimeout=15 - - "{{ vm_user }}@{{ vm_ip }}" - - bash - - -lc - - "{{ ('sudo ' if use_sudo else '') + item }}" - environment: - SSHPASS: "{{ vm_pass }}" # supply password via environment - loop: "{{ homarr_commands }}" - loop_control: - index_var: idx # capture loop index - label: "cmd-{{ idx }}" # avoid printing full command in (item=...) line - register: homarr_cmds - changed_when: false - no_log: "{{ DEBUG == 0 }}" # hide outputs and env when not debugging + - name: Wait for Homarr port + ansible.builtin.wait_for: + host: 127.0.0.1 + port: "{{ homarr_port }}" + timeout: 60 - - name: Show outputs for each Homarr command - ansible.builtin.debug: - msg: | - CMD: {{ item.item }} - RC: {{ item.rc }} - STDOUT: - {{ (item.stdout | default('')).strip() }} - STDERR: - {{ (item.stderr | default('')).strip() }} - loop: "{{ homarr_cmds.results }}" - when: DEBUG == 1 - - - name: Fail play if any Homarr command failed # also hide item label - ansible.builtin.assert: - that: "item.rc == 0" - fail_msg: "Homarr update failed on VM: {{ item.item }} (rc={{ item.rc }})" - success_msg: "All Homarr update commands succeeded." - loop: "{{ homarr_cmds.results }}" - loop_control: - index_var: idx - label: "cmd-{{ idx }}" - - # ------------------------- - # Readiness checks (controller first, then VM fallback) - # ------------------------- - - - name: Homarr | Wait for homepage (controller first, with retries) + - name: Check Homarr HTTP endpoint ansible.builtin.uri: - url: "{{ (homarr_url | regex_replace('/$','')) + '/' }}" - method: GET - return_content: true - # Validate TLS only when using https:// - validate_certs: "{{ (homarr_url | default('')) is match('^https://') }}" - status_code: 200 - register: homarr_controller - delegate_to: localhost - run_once: true - when: homarr_url is defined and (homarr_url | length) > 0 - retries: "{{ homarr_retries }}" - delay: "{{ homarr_delay }}" - until: homarr_controller.status == 200 - failed_when: false - changed_when: false - - - name: Homarr | VM-side fetch (HTML via Python, with retries) # use SSHPASS env here too - ansible.builtin.command: - argv: - - sshpass - - -e - - ssh - - -o - - StrictHostKeyChecking=no - - -o - - ConnectTimeout=15 - - "{{ vm_user }}@{{ vm_ip }}" - - bash - - -lc - - | - python3 - <<'PY' - # Fetch Homarr homepage from localhost and print HTML to stdout - import urllib.request, sys - try: - with urllib.request.urlopen("http://127.0.0.1:{{ homarr_port }}/", timeout=15) as r: - sys.stdout.write(r.read().decode(errors='ignore')) - except Exception: - pass - PY - environment: - SSHPASS: "{{ vm_pass }}" - register: homarr_vm - changed_when: false - failed_when: false - when: homarr_controller.status | default(0) != 200 or homarr_controller.content is not defined - retries: "{{ homarr_retries }}" - delay: "{{ homarr_delay }}" - until: (homarr_vm.stdout | default('') | trim | length) > 0 and ('Homarr' in (homarr_vm.stdout | default(''))) - no_log: "{{ DEBUG == 0 }}" - - - name: Homarr | Choose homepage HTML (controller wins, else VM) # safe guard against empty result - ansible.builtin.set_fact: - homarr_home_html: >- - {{ - ( - homarr_controller.content - if (homarr_controller is defined) - and ((homarr_controller.status|default(0))==200) - and (homarr_controller.content is defined) - else - (homarr_vm.stdout | default('') | trim) - ) - }} - when: - - (homarr_controller is defined and (homarr_controller.status|default(0))==200 and (homarr_controller.content is defined)) - or ((homarr_vm.stdout | default('') | trim | length) > 0) - - - name: Homarr | Print concise summary - ansible.builtin.debug: - msg: >- - Homarr homepage {{ 'reachable' if (homarr_home_html is defined) else 'NOT reachable' }}. - Source={{ 'controller' if ((homarr_controller is defined) and (homarr_controller.status|default(0))==200 and (homarr_controller.content is defined)) else 'vm' if (homarr_vm.stdout|default('')|trim|length>0) else 'n/a' }}; - length={{ (homarr_home_html | default('')) | length }}; - contains('Homarr')={{ (homarr_home_html is defined) and ('Homarr' in homarr_home_html) }} - when: DEBUG == 1 - - - name: Homarr | Homepage unavailable (after retries) - ansible.builtin.debug: - msg: "Homarr web není dostupný ani po pokusech." - when: homarr_home_html is not defined and DEBUG == 1 - - # Optional detailed dump (short excerpt only) - - name: Homarr | HTML excerpt (debug) - ansible.builtin.debug: - msg: "{{ (homarr_home_html | default(''))[:500] }}" - when: homarr_home_html is defined and DEBUG == 1 + url: "http://127.0.0.1:{{ homarr_port }}/" + status_code: 200 \ No newline at end of file diff --git a/update_homarr2.yml b/update_homarr2.yml deleted file mode 100644 index e241d2a..0000000 --- a/update_homarr2.yml +++ /dev/null @@ -1,40 +0,0 @@ -# update_homarr2.yml - -- name: Update Homarr - hosts: pve2_vm - gather_facts: false - - vars: - homarr_project: homarr - homarr_compose_file: /data/compose/homarr/docker-compose-homarr.yml - homarr_service: homarr - homarr_port: 7575 - - tasks: - - name: Pull latest Homarr image - community.docker.docker_compose_v2: - project_src: "{{ homarr_compose_file | dirname }}" - files: - - "{{ homarr_compose_file | basename }}" - pull: always - - - name: Recreate Homarr service - community.docker.docker_compose_v2: - project_src: "{{ homarr_compose_file | dirname }}" - files: - - "{{ homarr_compose_file | basename }}" - services: - - "{{ homarr_service }}" - state: present - recreate: always - - - name: Wait for Homarr port - ansible.builtin.wait_for: - host: 127.0.0.1 - port: "{{ homarr_port }}" - timeout: 60 - - - name: Check Homarr HTTP endpoint - ansible.builtin.uri: - url: "http://127.0.0.1:{{ homarr_port }}/" - status_code: 200