Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion hives/anthropic-server/install
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,6 @@ while read -r name; do
if [ -n "${!name:-}" ]; then args+=(--arg "$name=${!name}"); fi
done < <(sed -n 's/^ARG \([A-Za-z_][A-Za-z0-9_]*\)=.*/\1/p' "$here/Orchfile" | grep -vx -e HUM_SRC -e HUM_HIVE)

run() { "$orchd" --project-dir "$here" --namespace "hum-$hive" --user --state-dir "$state/orch" "${args[@]}" "$@"; }
run() { "$orchd" --project-dir "$here" --grove "hum-$hive" --user --state-dir "$state/orch" "${args[@]}" "$@"; }
run fell >/dev/null 2>&1 || true
run grow "$hive"
2 changes: 1 addition & 1 deletion hives/apple-fm/install
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,6 @@ done < <(sed -n 's/^ARG \([A-Za-z_][A-Za-z0-9_]*\)=.*/\1/p' "$here/Orchfile" | g
units=()
while read -r unit; do units+=("$unit"); done < <(sed -n 's/^SERVICE \([A-Za-z0-9_.-]*\)$/\1/p' "$here/Orchfile" | grep -vx hive-install)

run() { "$orchd" --project-dir "$here" --namespace "hum-$hive" --user --state-dir "$state/orch" "${args[@]}" "$@"; }
run() { "$orchd" --project-dir "$here" --grove "hum-$hive" --user --state-dir "$state/orch" "${args[@]}" "$@"; }
run fell >/dev/null 2>&1 || true
run grow "${units[@]}"
2 changes: 1 addition & 1 deletion hives/bash/install
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,6 @@ while read -r name; do
if [ -n "${!name:-}" ]; then args+=(--arg "$name=${!name}"); fi
done < <(sed -n 's/^ARG \([A-Za-z_][A-Za-z0-9_]*\)=.*/\1/p' "$here/Orchfile" | grep -vx -e HUM_SRC -e HUM_HIVE)

run() { "$orchd" --project-dir "$here" --namespace "hum-$hive" --user --state-dir "$state/orch" "${args[@]}" "$@"; }
run() { "$orchd" --project-dir "$here" --grove "hum-$hive" --user --state-dir "$state/orch" "${args[@]}" "$@"; }
run fell >/dev/null 2>&1 || true
run grow "$hive"
2 changes: 1 addition & 1 deletion hives/bp7/install
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,6 @@ while read -r name; do
if [ -n "${!name:-}" ]; then args+=(--arg "$name=${!name}"); fi
done < <(sed -n 's/^ARG \([A-Za-z_][A-Za-z0-9_]*\)=.*/\1/p' "$here/Orchfile" | grep -vx -e HUM_SRC -e HUM_HIVE)

run() { "$orchd" --project-dir "$here" --namespace "hum-$hive" --user --state-dir "$state/orch" "${args[@]}" "$@"; }
run() { "$orchd" --project-dir "$here" --grove "hum-$hive" --user --state-dir "$state/orch" "${args[@]}" "$@"; }
run fell >/dev/null 2>&1 || true
run grow "$hive"
2 changes: 1 addition & 1 deletion hives/claude-cli/install
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,6 @@ while read -r name; do
if [ -n "${!name:-}" ]; then args+=(--arg "$name=${!name}"); fi
done < <(sed -n 's/^ARG \([A-Za-z_][A-Za-z0-9_]*\)=.*/\1/p' "$here/Orchfile" | grep -vx -e HUM_SRC -e HUM_HIVE)

run() { "$orchd" --project-dir "$here" --namespace "hum-$hive" --user --state-dir "$state/orch" "${args[@]}" "$@"; }
run() { "$orchd" --project-dir "$here" --grove "hum-$hive" --user --state-dir "$state/orch" "${args[@]}" "$@"; }
run fell >/dev/null 2>&1 || true
run grow "$hive"
2 changes: 1 addition & 1 deletion hives/claude-repl/install
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,6 @@ while read -r name; do
if [ -n "${!name:-}" ]; then args+=(--arg "$name=${!name}"); fi
done < <(sed -n 's/^ARG \([A-Za-z_][A-Za-z0-9_]*\)=.*/\1/p' "$here/Orchfile" | grep -vx -e HUM_SRC -e HUM_HIVE)

run() { "$orchd" --project-dir "$here" --namespace "hum-$hive" --user --state-dir "$state/orch" "${args[@]}" "$@"; }
run() { "$orchd" --project-dir "$here" --grove "hum-$hive" --user --state-dir "$state/orch" "${args[@]}" "$@"; }
run fell >/dev/null 2>&1 || true
run grow "$hive"
2 changes: 1 addition & 1 deletion hives/dwarfstar/install
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,6 @@ done < <(sed -n 's/^ARG \([A-Za-z_][A-Za-z0-9_]*\)=.*/\1/p' "$here/Orchfile" | g
units=()
while read -r unit; do units+=("$unit"); done < <(sed -n 's/^SERVICE \([A-Za-z0-9_.-]*\)$/\1/p' "$here/Orchfile" | grep -vx hive-install)

run() { "$orchd" --project-dir "$here" --namespace "hum-$hive" --user --state-dir "$state/orch" "${args[@]}" "$@"; }
run() { "$orchd" --project-dir "$here" --grove "hum-$hive" --user --state-dir "$state/orch" "${args[@]}" "$@"; }
run fell >/dev/null 2>&1 || true
run grow "${units[@]}"
2 changes: 1 addition & 1 deletion hives/grpc/install
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,6 @@ while read -r name; do
if [ -n "${!name:-}" ]; then args+=(--arg "$name=${!name}"); fi
done < <(sed -n 's/^ARG \([A-Za-z_][A-Za-z0-9_]*\)=.*/\1/p' "$here/Orchfile" | grep -vx -e HUM_SRC -e HUM_HIVE)

run() { "$orchd" --project-dir "$here" --namespace "hum-$hive" --user --state-dir "$state/orch" "${args[@]}" "$@"; }
run() { "$orchd" --project-dir "$here" --grove "hum-$hive" --user --state-dir "$state/orch" "${args[@]}" "$@"; }
run fell >/dev/null 2>&1 || true
run grow "$hive"
2 changes: 1 addition & 1 deletion hives/gsm-modem/install
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,6 @@ while read -r name; do
if [ -n "${!name:-}" ]; then args+=(--arg "$name=${!name}"); fi
done < <(sed -n 's/^ARG \([A-Za-z_][A-Za-z0-9_]*\)=.*/\1/p' "$here/Orchfile" | grep -vx -e HUM_SRC -e HUM_HIVE)

run() { "$orchd" --project-dir "$here" --namespace "hum-$hive" --user --state-dir "$state/orch" "${args[@]}" "$@"; }
run() { "$orchd" --project-dir "$here" --grove "hum-$hive" --user --state-dir "$state/orch" "${args[@]}" "$@"; }
run fell >/dev/null 2>&1 || true
run grow "$hive"
2 changes: 1 addition & 1 deletion hives/ollama-server/install
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,6 @@ done < <(sed -n 's/^ARG \([A-Za-z_][A-Za-z0-9_]*\)=.*/\1/p' "$here/Orchfile" | g
units=()
while read -r unit; do units+=("$unit"); done < <(sed -n 's/^SERVICE \([A-Za-z0-9_.-]*\)$/\1/p' "$here/Orchfile" | grep -vx hive-install)

run() { "$orchd" --project-dir "$here" --namespace "hum-$hive" --user --state-dir "$state/orch" "${args[@]}" "$@"; }
run() { "$orchd" --project-dir "$here" --grove "hum-$hive" --user --state-dir "$state/orch" "${args[@]}" "$@"; }
run fell >/dev/null 2>&1 || true
run grow "${units[@]}"
2 changes: 1 addition & 1 deletion hives/openai-server/install
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ while read -r name; do
if [ -n "${!name:-}" ]; then args+=(--arg "$name=${!name}"); fi
done < <(sed -n 's/^ARG \([A-Za-z_][A-Za-z0-9_]*\)=.*/\1/p' "$here/Orchfile" | grep -vx -e HUM_SRC -e HUM_HIVE)

run() { "$orchd" --project-dir "$here" --namespace "hum-$hive" --user --state-dir "$state/orch" "${args[@]}" "$@"; }
run() { "$orchd" --project-dir "$here" --grove "hum-$hive" --user --state-dir "$state/orch" "${args[@]}" "$@"; }
run fell >/dev/null 2>&1 || true
run grow "$hive"
echo "openai-server: make a key with: node $here/dist/index.js key add <name>; until one exists, anyone who reaches its port may call"
2 changes: 1 addition & 1 deletion hives/paid-oracle/install
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,6 @@ while read -r name; do
if [ -n "${!name:-}" ]; then args+=(--arg "$name=${!name}"); fi
done < <(sed -n 's/^ARG \([A-Za-z_][A-Za-z0-9_]*\)=.*/\1/p' "$here/Orchfile" | grep -vx -e HUM_SRC -e HUM_HIVE)

run() { "$orchd" --project-dir "$here" --namespace "hum-$hive" --user --state-dir "$state/orch" "${args[@]}" "$@"; }
run() { "$orchd" --project-dir "$here" --grove "hum-$hive" --user --state-dir "$state/orch" "${args[@]}" "$@"; }
run fell >/dev/null 2>&1 || true
run grow "$hive"
2 changes: 1 addition & 1 deletion hives/twilio-sms/install
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,6 @@ while read -r name; do
if [ -n "${!name:-}" ]; then args+=(--arg "$name=${!name}"); fi
done < <(sed -n 's/^ARG \([A-Za-z_][A-Za-z0-9_]*\)=.*/\1/p' "$here/Orchfile" | grep -vx -e HUM_SRC -e HUM_HIVE)

run() { "$orchd" --project-dir "$here" --namespace "hum-$hive" --user --state-dir "$state/orch" "${args[@]}" "$@"; }
run() { "$orchd" --project-dir "$here" --grove "hum-$hive" --user --state-dir "$state/orch" "${args[@]}" "$@"; }
run fell >/dev/null 2>&1 || true
run grow "$hive"
2 changes: 1 addition & 1 deletion hives/web2/install
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ while read -r name; do
if [ -n "${!name:-}" ]; then args+=(--arg "$name=${!name}"); fi
done < <(sed -n 's/^ARG \([A-Za-z_][A-Za-z0-9_]*\)=.*/\1/p' "$here/Orchfile" | grep -vx -e HUM_SRC -e HUM_HIVE)

run() { "$orchd" --project-dir "$here" --namespace "hum-$hive" --user --state-dir "$state/orch" "${args[@]}" "$@"; }
run() { "$orchd" --project-dir "$here" --grove "hum-$hive" --user --state-dir "$state/orch" "${args[@]}" "$@"; }
run fell >/dev/null 2>&1 || true
run grow "$hive"
echo "web2 for people (wrepl, login, status, import): $here/target/release/web2"
2 changes: 1 addition & 1 deletion hum/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ hum-identity = { path = "../hum-identity" }
hum-thrum = { path = "../hum-thrum" }
humd = { path = "../humd" }
thehum = { path = "../thehum" }
the-orch = "0.2"
the-orch = "0.3"
anyhow = "1"
clap = { version = "4", features = ["derive"] }
serde_json = "1"
Expand Down
66 changes: 57 additions & 9 deletions hum/src/orchfile.rs
Original file line number Diff line number Diff line change
@@ -1,24 +1,30 @@
//! The one part of the Orch spec hum reads: a hive's Orchfile, resolved
//! well enough to find and run its `hive-install` unit. Every other unit
//! is left to the hive.
//!
//! Orch 0.3 takes runtime values as ARG overrides and applies only those
//! the Orchfile declares, so the whole environment goes in and a declared
//! ARG takes its value from it, the way orchd resolves the same file.

use std::collections::HashMap;
use std::path::Path;

use anyhow::{Result, anyhow};
use the_orch::types::{OrchFile, Service};

fn declared_args(body: &str) -> HashMap<String, String> {
the_orch::parser::parse_raw(body, 0)
.map(|raw| raw.args.into_iter().collect())
.unwrap_or_default()
pub fn resolve(hive: &str, src: &Path, body: &str) -> Result<OrchFile> {
let env = std::env::vars_os()
.filter_map(|(k, v)| Some((k.into_string().ok()?, v.into_string().ok()?)));
resolve_in(hive, src, body, env)
}

pub fn resolve(hive: &str, src: &Path, body: &str) -> Result<OrchFile> {
let mut args: HashMap<String, String> = declared_args(body)
.into_keys()
.filter_map(|k| std::env::var(&k).ok().map(|v| (k, v)))
.collect();
fn resolve_in(
hive: &str,
src: &Path,
body: &str,
env: impl IntoIterator<Item = (String, String)>,
) -> Result<OrchFile> {
let mut args: HashMap<String, String> = env.into_iter().collect();
args.insert("HOME".into(), hum_paths::home().display().to_string());
args.insert("HUM_SRC".into(), src.display().to_string());
args.insert("HUM_HIVE".into(), hive.to_string());
Expand All @@ -33,3 +39,45 @@ pub fn install_unit(file: &OrchFile) -> Option<&Service> {
.iter()
.find(|s| s.name == hum_paths::HIVE_INSTALL_UNIT)
}

#[cfg(test)]
mod tests {
use super::*;

const BODY: &str = "ARG PORT=1\nSERVICE hive-install\nRUN serve ${PORT} ${HUM_SRC}\nONESHOT true\n";

fn run_of(file: &OrchFile) -> Option<&str> {
install_unit(file).and_then(|u| u.run_command.as_deref())
}

#[test]
fn a_declared_arg_takes_its_value_from_the_environment() {
let body = format!("ARG HUM_SRC=/x\n{BODY}");
let env = [("PORT".to_string(), "9".to_string())];
let file = resolve_in("h", Path::new("/hives/h"), &body, env).unwrap();
assert_eq!(run_of(&file), Some("serve 9 /hives/h"));
}

#[test]
fn a_declared_arg_the_environment_lacks_keeps_its_default() {
let body = format!("ARG HUM_SRC=/x\n{BODY}");
let file = resolve_in("h", Path::new("/hives/h"), &body, []).unwrap();
assert_eq!(run_of(&file), Some("serve 1 /hives/h"));
}

#[test]
fn the_environment_adds_no_arg_the_orchfile_never_declared() {
let body = format!("ARG HUM_SRC=/x\n{BODY}");
let env = [("STRAY".to_string(), "leak".to_string())];
let file = resolve_in("h", Path::new("/hives/h"), &body, env).unwrap();
assert!(!file.args.contains_key("STRAY"), "args: {:?}", file.args);
}

#[test]
fn hum_sets_the_source_over_whatever_the_environment_says() {
let body = format!("ARG HUM_SRC=/x\n{BODY}");
let env = [("HUM_SRC".to_string(), "/stale".to_string())];
let file = resolve_in("h", Path::new("/hives/h"), &body, env).unwrap();
assert_eq!(run_of(&file), Some("serve 1 /hives/h"));
}
}
85 changes: 73 additions & 12 deletions install
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
#!/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.
# Builds + registers humd as a user service. Installs the orchd supervisor
# from its GitHub release, pinned exactly and checksum-verified, for hives
# to keep their own processes alive with; hum itself never calls it. The
# release carries orch, which orchd reads every Orchfile through, and
# orchd's rootstocks, so all of them are installed beside it.
# `hum hive <ref> install` teaches humd a hive, and the hive enters and
# runs its own bees. hum parses Orchfiles in-process with the `the-orch`
# library, the same Orch version orch speaks.
#
# Idempotent — re-running upgrades in place.
#
Expand Down Expand Up @@ -41,8 +42,9 @@ 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"
ORCHD_MIN_VERSION="0.5.0"
ORCHD_MAX_VERSION="0.5.0"
ORCHD_RELEASES="${ORCHD_DOWNLOAD_URL:-https://github.com/adiled/orchd/releases/download/v$ORCHD_MIN_VERSION}"

EDGE=0
declare -a REST=()
Expand Down Expand Up @@ -192,9 +194,66 @@ prepare_src() {
echo "$HUM_SRC"
}

orchd_target() {
local ARCH SYS
case "$(uname -m)" in
x86_64|amd64) ARCH=x86_64 ;;
arm64|aarch64) ARCH=aarch64 ;;
*) return 1 ;;
esac
case "$OS" in
Linux) SYS=linux ;;
Darwin) SYS=macos ;;
*) return 1 ;;
esac
case "$ARCH-$SYS" in
x86_64-linux|aarch64-linux|aarch64-macos) echo "$ARCH-$SYS" ;;
*) return 1 ;;
esac
}

