Repository navigation
Expand file tree
/
Copy pathinstall.sh
More file actions
executable file
·385 lines (333 loc) · 12.9 KB
/
Copy pathinstall.sh
File metadata and controls
executable file
·385 lines (333 loc) · 12.9 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
380
381
382
383
384
385
#!/bin/sh
# install.sh - Install or update ODDK on a Linux server
# POSIX compliant; downloads release binaries from GitHub.
#
# Usage:
# curl -fsSL https://raw.githubusercontent.com/andrianbdn/oddk/main/install.sh | sh
# curl -fsSL https://raw.githubusercontent.com/andrianbdn/oddk/main/install.sh | sh -s -- --version v0.1.31
#
# Fresh installs use the FHS layout: binary in /usr/local/bin, state in
# /var/lib/oddk. An existing installation is updated in place, wherever it
# lives: the binary path is taken from the oddk.service systemd unit, so
# legacy layouts (e.g. /home/oddk/bin) keep working unchanged.
set -eu
REPO="andrianbdn/oddk"
SERVICE="oddk"
SUMS_FILE="SHA256SUMS"
# ASSET / ARCHIVE_BINARY are set from the host architecture in pre-flight checks.
# Fresh-install (FHS) layout
USER_NAME="oddk"
FHS_BINARY="/usr/local/bin/oddk"
FHS_HOME="/var/lib/oddk"
SYSTEMD_UNIT="/etc/systemd/system/oddk.service"
# Colors for output (POSIX compatible)
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
NC='\033[0m' # No Color
print_msg() {
color="$1"
shift
printf "${color}%s${NC}\n" "$*"
}
TMP_DIR=$(mktemp -d /tmp/oddk-install.XXXXXX)
trap 'rm -rf "$TMP_DIR"' EXIT INT TERM
usage() {
echo "Usage: $0 [--version vX.Y.Z]"
echo
echo "Without --version the latest GitHub release is installed."
}
# --- Parse arguments ---
VERSION=""
while [ $# -gt 0 ]; do
case "$1" in
--version)
[ $# -ge 2 ] || { print_msg "$RED" "--version requires an argument"; exit 1; }
VERSION="$2"
shift 2
;;
--version=*)
VERSION="${1#--version=}"
shift
;;
-h|--help)
usage
exit 0
;;
*)
print_msg "$RED" "Unknown argument: $1"
usage
exit 1
;;
esac
done
case "$VERSION" in
""|v*) ;;
*) VERSION="v$VERSION" ;;
esac
if [ -n "$VERSION" ]; then
BASE_URL="https://github.com/$REPO/releases/download/$VERSION"
else
BASE_URL="https://github.com/$REPO/releases/latest/download"
fi
print_msg "$YELLOW" "ODDK Install/Update Script"
echo
# --- Pre-flight checks ---
if [ "$(uname -s)" != "Linux" ]; then
print_msg "$RED" "This script must run on Linux (detected: $(uname -s))"
exit 1
fi
case "$(uname -m)" in
x86_64|amd64) ARCH=amd64 ;;
aarch64|arm64) ARCH=arm64 ;;
*)
print_msg "$RED" "Unsupported architecture: $(uname -m) (need x86_64 or aarch64)"
exit 1
;;
esac
ASSET="oddk-linux-$ARCH.tar.gz"
ARCHIVE_BINARY="oddk-linux-$ARCH"
for tool in curl tar sha256sum systemctl; do
if ! command -v "$tool" >/dev/null 2>&1; then
print_msg "$RED" "Required tool not found: $tool"
exit 1
fi
done
if [ ! -d /run/systemd/system ]; then
print_msg "$RED" "systemd is not running on this system"
exit 1
fi
SUDO="sudo"
if [ "$(id -u)" -eq 0 ]; then
SUDO=""
elif ! sudo -n true 2>/dev/null; then
print_msg "$RED" "This script requires root or passwordless sudo"
exit 1
fi
if ! command -v docker >/dev/null 2>&1; then
print_msg "$RED" "Docker is not installed"
print_msg "$YELLOW" "Install Docker first: https://docs.docker.com/engine/install/"
exit 1
fi
if ! systemctl is-active --quiet docker; then
print_msg "$RED" "Docker service is not running"
print_msg "$YELLOW" "Start it with: sudo systemctl start docker"
exit 1
fi
# --- Detect existing installation ---
#
# This script only ever uses the FHS layout (/usr/local/bin + /var/lib/oddk).
# A fresh install creates it; an update replaces the binary in place. A legacy
# /home/oddk installation is NOT updated here. The one-time relocation script
# (oddk-migrate.sh) was retired in v0.1.45; relocate such an install by hand:
# stop the service, move the binary to /usr/local/bin/oddk and the data and
# backups directories under /var/lib/oddk, point the oddk user's home and the
# unit's ExecStart at them, then re-run this script. Backup catalogue rows that
# still name the old backup path are dropped at the next daemon start (the
# archive files are kept and reported, never deleted).
MODE="install"
TARGET_BINARY="$FHS_BINARY"
if systemctl list-unit-files 2>/dev/null | grep -q "^${SERVICE}\.service"; then
EXEC_START=$(systemctl cat "$SERVICE.service" 2>/dev/null | sed -n 's/^ExecStart=//p' | head -1)
INSTALLED_BINARY="${EXEC_START%% *}"
if [ "$INSTALLED_BINARY" != "$FHS_BINARY" ]; then
print_msg "$RED" "Non-FHS installation detected (binary at: ${INSTALLED_BINARY:-unknown})"
print_msg "$YELLOW" "This installer manages the FHS layout ($FHS_BINARY)."
print_msg "$YELLOW" "Relocate it by hand first: stop the service, move the binary to $FHS_BINARY and the"
print_msg "$YELLOW" "data/backups directories under /var/lib/oddk, update the oddk user's home and the"
print_msg "$YELLOW" "unit's ExecStart, then re-run."
exit 1
fi
if [ ! -f "$TARGET_BINARY" ]; then
print_msg "$RED" "$SERVICE.service points at $TARGET_BINARY but the file is missing"
exit 1
fi
MODE="update"
print_msg "$GREEN" "Existing FHS installation detected: $TARGET_BINARY"
else
if [ -f "$FHS_BINARY" ]; then
print_msg "$RED" "$FHS_BINARY exists but no $SERVICE.service unit was found"
print_msg "$YELLOW" "Remove the stray binary or restore the unit, then re-run"
exit 1
fi
print_msg "$GREEN" "No existing installation, performing fresh install"
fi
echo
# --- Download and verify ---
print_msg "$YELLOW" "Downloading $ASSET (${VERSION:-latest})..."
if ! curl -fsSL -o "$TMP_DIR/$ASSET" "$BASE_URL/$ASSET"; then
print_msg "$RED" "Failed to download $BASE_URL/$ASSET"
print_msg "$YELLOW" "Check that the release exists: https://github.com/$REPO/releases"
exit 1
fi
if ! curl -fsSL -o "$TMP_DIR/$SUMS_FILE" "$BASE_URL/$SUMS_FILE"; then
print_msg "$RED" "Failed to download checksums ($BASE_URL/$SUMS_FILE)"
exit 1
fi
print_msg "$YELLOW" "Verifying checksum..."
if ! (cd "$TMP_DIR" && grep -F "$ASSET" "$SUMS_FILE" | sha256sum -c - >/dev/null 2>&1); then
print_msg "$RED" "Checksum verification FAILED - aborting"
exit 1
fi
print_msg "$GREEN" "Checksum OK"
print_msg "$YELLOW" "Extracting..."
tar -xzf "$TMP_DIR/$ASSET" -C "$TMP_DIR"
TEMP_BINARY="$TMP_DIR/$ARCHIVE_BINARY"
if [ ! -f "$TEMP_BINARY" ]; then
print_msg "$RED" "Binary not found in archive (expected: $ARCHIVE_BINARY)"
exit 1
fi
chmod +x "$TEMP_BINARY"
if ! "$TEMP_BINARY" --version >/dev/null 2>&1; then
print_msg "$RED" "Downloaded binary failed sanity check (--version)"
exit 1
fi
NEW_VERSION=$("$TEMP_BINARY" --version 2>&1)
print_msg "$GREEN" "Binary OK: $NEW_VERSION"
echo
# =====================================================================
# Update path: replace the binary in place, restart, roll back on failure
# =====================================================================
if [ "$MODE" = "update" ]; then
OLD_VERSION=$("$TARGET_BINARY" --version 2>&1 || echo "unknown")
PREV_BINARY="${TARGET_BINARY}.prev"
OWNER=$(stat -c '%U:%G' "$TARGET_BINARY")
print_msg "$YELLOW" "Updating: $OLD_VERSION -> $NEW_VERSION"
rollback() {
print_msg "$RED" "Update failed, rolling back..."
if [ -f "$PREV_BINARY" ]; then
$SUDO cp -p "$PREV_BINARY" "$TARGET_BINARY"
$SUDO systemctl start "$SERVICE" || true
print_msg "$YELLOW" "Rollback completed, previous binary restored"
else
print_msg "$RED" "No backup found, manual intervention required"
fi
exit 1
}
print_msg "$YELLOW" "Backing up current binary to $PREV_BINARY"
$SUDO cp -p "$TARGET_BINARY" "$PREV_BINARY"
print_msg "$YELLOW" "Stopping $SERVICE service..."
$SUDO systemctl stop "$SERVICE" || rollback
sleep 2
print_msg "$YELLOW" "Installing new binary..."
$SUDO cp "$TEMP_BINARY" "$TARGET_BINARY" || rollback
$SUDO chown "$OWNER" "$TARGET_BINARY"
$SUDO chmod 755 "$TARGET_BINARY"
print_msg "$YELLOW" "Starting $SERVICE service..."
$SUDO systemctl start "$SERVICE" || rollback
sleep 3
systemctl is-active --quiet "$SERVICE" || rollback
print_msg "$GREEN" "Service restarted successfully"
echo
$SUDO systemctl status "$SERVICE" --no-pager -l | head -15
echo
print_msg "$GREEN" "ODDK update completed: $NEW_VERSION"
print_msg "$YELLOW" "Previous binary kept at: $PREV_BINARY"
exit 0
fi
# =====================================================================
# Fresh install: FHS layout
# =====================================================================
# Resolve a nologin shell - the oddk account is a service identity that nobody
# logs into. The CLI talks to the daemon over HTTP (localhost + bearer token),
# so administration never requires becoming the oddk user.
NOLOGIN=$(command -v nologin || echo /usr/sbin/nologin)
print_msg "$YELLOW" "Creating $USER_NAME user..."
if id "$USER_NAME" >/dev/null 2>&1; then
print_msg "$YELLOW" "User $USER_NAME already exists, skipping"
else
$SUDO useradd -r -d "$FHS_HOME" -s "$NOLOGIN" "$USER_NAME"
print_msg "$GREEN" "User $USER_NAME created (home: $FHS_HOME, shell: $NOLOGIN)"
fi
print_msg "$YELLOW" "Creating directories..."
$SUDO mkdir -p "$FHS_HOME/data" "$FHS_HOME/backups"
$SUDO chown -R "$USER_NAME:$USER_NAME" "$FHS_HOME"
print_msg "$GREEN" "Directories created: $FHS_HOME/{data,backups}"
print_msg "$YELLOW" "Adding $USER_NAME to docker group..."
if id -nG "$USER_NAME" | grep -qw docker; then
print_msg "$YELLOW" "User already in docker group, skipping"
else
$SUDO usermod -aG docker "$USER_NAME"
print_msg "$GREEN" "User added to docker group"
fi
print_msg "$YELLOW" "Installing binary to $FHS_BINARY..."
$SUDO cp "$TEMP_BINARY" "$FHS_BINARY"
$SUDO chown root:root "$FHS_BINARY"
$SUDO chmod 755 "$FHS_BINARY"
print_msg "$GREEN" "Binary installed"
echo
print_msg "$YELLOW" "Creating systemd service..."
$SUDO tee "$SYSTEMD_UNIT" > /dev/null << 'UNIT'
[Unit]
Description=ODDK
After=docker.service
Requires=docker.service
[Service]
Type=simple
User=oddk
ExecStart=/usr/local/bin/oddk daemon --data-dir /var/lib/oddk/data --backup-dir /var/lib/oddk/backups
Restart=always
RestartSec=5
StartLimitIntervalSec=600
StartLimitBurst=5
WorkingDirectory=/var/lib/oddk
[Install]
WantedBy=multi-user.target
UNIT
print_msg "$GREEN" "Systemd unit created: $SYSTEMD_UNIT"
print_msg "$YELLOW" "Enabling and starting service..."
$SUDO systemctl daemon-reload
$SUDO systemctl enable "$SERVICE"
$SUDO systemctl start "$SERVICE"
sleep 3
if ! systemctl is-active --quiet "$SERVICE"; then
print_msg "$RED" "Service failed to start"
print_msg "$YELLOW" "Check logs with: sudo journalctl -u $SERVICE -n 50"
exit 1
fi
print_msg "$GREEN" "Service started successfully"
echo
# --- Bootstrap the admin's CLI config ---
# Mint a fresh CLI token as the oddk service user and install it into the
# invoking admin's ~/.config/oddk/cli.json so they can run `oddk ...` directly -
# no sudo, no becoming the oddk user. (The daemon does not write any token file
# itself; `oddk auth mint` is the single way to provision a CLI token.)
CLI_CONFIGURED=""
ADMIN_USER="${SUDO_USER:-}"
if [ -n "$ADMIN_USER" ] && [ "$ADMIN_USER" != "root" ]; then
ADMIN_HOME=$(getent passwd "$ADMIN_USER" | cut -d: -f6)
if [ -n "$ADMIN_HOME" ] && [ -d "$ADMIN_HOME" ]; then
CLI_JSON=$($SUDO -u "$USER_NAME" "$FHS_BINARY" auth mint --json 2>/dev/null) || CLI_JSON=""
if [ -n "$CLI_JSON" ]; then
$SUDO mkdir -p "$ADMIN_HOME/.config/oddk"
printf '%s\n' "$CLI_JSON" | $SUDO tee "$ADMIN_HOME/.config/oddk/cli.json" >/dev/null
$SUDO chown -R "$ADMIN_USER:" "$ADMIN_HOME/.config/oddk"
$SUDO chmod 600 "$ADMIN_HOME/.config/oddk/cli.json"
CLI_CONFIGURED="$ADMIN_USER"
print_msg "$GREEN" "CLI configured for user '$ADMIN_USER' (~/.config/oddk/cli.json)"
fi
fi
fi
echo
$SUDO systemctl status "$SERVICE" --no-pager -l | head -15
echo
print_msg "$GREEN" "ODDK installation completed: $NEW_VERSION"
echo
if [ -z "$CLI_CONFIGURED" ]; then
print_msg "$YELLOW" "To use the CLI as your own user, mint a token and install the config:"
echo " eval \"\$(sudo -u oddk /usr/local/bin/oddk auth mint)\""
echo
fi
# Some minimal distros omit /usr/local/bin from the default PATH. Warn so the
# admin knows why `oddk` might be "command not found" (sudo invocations above
# use the absolute path and are unaffected).
case ":$PATH:" in
*:/usr/local/bin:*) ;;
*) print_msg "$YELLOW" "Note: /usr/local/bin is not in your PATH - add it, or run /usr/local/bin/oddk directly." ;;
esac
print_msg "$YELLOW" "Useful commands:"
echo " eval \"\$(sudo -u oddk /usr/local/bin/oddk auth mint)\" - Configure CLI for another user"
echo " oddk list - List instances"
echo " oddk pull --version 17 - Pull PostgreSQL 17"
echo " sudo systemctl status oddk - Check service status"
echo " sudo journalctl -u oddk -f - View logs"