selfhostedops-stack/managers/komodo.sh

47 lines
4.5 KiB
Bash

#!/usr/bin/env bash
# ep18 Komodo-Messung über die API. Aufruf: komodo.sh login USER PW|see|deploy|redeploy|updates|adopt
. /root/ep18/lib.sh; . /root/ep18/testcompose.sh
U=http://127.0.0.1:9120
K() { local p=$1; shift; curl -s -X POST "$U/$p" -H "authorization: $(cat /root/ep18/.ktok 2>/dev/null)" -H 'content-type: application/json' "$@"; }
case $1 in
login) R=$(curl -s -X POST $U/auth/login/LoginLocalUser -H 'content-type: application/json' -d "{\"username\":\"$2\",\"password\":\"$3\"}"); echo "$R" | jq -r '.data.jwt // empty' > /root/ep18/.ktok; chmod 600 /root/ep18/.ktok
echo "$R" | jq -c 'if .data.jwt then {type, jwt: ("<"+(.data.jwt|length|tostring)+" chars>")} else . end' ;;
see)
S=$(K read/ListServers -d '{}' | jq -r '.[0].id'); echo "server: $(K read/ListServers -d '{}' | jq -c '[.[]|{name, state: .info.state, version: .info.version}]')"
echo '$ /read/ListDockerContainers (Komodo liefert hier KEINE compose-Labels, nur Name und Zustand)'; K read/ListDockerContainers -d "{\"server\":\"$S\"}" | jq -r '.[] | [.name, .state] | @tsv' | sort | column -t
echo "containers seen: $(K read/ListDockerContainers -d "{\"server\":\"$S\"}" | jq length), docker ps -a says: $(docker ps -aq | wc -l)"
echo '$ /read/ListComposeProjects'; K read/ListComposeProjects -d "{\"server\":\"$S\"}" | jq -c '.[] | {name, status, compose_files}'
echo '$ /read/ListStacks (Komodo-Ressourcen)'; K read/ListStacks -d '{}' | jq -c '[.[] | {name, state: .info.state, project_missing: .info.project_missing}]'
;;
deploy)
S=$(K read/ListServers -d '{}' | jq -r '.[0].id'); D=$(mktemp -d); writetest $D 0
echo '$ /write/CreateStack ep18-whoami (Compose im UI-Editor gespeichert)'
jq -n --rawfile c $D/compose.yaml --arg s "$S" '{name:"ep18-whoami", config:{server_id:$s, file_contents:$c, auto_pull:false}}' | K write/CreateStack -d @- | jq -c '{id: ._id["$oid"], name, server: .config.server_id}'
echo '$ /execute/DeployStack'; export T0=$(now); K execute/DeployStack -d '{"stack":"ep18-whoami"}' | jq -c '{id: ._id["$oid"], operation, status, success}'; echo " API returned after $(el $T0 $(now)) s"
watchrun ep18-whoami 0
;;
redeploy)
D=$(mktemp -d); ID=$(K read/GetStack -d '{"stack":"ep18-whoami"}' | jq -r '._id["$oid"]')
for n in 1 2 3; do writetest $D $n
echo "\$ /write/UpdateStack (file_contents RUN=$n), dann /execute/DeployStack"
export T0=$(now); jq -n --rawfile c $D/compose.yaml --arg id "$ID" '{id:$id, config:{file_contents:$c}}' | K write/UpdateStack -d @- -o /dev/null -w " UpdateStack HTTP %{http_code} %{time_total} s\n"
K execute/DeployStack -d '{"stack":"ep18-whoami"}' -o /dev/null -w " DeployStack HTTP %{http_code}, API returned after %{time_total} s\n"
watchrun ep18-whoami $n; sleep 3; done
;;
updates)
for t in latest v1.11.0 v1.12.0 v1.10.4; do echo "registry traefik/whoami:$t = $(regdigest traefik/whoami $t)"; done
for c in ep18-whoami-pinned-1 ep18-whoami-floating-1; do echo "$c runs image $(docker inspect -f '{{.Image}}' $c | cut -c1-19)"; done
echo '$ /write/CheckStackForUpdate ep18-whoami'; K write/CheckStackForUpdate -d '{"stack":"ep18-whoami"}' | jq -c '.services[]? // .'
;;
adopt)
S=$(K read/ListServers -d '{}' | jq -r '.[0].id')
echo '# Versuch, den bestehenden selfhostedops-stack zu uebernehmen, ohne die compose.yml umzuziehen:'
echo '# Komodo-Stack im Modus "Files on Server" (files_on_host), run_directory auf den vorhandenen Ordner,'
echo '# project_name = selfhostedops-stack, damit docker compose -p denselben Namen nutzt.'
jq -n --arg s "$S" '{name:"adopt-sho", config:{server_id:$s, files_on_host:true, run_directory:"/opt/selfhostedops-stack", file_paths:["compose.yml"], project_name:"selfhostedops-stack", env_file_path:".env"}}' | K write/CreateStack -d @- | jq -c '{id: ._id["$oid"], name, files_on_host: .config.files_on_host, run_directory: .config.run_directory, project_name: .config.project_name}'
echo '$ /read/GetStack adopt-sho -> erkennt es die Services aus der vorhandenen compose.yml?'
K read/GetStack -d '{"stack":"adopt-sho"}' | jq -c '{state: .info.state, status: .info.status, services: ([.info.deployed_services // [] | .[]?.service] ), missing_files: .info.missing_files, deployed_contents_len: ((.info.deployed_contents // [])|length)}'
echo '# Nur lesen/anzeigen in diesem Lab, KEIN DeployStack auf den echten Stack. Danach wieder entfernt:'
ID=$(K read/GetStack -d '{"stack":"adopt-sho"}' | jq -r '._id["$oid"]'); K write/DeleteStack -d "{\"id\":\"$ID\"}" -o /dev/null -w "DeleteStack HTTP %{http_code}\n"
;;
esac