#!/usr/bin/env sh set -eu BASE_URL='https://station.dmstech.tech' AGENT_VERSION='2026-07-22-v5-macos-openinterpreter' echo 'RAC Station - installation/reparation Agent V5 Linux/macOS + Open Interpreter' echo 'Cet agent donne un acces distant admin a cette machine. Continuez seulement avec autorisation.' OS=$(uname -s 2>/dev/null || echo unknown) DIR=/opt/rac-station-agent CONFIG=$DIR/config.json STATE=$DIR/state.json mkdir -p "$DIR" TOKEN='' if [ -f "$STATE" ]; then echo 'Etat agent existant detecte: reparation/mise a jour sans nouveau token.' else if [ -n "${RAC_ENROLLMENT_TOKEN:-}" ]; then TOKEN=$RAC_ENROLLMENT_TOKEN echo 'Token d enrollement recu via RAC_ENROLLMENT_TOKEN.' elif [ -r /dev/tty ]; then printf 'Collez le token d enrollement: ' > /dev/tty IFS= read -r TOKEN < /dev/tty else echo 'Erreur: impossible de lire le token depuis le terminal.' >&2 echo 'Relance avec: curl -fsSL https://station.dmstech.tech/install | sudo RAC_ENROLLMENT_TOKEN=TON_TOKEN bash' >&2 exit 1 fi [ -n "$TOKEN" ] || { echo 'Token manquant' >&2; exit 1; } fi TMP_AGENT="$DIR/agent.sh.tmp.$$" echo "Telechargement agent depuis $BASE_URL/agent.sh ..." if command -v curl >/dev/null 2>&1; then curl -fL --connect-timeout 20 --max-time 180 "$BASE_URL/agent.sh" -o "$TMP_AGENT" elif command -v wget >/dev/null 2>&1; then wget -O "$TMP_AGENT" "$BASE_URL/agent.sh" else echo 'Erreur: curl ou wget requis.' >&2 exit 1 fi if [ ! -s "$TMP_AGENT" ]; then echo "Erreur: agent.sh vide ou non telecharge: $TMP_AGENT" >&2 exit 1 fi if ! head -n 1 "$TMP_AGENT" | grep -q '^#!'; then echo 'Erreur: reponse invalide pour agent.sh. Debut du fichier:' >&2 head -20 "$TMP_AGENT" >&2 || true exit 1 fi mv -f "$TMP_AGENT" "$DIR/agent.sh" chmod 700 "$DIR/agent.sh" echo 'Preparation Open Interpreter sur cette machine...' OI_LOG=$DIR/open-interpreter-install.log { if command -v python3 >/dev/null 2>&1; then OI_VENV=$DIR/oi-venv python3 -m venv "$OI_VENV" || { python3 -m ensurepip --upgrade || true; python3 -m venv "$OI_VENV"; } "$OI_VENV/bin/python" -m pip install --upgrade pip wheel 'setuptools<81' "$OI_VENV/bin/python" -m pip install --upgrade open-interpreter "$OI_VENV/bin/python" -m pip install --force-reinstall 'setuptools<81' "$OI_VENV/bin/interpreter" --version else echo 'Python3 absent: Open Interpreter non installe automatiquement.' if [ "$OS" = 'Darwin' ]; then echo 'Astuce macOS: installer Python depuis https://www.python.org/downloads/macos/ ou brew install python.'; fi fi } >"$OI_LOG" 2>&1 || true echo "Open Interpreter: installation best-effort terminee. Log: $OI_LOG" NOW=$(python3 - <<'PY' 2>/dev/null || date from datetime import datetime, timezone print(datetime.now(timezone.utc).isoformat()) PY ) if [ ! -f "$CONFIG" ]; then python3 - "$CONFIG" "$BASE_URL" "$TOKEN" "$AGENT_VERSION" "$NOW" <<'PY' import json,sys p,base,token,ver,now=sys.argv[1:6] json.dump({'baseUrl':base,'enrollmentToken':token,'agentVersion':ver,'installedAt':now},open(p,'w'),separators=(',',':')) PY else python3 - "$CONFIG" "$BASE_URL" "$AGENT_VERSION" "$NOW" <<'PY' import json,sys p,base,ver,now=sys.argv[1:5] try: d=json.load(open(p)) except Exception: d={} d['baseUrl']=base; d['agentVersion']=ver; d['updatedAt']=now json.dump(d,open(p,'w'),separators=(',',':')) PY fi chmod 600 "$CONFIG" [ -n "$TOKEN" ] && rm -f "$STATE" if [ "$OS" = 'Darwin' ]; then PLIST=/Library/LaunchDaemons/com.dmstech.rac-station-agent.plist cat > "$PLIST" <<'PLIST' Labelcom.dmstech.rac-station-agent ProgramArguments/bin/sh/opt/rac-station-agent/agent.sh RunAtLoad KeepAlive StandardOutPath/opt/rac-station-agent/launchd.out.log StandardErrorPath/opt/rac-station-agent/launchd.err.log WorkingDirectory/opt/rac-station-agent PLIST chown root:wheel "$PLIST" 2>/dev/null || true chmod 644 "$PLIST" launchctl bootout system "$PLIST" >/dev/null 2>&1 || true launchctl bootstrap system "$PLIST" launchctl kickstart -k system/com.dmstech.rac-station-agent >/dev/null 2>&1 || true echo "Agent V5 installe/repare et demarre: launchd com.dmstech.rac-station-agent ($AGENT_VERSION)" echo "Log local: $DIR/agent.log" echo "Diagnostic: sudo launchctl print system/com.dmstech.rac-station-agent" elif command -v systemctl >/dev/null 2>&1; then cat >/etc/systemd/system/rac-station-agent.service <<'UNIT' [Unit] Description=RAC Station Agent V5 After=network-online.target Wants=network-online.target [Service] Type=simple ExecStart=/bin/sh /opt/rac-station-agent/agent.sh Restart=always RestartSec=10 User=root KillSignal=SIGTERM TimeoutStopSec=20 [Install] WantedBy=multi-user.target UNIT systemctl daemon-reload systemctl enable --now rac-station-agent.service systemctl restart rac-station-agent.service echo "Agent V5 installe/repare et demarre: rac-station-agent.service ($AGENT_VERSION)" echo "Log local: $DIR/agent.log" else echo 'Systeme sans launchd/systemd: lance agent en arriere-plan simple.' nohup /bin/sh "$DIR/agent.sh" >> "$DIR/nohup.log" 2>&1 & fi