|
| 1 | +#!/usr/bin/env bash |
| 2 | +set -euo pipefail |
| 3 | + |
| 4 | +if [ "$(id -u)" -ne 0 ]; then |
| 5 | + echo "run this installer as root" >&2 |
| 6 | + exit 1 |
| 7 | +fi |
| 8 | + |
| 9 | +SOURCE_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" |
| 10 | +AGENTS=(cha0spunk coinpay knox nora quill riotcoder rootkey) |
| 11 | +MAIL_GROUP=fleet-agent-mail |
| 12 | +CLI_DIR=/opt/riotcoder-cli |
| 13 | +NODE_DIR=/home/riotcoder/.local/share/agent-phone |
| 14 | +CLAIM_DIR=/var/lib/fleet-agent-mail/claims |
| 15 | + |
| 16 | +id riotcoder >/dev/null |
| 17 | +[ -x "$NODE_DIR/node" ] || { echo "deploy group.call first to install private Node runtimes" >&2; exit 1; } |
| 18 | +[ -d "$CLI_DIR/node_modules" ] || { echo "CLI runtime dependencies are missing at $CLI_DIR" >&2; exit 1; } |
| 19 | +getent group "$MAIL_GROUP" >/dev/null || groupadd --system "$MAIL_GROUP" |
| 20 | +for agent in "${AGENTS[@]}"; do |
| 21 | + id "$agent" >/dev/null || { echo "missing Linux account: $agent" >&2; exit 1; } |
| 22 | + groups=" $(id -nG "$agent") " |
| 23 | + case "$groups" in *" sudo "*|*" wheel "*|*" admin "*) echo "$agent has an admin group" >&2; exit 1;; esac |
| 24 | + usermod -a -G "$MAIL_GROUP" "$agent" |
| 25 | +done |
| 26 | + |
| 27 | +# Root owns the deployed code. Only Fleet agent accounts need to read it. |
| 28 | +chgrp -R "$MAIL_GROUP" "$CLI_DIR" |
| 29 | +chmod -R o-rwx "$CLI_DIR" |
| 30 | +chmod -R g+rX "$CLI_DIR" |
| 31 | +install -d -o root -g "$MAIL_GROUP" -m 2770 /var/lib/fleet-agent-mail "$CLAIM_DIR" |
| 32 | +install -o root -g root -m 0755 "$SOURCE_DIR/qc-agent-responder.py" /usr/local/libexec/qc-agent-responder |
| 33 | + |
| 34 | +ready=() |
| 35 | +skipped=() |
| 36 | +for agent in "${AGENTS[@]}"; do |
| 37 | + home="$(getent passwd "$agent" | cut -d: -f6)" |
| 38 | + group="$(id -gn "$agent")" |
| 39 | + node="$home/.local/share/agent-phone/node" |
| 40 | + install -d -o "$agent" -g "$group" -m 0750 "$home/.config/agent-identities/$agent" |
| 41 | + identity_source="/home/anthony/.config/agent-identities/$agent" |
| 42 | + if [ -f "$identity_source/mail.env" ]; then |
| 43 | + install -o "$agent" -g "$group" -m 0600 "$identity_source/mail.env" "$home/.config/agent-identities/$agent/mail.env" |
| 44 | + rm -f "$identity_source/mail.env" |
| 45 | + fi |
| 46 | + if [ -f "$identity_source/mail-prompt.md" ]; then |
| 47 | + install -o "$agent" -g "$group" -m 0640 "$identity_source/mail-prompt.md" "$home/.config/agent-identities/$agent/mail-prompt.md" |
| 48 | + fi |
| 49 | + if [ -f "$identity_source/AGENTS.md" ]; then |
| 50 | + install -o "$agent" -g "$group" -m 0640 "$identity_source/AGENTS.md" "$home/.config/agent-identities/$agent/AGENTS.md" |
| 51 | + fi |
| 52 | + if [ -f "$identity_source/profile.json" ]; then |
| 53 | + install -o "$agent" -g "$group" -m 0640 "$identity_source/profile.json" "$home/.config/agent-identities/$agent/profile.json" |
| 54 | + fi |
| 55 | + missing=() |
| 56 | + for path in \ |
| 57 | + "$home/.config/agent-llm.json" \ |
| 58 | + "$home/.config/cli-tools/mail.json" \ |
| 59 | + "$home/.codex/auth.json" \ |
| 60 | + "$home/.config/qc/session.json" \ |
| 61 | + "$home/.config/qc-agent/qc-passphrase" \ |
| 62 | + "$home/.config/qc-agent/persona.md"; do |
| 63 | + [ -f "$path" ] || missing+=("${path#"$home"/}") |
| 64 | + done |
| 65 | + if [ "${#missing[@]}" -gt 0 ]; then |
| 66 | + skipped+=("$agent (${missing[*]})") |
| 67 | + continue |
| 68 | + fi |
| 69 | + if [ ! -x "$node" ]; then |
| 70 | + skipped+=("$agent (missing private Node runtime)") |
| 71 | + continue |
| 72 | + fi |
| 73 | + |
| 74 | + install -d -o "$agent" -g "$group" -m 0750 "$home/.local/bin" "$home/.local/state/agent-mail-poll" |
| 75 | + install -o "$agent" -g "$group" -m 0750 "$SOURCE_DIR/agent-mail-trusted.py" "$home/.local/bin/agent-mail-trusted" |
| 76 | + install -o "$agent" -g "$group" -m 0750 "$SOURCE_DIR/agent-mail-claim" "$home/.local/bin/agent-mail-claim" |
| 77 | + |
| 78 | + cat > "$home/.local/bin/mail.tmp" <<EOF |
| 79 | +#!/bin/sh |
| 80 | +export HOME=$home |
| 81 | +exec $node $CLI_DIR/bin/mail.ts "\$@" |
| 82 | +EOF |
| 83 | + chown "$agent:$group" "$home/.local/bin/mail.tmp" |
| 84 | + chmod 0750 "$home/.local/bin/mail.tmp" |
| 85 | + mv "$home/.local/bin/mail.tmp" "$home/.local/bin/mail" |
| 86 | + |
| 87 | + cat > "$home/.local/bin/agent-mail-poll.tmp" <<EOF |
| 88 | +#!/bin/sh |
| 89 | +export HOME=$home |
| 90 | +exec $node $CLI_DIR/bin/agent-mail-poll.ts "\$@" |
| 91 | +EOF |
| 92 | + chown "$agent:$group" "$home/.local/bin/agent-mail-poll.tmp" |
| 93 | + chmod 0750 "$home/.local/bin/agent-mail-poll.tmp" |
| 94 | + mv "$home/.local/bin/agent-mail-poll.tmp" "$home/.local/bin/agent-mail-poll" |
| 95 | + |
| 96 | + cat > "/etc/systemd/system/agent-mail-poll-$agent.service" <<EOF |
| 97 | +[Unit] |
| 98 | +Description=$agent trusted email task poller |
| 99 | +After=network-online.target |
| 100 | +Wants=network-online.target |
| 101 | +
|
| 102 | +[Service] |
| 103 | +Type=oneshot |
| 104 | +User=$agent |
| 105 | +Group=$group |
| 106 | +SupplementaryGroups=$MAIL_GROUP |
| 107 | +WorkingDirectory=$home |
| 108 | +Environment=HOME=$home |
| 109 | +Environment=AGENT_MAIL_CLAIM_ROOT=$CLAIM_DIR |
| 110 | +Environment=PATH=$home/.local/share/agent-phone:$home/.local/bin:/usr/local/bin:/usr/bin:/bin |
| 111 | +ExecStart=$home/.local/bin/agent-mail-poll poll --agent $agent |
| 112 | +EOF |
| 113 | + |
| 114 | + cat > "/etc/systemd/system/agent-mail-poll-$agent.timer" <<EOF |
| 115 | +[Unit] |
| 116 | +Description=Poll $agent's inbox every five minutes |
| 117 | +
|
| 118 | +[Timer] |
| 119 | +OnCalendar=*-*-* *:0/5:00 |
| 120 | +AccuracySec=30s |
| 121 | +Persistent=true |
| 122 | +
|
| 123 | +[Install] |
| 124 | +WantedBy=timers.target |
| 125 | +EOF |
| 126 | + chmod 0644 "/etc/systemd/system/agent-mail-poll-$agent.service" "/etc/systemd/system/agent-mail-poll-$agent.timer" |
| 127 | + ready+=("$agent") |
| 128 | +done |
| 129 | + |
| 130 | +systemctl daemon-reload |
| 131 | +for agent in "${ready[@]}"; do |
| 132 | + systemctl enable --now "agent-mail-poll-$agent.timer" >/dev/null |
| 133 | +done |
| 134 | +printf 'Installed contact runners for: %s\n' "${ready[*]:-(none)}" |
| 135 | +if [ "${#skipped[@]}" -gt 0 ]; then |
| 136 | + printf 'Skipped accounts missing their own credentials: %s\n' "${skipped[*]}" |
| 137 | +fi |
0 commit comments