-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathinstall.sh
More file actions
executable file
·354 lines (316 loc) · 11.8 KB
/
install.sh
File metadata and controls
executable file
·354 lines (316 loc) · 11.8 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
#!/usr/bin/env bash
set -euo pipefail
# SafeClaw installer
# Usage: curl -fsSL https://raw.githubusercontent.com/linuxdevel/safeclaw/main/install.sh | bash
# or: curl -fsSL https://raw.githubusercontent.com/linuxdevel/safeclaw/main/install.sh | bash -s -- --force
REPO="linuxdevel/safeclaw"
INSTALL_DIR="$HOME/.safeclaw"
BIN_DIR="$INSTALL_DIR/bin"
MIN_NODE_VERSION=22
FORCE=false
# ---------------------------------------------------------------------------
# Color helpers (disabled when stdout is not a terminal)
# ---------------------------------------------------------------------------
if [ -t 1 ]; then
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
BOLD='\033[1m'
RESET='\033[0m'
else
RED=''
GREEN=''
YELLOW=''
BOLD=''
RESET=''
fi
info() { printf "${BOLD}%s${RESET}\n" "$*"; }
success() { printf "${GREEN}%s${RESET}\n" "$*"; }
warn() { printf "${YELLOW}%s${RESET}\n" "$*"; }
error() { printf "${RED}%s${RESET}\n" "$*" >&2; }
# ---------------------------------------------------------------------------
# Parse arguments
# ---------------------------------------------------------------------------
for arg in "$@"; do
case "$arg" in
--force) FORCE=true ;;
*) error "Unknown option: $arg"; exit 1 ;;
esac
done
# ---------------------------------------------------------------------------
# OS and architecture detection
# ---------------------------------------------------------------------------
OS="$(uname -s)"
RAW_ARCH="$(uname -m)"
case "$OS" in
Linux)
case "$RAW_ARCH" in
x86_64) PLATFORM="linux-x64" ;;
aarch64) PLATFORM="linux-arm64" ;;
*)
error "Error: Unsupported architecture: $RAW_ARCH"
error "SafeClaw supports x86_64 and aarch64 on Linux."
exit 1
;;
esac
;;
Darwin)
case "$RAW_ARCH" in
arm64) PLATFORM="darwin-arm64" ;;
x86_64) PLATFORM="darwin-x64" ;;
*)
error "Error: Unsupported architecture: $RAW_ARCH"
error "SafeClaw supports arm64 and x86_64 on macOS."
exit 1
;;
esac
;;
*)
error "Error: Unsupported OS: $OS"
error "SafeClaw supports Linux and macOS."
exit 1
;;
esac
info "Detected platform: $OS/$RAW_ARCH (mapped to $PLATFORM)"
# ---------------------------------------------------------------------------
# Node.js version check (>= 22)
# ---------------------------------------------------------------------------
if ! command -v node &>/dev/null; then
error "Error: Node.js is not installed (or not in PATH)."
error ""
error "SafeClaw requires Node.js >= $MIN_NODE_VERSION."
error "Install it with one of the following methods:"
error ""
error " # Using nvm (recommended):"
error " curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.40.1/install.sh | bash"
error " nvm install $MIN_NODE_VERSION"
error ""
if [ "$OS" = "Darwin" ]; then
error " # Using Homebrew:"
error " brew install node@$MIN_NODE_VERSION"
error ""
else
error " # Using NodeSource (Linux):"
error " curl -fsSL https://deb.nodesource.com/setup_${MIN_NODE_VERSION}.x | sudo -E bash -"
error " sudo apt-get install -y nodejs"
error ""
fi
error " # Using fnm:"
error " curl -fsSL https://fnm.vercel.app/install | bash"
error " fnm install $MIN_NODE_VERSION"
exit 1
fi
NODE_VERSION="$(node --version)" # e.g. v22.5.0
NODE_MAJOR="${NODE_VERSION#v}" # 22.5.0
NODE_MAJOR="${NODE_MAJOR%%.*}" # 22
if [ "$NODE_MAJOR" -lt "$MIN_NODE_VERSION" ] 2>/dev/null; then
error "Error: Node.js $NODE_VERSION is too old."
error "SafeClaw requires Node.js >= $MIN_NODE_VERSION."
error ""
error "Upgrade with:"
error " nvm install $MIN_NODE_VERSION # if using nvm"
error " fnm install $MIN_NODE_VERSION # if using fnm"
exit 1
fi
success "Node.js $NODE_VERSION detected — OK"
# ---------------------------------------------------------------------------
# Handle existing installation
#
# On upgrade we preserve user data (vault.json, vault.json.salt) and
# overwrite everything else. --force skips the confirmation prompt.
# ---------------------------------------------------------------------------
VAULT_FILES=("vault.json" "vault.json.salt")
if [ -d "$INSTALL_DIR" ]; then
if [ "$FORCE" != true ] && [ -t 0 ]; then
warn "An existing SafeClaw installation was found at $INSTALL_DIR"
printf "Upgrade in place? (vault data is preserved) [Y/n] "
read -r REPLY
case "$REPLY" in
[nN]|[nN][oO])
error "Aborted."
exit 1
;;
esac
fi
info "Upgrading existing installation..."
# Back up vault files that exist
VAULT_TMPDIR="$(mktemp -d)"
for f in "${VAULT_FILES[@]}"; do
if [ -f "$INSTALL_DIR/$f" ]; then
cp -p "$INSTALL_DIR/$f" "$VAULT_TMPDIR/$f"
fi
done
# Wipe the install directory
rm -rf "$INSTALL_DIR"
# Restore vault files (mkdir first since we just removed the dir)
mkdir -p "$INSTALL_DIR"
for f in "${VAULT_FILES[@]}"; do
if [ -f "$VAULT_TMPDIR/$f" ]; then
mv "$VAULT_TMPDIR/$f" "$INSTALL_DIR/$f"
fi
done
rm -rf "$VAULT_TMPDIR"
fi
# ---------------------------------------------------------------------------
# Determine latest release tag
# ---------------------------------------------------------------------------
info "Fetching latest release information..."
API_URL="https://api.github.com/repos/$REPO/releases/latest"
RELEASE_JSON="$(curl -fsSL "$API_URL")" || {
error "Error: Failed to fetch release information from GitHub."
error "URL: $API_URL"
exit 1
}
TAG="$(printf '%s' "$RELEASE_JSON" | grep '"tag_name"' | head -1 | sed 's/.*"tag_name"[[:space:]]*:[[:space:]]*"\([^"]*\)".*/\1/')"
if [ -z "$TAG" ]; then
error "Error: Could not determine the latest release tag."
exit 1
fi
info "Latest release: $TAG"
# ---------------------------------------------------------------------------
# Download tarball
# ---------------------------------------------------------------------------
ASSET_NAME="safeclaw-${PLATFORM}.tar.gz"
DOWNLOAD_URL="https://github.com/$REPO/releases/download/$TAG/$ASSET_NAME"
TMPDIR="$(mktemp -d)"
TARBALL="$TMPDIR/$ASSET_NAME"
# Ensure temp files are always cleaned up
cleanup() { rm -rf "$TMPDIR"; }
trap cleanup EXIT
info "Downloading $ASSET_NAME..."
curl -fSL --progress-bar -o "$TARBALL" "$DOWNLOAD_URL" || {
error "Error: Failed to download $DOWNLOAD_URL"
exit 1
}
success "Download complete."
# ---------------------------------------------------------------------------
# Extract to ~/.safeclaw (strip one level)
# ---------------------------------------------------------------------------
info "Installing to $INSTALL_DIR..."
mkdir -p "$INSTALL_DIR"
tar xzf "$TARBALL" -C "$INSTALL_DIR" --strip-components=1
# ---------------------------------------------------------------------------
# Download sandbox helper binary (Linux only)
# ---------------------------------------------------------------------------
if [ "$OS" = "Linux" ]; then
HELPER_ASSET="safeclaw-sandbox-helper-linux-${RAW_ARCH}"
HELPER_URL="https://github.com/$REPO/releases/download/$TAG/$HELPER_ASSET"
SHA_URL="https://github.com/$REPO/releases/download/$TAG/SHA256SUMS"
info "Downloading sandbox helper..."
HELPER_DL="$TMPDIR/$HELPER_ASSET"
SHA_DL="$TMPDIR/SHA256SUMS"
if curl -fSL --progress-bar -o "$HELPER_DL" "$HELPER_URL" 2>/dev/null && \
curl -fsSL -o "$SHA_DL" "$SHA_URL" 2>/dev/null; then
EXPECTED_HASH="$(grep "$HELPER_ASSET" "$SHA_DL" | awk '{print $1}')"
ACTUAL_HASH="$(sha256sum "$HELPER_DL" | awk '{print $1}')"
if [ -n "$EXPECTED_HASH" ] && [ "$EXPECTED_HASH" = "$ACTUAL_HASH" ]; then
mkdir -p "$BIN_DIR"
install -m755 "$HELPER_DL" "$BIN_DIR/safeclaw-sandbox-helper"
success "Sandbox helper installed (SHA-256 verified)."
else
warn "Warning: Sandbox helper checksum mismatch — skipping."
warn "Expected: $EXPECTED_HASH"
warn "Actual: $ACTUAL_HASH"
warn "SafeClaw will run with namespace-only sandboxing."
fi
else
warn "Sandbox helper not available for $RAW_ARCH — skipping."
warn "SafeClaw will run with namespace-only sandboxing."
fi
else
info "Sandbox helper is Linux-only — skipping on macOS."
fi
# ---------------------------------------------------------------------------
# Create wrapper script
# ---------------------------------------------------------------------------
mkdir -p "$BIN_DIR"
cat > "$BIN_DIR/safeclaw" <<'WRAPPER'
#!/usr/bin/env bash
exec node ~/.safeclaw/packages/cli/dist/cli.js "$@"
WRAPPER
chmod +x "$BIN_DIR/safeclaw"
# ---------------------------------------------------------------------------
# PATH setup
# ---------------------------------------------------------------------------
PATH_LINE='export PATH="$HOME/.safeclaw/bin:$PATH"'
MODIFIED_CONFIGS=()
if echo "$PATH" | tr ':' '\n' | grep -qx "$HOME/.safeclaw/bin" 2>/dev/null; then
info "$BIN_DIR is already in PATH — skipping shell config update."
else
# ~/.profile — always
if [ -f "$HOME/.profile" ]; then
if ! grep -qF '.safeclaw/bin' "$HOME/.profile" 2>/dev/null; then
printf '\n# SafeClaw\n%s\n' "$PATH_LINE" >> "$HOME/.profile"
MODIFIED_CONFIGS+=("~/.profile")
fi
else
printf '# SafeClaw\n%s\n' "$PATH_LINE" > "$HOME/.profile"
MODIFIED_CONFIGS+=("~/.profile (created)")
fi
# ~/.bashrc — if bash is installed
if command -v bash &>/dev/null && [ -f "$HOME/.bashrc" ]; then
if ! grep -qF '.safeclaw/bin' "$HOME/.bashrc" 2>/dev/null; then
printf '\n# SafeClaw\n%s\n' "$PATH_LINE" >> "$HOME/.bashrc"
MODIFIED_CONFIGS+=("~/.bashrc")
fi
fi
# ~/.zshrc — if zsh is installed
if command -v zsh &>/dev/null; then
if [ -f "$HOME/.zshrc" ] && ! grep -qF '.safeclaw/bin' "$HOME/.zshrc" 2>/dev/null; then
printf '\n# SafeClaw\n%s\n' "$PATH_LINE" >> "$HOME/.zshrc"
MODIFIED_CONFIGS+=("~/.zshrc")
fi
# ~/.zprofile — login shells on macOS (Terminal.app opens login shells)
if [ "$OS" = "Darwin" ]; then
if [ -f "$HOME/.zprofile" ] && ! grep -qF '.safeclaw/bin' "$HOME/.zprofile" 2>/dev/null; then
printf '\n# SafeClaw\n%s\n' "$PATH_LINE" >> "$HOME/.zprofile"
MODIFIED_CONFIGS+=("~/.zprofile")
fi
fi
fi
if [ ${#MODIFIED_CONFIGS[@]} -gt 0 ]; then
info "Added PATH entry to: ${MODIFIED_CONFIGS[*]}"
fi
fi
# ---------------------------------------------------------------------------
# Verify installation
# ---------------------------------------------------------------------------
info "Verifying installation..."
SAFECLAW_VERSION="$("$BIN_DIR/safeclaw" version 2>&1)" && {
success "Verification passed: $SAFECLAW_VERSION"
} || {
warn "Warning: 'safeclaw version' exited with an error."
warn "Output: $SAFECLAW_VERSION"
warn "The installation may still be usable — check the output above."
}
# ---------------------------------------------------------------------------
# Success message
# ---------------------------------------------------------------------------
printf '\n'
success "============================================"
success " SafeClaw installed successfully!"
success "============================================"
printf '\n'
info "Installation path: $INSTALL_DIR"
printf '\n'
if [ ${#MODIFIED_CONFIGS[@]} -gt 0 ]; then
warn "To use safeclaw right away, run:"
warn ""
if [[ " ${MODIFIED_CONFIGS[*]} " == *"~/.bashrc"* ]]; then
warn " source ~/.bashrc"
elif [[ " ${MODIFIED_CONFIGS[*]} " == *"~/.zshrc"* ]]; then
warn " source ~/.zshrc"
else
warn " source ~/.profile"
fi
warn ""
warn "Or restart your shell."
printf '\n'
fi
info "Getting started:"
info " safeclaw onboard — first-time setup wizard"
info " safeclaw chat — interactive AI chat (CLI)"
info " safeclaw serve — start gateway + webchat server"
info " safeclaw audit — security audit"
info " safeclaw help — usage information"
info " safeclaw version — show version"