Repository navigation
Expand file tree
/
Copy pathinfra
More file actions
executable file
·188 lines (175 loc) · 7.04 KB
/
Copy pathinfra
File metadata and controls
executable file
·188 lines (175 loc) · 7.04 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
#!/usr/bin/env bash
# infra local identity, repo state, available command categories
# infra CATEGORY commands for an available layer or environment group
# infra env available environments
# infra env NAME commands for one environment
# infra tailnet local identity, exit node, and peers
# infra joceline | infra maeve test a collaborator's actual SSH login and welcome
# infra devsync ENV [PATH...] laptop -> ~/inbox on dev-ENV (no PATH: clipboard image)
# infra help TARGET the full docs for one make target (comment block + recipe)
#
# Commands come from the Makefiles: every documented target has
# `target: ## [layer] one-line summary`. Layers: setup, foundation, dev.
set -uo pipefail
INFRA_DIR="${INFRA_DIR:-$HOME/infrastructure}"
cd "$INFRA_DIR" 2>/dev/null || { echo "repo not found at $INFRA_DIR (set INFRA_DIR)" >&2; exit 1; }
# targets LAYER FILE... — "make target summary" lines for one layer.
targets() {
local layer="$1"; shift
awk -F' ## ' -v layer="$layer" '
/^[a-z-]+:.* ## \[/ {
split($1, t, ":"); match($2, /^\[[a-z]+\] /)
if (substr($2, 2, RLENGTH - 3) == layer)
printf " make %-20s %s\n", t[1], substr($2, RLENGTH + 1)
}' "$@"
}
# includes FILE — the mk/*.mk files a Makefile includes (environments share
# mk/devbox.mk), so their targets are listed with the environment's.
includes() { sed -nE 's|^include \$\(ROOT\)/(mk/[a-z]+\.mk).*|\1|p' "$1"; }
# help TARGET FILE... — the comment block above the target, then the recipe.
help() {
local target="$1"; shift
awk -v target="$target" '
FNR == 1 { doc = "" }
/^#/ { doc = doc $0 "\n"; next }
$0 ~ "^" target ":" {
sub(/ ## \[[a-z]+\] /, " — "); print FILENAME ": " $0; print doc
while ((getline line) > 0 && line ~ /^\t/) print line
found = 1; exit
}
{ doc = "" }
END { if (!found) { print "no target " target; exit 1 } }
' "$@"
}
# category NAME — show commands only when this checkout provides them.
category() {
local name="$1" mk
case "$name" in
setup|foundation|dev) targets "$name" Makefile mk/common.mk ;;
*)
for mk in projects/"$name"/Makefile environments/"$name"/Makefile; do
[[ -f "$mk" ]] || continue
echo " cd $(dirname "$mk")"
targets setup "$mk" $(includes "$mk")
targets foundation "$mk" $(includes "$mk")
targets dev "$mk" $(includes "$mk")
done ;;
esac
}
environments() {
local mk name
for mk in projects/*/Makefile environments/*/Makefile; do
[[ -f "$mk" ]] || continue
name="$(basename "$(dirname "$mk")")"
echo " infra env $name"
done
}
tailnet() {
local ts state self exit_node
if ! command -v tailscale >/dev/null 2>&1; then
echo "tailnet: tailscale not installed"
return
fi
ts="$(tailscale status --json 2>/dev/null)" || ts='{}'
state="$(jq -r '.BackendState // "unknown"' <<<"$ts")"
self="$(jq -r '.Self.DNSName // .Self.HostName // "unknown"' <<<"$ts")"
exit_node="$(jq -r '.ExitNodeStatus.ID // empty' <<<"$ts")"
echo "tailnet: $state self=$self"
if [[ -n "$exit_node" ]]; then
jq -r --arg id "$exit_node" '.Peer // {} | .[] | select(.ID == $id) | "exit node: \(.HostName)"' <<<"$ts"
else
echo "exit node: none"
fi
if [[ "${1:-}" == peers ]]; then
jq -r '.Peer // {} | .[] | " peer: \(.HostName)=\(if .Online then "online" else "offline" end)"' <<<"$ts"
fi
}
# Prefer generated onboarding keys; laptops can use their downloaded copies.
collaborator_key() {
local name="$1" key
for key in "$HOME/.local/share/infra/onboarding/$name/$name-dev-us" "$HOME/.ssh/$name-dev-us"; do
[[ -r "$key" ]] && { echo "$key"; return; }
done
return 1
}
collaborator_help() {
local name="$1" key
key="$(collaborator_key "$name")" || key="$HOME/.ssh/$name-dev-us"
echo "infra $name — Test $name's SSH login, welcome, and shell on dev-us"
echo "Requires a Tailscale connection and $name's private key."
echo "Key: $key"
echo "Run infra $name; type exit to disconnect."
}
case "${1:-}" in
help)
if [[ "${2:-}" =~ ^(joceline|maeve)$ && -z "${3:-}" ]]; then collaborator_help "$2"; exit; fi
[[ -n "${2:-}" ]] || { echo "usage: infra help TARGET | infra help ENV TARGET" >&2; exit 1; }
if [[ -n "${3:-}" ]]; then mk=$(ls projects/$2/Makefile environments/$2/Makefile 2>/dev/null); help "$3" $mk $(includes $mk) mk/common.mk
else help "$2" Makefile mk/common.mk; fi
exit ;;
joceline|maeve)
name="$1"
key="$(collaborator_key "$name")" || { echo "$name's private key is missing; save it as ~/.ssh/$name-dev-us" >&2; exit 1; }
domain="$(sed -nE 's/^[[:space:]]*domain[[:space:]]*=[[:space:]]*"(.*)".*/\1/p' tofu/locals.tf)"
[[ -n "$domain" ]] || { echo "Domain missing from tofu/locals.tf" >&2; exit 1; }
exec ssh -t -o IdentitiesOnly=yes -i "$key" "$name@dev-us.tailnet.$domain" ;;
tailnet) tailnet peers; exit ;;
uptime) exec python3 scripts/infra-status.py --uptime ;;
env)
if [[ -n "${2:-}" && -z "${3:-}" ]]; then
commands="$(category "$2")"
[[ -n "$commands" ]] || { echo "no environment $2; run infra env for available environments" >&2; exit 1; }
echo "$commands"
elif [[ -z "${2:-}" ]]; then
environments
else
echo "usage: infra env [NAME]" >&2
exit 1
fi
exit ;;
devsync)
[[ -f scripts/devsync.sh && ! -f /etc/dev-vps ]] || { echo "devsync is available only on laptops with scripts/devsync.sh" >&2; exit 1; }
shift; exec scripts/devsync.sh "$@" ;;
"") ;;
*)
commands="$(category "$1")"
[[ -n "$commands" ]] || { echo "no category $1; run infra for available categories" >&2; exit 1; }
echo "$commands"
exit ;;
esac
if [[ -f /etc/dev-vps ]]; then
echo "host: $(cat /etc/dev-vps) (dev box)"
else
echo "host: $(hostname -s) (laptop)"
fi
tailnet
dirty="$(git status --porcelain | wc -l | tr -d ' ')"
echo "repo: $INFRA_DIR $(git status -sb | head -1 | sed 's/^## //') ${dirty} uncommitted"
echo
if [[ -f scripts/infra-status.py ]]; then
python3 scripts/infra-status.py || true
echo
fi
echo "Commands:"
for name in setup foundation dev; do
case "$name" in
setup) description="Prepare tools, keys, and tokens" ;;
foundation) description="Manage control-plane and tailnet" ;;
dev) description="Access dev box and inspect builds" ;;
esac
[[ -n "$(category "$name")" ]] && echo " infra $name — $description"
done
[[ -n "$(environments)" ]] && echo " infra env — List available environments"
command -v tailscale >/dev/null 2>&1 && echo " infra tailnet — Show self, peers, and exit node"
if [[ -f scripts/devsync.sh ]]; then
if [[ -f /etc/dev-vps ]]; then
echo " infra devsync ENV [PATH...] — Unavailable here; run on laptop"
else
echo " infra devsync ENV [PATH...] — Send files or clipboard image to dev box"
fi
fi
[[ -f scripts/infra-status.py ]] && echo " infra uptime — Show Gatus health over last 7 days"
for name in joceline maeve; do
collaborator_key "$name" >/dev/null && echo " infra $name — Test $name's SSH login and welcome on dev-us"
done
echo " infra help TARGET | infra help ENV TARGET — Show target docs and recipe"