#!/bin/sh # One reviewed inventory report from the current Docker context. set +x set +a set -eu umask 077 fail() { printf '%s\n' "$1" >&2 exit 1 } cleanup() { if [ -n "$terminal_mode" ]; then stty "$terminal_mode" <&3 || : printf '\n' >&3 || : fi [ -z "$work" ] || rm -rf "$work" } draw_choices() { for number in 1 2 3; do case "$number" in 1) label='Send report' ;; 2) label='View full JSON' ;; 3) label='Cancel' ;; esac marker=' ' if [ "$choice" = "$number" ]; then marker='>'; fi printf '%s %s\n' "$marker" "$label" >&3 done } read_key() { # dd reads exactly one byte from the terminal, even when several keys are queued. # The suffix preserves an Enter byte across shell command substitution. key=$(dd bs=1 count=1 <&3 2>/dev/null && printf '.') || fail 'Could not read the terminal.' key=${key%.} # Handle the interrupt byte here so Ctrl-C also works between short-lived key readers. if [ "$key" = "$ctrl_c" ]; then exit 130; fi } choose_with_arrows() { choice=1 escape=$(printf '\033') ctrl_c=$(printf '\003') ctrl_d=$(printf '\004') cr=$(printf '\r') terminal_mode=$(stty -g <&3) stty -icanon -echo -isig min 1 time 0 <&3 printf '\nUse ↑/↓ to choose, then press Enter.\n' >&3 draw_choices while :; do read_key case "$key" in ' '|"$cr") break ;; ''|"$ctrl_d") choice=3; break ;; "$escape") # Read CSI and application-mode arrow keys. Escape on its own cancels. stty min 0 time 3 <&3 read_key case "$key" in '') choice=3; break ;; '['|O) read_key case "$key" in A) choice=$(( (choice + 1) % 3 + 1 )) ;; B) choice=$(( choice % 3 + 1 )) ;; esac ;; esac stty min 1 time 0 <&3 printf '\033[3A' >&3 draw_choices ;; esac done stty "$terminal_mode" <&3 terminal_mode='' } choose_by_number() { printf '\n1) Send report (default)\n2) View full JSON\n3) Cancel\n' >&3 while :; do printf 'Choice [1]: ' >&3 if ! IFS= read -r choice <&3; then choice=3; fi case "$choice" in '') choice=1; return ;; 1|2|3) return ;; *) printf 'Choose 1, 2, or 3.\n' >&3 ;; esac done } review_inventory() { printf '\nInventory preview\n' >&3 jq -r '"Containers: \(.containers | length) Complete: \(.collector.complete) Errors: \([.containers[] | select(.error)] | length)"' "$report" >&3 jq -Mac '.containers[] | {name,image:.image_ref} + (if .error then {error} else {} end)' "$report" >&3 if jq -e '.collector.complete == false' "$report" >/dev/null; then printf 'Some inventory could not be collected. You can review the partial report or cancel.\n' >&3 fi while :; do case "${TERM:-}" in xterm*|screen*|tmux*|rxvt*|vt100*|vt220*|linux|ansi|alacritty*|foot*|kitty*|st-*) choose_with_arrows ;; *) choose_by_number ;; esac case "$choice" in 1) return ;; 2) jq -Ma . "$report" >&3 ;; 3) printf 'Cancelled. No report was sent.\n' >&3; exit 0 ;; esac done } read_host_token() { terminal_mode=$(stty -g <&3) stty -echo <&3 printf 'Host token (input hidden): ' >&3 IFS= read -r host_token <&3 || host_token='' stty "$terminal_mode" <&3 terminal_mode='' printf '\n' >&3 case "$host_token" in ''|*[!A-Za-z0-9_-]*) fail 'Enter the host token from the Zero Daddy host page.' ;; esac [ "${#host_token}" -eq 43 ] || fail 'Enter the full 43-character host token.' # The private header file keeps the bearer value out of process arguments. cat > "$work/header" <
/dev/null 2>&1 || fail "Setup requires $tool on this Docker host." done ( : <> /dev/tty ) 2>/dev/null || fail 'Run setup in an interactive terminal on the Docker host.' exec 3<>/dev/tty trap cleanup 0 trap 'exit 130' INT trap 'exit 129' HUP trap 'exit 143' TERM docker ps -q >/dev/null 2>&1 || fail 'Docker is unavailable. Run setup with access to the intended Docker context.' work=$(mktemp -d "${TMPDIR:-/tmp}/zd-setup.XXXXXXXX") report="$work/report.json" collection_url='https://app.zero-daddy.com/collector/zd-collect.sh' report_url='https://app.zero-daddy.com/v1/report' printf 'Reading inventory from the current Docker context…\n' >&3 code=$(curl -q --proto '=https' --silent --show-error --max-time 30 --output "$work/collector.sh" \ --write-out '%{http_code}' "$collection_url") || fail 'Could not download the collector.' [ "$code" = 200 ] || fail "Collector download returned HTTP $code." sh "$work/collector.sh" --dry-run > "$report" || fail 'The collector could not generate an inventory.' [ "$(wc -c < "$report")" -le 2097152 ] || fail 'This report exceeds the 2 MiB limit. Reduce the inventory and run setup again.' jq -se 'length == 1 and (.[0] | .schema == "zd-report/1" and (.containers | type == "array") and (.collector.complete | type == "boolean"))' "$report" >/dev/null || fail 'The collector returned an invalid report.' review_inventory read_host_token code=$(curl -q --proto '=https' --silent --show-error --max-time 30 --output "$work/receipt" \ --write-out '%{http_code}' --header "@$work/header" --header 'Content-Type: application/json' \ --data-binary "@$report" "$report_url") || fail 'Could not confirm submission. Check the host page for a new report.' case "$code" in 202) ;; 401) fail 'HTTP 401. Copy the current token for this host and run setup again.' ;; *) fail "Report submission returned HTTP $code. Check the host page before retrying." ;; esac receipt=$(jq -er '.report_id | select(type == "string" and test("^[a-f0-9]{32}$"))' "$work/receipt") || fail 'HTTP 202 received without a valid report ID. Check the host page for the report.' printf 'Report accepted (HTTP 202). Report ID: %s\n' "$receipt" >&3 } # A partial script download cannot start collection or submission. main "$@"