2026-01-13 21:58:12 -08:00
|
|
|
---
|
2026-01-13 22:36:53 -08:00
|
|
|
- name: Ensure kiwix ZIM directory exists
|
|
|
|
|
ansible.builtin.file:
|
|
|
|
|
path: "{{ kiwix_zim_dir }}"
|
|
|
|
|
state: directory
|
|
|
|
|
mode: '0755'
|
|
|
|
|
|
2026-01-16 15:14:00 -08:00
|
|
|
# --- Transmission-based torrent management ---
|
|
|
|
|
# This section ensures declared ZIM archives have torrents added to transmission.
|
|
|
|
|
# It does NOT wait for downloads to complete - kiwix startup handles that separately.
|
|
|
|
|
|
2026-01-14 11:06:02 -08:00
|
|
|
- name: Check transmission daemon is responding
|
|
|
|
|
ansible.builtin.command: transmission-remote -l
|
|
|
|
|
register: transmission_check
|
|
|
|
|
changed_when: false
|
|
|
|
|
failed_when: false
|
|
|
|
|
when: kiwix_use_transmission
|
|
|
|
|
|
|
|
|
|
- name: Fail if transmission is not running
|
|
|
|
|
ansible.builtin.fail:
|
|
|
|
|
msg: "Transmission daemon is not responding. Ensure transmission role ran successfully."
|
|
|
|
|
when: kiwix_use_transmission and transmission_check.rc != 0
|
|
|
|
|
|
2026-01-16 15:14:00 -08:00
|
|
|
# Find which declared archives don't have torrents yet (single shell command)
|
|
|
|
|
- name: Find declared archives missing from transmission
|
2026-01-14 11:06:02 -08:00
|
|
|
ansible.builtin.shell: |
|
2026-01-16 15:14:00 -08:00
|
|
|
set -euo pipefail
|
|
|
|
|
# Get current torrent list (skip header and footer)
|
|
|
|
|
torrents=$(transmission-remote -l 2>/dev/null | tail -n +2 | head -n -1 || true)
|
|
|
|
|
|
|
|
|
|
# Check each declared archive
|
|
|
|
|
{% for archive in kiwix_zim_archives %}
|
|
|
|
|
base="{{ archive.filename | regex_replace('\\.zim$', '') }}"
|
|
|
|
|
if ! echo "$torrents" | grep -qF "$base"; then
|
|
|
|
|
echo "{{ archive.category }}/{{ archive.filename }}"
|
2026-01-14 11:06:02 -08:00
|
|
|
fi
|
2026-01-16 15:14:00 -08:00
|
|
|
{% endfor %}
|
2026-01-14 11:06:02 -08:00
|
|
|
args:
|
|
|
|
|
executable: /bin/bash
|
2026-01-16 15:14:00 -08:00
|
|
|
register: missing_torrents
|
2026-01-14 11:06:02 -08:00
|
|
|
changed_when: false
|
|
|
|
|
when: kiwix_use_transmission
|
|
|
|
|
|
2026-01-16 15:14:00 -08:00
|
|
|
# Add only the missing torrents
|
2026-01-14 11:06:02 -08:00
|
|
|
- name: Add missing torrents to transmission
|
|
|
|
|
ansible.builtin.command: >
|
2026-01-16 15:14:00 -08:00
|
|
|
transmission-remote -a "{{ kiwix_torrent_base_url }}/{{ item }}.torrent"
|
|
|
|
|
loop: "{{ missing_torrents.stdout_lines | default([]) }}"
|
2026-01-14 11:06:02 -08:00
|
|
|
loop_control:
|
2026-01-16 15:14:00 -08:00
|
|
|
label: "{{ item | basename }}"
|
2026-01-14 11:06:02 -08:00
|
|
|
when:
|
|
|
|
|
- kiwix_use_transmission
|
2026-01-16 15:14:00 -08:00
|
|
|
- missing_torrents.stdout_lines | default([]) | length > 0
|
2026-01-14 11:06:02 -08:00
|
|
|
register: torrent_add
|
|
|
|
|
changed_when: torrent_add.rc == 0
|
|
|
|
|
|
2026-01-16 15:14:00 -08:00
|
|
|
# --- Kiwix startup: serve whatever completed ZIM files exist ---
|
|
|
|
|
# This is decoupled from the declared inventory - it just serves what's available.
|
2026-01-15 13:56:11 -08:00
|
|
|
|
2026-01-16 15:14:00 -08:00
|
|
|
# Find all completed ZIM files in transmission download directory
|
|
|
|
|
- name: Find completed ZIM files in transmission download directory
|
|
|
|
|
ansible.builtin.find:
|
|
|
|
|
paths: "{{ transmission_download_dir }}"
|
|
|
|
|
patterns: "*.zim"
|
|
|
|
|
file_type: file
|
|
|
|
|
register: completed_zim_files
|
2026-01-14 11:06:02 -08:00
|
|
|
when: kiwix_use_transmission
|
|
|
|
|
|
2026-01-16 15:14:00 -08:00
|
|
|
# Check which ZIM files already have symlinks in kiwix directory
|
|
|
|
|
- name: Check existing symlinks in kiwix directory
|
2026-01-14 11:06:02 -08:00
|
|
|
ansible.builtin.stat:
|
2026-01-16 15:14:00 -08:00
|
|
|
path: "{{ kiwix_zim_dir }}/{{ item.path | basename }}"
|
2026-01-14 11:06:02 -08:00
|
|
|
get_checksum: false
|
2026-01-16 15:14:00 -08:00
|
|
|
loop: "{{ completed_zim_files.files | default([]) }}"
|
2026-01-14 11:06:02 -08:00
|
|
|
loop_control:
|
2026-01-16 15:14:00 -08:00
|
|
|
label: "{{ item.path | basename }}"
|
|
|
|
|
register: existing_symlinks
|
|
|
|
|
when: kiwix_use_transmission
|
2026-01-14 11:06:02 -08:00
|
|
|
|
2026-01-16 15:14:00 -08:00
|
|
|
# Create symlinks for any completed ZIM files not yet linked
|
|
|
|
|
- name: Symlink completed ZIM files to kiwix directory
|
2026-01-14 11:06:02 -08:00
|
|
|
ansible.builtin.file:
|
2026-01-16 15:14:00 -08:00
|
|
|
src: "{{ item.item.path }}"
|
|
|
|
|
dest: "{{ kiwix_zim_dir }}/{{ item.item.path | basename }}"
|
2026-01-14 11:06:02 -08:00
|
|
|
state: link
|
2026-01-16 15:14:00 -08:00
|
|
|
loop: "{{ existing_symlinks.results | default([]) }}"
|
2026-01-14 11:06:02 -08:00
|
|
|
loop_control:
|
2026-01-16 15:14:00 -08:00
|
|
|
label: "{{ item.item.path | basename }}"
|
2026-01-14 11:06:02 -08:00
|
|
|
when:
|
|
|
|
|
- kiwix_use_transmission
|
|
|
|
|
- item.stat is defined
|
|
|
|
|
- not item.stat.exists
|
|
|
|
|
notify: restart kiwix-serve
|
|
|
|
|
|
|
|
|
|
# --- Fallback: Direct HTTP download (original behavior) ---
|
|
|
|
|
- name: Check which ZIM archives exist (direct download mode)
|
2026-01-13 22:36:53 -08:00
|
|
|
ansible.builtin.stat:
|
|
|
|
|
path: "{{ kiwix_zim_dir }}/{{ item.filename }}"
|
|
|
|
|
get_checksum: false
|
|
|
|
|
loop: "{{ kiwix_zim_archives }}"
|
|
|
|
|
loop_control:
|
|
|
|
|
label: "{{ item.filename }}"
|
|
|
|
|
register: zim_stat
|
2026-01-14 11:06:02 -08:00
|
|
|
when: not kiwix_use_transmission
|
2026-01-13 22:36:53 -08:00
|
|
|
|
2026-01-14 11:06:02 -08:00
|
|
|
- name: Download missing ZIM archives (direct download mode)
|
2026-01-13 22:36:53 -08:00
|
|
|
ansible.builtin.get_url:
|
|
|
|
|
url: "https://download.kiwix.org/zim/{{ item.item.category }}/{{ item.item.filename }}"
|
|
|
|
|
dest: "{{ kiwix_zim_dir }}/{{ item.item.filename }}"
|
|
|
|
|
mode: '0644'
|
|
|
|
|
timeout: 3600
|
2026-01-14 11:06:02 -08:00
|
|
|
loop: "{{ zim_stat.results | default([]) }}"
|
2026-01-13 22:36:53 -08:00
|
|
|
loop_control:
|
2026-01-14 11:06:02 -08:00
|
|
|
label: "{{ item.item.filename | default('unknown') }}"
|
|
|
|
|
when:
|
|
|
|
|
- not kiwix_use_transmission
|
|
|
|
|
- item.stat is defined
|
|
|
|
|
- not item.stat.exists
|
2026-01-13 22:36:53 -08:00
|
|
|
notify: restart kiwix-serve
|
|
|
|
|
|
2026-01-14 13:17:55 -08:00
|
|
|
# --- Determine which archives are available ---
|
|
|
|
|
- name: Find available ZIM archives in kiwix directory
|
|
|
|
|
ansible.builtin.find:
|
|
|
|
|
paths: "{{ kiwix_zim_dir }}"
|
|
|
|
|
patterns: "*.zim"
|
|
|
|
|
file_type: any # includes symlinks
|
|
|
|
|
register: available_zim_files
|
|
|
|
|
|
|
|
|
|
- name: Build list of available archive filenames
|
|
|
|
|
ansible.builtin.set_fact:
|
|
|
|
|
kiwix_available_archives: "{{ available_zim_files.files | map(attribute='path') | map('basename') | list }}"
|
|
|
|
|
|
2026-01-14 11:06:02 -08:00
|
|
|
# --- LaunchAgent deployment ---
|
2026-01-13 21:58:12 -08:00
|
|
|
- name: Deploy kiwix-serve LaunchAgent plist
|
|
|
|
|
ansible.builtin.template:
|
|
|
|
|
src: kiwix-serve.plist.j2
|
|
|
|
|
dest: ~/Library/LaunchAgents/mcquack.eblume.kiwix-serve.plist
|
|
|
|
|
mode: '0644'
|
|
|
|
|
notify: restart kiwix-serve
|
|
|
|
|
|
2026-01-14 14:14:52 -08:00
|
|
|
- name: Check if kiwix-serve LaunchAgent is loaded
|
|
|
|
|
ansible.builtin.command: launchctl list mcquack.eblume.kiwix-serve
|
|
|
|
|
register: launchctl_check
|
|
|
|
|
changed_when: false
|
|
|
|
|
failed_when: false
|
|
|
|
|
|
|
|
|
|
- name: Load kiwix-serve LaunchAgent if not loaded
|
2026-01-13 21:58:12 -08:00
|
|
|
ansible.builtin.command: launchctl load ~/Library/LaunchAgents/mcquack.eblume.kiwix-serve.plist
|
2026-01-14 14:14:52 -08:00
|
|
|
when: launchctl_check.rc != 0
|
2026-01-13 21:58:12 -08:00
|
|
|
failed_when: false
|