-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathdetect-openclaw.sh
More file actions
executable file
·379 lines (339 loc) · 8.86 KB
/
detect-openclaw.sh
File metadata and controls
executable file
·379 lines (339 loc) · 8.86 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
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
#!/usr/bin/env bash
# openclaw detection script for mdm deployment
# exit codes: 0=not-installed (clean), 1=found (non-compliant), 2=error
set -euo pipefail
PROFILE="${OPENCLAW_PROFILE:-}"
PORT="${OPENCLAW_GATEWAY_PORT:-18789}"
print_banner() {
echo ''
echo ' ___ ___ _ '
echo ' / __/ __| /_\ '
echo ' | (__\__ \/ _ \ '
echo ' \___|___/_/ \_\'
echo ''
echo ' CSA + Knostic'
echo ' OpenClaw Detection Script'
echo ''
}
print_banner
detect_platform() {
case "$(uname -s)" in
Darwin) echo "darwin" ;;
Linux) echo "linux" ;;
*) echo "unknown" ;;
esac
}
get_state_dir() {
local home="$1"
if [[ -n "$PROFILE" ]]; then
echo "${home}/.openclaw-${PROFILE}"
else
echo "${home}/.openclaw"
fi
}
get_users_to_check() {
local platform="$1"
if [[ $EUID -eq 0 ]]; then
case "$platform" in
darwin)
for dir in /Users/*; do
[[ -d "$dir" && "$(basename "$dir")" != "Shared" ]] && basename "$dir"
done
;;
linux)
for dir in /home/*; do
[[ -d "$dir" ]] && basename "$dir"
done
;;
esac
else
whoami
fi
}
get_home_dir() {
local user="$1"
local platform="$2"
case "$platform" in
darwin) echo "/Users/$user" ;;
linux) echo "/home/$user" ;;
esac
}
check_cli_in_path() {
local path
path=$(command -v openclaw 2>/dev/null) || true
if [[ -n "$path" ]]; then
echo "$path"
return 0
fi
return 1
}
check_cli_for_user() {
local home="$1"
local locations=(
"${home}/.volta/bin/openclaw"
"${home}/.local/bin/openclaw"
"${home}/.nvm/current/bin/openclaw"
"${home}/bin/openclaw"
)
for loc in "${locations[@]}"; do
if [[ -x "$loc" ]]; then
echo "$loc"
return 0
fi
done
return 1
}
check_cli_global() {
local locations=(
"/usr/local/bin/openclaw"
"/opt/homebrew/bin/openclaw"
"/usr/bin/openclaw"
)
for loc in "${locations[@]}"; do
if [[ -x "$loc" ]]; then
echo "$loc"
return 0
fi
done
return 1
}
check_mac_app() {
local app_path="/Applications/OpenClaw.app"
if [[ -d "$app_path" ]]; then
echo "$app_path"
return 0
else
echo "not-found"
return 1
fi
}
check_state_dir() {
local state_dir="$1"
if [[ -d "$state_dir" ]]; then
echo "$state_dir"
return 0
else
echo "not-found"
return 1
fi
}
check_config() {
local config_file="${1}/openclaw.json"
if [[ -f "$config_file" ]]; then
echo "$config_file"
else
echo "not-found"
fi
}
check_launchd_service() {
local label uid
uid=$(id -u)
if [[ -n "$PROFILE" ]]; then
label="bot.molt.${PROFILE}"
else
label="bot.molt.gateway"
fi
if launchctl print "gui/${uid}/${label}" &>/dev/null; then
echo "gui/${uid}/${label}"
else
echo "not-loaded"
fi
}
check_systemd_service() {
local service
if [[ -n "$PROFILE" ]]; then
service="openclaw-gateway-${PROFILE}.service"
else
service="openclaw-gateway.service"
fi
if systemctl --user is-active "$service" &>/dev/null; then
echo "$service"
else
echo "inactive"
fi
}
get_configured_port() {
local config_file="$1"
if [[ -f "$config_file" ]]; then
# extract port from json without jq (mdm environments may not have it)
grep -o '"port"[[:space:]]*:[[:space:]]*[0-9]*' "$config_file" 2>/dev/null | head -1 | grep -o '[0-9]*$' || true
fi
}
check_gateway_port() {
local port="$1"
if nc -z localhost "$port" &>/dev/null; then
echo "listening"
return 0
else
echo "not-listening"
return 1
fi
}
check_docker_containers() {
if ! command -v docker &>/dev/null; then
return 0
fi
docker ps --format '{{.Names}} ({{.Image}})' 2>/dev/null | grep -i openclaw || true
}
check_docker_images() {
if ! command -v docker &>/dev/null; then
return 0
fi
docker images --format '{{.Repository}}:{{.Tag}}' 2>/dev/null | grep -i openclaw || true
}
main() {
local platform cli_found=false app_found=false state_found=false service_running=false port_listening=false
local output=""
out() { output+="$1"$'\n'; }
platform=$(detect_platform)
out "platform: $platform"
if [[ "$platform" == "unknown" ]]; then
echo "summary: error"
echo "$output"
exit 2
fi
# check global CLI locations first
local cli_result=""
cli_result=$(check_cli_in_path) || cli_result=$(check_cli_global) || true
if [[ -n "$cli_result" ]]; then
cli_found=true
out "cli: $cli_result"
out "cli-version: $("$cli_result" --version 2>/dev/null | head -1 || echo "unknown")"
fi
if [[ "$platform" == "darwin" ]]; then
local app_result
app_result=$(check_mac_app) && app_found=true || app_found=false
out "app: $app_result"
fi
local users
users=$(get_users_to_check "$platform")
local multi_user=false
local user_count
user_count=$(echo "$users" | wc -l | tr -d ' ')
[[ $user_count -gt 1 ]] && multi_user=true
local ports_to_check="$PORT"
for user in $users; do
local home_dir state_dir
home_dir=$(get_home_dir "$user" "$platform")
state_dir=$(get_state_dir "$home_dir")
if $multi_user; then
out "user: $user"
# check user-specific CLI if not already found
if ! $cli_found; then
local user_cli
user_cli=$(check_cli_for_user "$home_dir") || true
if [[ -n "$user_cli" ]]; then
cli_found=true
out " cli: $user_cli"
out " cli-version: $("$user_cli" --version 2>/dev/null | head -1 || echo "unknown")"
fi
fi
local state_result
state_result=$(check_state_dir "$state_dir") && state_found=true
out " state-dir: $state_result"
local config_result
config_result=$(check_config "$state_dir")
out " config: $config_result"
local configured_port
configured_port=$(get_configured_port "${state_dir}/openclaw.json")
if [[ -n "$configured_port" ]]; then
out " config-port: $configured_port"
ports_to_check="$ports_to_check $configured_port"
fi
else
# single user mode - check user CLI
if ! $cli_found; then
local user_cli
user_cli=$(check_cli_for_user "$home_dir") || true
if [[ -n "$user_cli" ]]; then
cli_found=true
out "cli: $user_cli"
out "cli-version: $("$user_cli" --version 2>/dev/null | head -1 || echo "unknown")"
fi
fi
if ! $cli_found; then
out "cli: not-found"
out "cli-version: n/a"
fi
local state_result
state_result=$(check_state_dir "$state_dir") && state_found=true
out "state-dir: $state_result"
out "config: $(check_config "$state_dir")"
local configured_port
configured_port=$(get_configured_port "${state_dir}/openclaw.json")
if [[ -n "$configured_port" ]]; then
out "config-port: $configured_port"
ports_to_check="$ports_to_check $configured_port"
fi
fi
done
# print cli not-found for multi-user if none found
if $multi_user && ! $cli_found; then
out "cli: not-found"
out "cli-version: n/a"
fi
case "$platform" in
darwin)
local service_result
service_result=$(check_launchd_service) && service_running=true || service_running=false
out "gateway-service: $service_result"
;;
linux)
local service_result
service_result=$(check_systemd_service) && service_running=true || service_running=false
out "gateway-service: $service_result"
;;
esac
# check all unique ports (default + any configured in user configs)
local unique_ports listening_port=""
unique_ports=$(echo "$ports_to_check" | tr ' ' '\n' | sort -u | tr '\n' ' ')
for port in $unique_ports; do
if check_gateway_port "$port" >/dev/null; then
port_listening=true
listening_port="$port"
break
fi
done
if $port_listening; then
out "gateway-port: $listening_port"
else
out "gateway-port: not-listening"
fi
local docker_containers docker_images docker_running=false docker_installed=false
docker_containers=$(check_docker_containers)
if [[ -n "$docker_containers" ]]; then
docker_running=true
out "docker-container: $docker_containers"
else
out "docker-container: not-found"
fi
docker_images=$(check_docker_images)
if [[ -n "$docker_images" ]]; then
docker_installed=true
out "docker-image: $docker_images"
else
out "docker-image: not-found"
fi
local installed=false running=false
if $cli_found || $app_found || $state_found || $docker_installed; then
installed=true
fi
if $service_running || $port_listening || $docker_running; then
running=true
fi
# exit codes: 0=not-installed (clean), 1=found (non-compliant), 2=error
if ! $installed; then
echo "summary: not-installed"
printf "%s" "$output"
exit 0
elif $running; then
echo "summary: installed-and-running"
printf "%s" "$output"
exit 1
else
echo "summary: installed-not-running"
printf "%s" "$output"
exit 1
fi
}
main