Repository navigation
Expand file tree
/
Copy pathinstall
More file actions
executable file
·403 lines (368 loc) · 13.8 KB
/
Copy pathinstall
File metadata and controls
executable file
·403 lines (368 loc) · 13.8 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
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
#!/usr/bin/env bash
# hum installer.
#
# Builds + registers humd as a user service. Pulls the orchd supervisor
# from crates.io, pinned exactly, for hives to keep their own processes
# alive with; hum itself never calls it. `hum hive <ref> install` teaches
# humd a hive, and the hive enters and runs its own bees. The `orch`
# Orchfile CLI is not installed — it's dead weight; hum parses Orchfiles
# in-process, and Orchfile parsing is available as the `the-orch` library
# should we ever need it.
#
# Idempotent — re-running upgrades in place.
#
# Two channels:
# default the latest release tag — what ships, what others run
# --edge the latest main HEAD — unreleased tip, for the machine
# that lives on the edge
#
# Usage:
# ./install # build + install the latest release tag
# ./install --edge # build + install the latest main HEAD
# ./install status # humd state + paths
# ./install logs # tail humd logs
# ./install uninstall # stop + remove humd unit (state preserved)
# ./install purge # remove everything INCLUDING state
set -euo pipefail
XDG_CONFIG_HOME="${XDG_CONFIG_HOME:-$HOME/.config}"
XDG_DATA_HOME="${XDG_DATA_HOME:-$HOME/.local/share}"
XDG_STATE_HOME="${XDG_STATE_HOME:-$HOME/.local/state}"
HUM_CONFIG="$XDG_CONFIG_HOME/hum"
HUM_DATA="$XDG_DATA_HOME/hum"
HUM_STATE="$XDG_STATE_HOME/hum"
HUM_BIN_ROOT="$HOME/.local"
HUMD_BIN="$HUM_BIN_ROOT/bin/humd"
HUMCTL_BIN="$HUM_BIN_ROOT/bin/humctl"
HUM_CLI_BIN="$HUM_BIN_ROOT/bin/hum"
ORCHD_BIN="$HUM_BIN_ROOT/bin/orchd"
HUM_REPO_URL="${HUM_REPO_URL:-https://github.com/adiled/hum.git}"
HUM_SRC="$HUM_DATA/src"
ORCHD_MIN_VERSION="0.4.0"
ORCHD_MAX_VERSION="0.4.0"
EDGE=0
declare -a REST=()
for arg in "$@"; do
case "$arg" in
--edge) EDGE=1 ;;
-h|--help) sed -n '2,24p' "$0" | sed 's/^# \{0,1\}//'; exit 0 ;;
*) REST+=("$arg") ;;
esac
done
CMD="${REST[0]:-install}"
OS="$(uname -s)"
log() { printf '\033[1m[hum]\033[0m %s\n' "$*"; }
warn() { printf '\033[1;33m[hum]\033[0m %s\n' "$*" >&2; }
fail() { printf '\033[1;31m[hum]\033[0m %s\n' "$*" >&2; exit 1; }
version_gte() { printf '%s\n%s\n' "$2" "$1" | sort -V -C; }
read_version() { "$1" --version 2>/dev/null | sed -n 's/.*\([0-9][0-9]*\.[0-9][0-9]*\.[0-9][0-9]*\).*/\1/p' | head -1; }
ensure_min_version() {
local NAME="$1" BIN="$2" MIN="$3"
if ! command -v "$BIN" >/dev/null 2>&1; then fail "$NAME not found on PATH."; fi
local CUR; CUR="$(read_version "$BIN" || true)"
if [ -z "$CUR" ]; then fail "$NAME found but couldn't read version."; fi
if version_gte "$CUR" "$MIN"; then log "$NAME ${CUR} ≥ ${MIN} ✓"
else fail "$NAME ${CUR} < ${MIN} — upgrade and re-run."; fi
}
ensure_orchd_api() {
local BIN="$1" HELP MISSING=""
HELP="$("$BIN" --help 2>/dev/null || true)"
for sub in grow survey fell logs; do
printf '%s' "$HELP" | grep -Eq "^ +${sub}( |$)" || MISSING="$MISSING $sub"
done
if [ -n "$MISSING" ]; then
fail "orchd is missing the subcommand(s):$MISSING — the bundled hives call grow/fell. Update orchd (or re-run ./install)."
fi
log "orchd API: grow survey fell logs ✓"
}
ensure_prereqs() {
if ! command -v cargo >/dev/null 2>&1; then
if [ -x "$HUMD_BIN" ] && [ -x "$HUMCTL_BIN" ] && [ -x "$HUM_CLI_BIN" ] && [ -x "$ORCHD_BIN" ]; then
warn "cargo not found; using pre-built binaries in $HUM_BIN_ROOT/bin"
SKIP_BUILD=1
else
fail "cargo (rustup) not found. Install rustup: https://rustup.rs"
fi
fi
}
ensure_identity() {
mkdir -p "$HUM_STATE"
local KEY="$HUM_STATE/humd.key"
if [ -s "$KEY" ]; then log "humd identity: $KEY ✓"; return 0; fi
log "minting humd identity at $KEY"
dd if=/dev/urandom of="$KEY" bs=32 count=1 2>/dev/null
chmod 600 "$KEY"
}
ensure_peers_config() {
mkdir -p "$HUM_CONFIG"
local PEERS="$HUM_CONFIG/peers.json"
if [ -s "$PEERS" ]; then log "peers.json: $PEERS ✓"; return 0; fi
echo "[]" > "$PEERS"
log "wrote empty peers.json at $PEERS"
}
ensure_hum_config() {
mkdir -p "$HUM_CONFIG"
local CFG="$HUM_CONFIG/hum.json"
if [ -s "$CFG" ]; then log "hum.json: $CFG ✓"; return 0; fi
cat > "$CFG" <<JSON
{
"\$schema": "https://adiled.github.io/hum/hum.schema.json",
"fs": {
"roots": [
{ "path": "~/code", "mode": "rw" },
{ "path": "/tmp", "mode": "rw" }
],
"denied": ["~/.ssh", "~/.aws", "~/.gnupg", "~/.config/hum"]
}
}
JSON
log "wrote default hum.json at $CFG"
}
install_man_page() {
local PAGE
for PAGE in "$(dirname "$0")/man/hum.1" "$HUM_SRC/man/hum.1"; do
[ -f "$PAGE" ] || continue
local DIR
for DIR in /usr/local/share/man "$XDG_DATA_HOME/man"; do
if mkdir -p "$DIR/man1" 2>/dev/null && cp "$PAGE" "$DIR/man1/hum.1" 2>/dev/null; then
log "man page: $DIR/man1/hum.1"
return 0
fi
done
warn "could not install the man page — copy $PAGE into a man1 directory on your man path"
return 0
done
}
prepare_src() {
local MODE="$1" LOCAL_SRC=""
if [ -f "$(dirname "$0")/Cargo.toml" ] && grep -q '"humd"' "$(dirname "$0")/Cargo.toml"; then
LOCAL_SRC="$(cd "$(dirname "$0")" && pwd)"
fi
if [ "$MODE" = edge ] && [ -n "$LOCAL_SRC" ]; then
local BRANCH; BRANCH="$(git -C "$LOCAL_SRC" rev-parse --abbrev-ref HEAD 2>/dev/null || echo '?')"
if [ "$BRANCH" != "main" ]; then
fail "--edge builds main HEAD, but $LOCAL_SRC is on '$BRANCH'. Check out main, or drop --edge."
fi
if git -C "$LOCAL_SRC" diff --quiet && git -C "$LOCAL_SRC" diff --cached --quiet; then
log "updating $LOCAL_SRC to origin/main" >&2
git -C "$LOCAL_SRC" pull --ff-only --quiet origin main >&2 || warn "could not fast-forward — building the local HEAD as-is" >&2
else
warn "working tree has uncommitted changes — the build will include them" >&2
fi
echo "$LOCAL_SRC"
return 0
fi
if [ ! -d "$HUM_SRC/.git" ]; then
log "cloning $HUM_REPO_URL into $HUM_SRC" >&2
mkdir -p "$HUM_DATA"
git clone --quiet "$HUM_REPO_URL" "$HUM_SRC" >&2
fi
if [ "$(git -C "$HUM_SRC" rev-parse --is-shallow-repository 2>/dev/null || echo false)" = "true" ]; then
git -C "$HUM_SRC" fetch --quiet --unshallow --tags --force origin >&2
else
git -C "$HUM_SRC" fetch --quiet --tags --force origin >&2
fi
if [ "$MODE" = edge ]; then
log "checking out origin/main in $HUM_SRC" >&2
git -C "$HUM_SRC" checkout --quiet -B main origin/main >&2
git -C "$HUM_SRC" reset --quiet --hard origin/main >&2
else
local TAG; TAG="$(git -C "$HUM_SRC" tag --sort=-v:refname | head -1)"
[ -n "$TAG" ] || fail "no release tags found in $HUM_SRC"
log "checking out $TAG in $HUM_SRC" >&2
git -C "$HUM_SRC" checkout --quiet --detach "$TAG" >&2
fi
echo "$HUM_SRC"
}
install_orchd() {
log "installing orchd (bee supervisor) ${ORCHD_MIN_VERSION} from crates.io, pinned"
cargo install --quiet orchd --version "=$ORCHD_MIN_VERSION" --root "$HUM_BIN_ROOT" --force
ensure_min_version orchd "$ORCHD_BIN" "$ORCHD_MIN_VERSION"
ensure_orchd_api "$ORCHD_BIN"
local CUR; CUR="$(read_version "$ORCHD_BIN" || true)"
if [ -n "$CUR" ] && ! version_gte "$ORCHD_MAX_VERSION" "$CUR"; then
fail "orchd ${CUR} > pinned ${ORCHD_MAX_VERSION} — hum pins it exactly; update the pin in ./install"
fi
}
build_binaries() {
local SRC
SRC="$(prepare_src "$( [ "$EDGE" = 1 ] && echo edge || echo stable )")"
log "building Rust binaries from $SRC"
cargo install --quiet --locked --path "$SRC/humd" --root "$HUM_BIN_ROOT" --force
cargo install --quiet --locked --path "$SRC/humctl" --root "$HUM_BIN_ROOT" --force
cargo install --quiet --locked --path "$SRC/hum" --root "$HUM_BIN_ROOT" --force
install_orchd
[ -x "$HUMD_BIN" ] || fail "humd not at $HUMD_BIN after build"
[ -x "$HUMCTL_BIN" ] || fail "humctl not at $HUMCTL_BIN after build"
[ -x "$HUM_CLI_BIN" ] || fail "hum not at $HUM_CLI_BIN after build"
[ -x "$ORCHD_BIN" ] || fail "orchd not at $ORCHD_BIN after build"
log "humd $HUMD_BIN ; humctl $HUMCTL_BIN ; hum $HUM_CLI_BIN ; orchd $ORCHD_BIN"
}
linux_unit_dir="${XDG_CONFIG_HOME}/systemd/user"
darwin_unit_dir="$HOME/Library/LaunchAgents"
trust_remote_nurses="${HUM_TRUST_REMOTE_NURSES:-0}"
write_linux_unit() {
mkdir -p "$linux_unit_dir"
local TRUST_LINE=""
if [ -f "$linux_unit_dir/humd.service" ]; then
TRUST_LINE="$(grep -E '^Environment=' "$linux_unit_dir/humd.service" || true)"
fi
if [ -z "$TRUST_LINE" ] && [ "$trust_remote_nurses" != "0" ]; then
TRUST_LINE="Environment=HUM_TRUST_REMOTE_NURSES=$trust_remote_nurses"
fi
cat > "$linux_unit_dir/humd.service" <<EOF
[Unit]
Description=hum router daemon
[Service]
Type=simple
ExecStart=$HUMD_BIN
$TRUST_LINE
Restart=always
RestartSec=2
[Install]
WantedBy=default.target
EOF
systemctl --user daemon-reload
systemctl --user enable humd.service >/dev/null 2>&1 || true
systemctl --user restart humd.service
}
write_darwin_unit() {
mkdir -p "$darwin_unit_dir"
local TRUST_XML=""
if [ -f "$darwin_unit_dir/humd.plist" ]; then
TRUST_XML="$(awk '/<key>EnvironmentVariables<\/key>/{f=1} f{print} /<\/dict>/{if(f){exit}}' "$darwin_unit_dir/humd.plist")"
fi
if [ -z "$TRUST_XML" ] && [ "$trust_remote_nurses" != "0" ]; then
TRUST_XML=" <key>EnvironmentVariables</key>
<dict>
<key>HUM_TRUST_REMOTE_NURSES</key><string>$trust_remote_nurses</string>
</dict>"
fi
cat > "$darwin_unit_dir/humd.plist" <<EOF
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0"><dict>
<key>Label</key><string>humd</string>
<key>Program</key><string>$HUMD_BIN</string>
<key>RunAtLoad</key><true/>
<key>KeepAlive</key><true/>
<key>StandardOutPath</key><string>$HOME/Library/Logs/sh.hum.humd.out.log</string>
<key>StandardErrorPath</key><string>$HOME/Library/Logs/sh.hum.humd.err.log</string>
$TRUST_XML
</dict></plist>
EOF
local uid; uid="$(id -u)"
launchctl bootout "gui/$uid/humd" 2>/dev/null || true
local i
for i in $(seq 1 20); do
launchctl print "gui/$uid/humd" >/dev/null 2>&1 || break
sleep 0.25
done
if ! launchctl bootstrap "gui/$uid" "$darwin_unit_dir/humd.plist" 2>/dev/null; then
sleep 1
launchctl bootstrap "gui/$uid" "$darwin_unit_dir/humd.plist" \
|| fail "could not register humd as a launchd agent ($darwin_unit_dir/humd.plist)"
fi
}
start_daemons() {
case "$OS" in
Linux) write_linux_unit ;;
Darwin) write_darwin_unit ;;
*) warn "service install not supported on $OS — run '$HUMD_BIN' manually"; return 0 ;;
esac
log "humd registered + started ($OS)"
sleep 1
}
print_next_steps() {
log ""
log "humd is up. Install a hive; it enters and runs its own bee:"
log " hum hive openai-server install # OpenAI inference"
log " hum hive bash install # lend this host's shell to the blooms that build here"
log " hum hive paid-oracle install # x402 paid price oracle"
log ""
log "Bee state: hum bee --list"
log "humd state: hum status / hum doctor / humctl logs"
check_path_hint
}
check_path_hint() {
case ":$PATH:" in
*":$HUM_BIN_ROOT/bin:"*) return 0 ;;
esac
log ""
warn "$HUM_BIN_ROOT/bin is not on \$PATH."
warn "Add this to your shell config (\$HOME/.bashrc, \$HOME/.zshrc, \$HOME/.profile):"
warn " export PATH=\"\$HOME/.local/bin:\$PATH\""
warn "Then open a new shell. Until then, invoke binaries by absolute path:"
warn " $HUM_CLI_BIN status"
}
show_status() {
echo "humd binary: $HUMD_BIN"
echo " version: $($HUMD_BIN --version 2>&1 || echo 'not installed')"
echo "orchd binary: $ORCHD_BIN"
echo " version: $($ORCHD_BIN --version 2>&1 || echo 'not installed')"
echo " required: >= $ORCHD_MIN_VERSION (subcommands: grow survey fell logs)"
echo "identity: $HUM_STATE/humd.key $([ -s "$HUM_STATE/humd.key" ] && echo ✓ || echo MISSING)"
echo "peers.json: $HUM_CONFIG/peers.json $([ -s "$HUM_CONFIG/peers.json" ] && echo ✓ || echo MISSING)"
echo "hum.json: $HUM_CONFIG/hum.json $([ -s "$HUM_CONFIG/hum.json" ] && echo ✓ || echo MISSING)"
case "$OS" in
Linux) systemctl --user status humd --no-pager 2>&1 | head -6 || true ;;
Darwin) launchctl print "gui/$(id -u)/humd" 2>&1 | head -6 || true ;;
esac
if [ -x "$HUM_CLI_BIN" ]; then
echo "--- bees ---"
"$HUM_CLI_BIN" bee --list 2>&1 || true
fi
}
show_logs() {
if [ -x "$HUMCTL_BIN" ]; then
"$HUMCTL_BIN" logs -n 100
else
warn "humctl not installed"
fi
}
uninstall() {
case "$OS" in
Linux)
systemctl --user stop humd.service 2>/dev/null || true
systemctl --user disable humd.service 2>/dev/null || true
rm -f "$linux_unit_dir/humd.service"
systemctl --user daemon-reload 2>/dev/null || true
;;
Darwin)
local uid; uid="$(id -u)"
launchctl bootout "gui/$uid/humd" 2>/dev/null || true
rm -f "$darwin_unit_dir/humd.plist"
;;
esac
rm -f "$HUMD_BIN" "$HUMCTL_BIN" "$HUM_CLI_BIN"
rm -f /usr/local/share/man/man1/hum.1 "$XDG_DATA_HOME/man/man1/hum.1"
log "uninstalled humd. orchd left in place. State preserved in $HUM_STATE + $HUM_CONFIG."
log " run \`./install purge\` to remove state too."
}
purge() {
uninstall
rm -rf "$HUM_STATE" "$HUM_CONFIG" "$HUM_DATA"
log "purged."
}
do_install() {
log "hum installer"
if [ "$EDGE" = 1 ]; then log "channel: edge — latest main HEAD"
else log "channel: stable — latest release tag"; fi
ensure_prereqs
if [ "${SKIP_BUILD:-0}" != "1" ]; then build_binaries; fi
install_man_page
ensure_identity
ensure_peers_config
ensure_hum_config
"$HUM_CLI_BIN" config >/dev/null 2>&1 || warn "hum config failed — run 'hum config' to see why"
start_daemons
log ""
print_next_steps
}
case "$CMD" in
install|"") do_install ;;
status) show_status ;;
logs) show_logs ;;
uninstall) uninstall ;;
purge) purge ;;
*) fail "unknown command: $CMD (try: install, status, logs, uninstall, purge)" ;;
esac