treefmt v2.6.0traversed 442 files emitted 408 files for processing formatted 408 files (10 changed) in 1.363s M docs/gen-ssh-config.sh M docs/generate-host-info.sh M docs/generate-lldp-graph.sh M docs/get-lldp-neighbors.sh M modules/db-sync/scripts/db-freeze.sh M modules/db-sync/scripts/db-sync-all.sh M modules/db-sync/scripts/db-sync-status.sh M modules/db-sync/scripts/db-sync-stop.sh M modules/db-sync/scripts/db-thaw.sh M terraform/authentik/import-existing.sh diff --git a/docs/gen-ssh-config.sh b/docs/gen-ssh-config.sh index 575c5a6..63a3489 100755 --- a/docs/gen-ssh-config.sh +++ b/docs/gen-ssh-config.sh @@ -4,9 +4,9 @@ set -euo pipefail user=${1:-} if [[ -z $user ]]; then - echo "Missing username" - echo "Usage: $(basename "$0") username" - exit 1 + echo "Missing username" + echo "Usage: $(basename "$0") username" + exit 1 fi # GitHub directory pages change independently of this script; use the stable API. @@ -28,20 +28,20 @@ Host eta EOF curl -fsSL \ - -H 'Accept: application/vnd.github+json' \ - -H 'X-GitHub-Api-Version: 2022-11-28' \ - 'https://api.github.com/repos/SBEE-Lab/infra/contents/hosts?ref=main' \ - >"$tempfile" + -H 'Accept: application/vnd.github+json' \ + -H 'X-GitHub-Api-Version: 2022-11-28' \ + 'https://api.github.com/repos/SBEE-Lab/infra/contents/hosts?ref=main' \ + >"$tempfile" machines=$(grep -oE '"name"[[:space:]]*:[[:space:]]*"[^"]+\.nix"' "$tempfile" | - sed -E 's/.*"([^"]+)\.nix"/\1/' | - sort -u) + sed -E 's/.*"([^"]+)\.nix"/\1/' | + sort -u) while read -r machine; do - if [[ -z $machine || $machine == "eta" ]]; then - continue - fi + if [[ -z $machine || $machine == "eta" ]]; then + continue + fi - cat <&2 - exit 1 + echo "Usage: $(basename "$0") host" >&2 + exit 1 fi target=$1 diff --git a/docs/generate-lldp-graph.sh b/docs/generate-lldp-graph.sh index 7fbaa06..0a099a6 100755 --- a/docs/generate-lldp-graph.sh +++ b/docs/generate-lldp-graph.sh @@ -2,8 +2,8 @@ set -euo pipefail if ((BASH_VERSINFO[0] < 4)); then - echo "$(basename "$0") requires Bash 4 or newer" >&2 - exit 1 + echo "$(basename "$0") requires Bash 4 or newer" >&2 + exit 1 fi report="../graph.md" @@ -11,60 +11,60 @@ url="https://github.com/SBEE-Lab/infra/blob/main/docs/hosts" declare -A interfaces { - echo '```mermaid' - echo ' graph TD' + echo '```mermaid' + echo ' graph TD' - for host_file in *; do - [[ -f $host_file ]] || continue - while read -r neighbor; do - [[ -z $neighbor ]] && continue - host_interface=$(echo "$neighbor" | cut -d' ' -f1) - link_speed=$(echo "$neighbor" | cut -d' ' -f2) - neighbor_interface=$(echo "$neighbor" | cut -d' ' -f3 | tr -cd '[:alnum:]._-') - neighbor_name=$(echo "$neighbor" | cut -d' ' -f4 | tr -cd '[:alnum:]._-') + for host_file in *; do + [[ -f $host_file ]] || continue + while read -r neighbor; do + [[ -z $neighbor ]] && continue + host_interface=$(echo "$neighbor" | cut -d' ' -f1) + link_speed=$(echo "$neighbor" | cut -d' ' -f2) + neighbor_interface=$(echo "$neighbor" | cut -d' ' -f3 | tr -cd '[:alnum:]._-') + neighbor_name=$(echo "$neighbor" | cut -d' ' -f4 | tr -cd '[:alnum:]._-') - interfaces[$host_file]+=" $host_interface" - interfaces[$neighbor_name]+=" $neighbor_interface" + interfaces[$host_file]+=" $host_interface" + interfaces[$neighbor_name]+=" $neighbor_interface" - echo " ${host_file}.${host_interface}[\"$host_interface\"]-- $link_speed ---${neighbor_name}.${neighbor_interface}[\"$neighbor_interface\"]" - done <"$host_file" - done + echo " ${host_file}.${host_interface}[\"$host_interface\"]-- $link_speed ---${neighbor_name}.${neighbor_interface}[\"$neighbor_interface\"]" + done <"$host_file" + done - for host in "${!interfaces[@]}"; do - interfaces[$host]=$(xargs -n 1 <<<"${interfaces[$host]}" | sort -u | xargs) - echo " subgraph $host" - for interface in ${interfaces[$host]}; do - echo " ${host}.${interface}[\"$interface\"]" - done - echo " end" - done + for host in "${!interfaces[@]}"; do + interfaces[$host]=$(xargs -n 1 <<<"${interfaces[$host]}" | sort -u | xargs) + echo " subgraph $host" + for interface in ${interfaces[$host]}; do + echo " ${host}.${interface}[\"$interface\"]" + done + echo " end" + done - for host in "${!interfaces[@]}"; do - [[ -f "../$host.md" ]] || continue - for interface in ${interfaces[$host]}; do - echo " click ${host}.${interface} \"$url/$host.md\" \"$host\"" - done - done + for host in "${!interfaces[@]}"; do + [[ -f "../$host.md" ]] || continue + for interface in ${interfaces[$host]}; do + echo " click ${host}.${interface} \"$url/$host.md\" \"$host\"" + done + done - echo '```' + echo '```' } >"$report" # Remove inverse duplicate links. while true; do - duplicate="" - while read -r link; do - interface_1=$(awk '{print $1}' <<<"$link" | rev | cut -c 3- | rev) - link_speed=$(awk '{print $2}' <<<"$link") - interface_2=$(awk '{print $3}' <<<"$link" | cut -c 4-) - candidate="$interface_2-- $link_speed ---$interface_1" - if grep -q -F "$candidate" "$report"; then - duplicate=$candidate - break - fi - done < <(grep -E -- '-- [[:digit:]]+[GM] ---' "$report") + duplicate="" + while read -r link; do + interface_1=$(awk '{print $1}' <<<"$link" | rev | cut -c 3- | rev) + link_speed=$(awk '{print $2}' <<<"$link") + interface_2=$(awk '{print $3}' <<<"$link" | cut -c 4-) + candidate="$interface_2-- $link_speed ---$interface_1" + if grep -q -F "$candidate" "$report"; then + duplicate=$candidate + break + fi + done < <(grep -E -- '-- [[:digit:]]+[GM] ---' "$report") - [[ -n $duplicate ]] || break - tempfile=$(mktemp -t sbee-lldp-graph.XXXXXX) - awk -v pattern="$duplicate" 'index($0, pattern) == 0' "$report" >"$tempfile" - mv "$tempfile" "$report" + [[ -n $duplicate ]] || break + tempfile=$(mktemp -t sbee-lldp-graph.XXXXXX) + awk -v pattern="$duplicate" 'index($0, pattern) == 0' "$report" >"$tempfile" + mv "$tempfile" "$report" done diff --git a/docs/get-lldp-neighbors.sh b/docs/get-lldp-neighbors.sh index 1396412..dd84cfb 100755 --- a/docs/get-lldp-neighbors.sh +++ b/docs/get-lldp-neighbors.sh @@ -2,8 +2,8 @@ set -euo pipefail if [[ -z ${1:-} ]]; then - echo "Usage: $(basename "$0") host" >&2 - exit 1 + echo "Usage: $(basename "$0") host" >&2 + exit 1 fi target=$1 @@ -12,17 +12,17 @@ report="$hostname" lldp=$(ssh "$target" -- networkctl lldp --json=short) while IFS=$' ' read -r interface neighbor_interface neighbor_name; do - [[ -z $interface ]] && continue - link_speed=$(ssh "$target" -- cat "/sys/class/net/$interface/speed" = 1000)); then - speed="$((link_speed / 1000))G" - else - speed="${link_speed}M" - fi - echo "$interface $speed $neighbor_interface $neighbor_name" + if ((link_speed >= 1000)); then + speed="$((link_speed / 1000))G" + else + speed="${link_speed}M" + fi + echo "$interface $speed $neighbor_interface $neighbor_name" done < <( - python3 -c ' + python3 -c ' import json import sys diff --git a/modules/db-sync/scripts/db-freeze.sh b/modules/db-sync/scripts/db-freeze.sh index 6c31d2c..b709211 100644 --- a/modules/db-sync/scripts/db-freeze.sh +++ b/modules/db-sync/scripts/db-freeze.sh @@ -1,24 +1,24 @@ #!/usr/bin/env bash set -euo pipefail if [[ $# -ne 2 ]]; then - echo "Usage: db-freeze " >&2 - exit 1 + echo "Usage: db-freeze " >&2 + exit 1 fi db="$1" tag="$2" src="@dbRoot@/$db" dst="@dbRoot@/$db.frozen.$tag" [[ -d $src ]] || { - echo "Not found: $db" >&2 - exit 1 + echo "Not found: $db" >&2 + exit 1 } [[ ! -e $dst ]] || { - echo "Already exists: $dst" >&2 - exit 1 + echo "Already exists: $dst" >&2 + exit 1 } if systemctl is-active --quiet "db-sync-$db.service" 2>/dev/null; then - echo "Sync in progress for $db, abort" >&2 - exit 1 + echo "Sync in progress for $db, abort" >&2 + exit 1 fi cp --reflink=auto -a "$src" "$dst" echo "Frozen: $dst" diff --git a/modules/db-sync/scripts/db-sync-all.sh b/modules/db-sync/scripts/db-sync-all.sh index 442bc74..9d558f0 100644 --- a/modules/db-sync/scripts/db-sync-all.sh +++ b/modules/db-sync/scripts/db-sync-all.sh @@ -1,8 +1,8 @@ #!/usr/bin/env bash set -euo pipefail for timer in $(systemctl list-units 'db-sync-*.timer' --plain --no-legend | awk '{print $1}'); do - svc="${timer%.timer}.service" - echo "Starting $svc ..." - systemctl start --no-block "$svc" + svc="${timer%.timer}.service" + echo "Starting $svc ..." + systemctl start --no-block "$svc" done echo "All sync services triggered (running in background)" diff --git a/modules/db-sync/scripts/db-sync-status.sh b/modules/db-sync/scripts/db-sync-status.sh index d901078..13ece0c 100644 --- a/modules/db-sync/scripts/db-sync-status.sh +++ b/modules/db-sync/scripts/db-sync-status.sh @@ -5,14 +5,14 @@ echo "" printf "%-40s %-10s %-10s %s\n" "SERVICE" "STATE" "RESULT" "LAST RUN" printf "%-40s %-10s %-10s %s\n" "-------" "-----" "------" "--------" for svc in $(systemctl list-units 'db-sync-*.service' --plain --no-legend --all | awk '{print $1}' | grep -v notify); do - result=$(systemctl show "$svc" -p Result --value) - active=$(systemctl show "$svc" -p ActiveState --value) - started=$(systemctl show "$svc" -p ExecMainStartTimestamp --value) - if [[ -z $started ]]; then - started="never" - result="-" - fi - printf "%-40s %-10s %-10s %s\n" "$svc" "$active" "$result" "$started" + result=$(systemctl show "$svc" -p Result --value) + active=$(systemctl show "$svc" -p ActiveState --value) + started=$(systemctl show "$svc" -p ExecMainStartTimestamp --value) + if [[ -z $started ]]; then + started="never" + result="-" + fi + printf "%-40s %-10s %-10s %s\n" "$svc" "$active" "$result" "$started" done echo "" echo "Disk usage:" diff --git a/modules/db-sync/scripts/db-sync-stop.sh b/modules/db-sync/scripts/db-sync-stop.sh index 5fe0b69..c2035a8 100644 --- a/modules/db-sync/scripts/db-sync-stop.sh +++ b/modules/db-sync/scripts/db-sync-stop.sh @@ -1,17 +1,17 @@ #!/usr/bin/env bash set -euo pipefail if [[ $# -eq 0 ]]; then - # Stop all running sync services - for svc in $(systemctl list-units 'db-sync-*.service' --plain --no-legend --state=activating,running | awk '{print $1}'); do - echo "Stopping $svc ..." - systemctl stop "$svc" - done - echo "All sync services stopped" + # Stop all running sync services + for svc in $(systemctl list-units 'db-sync-*.service' --plain --no-legend --state=activating,running | awk '{print $1}'); do + echo "Stopping $svc ..." + systemctl stop "$svc" + done + echo "All sync services stopped" else - # Stop specific database(s) - for db in "$@"; do - svc="db-sync-${db}.service" - echo "Stopping $svc ..." - systemctl stop "$svc" - done + # Stop specific database(s) + for db in "$@"; do + svc="db-sync-${db}.service" + echo "Stopping $svc ..." + systemctl stop "$svc" + done fi diff --git a/modules/db-sync/scripts/db-thaw.sh b/modules/db-sync/scripts/db-thaw.sh index bf0ab2f..f7a388b 100644 --- a/modules/db-sync/scripts/db-thaw.sh +++ b/modules/db-sync/scripts/db-thaw.sh @@ -1,15 +1,15 @@ #!/usr/bin/env bash set -euo pipefail if [[ $# -ne 2 ]]; then - echo "Usage: db-thaw " >&2 - exit 1 + echo "Usage: db-thaw " >&2 + exit 1 fi db="$1" tag="$2" target="@dbRoot@/$db.frozen.$tag" [[ -d $target ]] || { - echo "Not found: $target" >&2 - exit 1 + echo "Not found: $target" >&2 + exit 1 } rm -rf "$target" echo "Thawed: $db.frozen.$tag" diff --git a/terraform/authentik/import-existing.sh b/terraform/authentik/import-existing.sh index 0b6c4d5..fa14c4d 100755 --- a/terraform/authentik/import-existing.sh +++ b/terraform/authentik/import-existing.sh @@ -6,83 +6,83 @@ AK_TOKEN=${AK_TOKEN:-$(sops -d --extract '["AUTHENTIK_TOKEN"]' secrets.yaml)} TG=${TG:-terragrunt} api() { - curl -sSf -H "Authorization: Bearer ${AK_TOKEN}" "$AK/api/v3/$1" + curl -sSf -H "Authorization: Bearer ${AK_TOKEN}" "$AK/api/v3/$1" } state_has() { - $TG state show "$1" >/dev/null 2>&1 + $TG state show "$1" >/dev/null 2>&1 } import_if_missing() { - local address=$1 - local id=$2 + local address=$1 + local id=$2 - if [ -z "$id" ] || [ "$id" = "null" ]; then - echo "skip $address: no remote object" - return 0 - fi + if [ -z "$id" ] || [ "$id" = "null" ]; then + echo "skip $address: no remote object" + return 0 + fi - if state_has "$address"; then - echo "ok $address already imported" - return 0 - fi + if state_has "$address"; then + echo "ok $address already imported" + return 0 + fi - echo "import $address <- $id" - $TG import "$address" "$id" + echo "import $address <- $id" + $TG import "$address" "$id" } json_quote() { - jq -Rn --arg v "$1" '$v' + jq -Rn --arg v "$1" '$v' } group_id_by_name() { - local name=$1 - api "core/groups/?search=$(jq -rn --arg v "$name" '$v|@uri')&page_size=100" | - jq -r --arg name "$name" '.results[] | select(.name == $name) | .pk' | - head -n1 + local name=$1 + api "core/groups/?search=$(jq -rn --arg v "$name" '$v|@uri')&page_size=100" | + jq -r --arg name "$name" '.results[] | select(.name == $name) | .pk' | + head -n1 } user_id_by_username() { - local username=$1 - api "core/users/?search=$(jq -rn --arg v "$username" '$v|@uri')&page_size=100" | - jq -r --arg username "$username" '.results[] | select(.username == $username) | .pk' | - head -n1 + local username=$1 + api "core/users/?search=$(jq -rn --arg v "$username" '$v|@uri')&page_size=100" | + jq -r --arg username "$username" '.results[] | select(.username == $username) | .pk' | + head -n1 } proxy_id_by_external_host() { - local external_host=$1 - api "providers/proxy/?page_size=100" | - jq -r --arg external_host "$external_host" '.results[] | select(.external_host == $external_host) | .pk' | - head -n1 + local external_host=$1 + api "providers/proxy/?page_size=100" | + jq -r --arg external_host "$external_host" '.results[] | select(.external_host == $external_host) | .pk' | + head -n1 } app_uuid_by_slug() { - local slug=$1 - curl -sS -H "Authorization: Bearer ${AK_TOKEN}" "$AK/api/v3/core/applications/${slug}/" 2>/dev/null | - jq -r '.pk // empty' 2>/dev/null || true + local slug=$1 + curl -sS -H "Authorization: Bearer ${AK_TOKEN}" "$AK/api/v3/core/applications/${slug}/" 2>/dev/null | + jq -r '.pk // empty' 2>/dev/null || true } oauth2_id_by_name() { - local name=$1 - api "providers/oauth2/?search=$(jq -rn --arg v "$name" '$v|@uri')&page_size=100" | - jq -r --arg name "$name" '.results[] | select(.name == $name) | .pk' | - head -n1 + local name=$1 + api "providers/oauth2/?search=$(jq -rn --arg v "$name" '$v|@uri')&page_size=100" | + jq -r --arg name "$name" '.results[] | select(.name == $name) | .pk' | + head -n1 } scope_mapping_id_by_name() { - local name=$1 - api "propertymappings/provider/scope/?search=$(jq -rn --arg v "$name" '$v|@uri')&page_size=100" | - jq -r --arg name "$name" '.results[] | select(.name == $name) | .pk' | - head -n1 + local name=$1 + api "propertymappings/provider/scope/?search=$(jq -rn --arg v "$name" '$v|@uri')&page_size=100" | + jq -r --arg name "$name" '.results[] | select(.name == $name) | .pk' | + head -n1 } outpost_id=$(api 'outposts/instances/?search=authentik%20Embedded%20Outpost&page_size=100' | - jq -r '.results[] | select(.name == "authentik Embedded Outpost") | .pk' | - head -n1) + jq -r '.results[] | select(.name == "authentik Embedded Outpost") | .pk' | + head -n1) # Keep keys in sync with local.groups in groups.tf. while IFS='|' read -r key name; do - import_if_missing "authentik_group.group[$(json_quote "$key")]" "$(group_id_by_name "$name")" + import_if_missing "authentik_group.group[$(json_quote "$key")]" "$(group_id_by_name "$name")" done <<'EOF' authentik_admins|authentik Admins sjanglab_admins|sjanglab-admins @@ -93,45 +93,45 @@ sjanglab_pending|sjanglab-pending EOF for username in akadmin terraform; do - import_if_missing "authentik_user.service[$(json_quote "$username")]" "$(user_id_by_username "$username")" + import_if_missing "authentik_user.service[$(json_quote "$username")]" "$(user_id_by_username "$username")" done while IFS='|' read -r app external_host; do - provider_id=$(proxy_id_by_external_host "$external_host") - app_id=$(app_uuid_by_slug "$app") - - import_if_missing "authentik_provider_proxy.app[$(json_quote "$app")]" "$provider_id" - if [ -n "$app_id" ]; then - import_if_missing "authentik_application.app[$(json_quote "$app")]" "$app" - else - echo "skip application for $app: no remote object" - fi - - if [ -n "$outpost_id" ] && [ -n "$provider_id" ]; then - import_if_missing "authentik_outpost_provider_attachment.embedded[$(json_quote "$app")]" "${outpost_id}:${provider_id}" - else - echo "skip outpost attachment for $app: no outpost/provider" - fi + provider_id=$(proxy_id_by_external_host "$external_host") + app_id=$(app_uuid_by_slug "$app") + + import_if_missing "authentik_provider_proxy.app[$(json_quote "$app")]" "$provider_id" + if [ -n "$app_id" ]; then + import_if_missing "authentik_application.app[$(json_quote "$app")]" "$app" + else + echo "skip application for $app: no remote object" + fi + + if [ -n "$outpost_id" ] && [ -n "$provider_id" ]; then + import_if_missing "authentik_outpost_provider_attachment.embedded[$(json_quote "$app")]" "${outpost_id}:${provider_id}" + else + echo "skip outpost attachment for $app: no outpost/provider" + fi done <<'EOF' n8n|https://n8n.sjanglab.org logging|https://logging.sjanglab.org EOF while IFS='|' read -r key name; do - import_if_missing "authentik_property_mapping_provider_scope.oidc_group[$(json_quote "$key")]" "$(scope_mapping_id_by_name "$name")" + import_if_missing "authentik_property_mapping_provider_scope.oidc_group[$(json_quote "$key")]" "$(scope_mapping_id_by_name "$name")" done <<'EOF' headscale|headscale-groups nextcloud|nextcloud-groups EOF while IFS='|' read -r key name slug; do - import_if_missing "authentik_provider_oauth2.oidc[$(json_quote "$key")]" "$(oauth2_id_by_name "$name")" - app_id=$(app_uuid_by_slug "$slug") - if [ -n "$app_id" ]; then - import_if_missing "authentik_application.oidc[$(json_quote "$key")]" "$slug" - else - echo "skip application for $slug: no remote object" - fi + import_if_missing "authentik_provider_oauth2.oidc[$(json_quote "$key")]" "$(oauth2_id_by_name "$name")" + app_id=$(app_uuid_by_slug "$slug") + if [ -n "$app_id" ]; then + import_if_missing "authentik_application.oidc[$(json_quote "$key")]" "$slug" + else + echo "skip application for $slug: no remote object" + fi done <<'EOF' headscale|Headscale|headscale nextcloud|Nextcloud|nextcloud