-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsetup.sh
More file actions
executable file
·269 lines (240 loc) · 9.99 KB
/
setup.sh
File metadata and controls
executable file
·269 lines (240 loc) · 9.99 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
#!/bin/bash
# OpenClaw E2E Test Suite — Interactive Setup
#
# Generates a .env file by auto-detecting your deployment or asking questions.
#
# Usage:
# ./setup.sh # Interactive mode
# ./setup.sh --auto # Auto-detect only, no prompts
set -uo pipefail
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
ENV_FILE="$SCRIPT_DIR/.env"
AUTO_MODE=false
[[ "${1:-}" == "--auto" ]] && AUTO_MODE=true
# Colors
GREEN='\033[0;32m'; YELLOW='\033[1;33m'; CYAN='\033[0;36m'; RED='\033[0;31m'; NC='\033[0m'; BOLD='\033[1m'
echo ""
echo -e "${BOLD}OpenClaw E2E Test Suite — Setup${NC}"
echo ""
if [ -f "$ENV_FILE" ]; then
echo -e "${YELLOW}Warning: .env already exists. This will overwrite it.${NC}"
if ! $AUTO_MODE; then
read -rp "Continue? [y/N] " confirm
[[ "$confirm" != [yY] ]] && echo "Aborted." && exit 0
fi
fi
# ─── Discover gateway ────────────────────────────────────────────────
GATEWAY_URL=""
SSH_HOST=""
DOCKER_BIN=""
CONTAINER="openclaw-gateway"
NATIVE_MODE=""
MEMORY_URL=""
MEMORY_NS="default"
SLACK_ENABLED=""
DISCORD_ENABLED=""
echo -e "${CYAN}Checking localhost:18789...${NC}"
if curl -sf --max-time 3 "http://localhost:18789" >/dev/null 2>&1; then
GATEWAY_URL="http://localhost:18789"
echo -e "${GREEN} Found local gateway at $GATEWAY_URL${NC}"
else
echo -e " No local gateway detected."
if ! $AUTO_MODE; then
read -rp "Gateway URL (e.g., http://192.168.1.100:18789): " GATEWAY_URL
fi
fi
if [ -z "$GATEWAY_URL" ]; then
echo -e "${RED}No gateway URL configured. Cannot continue.${NC}"
exit 1
fi
# ─── Detect native install ──────────────────────────────────────────
echo ""
echo -e "${CYAN}Checking for native OpenClaw install...${NC}"
if command -v openclaw &>/dev/null; then
OC_VERSION=$(openclaw --version 2>/dev/null | head -1)
# Validate it's actually OpenClaw (version output should be numeric-ish)
if [ -n "$OC_VERSION" ] && [ -d "${OPENCLAW_MAC_CONFIG_DIR:-$HOME/.openclaw}" ]; then
echo -e "${GREEN} Found native OpenClaw: $OC_VERSION${NC}"
# Check if a Docker container also exists
if command -v docker &>/dev/null && docker inspect "$CONTAINER" &>/dev/null 2>&1; then
echo -e "${YELLOW} NOTE: Docker container '$CONTAINER' also found.${NC}"
if ! $AUTO_MODE; then
read -rp " Use native mode? [Y/n] " use_native
[[ "$use_native" == [nN] ]] || NATIVE_MODE="true"
else
# Auto mode: prefer native when CLI is installed
NATIVE_MODE="true"
fi
else
NATIVE_MODE="true"
fi
fi
fi
# ─── Discover container access ────────────────────────────────────────
if [ "$NATIVE_MODE" != "true" ]; then
echo ""
echo -e "${CYAN}Checking container access...${NC}"
# Try local Docker first
if command -v docker &>/dev/null; then
if docker inspect "$CONTAINER" &>/dev/null 2>&1; then
DOCKER_BIN="docker"
echo -e "${GREEN} Found local Docker container '$CONTAINER'${NC}"
fi
fi
# If no local container, ask about SSH
if [ -z "$DOCKER_BIN" ] && ! $AUTO_MODE; then
echo " No local container found."
read -rp "SSH host for remote Docker (leave empty for API-only): " SSH_HOST
if [ -n "$SSH_HOST" ]; then
echo -e "${CYAN} Testing SSH connection to $SSH_HOST...${NC}"
if ssh -o ConnectTimeout=5 -o BatchMode=yes "$SSH_HOST" "echo ok" &>/dev/null; then
echo -e "${GREEN} SSH connected.${NC}"
# Try to find Docker binary
for try_bin in docker /usr/bin/docker /usr/local/bin/docker; do
if ssh "$SSH_HOST" "command -v $try_bin || test -x $try_bin" &>/dev/null 2>&1; then
DOCKER_BIN="$try_bin"
echo -e "${GREEN} Found Docker at $DOCKER_BIN${NC}"
break
fi
done
# Check QNAP Container Station path
if [ -z "$DOCKER_BIN" ]; then
QNAP_DOCKER="/share/CACHEDEV1_DATA/.qpkg/container-station/bin/docker"
if ssh "$SSH_HOST" "test -x $QNAP_DOCKER" &>/dev/null 2>&1; then
DOCKER_BIN="$QNAP_DOCKER"
echo -e "${GREEN} Found QNAP Docker at $DOCKER_BIN${NC}"
fi
fi
if [ -z "$DOCKER_BIN" ]; then
read -rp " Docker binary path on remote host: " DOCKER_BIN
fi
else
echo -e "${RED} SSH connection failed. Continuing in API-only mode.${NC}"
SSH_HOST=""
fi
fi
fi
else
echo -e "${GREEN} Using native mode (no Docker needed)${NC}"
fi
# ─── Discover memory server ──────────────────────────────────────────
echo ""
echo -e "${CYAN}Checking for memory server...${NC}"
# Try to read gateway config — from disk (native) or container (Docker)
GW_CONFIG=""
if [ "$NATIVE_MODE" = "true" ]; then
CONFIG_DIR="${OPENCLAW_MAC_CONFIG_DIR:-$HOME/.openclaw}"
GW_CONFIG=$(cat "$CONFIG_DIR/openclaw.json" 2>/dev/null || true)
elif [ -n "$DOCKER_BIN" ]; then
CONFIG_CMD="cat /home/node/.openclaw/openclaw.json 2>/dev/null"
if [ -n "$SSH_HOST" ]; then
GW_CONFIG=$(ssh "$SSH_HOST" "$DOCKER_BIN exec $CONTAINER sh -c '$CONFIG_CMD'" 2>/dev/null || true)
else
GW_CONFIG=$($DOCKER_BIN exec "$CONTAINER" sh -c "$CONFIG_CMD" 2>/dev/null || true)
fi
fi
if [ -n "$GW_CONFIG" ]; then
MEMORY_URL=$(echo "$GW_CONFIG" | python3 -c "
import sys, json
try:
cfg = json.load(sys.stdin)
# Check plugins.entries (current format) and plugins (legacy format)
entries = cfg.get('plugins', {}).get('entries', cfg.get('plugins', {}))
for name, pcfg in entries.items():
if 'memory' in name.lower():
# Try all known config paths
url = (pcfg.get('config', {}).get('serverUrl', '') or
pcfg.get('settings', {}).get('apiUrl', '') or
pcfg.get('settings', {}).get('serverUrl', ''))
if url: print(url); break
except: pass
" 2>/dev/null || true)
if [ -n "$MEMORY_URL" ]; then
echo -e "${GREEN} Found memory server: $MEMORY_URL${NC}"
MEMORY_NS=$(echo "$GW_CONFIG" | python3 -c "
import sys, json
try:
cfg = json.load(sys.stdin)
entries = cfg.get('plugins', {}).get('entries', cfg.get('plugins', {}))
for name, pcfg in entries.items():
if 'memory' in name.lower():
ns = (pcfg.get('config', {}).get('namespace', '') or
pcfg.get('settings', {}).get('namespace', 'default'))
print(ns); break
except: print('default')
" 2>/dev/null || echo "default")
fi
fi
if [ -z "$MEMORY_URL" ] && ! $AUTO_MODE; then
read -rp "Memory server URL (leave empty to skip): " MEMORY_URL
fi
# ─── Discover channels ───────────────────────────────────────────────
if [ -n "${GW_CONFIG:-}" ]; then
HAS_SLACK=$(echo "$GW_CONFIG" | python3 -c "
import sys, json
try:
cfg = json.load(sys.stdin)
slack = cfg.get('channels', {}).get('slack', cfg.get('slack', {}))
if slack.get('appToken') or slack.get('botToken'):
print('true')
except: pass
" 2>/dev/null || true)
HAS_DISCORD=$(echo "$GW_CONFIG" | python3 -c "
import sys, json
try:
cfg = json.load(sys.stdin)
discord = cfg.get('channels', {}).get('discord', cfg.get('discord', {}))
if discord.get('token'):
print('true')
except: pass
" 2>/dev/null || true)
[ "$HAS_SLACK" = "true" ] && SLACK_ENABLED="true" && echo -e "${GREEN} Detected Slack channel${NC}"
[ "$HAS_DISCORD" = "true" ] && DISCORD_ENABLED="true" && echo -e "${GREEN} Detected Discord channel${NC}"
fi
# ─── Write .env ───────────────────────────────────────────────────────
echo ""
echo -e "${CYAN}Writing .env...${NC}"
cat > "$ENV_FILE" << EOF
# OpenClaw E2E Test Suite Configuration
# Generated by setup.sh on $(date -u +"%Y-%m-%dT%H:%M:%SZ")
OPENCLAW_GATEWAY_URL="$GATEWAY_URL"
EOF
if [ "$NATIVE_MODE" = "true" ]; then
cat >> "$ENV_FILE" << EOF
OPENCLAW_NATIVE=true
EOF
elif [ -n "$SSH_HOST" ]; then
cat >> "$ENV_FILE" << EOF
OPENCLAW_SSH_HOST="$SSH_HOST"
OPENCLAW_DOCKER_BIN="$DOCKER_BIN"
OPENCLAW_CONTAINER="$CONTAINER"
EOF
elif [ -n "$DOCKER_BIN" ]; then
cat >> "$ENV_FILE" << EOF
OPENCLAW_DOCKER_BIN="$DOCKER_BIN"
OPENCLAW_CONTAINER="$CONTAINER"
EOF
fi
if [ -n "$MEMORY_URL" ]; then
cat >> "$ENV_FILE" << EOF
OPENCLAW_MEMORY_SERVER_URL="$MEMORY_URL"
OPENCLAW_MEMORY_NAMESPACE="$MEMORY_NS"
EOF
fi
[ "$SLACK_ENABLED" = "true" ] && echo "OPENCLAW_SLACK_ENABLED=true" >> "$ENV_FILE"
[ "$DISCORD_ENABLED" = "true" ] && echo "OPENCLAW_DISCORD_ENABLED=true" >> "$ENV_FILE"
echo -e "${GREEN} Written to $ENV_FILE${NC}"
# ─── Smoke test ───────────────────────────────────────────────────────
echo ""
echo -e "${CYAN}Running smoke test...${NC}"
HTTP_CODE=$(curl -sf -o /dev/null -w "%{http_code}" --max-time 5 "$GATEWAY_URL" 2>/dev/null || echo "000")
if [ "$HTTP_CODE" = "200" ] || [ "$HTTP_CODE" = "401" ]; then
echo -e "${GREEN} Gateway responded (HTTP $HTTP_CODE) — setup complete!${NC}"
else
echo -e "${YELLOW} Gateway returned HTTP $HTTP_CODE — check your OPENCLAW_GATEWAY_URL${NC}"
fi
echo ""
echo -e "${BOLD}Next steps:${NC}"
echo -e " 1. Review and edit .env if needed"
echo -e " 2. Run: ${CYAN}./openclaw-test.sh${NC}"
echo ""