summaryrefslogtreecommitdiff
path: root/execute_commands.sh
diff options
context:
space:
mode:
authorhaoyuren <13851610112@163.com>2026-03-31 19:53:56 -0500
committerhaoyuren <13851610112@163.com>2026-03-31 19:53:56 -0500
commit816e289954f03ed56dfc6d35647e9626afc3d235 (patch)
treef184c3cc57555a8e60c6fe76a1752d94ebd32428 /execute_commands.sh
parent974726fde9d027422d935d5a3c1cd740ad738cf5 (diff)
Filter commands by host, fix restart, add stop
Diffstat (limited to 'execute_commands.sh')
-rwxr-xr-xexecute_commands.sh52
1 files changed, 47 insertions, 5 deletions
diff --git a/execute_commands.sh b/execute_commands.sh
index 329e5ef..8093ad8 100755
--- a/execute_commands.sh
+++ b/execute_commands.sh
@@ -24,12 +24,30 @@ for cmd in d.get('commands', []):
echo "$(date): Executing command $CMD_ID: $ACTION on $TARGET"
+ # 只执行属于本机的命令(target 在 .sessions 里,或者 create_worker 的 host 匹配)
+ if [ "$ACTION" = "create_worker" ]; then
+ CMD_HOST=$(echo "$PARAMS" | python3 -c "import sys,json; print(json.load(sys.stdin).get('host',''))" 2>/dev/null)
+ MY_HOST=$(hostname)
+ if [ -n "$CMD_HOST" ] && [ "$CMD_HOST" != "$MY_HOST" ]; then
+ echo "$(date): Skipping $CMD_ID - host $CMD_HOST != $MY_HOST"
+ continue
+ fi
+ else
+ if ! grep -q "^${TARGET}$(printf '\t')" "$SCRIPT_DIR/.sessions" 2>/dev/null; then
+ echo "$(date): Skipping $CMD_ID - $TARGET not in local .sessions"
+ continue
+ fi
+ fi
+
# 检查 tmux session 存在
- if ! tmux has-session -t "$TARGET" 2>/dev/null; then
- curl -sf -X POST -H "$AUTH" -H "Content-Type: application/json" \
- -d "{\"result\": \"ERROR: tmux session $TARGET not found\"}" \
- "$BROKER_URL/commands/$CMD_ID/done" >/dev/null 2>&1
- continue
+ # create_worker 不需要 session 已存在
+ if [ "$ACTION" != "create_worker" ]; then
+ if ! tmux has-session -t "$TARGET" 2>/dev/null; then
+ curl -sf -X POST -H "$AUTH" -H "Content-Type: application/json" \
+ -d "{\"result\": \"ERROR: tmux session $TARGET not found\"}" \
+ "$BROKER_URL/commands/$CMD_ID/done" >/dev/null 2>&1
+ continue
+ fi
fi
case "$ACTION" in
@@ -75,6 +93,30 @@ for cmd in d.get('commands', []):
RESULT="OK: $TARGET restarted"
;;
+ create_worker)
+ SESSION_NAME=$(echo "$PARAMS" | python3 -c "import sys,json; print(json.load(sys.stdin).get('session_name',''))")
+ DIR=$(echo "$PARAMS" | python3 -c "import sys,json; print(json.load(sys.stdin).get('path',''))")
+ SLACK_CH=$(echo "$PARAMS" | python3 -c "import sys,json; print(json.load(sys.stdin).get('slack_channel',''))")
+ if [ -z "$SESSION_NAME" ] || [ -z "$DIR" ]; then
+ RESULT="ERROR: missing session_name or path"
+ elif tmux has-session -t "$SESSION_NAME" 2>/dev/null; then
+ RESULT="OK: session $SESSION_NAME already exists"
+ else
+ # 用交互式脚本创建,自动处理弹窗
+ RESULT=$(bash "$SCRIPT_DIR/create_worker.sh" "$SESSION_NAME" "$DIR" "$BROKER_URL" "$API_SECRET" "$SLACK_CH" 2>&1 | tail -1)
+ fi
+ ;;
+
+ stop)
+ tmux send-keys -t "$TARGET" "/exit" Enter
+ sleep 3
+ tmux kill-session -t "$TARGET" 2>/dev/null || true
+ # 从 .sessions 移除
+ grep -v "^${TARGET}$(printf '\t')" "$SCRIPT_DIR/.sessions" > "${SCRIPT_DIR}/.sessions.tmp" 2>/dev/null || true
+ mv "${SCRIPT_DIR}/.sessions.tmp" "$SCRIPT_DIR/.sessions"
+ RESULT="OK: $TARGET stopped and removed"
+ ;;
+
*)
RESULT="ERROR: unknown action $ACTION"
;;