nex-hindsight main (0abbca1, 22.10.) stellt ein Gate vor Port 8888: HINDSIGHT_API_URL für die
Jobs im LXC auf 127.0.0.1:8889, GATE_KEYS_DIR, keys.json (nur Hashes) aus ops
/root/.config/knaster/hindsight-gate-keys.json oder einmalig {}. Freigabeliste um Laptop-WLAN und
die k3s-Knoten (NEX-Pods, SNAT) ergänzt. /metrics liegt hinter dem Gate, deshalb Blackbox-Exporter
im Infra-Stack und Alert HindsightDown auf probe_success. Rulings 10–12 im Auftrag, BETRIEB §16.
Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
243 lines
8.6 KiB
YAML
243 lines
8.6 KiB
YAML
# Compose-Stack nex-hindsight: Hindsight-API + pgvector-Postgres im LXC hindsight. Idempotent wie infra-stack:
|
||
# Geheimnisse nur anlegen, wenn sie fehlen; compose up ändert nur, was sich geändert hat.
|
||
- name: Repo-Clone auf ops vorhanden?
|
||
ansible.builtin.stat:
|
||
path: "{{ hindsight_repo_src }}/deploy/compose/hindsight/docker-compose.yml"
|
||
delegate_to: localhost
|
||
become: false
|
||
register: repo_src
|
||
|
||
- name: Abbruch ohne Repo-Clone
|
||
ansible.builtin.assert:
|
||
that: repo_src.stat.exists
|
||
fail_msg: "{{ hindsight_repo_src }} fehlt auf ops: git clone https://gitea.mindboost.team/mygodel/nex-hindsight.git {{ hindsight_repo_src }} (privates Repo, eigener Zugang)"
|
||
|
||
- name: Schlüsseldateien auf ops vorhanden?
|
||
ansible.builtin.stat:
|
||
path: "{{ item }}"
|
||
loop: ["{{ hindsight_registry_env }}", "{{ hindsight_litellm_env }}"]
|
||
delegate_to: localhost
|
||
become: false
|
||
register: key_files
|
||
|
||
- name: Abbruch ohne Schlüsseldateien
|
||
ansible.builtin.assert:
|
||
that: key_files.results | map(attribute='stat.exists') | min
|
||
fail_msg: "Auf ops fehlt {{ hindsight_registry_env }} (REGISTRY_USER/REGISTRY_TOKEN) oder {{ hindsight_litellm_env }} (HINDSIGHT_LLM_API_KEY/LITELLM_API_KEY)"
|
||
|
||
- name: Zeitzone (Jobfenster 22:00–06:00 gilt sonst in UTC)
|
||
community.general.timezone:
|
||
name: "{{ hindsight_timezone }}"
|
||
|
||
- name: Werkzeuge (node für die Cron-Skripte, ufw, cron)
|
||
ansible.builtin.apt:
|
||
name: [rsync, openssl, curl, nodejs, ufw, cron, python3]
|
||
state: present
|
||
update_cache: true
|
||
cache_valid_time: 3600
|
||
|
||
- name: Verzeichnisse
|
||
ansible.builtin.file:
|
||
path: "{{ item.path }}"
|
||
state: directory
|
||
mode: "{{ item.mode }}"
|
||
loop:
|
||
- {path: /opt/knaster, mode: "0755"}
|
||
- {path: "{{ hindsight_state_dir }}", mode: "0700"}
|
||
- {path: "{{ hindsight_repo_dir }}", mode: "0755"}
|
||
- {path: "{{ hindsight_state_dir }}/postgres", mode: "0700"}
|
||
- {path: "{{ hindsight_state_dir }}/backup", mode: "0700"}
|
||
- {path: "{{ hindsight_state_dir }}/export", mode: "0700"}
|
||
- {path: "{{ hindsight_state_dir }}/logs", mode: "0755"}
|
||
- {path: "{{ hindsight_state_dir }}/gate", mode: "0700"}
|
||
|
||
# Nie .git, node_modules oder .env-Dateien; Container-Benutzer müssen lesen können
|
||
- name: Repo nex-hindsight von ops abgleichen
|
||
ansible.posix.synchronize:
|
||
src: "{{ hindsight_repo_src }}/"
|
||
dest: "{{ hindsight_repo_dir }}/"
|
||
delete: true
|
||
rsync_opts:
|
||
- "--chmod=go+rX,go-w"
|
||
- "--exclude=.git"
|
||
- "--exclude=node_modules"
|
||
- "--exclude=.env"
|
||
- "--exclude=postgres/"
|
||
|
||
- name: Einstellungen in .env
|
||
ansible.builtin.lineinfile:
|
||
path: "{{ hindsight_state_dir }}/.env"
|
||
regexp: "^{{ item.key }}="
|
||
line: "{{ item.key }}={{ item.value }}"
|
||
create: true
|
||
mode: "0600"
|
||
loop: "{{ hindsight_settings | dict2items }}"
|
||
|
||
# Gibt nur Namen aus, nie Werte
|
||
- name: Fehlende Geheimnisse im Container erzeugen
|
||
ansible.builtin.shell: |
|
||
set -eu
|
||
umask 077
|
||
f={{ hindsight_state_dir }}/.env
|
||
for k in {{ hindsight_secrets | join(' ') }}; do
|
||
grep -q "^$k=" "$f" || { echo "$k=$(openssl rand -hex 24)" >> "$f"; echo "neu: $k"; }
|
||
done
|
||
chmod 0600 "$f"
|
||
args:
|
||
executable: /bin/bash
|
||
register: secrets_gen
|
||
changed_when: "'neu:' in secrets_gen.stdout"
|
||
|
||
# LiteLLM-Schlüssel aus der Datei auf ops (Zeilen KEY=WERT), nie im Protokoll
|
||
- name: LiteLLM-Schlüssel in .env
|
||
ansible.builtin.lineinfile:
|
||
path: "{{ hindsight_state_dir }}/.env"
|
||
regexp: "^{{ item.0 }}="
|
||
line: "{{ item.0 }}={{ item.1 }}"
|
||
mode: "0600"
|
||
loop: "{{ lookup('file', hindsight_litellm_env) | regex_findall('(?m)^([A-Z_]+)=(.*)$') }}"
|
||
loop_control:
|
||
label: "{{ item.0 }}"
|
||
no_log: true
|
||
|
||
# Gate-Schlüssel (nur Hashes) von ops; das Gate liest die Datei bei Änderung neu, ohne Neustart. Ohne Datei auf ops
|
||
# bleibt ein einmal angelegtes {} stehen (nie überschrieben), damit ein späterer Lauf keine Schlüssel löscht.
|
||
- name: Gate-Schlüsseldatei auf ops vorhanden?
|
||
ansible.builtin.stat:
|
||
path: "{{ hindsight_gate_keys_src }}"
|
||
delegate_to: localhost
|
||
become: false
|
||
register: gate_keys_src
|
||
|
||
- name: Gate-Schlüssel (keys.json) aus ops übernehmen
|
||
ansible.builtin.copy:
|
||
src: "{{ hindsight_gate_keys_src }}"
|
||
dest: "{{ hindsight_state_dir }}/gate/keys.json"
|
||
mode: "0600"
|
||
validate: python3 -c 'import json,sys; d=json.load(open(sys.argv[1])); assert isinstance(d, dict)' %s
|
||
when: gate_keys_src.stat.exists
|
||
no_log: true
|
||
|
||
- name: Gate ohne Schlüssel (leeres keys.json, nur einmal)
|
||
ansible.builtin.copy:
|
||
content: "{}\n"
|
||
dest: "{{ hindsight_state_dir }}/gate/keys.json"
|
||
mode: "0600"
|
||
force: false
|
||
when: not gate_keys_src.stat.exists
|
||
|
||
- name: .env prüfen (Altnamen, CHANGE_ME-Reste)
|
||
ansible.builtin.command: node {{ hindsight_repo_dir }}/scripts/validate.mjs --env {{ hindsight_state_dir }}/.env
|
||
changed_when: false
|
||
|
||
- name: Registry-Anmeldung für das private Image
|
||
community.docker.docker_login:
|
||
registry_url: "{{ hindsight_registry }}"
|
||
username: "{{ _reg | selectattr(0, 'equalto', 'REGISTRY_USER') | map(attribute=1) | first }}"
|
||
password: "{{ _reg | selectattr(0, 'equalto', 'REGISTRY_TOKEN') | map(attribute=1) | first }}"
|
||
vars:
|
||
_reg: "{{ lookup('file', hindsight_registry_env) | regex_findall('(?m)^([A-Z_]+)=(.*)$') }}"
|
||
no_log: true
|
||
|
||
# HA-Umzug: Nach zfs receive liefert der Zielknoten veralteten Seiten-Cache (BETRIEB.md Abschnitt 7); gleicher
|
||
# Schutz wie im Infra-LXC, Dateien aus der Rolle infra-stack.
|
||
- name: Schutz gegen veralteten Seiten-Cache – Skript
|
||
ansible.builtin.copy:
|
||
src: "{{ role_path }}/../infra-stack/files/knaster-drop-stale-cache.py"
|
||
dest: /usr/local/sbin/knaster-drop-stale-cache
|
||
mode: "0755"
|
||
|
||
- name: Schutz gegen veralteten Seiten-Cache – systemd-Dienst vor containerd/docker
|
||
ansible.builtin.copy:
|
||
src: "{{ role_path }}/../infra-stack/files/knaster-drop-stale-cache.service"
|
||
dest: /etc/systemd/system/knaster-drop-stale-cache.service
|
||
mode: "0644"
|
||
register: dropcache_unit
|
||
|
||
- name: Schutz gegen veralteten Seiten-Cache – aktivieren
|
||
ansible.builtin.systemd:
|
||
name: knaster-drop-stale-cache.service
|
||
enabled: true
|
||
daemon_reload: "{{ dropcache_unit.changed }}"
|
||
|
||
# --- Firewall: ufw für SSH und alles Übrige, DOCKER-USER für den veröffentlichten Port 8888 ---
|
||
- name: ufw ohne IPv6 (unprivilegierter LXC, nur IPv4 im LAN)
|
||
ansible.builtin.lineinfile:
|
||
path: /etc/default/ufw
|
||
regexp: "^IPV6="
|
||
line: IPV6=no
|
||
|
||
- name: ufw – SSH aus dem LAN erlauben (vor dem Einschalten, sonst sperrt sich Ansible aus)
|
||
community.general.ufw:
|
||
rule: allow
|
||
from_ip: "{{ hindsight_ssh_from }}"
|
||
port: "22"
|
||
proto: tcp
|
||
comment: ssh-lan
|
||
|
||
- name: ufw – eingehend sonst ablehnen, ausgehend erlauben
|
||
community.general.ufw:
|
||
direction: "{{ item.direction }}"
|
||
default: "{{ item.policy }}"
|
||
loop:
|
||
- {direction: incoming, policy: deny}
|
||
- {direction: outgoing, policy: allow}
|
||
|
||
- name: ufw einschalten
|
||
community.general.ufw:
|
||
state: enabled
|
||
|
||
- name: DOCKER-USER-Regel für Port {{ hindsight_api_port }} – Skript
|
||
ansible.builtin.template:
|
||
src: knaster-hindsight-fw.sh.j2
|
||
dest: /usr/local/sbin/knaster-hindsight-fw
|
||
mode: "0755"
|
||
register: fw_script
|
||
|
||
- name: DOCKER-USER-Regel – systemd-Dienst nach Docker
|
||
ansible.builtin.copy:
|
||
src: knaster-hindsight-fw.service
|
||
dest: /etc/systemd/system/knaster-hindsight-fw.service
|
||
mode: "0644"
|
||
register: fw_unit
|
||
|
||
- name: DOCKER-USER-Regel – aktivieren und anwenden
|
||
ansible.builtin.systemd:
|
||
name: knaster-hindsight-fw.service
|
||
enabled: true
|
||
state: "{{ 'restarted' if (fw_script.changed or fw_unit.changed) else 'started' }}"
|
||
daemon_reload: "{{ fw_unit.changed }}"
|
||
|
||
# --- Stack ---
|
||
- name: Image ziehen (gepinnter Tag)
|
||
ansible.builtin.command: >
|
||
docker compose -p {{ hindsight_compose_project }} --env-file {{ hindsight_state_dir }}/.env
|
||
-f {{ hindsight_compose_file }} pull --quiet
|
||
changed_when: false
|
||
|
||
- name: Stack starten
|
||
ansible.builtin.command: >
|
||
docker compose -p {{ hindsight_compose_project }} --env-file {{ hindsight_state_dir }}/.env
|
||
-f {{ hindsight_compose_file }} up -d --remove-orphans
|
||
register: compose_up
|
||
changed_when: compose_up.stderr is search('Container \S+\s+(Created|Recreated|Started)')
|
||
|
||
- name: Warten, bis das Gate gesund ist (/health ist ohne Schlüssel erreichbar)
|
||
ansible.builtin.uri:
|
||
url: "http://127.0.0.1:{{ hindsight_api_port }}/health"
|
||
status_code: 200
|
||
register: hs_health
|
||
until: hs_health.status == 200
|
||
retries: 30
|
||
delay: 10
|
||
|
||
- name: Cron (Jobs im Lastfenster, Backup)
|
||
ansible.builtin.cron:
|
||
name: "{{ item.name }}"
|
||
minute: "{{ item.minute }}"
|
||
hour: "{{ item.hour }}"
|
||
job: "{{ item.job }}"
|
||
loop: "{{ hindsight_cron }}"
|
||
loop_control:
|
||
label: "{{ item.name }}"
|