Skip to content

armgit / ODS

Public Imported from GitHub
0

ODS V3 Pre-Release: Public testing and refinement ahead of the official V3 launch. Turn your PC, Mac, or Linux box into a private AI server.

7.8 KB
Raw Blame
#!/usr/bin/env bash
# Execute the actual ods update entry point with isolated installer and tools.
set -euo pipefail

if (( BASH_VERSINFO[0] < 4 )); then
    for modern_bash in /opt/homebrew/bin/bash /usr/local/bin/bash; do
        if [[ -x "$modern_bash" ]]; then
            exec "$modern_bash" "$0" "$@"
        fi
    done
    printf '[SKIP] ods-cli requires Bash 4+\n'
    exit 0
fi

script_dir="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
root_dir="$(cd "$script_dir/.." && pwd)"
ods_cli="$root_dir/ods-cli"

PASSED=0
FAILED=0
pass() { printf '  [PASS] %s\n' "$1"; PASSED=$((PASSED + 1)); }
fail() { printf '  [FAIL] %s\n' "$1"; FAILED=$((FAILED + 1)); }

# run_update <env_content> <missing_tools> <failing_tools>
# Builds a fake install and runs `ods update --force` with a synthetic PATH:
# tools in <missing_tools> are genuinely absent (clean-room symlink dir), and
# tools in <failing_tools> are stubs that exit 1. Sets RC, OUT, ENV_AFTER.
run_update() {
    local env_content="$1" missing_tools="$2" failing_tools="$3 systemctl sudo curl"
    local tmp_dir install_dir bin_dir clean_dir
    tmp_dir="$(mktemp -d)"
    install_dir="$tmp_dir/install"
    bin_dir="$tmp_dir/bin"
    clean_dir="$tmp_dir/clean"
    mkdir -p "$install_dir/data" "$bin_dir" "$clean_dir" "$install_dir/installers/lib"
    cp "$root_dir/docker-compose.base.yml" "$install_dir/docker-compose.base.yml"
    cp "$root_dir/manifest.json" "$install_dir/manifest.json"
    cp "$root_dir/installers/lib/compose-images.sh" "$install_dir/installers/lib/compose-images.sh"
    printf '%s\n' '-f docker-compose.base.yml' > "$install_dir/.compose-flags"
    printf '%s\n' "$env_content" > "$install_dir/.env"

    cat > "$install_dir/ods-update.sh" <<'UPDATE'
#!/usr/bin/env bash
exit 0
UPDATE
    chmod +x "$install_dir/ods-update.sh"

    cat > "$bin_dir/docker" <<'DOCKER'
#!/usr/bin/env bash
printf 'docker\n' >> "$ODS_FIXTURE_TOOL_LOG"
if [[ "${1:-}" == "info" ]]; then
    [[ "$*" == *NCPU* ]] && printf '16\n'
    exit 0
fi
if [[ "${1:-}" == "compose" ]]; then
    shift
    joined=" $* "
    if [[ "$joined" == *" config --format json "* ]]; then
        printf '%s\n' '{"services":{"dashboard":{"image":"ods-dashboard:local","build":{"context":"./extensions/services/dashboard"}}}}'
        exit 0
    fi
    if [[ "$joined" == *" config --services "* ]]; then
        printf '%s\n' dashboard
        exit 0
    fi
    if [[ "$joined" == *" --services "* && "$joined" == *" --status running "* ]]; then
        printf '%s\n' dashboard
        exit 0
    fi
    if [[ "$joined" == *" ps "* ]]; then
        exit 0
    fi
    exit 0
fi
if [[ "${1:-}" == "ps" || "${1:-}" == "pull" ]]; then
    exit 0
fi
exit 0
DOCKER

    cat > "$bin_dir/sleep" <<'SLEEP'
#!/usr/bin/env bash
exit 0
SLEEP

    # A clean-room PATH: symlink every real tool except the ones the case
    # marks absent, so `command -v` (not a failing stub) drives the fallback.
    local d c b
    for d in /usr/bin /bin /usr/local/bin; do
        [[ -d "$d" ]] || continue
        for c in "$d"/*; do
            b="${c##*/}"
            case " $missing_tools $failing_tools " in
                *" $b "*) continue ;;
            esac
            [[ -e "$clean_dir/$b" ]] || ln -s "$c" "$clean_dir/$b" 2>/dev/null || true
        done
    done
    for b in $failing_tools; do
        printf '#!/usr/bin/env bash\nexit 1\n' > "$bin_dir/$b"
    done
    for b in sudo systemctl; do
        printf '#!/usr/bin/env bash\nprintf "%%s\\n" "${0##*/}" >> "$ODS_FIXTURE_TOOL_LOG"\nexit 1\n' > "$bin_dir/$b"
    done
    chmod +x "$bin_dir"/*

    set +e
    PATH="$bin_dir:$clean_dir" \
    ODS_FIXTURE_TOOL_LOG="$tmp_dir/tools.log" \
    INSTALL_DIR="$install_dir" \
    ODS_HOME="$install_dir" \
    NO_COLOR=1 \
    ODS_COMPOSE_PULL_RETRY_DELAY_1=0 \
    ODS_COMPOSE_PULL_RETRY_DELAY_2=0 \
    ODS_COMPOSE_PULL_RETRY_DELAY_N=0 \
        "$BASH" "$ods_cli" update --force > "$tmp_dir/update.out" 2>&1
    RC=$?
    set -e
    OUT="$(cat "$tmp_dir/update.out")"
    ENV_AFTER="$(cat "$install_dir/.env")"
    TOOL_CALLS="$(cat "$tmp_dir/tools.log" 2>/dev/null || true)"
    rm -rf "$tmp_dir"
}

# HERMES_DASHBOARD_SESSION_TOKEN is preset so the unrelated token backfill in
# get_compose_flags stays inert even when the openssl stub is poisoned.
BASE_ENV='ODS_VERSION=2.6.0
ODS_MODE=local
GPU_BACKEND=cpu
GPU_COUNT=1
TIER=1
LLAMA_CPU_LIMIT=8.0
LLAMA_CPU_RESERVATION=2.0
HERMES_DASHBOARD_SESSION_TOKEN=bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb'

# 1. Missing key: generated and written as a 64-hex value.
run_update "$BASE_ENV" "" ""
if [[ $RC -eq 0 ]] && printf '%s\n' "$ENV_AFTER" | grep -qE '^SHIELD_API_KEY=[0-9a-f]{64}$'; then
    [[ "$TOOL_CALLS" == *systemctl* && "$TOOL_CALLS" == *docker* ]] \
        || { fail "host lifecycle and Docker fixture were not exercised"; exit 1; }
    pass "missing SHIELD_API_KEY is backfilled with a 64-hex key"
else
    fail "missing key should be backfilled (rc=$RC): $(printf '%s' "$OUT" | tail -3)"
fi

# 2. openssl and xxd absent (minimal install): the od fallback yields a key.
run_update "$BASE_ENV" "openssl xxd" ""
if [[ $RC -eq 0 ]] && printf '%s\n' "$ENV_AFTER" | grep -qE '^SHIELD_API_KEY=[0-9a-f]{64}$'; then
    pass "missing openssl falls back to od/urandom and still writes a real key"
else
    fail "od fallback should produce a key (rc=$RC): $(printf '%s' "$OUT" | tail -3)"
fi

# 3. No usable random source: update must abort and never write an empty key.
run_update "$BASE_ENV" "xxd" "openssl"
if [[ $RC -eq 0 ]] && printf '%s\n' "$ENV_AFTER" | grep -qE '^SHIELD_API_KEY=[0-9a-f]{64}$'; then
    pass "failed openssl recovers with validated od entropy"
else
    fail "failed openssl should recover through od (rc=$RC)"
fi

run_update "$BASE_ENV" "openssl xxd" "od"
if [[ $RC -ne 0 && "$ENV_AFTER" == "$BASE_ENV" ]]; then
    pass "no random source aborts the update without writing an empty key"
else
    fail "update should abort and leave .env untouched (rc=$RC)"
fi

# 4. An empty first duplicate must preserve the effective last credential.
REAL_KEY="$(printf 'a%.0s' $(seq 64))"
DUP_ENV="$BASE_ENV
SHIELD_API_KEY=
SHIELD_API_KEY=$REAL_KEY"
run_update "$DUP_ENV" "" ""
if [[ $RC -eq 0 ]] && printf '%s\n' "$ENV_AFTER" | grep -q "^SHIELD_API_KEY=$REAL_KEY$"; then
    pass "effective last credential is preserved after an empty first assignment"
else
    fail "effective real key must survive duplicate entries (rc=$RC)"
fi

# 5. Existing real key is preserved untouched.
SET_ENV="$BASE_ENV
SHIELD_API_KEY=$REAL_KEY"
run_update "$SET_ENV" "openssl xxd" ""
if [[ $RC -eq 0 ]] && [[ "$(printf '%s\n' "$ENV_AFTER" | grep -c "^SHIELD_API_KEY=$REAL_KEY$")" == "1" ]]; then
    pass "existing SHIELD_API_KEY is preserved"
else
    fail "existing key must be preserved (rc=$RC)"
fi

run_update "$BASE_ENV
SHIELD_API_KEY='owner literal # credential'" "xxd" "openssl od"
if [[ $RC -eq 0 ]] && [[ "$(printf '%s\n' "$ENV_AFTER" | grep '^SHIELD_API_KEY=')" == "SHIELD_API_KEY='owner literal # credential'" ]]; then
    pass "quoted owner credential survives without available entropy"
else
    fail "quoted owner credential must remain byte-for-byte (rc=$RC)"
fi

# The CLI prioritizes INSTALL_DIR over ODS_HOME. An inherited operator setting
# must never let this test select another installation or its updater.
sentinel="$(mktemp -d)"
trap 'rm -rf "$sentinel"' EXIT
printf 'OWNER_DATA=preserve\n' > "$sentinel/.env"
cp "$sentinel/.env" "$sentinel/env.orig"
printf '#!/bin/sh\ntouch "${INSTALL_DIR}/updater-ran"\n' > "$sentinel/ods-update.sh"
chmod +x "$sentinel/ods-update.sh"
INSTALL_DIR="$sentinel" run_update "$SET_ENV" "xxd" "openssl od"
if [[ $RC -eq 0 ]] && cmp -s "$sentinel/.env" "$sentinel/env.orig" && [[ ! -e "$sentinel/updater-ran" ]]; then
    pass "inherited INSTALL_DIR cannot redirect the isolated update fixture"
else
    fail "inherited INSTALL_DIR escaped the isolated fixture (rc=$RC)"
fi

echo ""
echo "Result: $PASSED passed, $FAILED failed"
[[ $FAILED -eq 0 ]]