sha256_of() {
if command -v sha256sum >/dev/null 2>&1; then sha256sum "$1" | cut -d' ' -f1
elif command -v shasum >/dev/null 2>&1; then shasum -a 256 "$1" | cut -d' ' -f1
else fail "need sha256sum or shasum to check the orchd download"; fi
}

fetch_orchd_release() {
local TMP="$2" TARBALL="orchd-$1.tar.gz"
curl -fsSL "$ORCHD_RELEASES/$TARBALL" -o "$TMP/$TARBALL" \
|| fail "could not fetch $ORCHD_RELEASES/$TARBALL"
curl -fsSL "$ORCHD_RELEASES/$TARBALL.sha256" -o "$TMP/$TARBALL.sha256" \
|| fail "could not fetch $ORCHD_RELEASES/$TARBALL.sha256"
local WANT GOT
WANT="$(cut -d' ' -f1 < "$TMP/$TARBALL.sha256")"
GOT="$(sha256_of "$TMP/$TARBALL")"
[ "$WANT" = "$GOT" ] || fail "checksum mismatch for $TARBALL: expected $WANT, got $GOT"
tar -xzf "$TMP/$TARBALL" -C "$TMP" || fail "could not unpack $TARBALL"
{ [ -x "$TMP/bin/orchd" ] && [ -x "$TMP/bin/orch" ]; } || fail "$TARBALL holds no bin/orchd and bin/orch"
mkdir -p "$HUM_BIN_ROOT/bin" || fail "cannot make $HUM_BIN_ROOT/bin"
local FILE NAME
for FILE in "$TMP"/bin/*; do
NAME="$(basename "$FILE")"
{ cp "$FILE" "$HUM_BIN_ROOT/bin/.$NAME.new" \
&& chmod 755 "$HUM_BIN_ROOT/bin/.$NAME.new" \
&& mv -f "$HUM_BIN_ROOT/bin/.$NAME.new" "$HUM_BIN_ROOT/bin/$NAME"; } \
|| fail "cannot install $NAME into $HUM_BIN_ROOT/bin"
done
}

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
local TARGET
if ! TARGET="$(orchd_target)"; then
warn "orchd ${ORCHD_MIN_VERSION} ships no build for $(uname -m) $OS (only x86_64-linux, aarch64-linux, aarch64-macos)."
warn "hum runs without it, but hives that keep processes alive need it: build it from https://github.com/adiled/orchd"
return 0
fi
log "installing orchd (bee supervisor) ${ORCHD_MIN_VERSION} for $TARGET, with orch and its rootstocks, pinned"
ORCHD_TMP="$(mktemp -d)"
trap 'rm -rf "$ORCHD_TMP"' EXIT
fetch_orchd_release "$TARGET" "$ORCHD_TMP"
rm -rf "$ORCHD_TMP"
trap - EXIT
ensure_min_version orchd "$ORCHD_BIN" "$ORCHD_MIN_VERSION"
ensure_orchd_api "$ORCHD_BIN"
local CUR; CUR="$(read_version "$ORCHD_BIN" || true)"
Expand All @@ -214,7 +273,9 @@ build_binaries() {
[ -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"
if orchd_target >/dev/null; then
[ -x "$ORCHD_BIN" ] || fail "orchd not at $ORCHD_BIN after build"
fi
log "humd $HUMD_BIN ; humctl $HUMCTL_BIN ; hum $HUM_CLI_BIN ; orchd $ORCHD_BIN"
}

Expand Down
Loading