Files
CyberGril/deploy/hel914-user/activate.sh
T

67 lines
3.4 KiB
Bash

#!/usr/bin/env bash
# Authorized once by HEL-797 comment 01a18a81-21fd-7322-b2c7-295c3fa517aa.
# Install the deadline FIRST, then start the empty user instance. No model probe.
set -euo pipefail
[ "$#" -eq 0 ] || { echo "No deadline override: this command opens only the approved three-day window" >&2; exit 2; }
BASE=/opt/cybergril-hel913
UNIT=cybergril-hel913-user
if systemctl is-active --quiet "$UNIT.service" || [ -f "$BASE/window-active" ]; then
echo 'REFUSED: user window already active; no second activation' >&2; exit 1
fi
"$BASE/venv311/bin/python" - "$BASE" <<'PY'
import pathlib,sqlite3,sys
b=pathlib.Path(sys.argv[1]);p=b/'data/companion.db'
if p.exists():
c=sqlite3.connect('file:'+str(p)+'?mode=ro',uri=True)
if c.execute('select count(*) from companion').fetchone()[0]:
raise SystemExit('REFUSED: an existing companion must not be reinitialized')
c.close()
PY
cp "$BASE/etc/models.yaml" "$BASE/etc/models-before-activation.yaml"
failed() {
rm -f "$BASE/window-active"
systemctl stop "$UNIT.service" "$UNIT-expiry.timer" || true
systemctl disable "$UNIT.service" "$UNIT-expiry.timer" || true
cp "$BASE/etc/models-before-activation.yaml" "$BASE/etc/models.yaml"
echo 'Activation failed; owned instance stopped, previous budget restored; logs preserved' >&2
}
trap failed ERR
"$BASE/venv311/bin/python" - "$BASE" <<'PY'
import datetime,json,pathlib,sys,zoneinfo
b=pathlib.Path(sys.argv[1]);tz=zoneinfo.ZoneInfo('Asia/Shanghai')
now=datetime.datetime.now(tz)
end=datetime.datetime.combine(now.date()+datetime.timedelta(days=3),datetime.time(),tzinfo=tz)
w={'approved':True,'authorization_comment':'01a18a81-21fd-7322-b2c7-295c3fa517aa','started_at':now.isoformat(),'deadline_at':end.isoformat(),'deadline_epoch':int(end.timestamp())}
(b/'window.json').write_text(json.dumps(w,indent=2)+'\n');(b/'window.json').chmod(0o600)
calendar=end.astimezone(datetime.timezone.utc).strftime('%Y-%m-%d %H:%M:%S UTC')
p=pathlib.Path('/etc/systemd/system/cybergril-hel913-user-expiry.timer')
p.write_text('[Unit]\nDescription=Deadline of approved CyberGril first-use window\n\n[Timer]\nOnCalendar='+calendar+'\nPersistent=true\nUnit=cybergril-hel913-user-expiry.service\n\n[Install]\nWantedBy=timers.target\n')
PY
install -m 644 "$BASE/ops/$UNIT.service" /etc/systemd/system/
install -m 644 "$BASE/ops/$UNIT-expiry.service" /etc/systemd/system/
systemd-analyze verify "/etc/systemd/system/$UNIT.service" "/etc/systemd/system/$UNIT-expiry.service" "/etc/systemd/system/$UNIT-expiry.timer"
systemctl daemon-reload
systemctl enable --now "$UNIT-expiry.timer"
systemctl is-active --quiet "$UNIT-expiry.timer"
next=$(systemctl show "$UNIT-expiry.timer" -p NextElapseUSecRealtime --value)
[ -n "$next" ] && [ "$next" != n/a ]
install -m 600 "$BASE/etc/models-user-approved.yaml" "$BASE/etc/models.yaml.next"
mv "$BASE/etc/models.yaml.next" "$BASE/etc/models.yaml"
touch "$BASE/window-active"
systemctl enable --now "$UNIT.service"
ready=0
for attempt in $(seq 1 20); do
if curl --fail --silent --max-time 2 http://127.0.0.1:8360/api/health > "$BASE/logs/user-activation-health.json"; then ready=1; break; fi
sleep 1
done
[ "$ready" = 1 ]
"$BASE/venv311/bin/python" - "$BASE/logs/user-activation-health.json" <<'PY'
import json,sys
h=json.load(open(sys.argv[1]))
assert h['ok'] and h['llm']['ok'] and not h['companion_created']
print('PASS: empty user instance ready, model routes configured; no model probe sent')
PY
trap - ERR
cat "$BASE/window.json"
systemctl list-timers "$UNIT-expiry.timer" --no-pager