-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhyperctl_install.sh
More file actions
executable file
·381 lines (302 loc) · 11.1 KB
/
Copy pathhyperctl_install.sh
File metadata and controls
executable file
·381 lines (302 loc) · 11.1 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
#!/usr/bin/env sh
OWNER=openlabsro
REPO=openhack-hypervisor
ASSET_NAME=hyperctl
BINARY_NAME=hyperctl
INSTALL_DIR=/usr/local/bin
DOWNLOAD_URL="https://github.com/${OWNER}/${REPO}/releases/latest/download/${ASSET_NAME}"
OPENHACK_USER="openhack"
OPENHACK_ADMIN_GROUP="openhack-admins"
OPENHACK_HOME="/var/openhack"
err() {
printf "hyperctl installer error: %s\n" "$1" >&2
}
die() {
err "$1"
exit 1
}
usage() {
cat <<USAGE
Usage: ./hyperctl_install.sh [--nodeps|-nodeps|--fedora-deps]
Installs the hyperctl binary (and, by default, its prerequisites:
Go 1.25.1, redis-server, nginx, certbot integration, vim, and the swag CLI).
Also creates the openhack system user and openhack-admins group for managing
hypervisor operations, and adds the invoking user to the admin group.
Options:
--nodeps, -nodeps Skip installing prerequisite packages and toolchains.
--fedora-deps Install prerequisites for Fedora/RHEL-based systems (uses dnf).
-h, --help Show this help text.
USAGE
}
require_cmd() {
command -v "$1" >/dev/null 2>&1 || die "Missing required command: $1"
}
run_privileged() {
if [ "$(id -u)" -eq 0 ]; then
"$@"
return
fi
if command -v sudo >/dev/null 2>&1; then
sudo "$@"
else
die "This action requires root privileges. Re-run as root or install dependencies manually with --nodeps."
fi
}
append_path_entry() {
ENTRY=$1
case ":${PATH}:" in
*":${ENTRY}:"*) ;;
*) PATH="${PATH}:${ENTRY}"
export PATH
;;
esac
PROFILE_PATH="${HOME}/.profile"
if [ ! -e "$PROFILE_PATH" ]; then
if ! touch "$PROFILE_PATH" 2>/dev/null; then
printf "Warning: unable to create %s to persist PATH entry %s\n" "$PROFILE_PATH" "$ENTRY"
return
fi
fi
if [ -w "$PROFILE_PATH" ]; then
if ! grep -qs "$ENTRY" "$PROFILE_PATH"; then
printf 'export PATH="$PATH:%s"\n' "$ENTRY" >>"$PROFILE_PATH"
printf "Added %s to PATH in %s\n" "$ENTRY" "$PROFILE_PATH"
fi
else
printf "Warning: %s is not writable; ensure %s is on PATH manually.\n" "$PROFILE_PATH" "$ENTRY"
fi
source ~/.profile
}
install_go_toolchain() {
REQUIRED_GO_VERSION="go1.25.1"
GO_ARCHIVE="${REQUIRED_GO_VERSION}.linux-amd64.tar.gz"
GO_URL="https://go.dev/dl/${GO_ARCHIVE}"
CURRENT_GO_VERSION=""
if command -v go >/dev/null 2>&1; then
CURRENT_GO_VERSION="$(go version | awk '{print $3}')"
fi
if [ "$CURRENT_GO_VERSION" = "$REQUIRED_GO_VERSION" ]; then
printf "Go %s already installed; skipping download.\n" "$CURRENT_GO_VERSION"
return
fi
printf "Installing Go %s...\n" "$REQUIRED_GO_VERSION"
GO_TMP_DIR=$(mktemp -d)
GO_TARBALL="${GO_TMP_DIR}/${GO_ARCHIVE}"
if ! curl -fsSL "$GO_URL" -o "$GO_TARBALL"; then
rm -rf "$GO_TMP_DIR"
die "Failed to download Go toolchain from ${GO_URL}"
fi
run_privileged rm -rf /usr/local/go
run_privileged tar -C /usr/local -xzf "$GO_TARBALL"
rm -rf "$GO_TMP_DIR"
printf "Go %s installed to /usr/local/go.\n" "$REQUIRED_GO_VERSION"
# Symlink go binary to /usr/local/bin for direct PATH access
printf "Creating symlink: /usr/local/bin/go -> /usr/local/go/bin/go\n"
run_privileged ln -sf /usr/local/go/bin/go /usr/local/bin/go
run_privileged ln -sf /usr/local/go/bin/gofmt /usr/local/bin/gofmt
printf "Go binaries symlinked to /usr/local/bin\n"
}
install_swag_cli() {
if command -v swag >/dev/null 2>&1; then
printf "swag CLI already present (%s).\n" "$(swag --version 2>/dev/null | head -n 1 || echo "version unknown")"
return
fi
command -v go >/dev/null 2>&1 || die "Go command not available; cannot install swag CLI."
printf "Installing swag CLI via go install to /usr/local/bin...\n"
GOBIN=/usr/local/bin go install github.com/swaggo/swag/cmd/swag@latest
if command -v swag >/dev/null 2>&1; then
printf "swag CLI installed successfully to /usr/local/bin\n"
else
printf "Warning: swag CLI installation may have failed. Check /usr/local/bin/swag\n"
fi
}
setup_openhack_user_and_group() {
printf "Setting up openhack system user and group...\n"
# Determine who is actually running this script (the real user, not root)
REAL_USER="${SUDO_USER:-$(id -un)}"
if [ "$REAL_USER" = "root" ] && [ -z "$SUDO_USER" ]; then
printf "Running as root with no SUDO_USER. Skipping user group membership.\n"
REAL_USER=""
fi
# Create openhack system user if it doesn't exist
if id "$OPENHACK_USER" >/dev/null 2>&1; then
printf "openhack user already exists\n"
else
printf "Creating openhack system user...\n"
run_privileged useradd \
--system \
--home-dir "$OPENHACK_HOME" \
--shell /usr/sbin/nologin \
"$OPENHACK_USER"
printf "openhack user created\n"
fi
# Create openhack-admins group if it doesn't exist
if getent group "$OPENHACK_ADMIN_GROUP" >/dev/null 2>&1; then
printf "openhack-admins group already exists\n"
else
printf "Creating openhack-admins group...\n"
run_privileged groupadd "$OPENHACK_ADMIN_GROUP"
printf "openhack-admins group created\n"
fi
# Add the invoking user to openhack-admins group (if not root)
if [ -n "$REAL_USER" ] && [ "$REAL_USER" != "root" ]; then
if id -Gn "$REAL_USER" | grep -q "$OPENHACK_ADMIN_GROUP"; then
printf "%s is already in %s group\n" "$REAL_USER" "$OPENHACK_ADMIN_GROUP"
else
printf "Adding %s to %s group...\n" "$REAL_USER" "$OPENHACK_ADMIN_GROUP"
run_privileged usermod -a -G "$OPENHACK_ADMIN_GROUP" "$REAL_USER"
printf "%s added to %s group\n" "$REAL_USER" "$OPENHACK_ADMIN_GROUP"
printf "Note: You may need to log out and log back in for group membership to take effect.\n"
fi
fi
# Add openhack user to systemd-journal group so it can read service logs
if id -Gn "$OPENHACK_USER" | grep -q "systemd-journal"; then
printf "openhack user is already in systemd-journal group\n"
else
printf "Adding openhack user to systemd-journal group for log access...\n"
run_privileged usermod -a -G systemd-journal "$OPENHACK_USER"
printf "openhack user added to systemd-journal group\n"
fi
}
setup_directories() {
printf "Setting up hypervisor and openhack directories...\n"
# Create all required directories
for dir in \
/var/hypervisor \
/var/hypervisor/repos \
/var/hypervisor/builds \
/var/hypervisor/env \
/var/hypervisor/logs \
/var/openhack \
/var/openhack/repos \
/var/openhack/builds \
/var/openhack/env \
/var/openhack/env/template \
/var/openhack/runtime \
/var/openhack/runtime/logs; do
if [ ! -d "$dir" ]; then
printf "Creating directory %s...\n" "$dir"
run_privileged mkdir -p "$dir"
fi
# Set ownership to openhack user and openhack-admins group
run_privileged chown "$OPENHACK_USER:$OPENHACK_ADMIN_GROUP" "$dir"
# Set permissions to 0755 (rwxr-xr-x)
run_privileged chmod 0755 "$dir"
done
printf "Directory structure created and configured\n"
}
setup_sudoers() {
printf "Configuring sudoers for openhack-admins group...\n"
SUDOERS_FILE="/etc/sudoers.d/openhack-admins"
SUDOERS_CONTENT="%${OPENHACK_ADMIN_GROUP} ALL=(ALL) NOPASSWD: /usr/bin/systemctl, /usr/bin/tee, /usr/bin/chmod, /usr/bin/chown, /usr/bin/mkdir, /usr/bin/rm, /bin/bash, /usr/bin/useradd, /usr/bin/groupadd, /usr/bin/usermod, /usr/local/bin/hyperctl"
# Write sudoers file using printf to avoid echo issues
printf "%s\n" "$SUDOERS_CONTENT" | run_privileged tee "$SUDOERS_FILE" >/dev/null
# Set correct permissions on sudoers file
run_privileged chmod 0440 "$SUDOERS_FILE"
printf "Sudoers file configured at %s\n" "$SUDOERS_FILE"
printf "Configuring sudoers for openhack user (service account)...\n"
OPENHACK_SUDOERS_FILE="/etc/sudoers.d/openhack"
OPENHACK_SUDOERS_CONTENT="${OPENHACK_USER} ALL=(ALL) NOPASSWD: /usr/bin/tee, /usr/bin/systemctl, /usr/bin/rm"
# Write sudoers file for openhack user
printf "%s\n" "$OPENHACK_SUDOERS_CONTENT" | run_privileged tee "$OPENHACK_SUDOERS_FILE" >/dev/null
# Set correct permissions on sudoers file
run_privileged chmod 0440 "$OPENHACK_SUDOERS_FILE"
printf "Sudoers file configured at %s\n" "$OPENHACK_SUDOERS_FILE"
}
install_dependencies_debian() {
printf "Installing hyperctl prerequisites for Debian-based systems (apt)...\n"
if ! command -v apt-get >/dev/null 2>&1; then
die "apt-get is required to install dependencies automatically. Use --nodeps to skip."
fi
run_privileged apt-get update
run_privileged apt-get install -y curl redis-server nginx certbot python3-certbot-nginx vim git
install_go_toolchain
if command -v go >/dev/null 2>&1; then
printf "Go ready: %s\n" "$(go version)"
else
die "Go installation failed or go not on PATH."
fi
install_swag_cli
}
install_dependencies_fedora() {
printf "Installing hyperctl prerequisites for Fedora/RHEL-based systems (dnf)...\n"
if ! command -v dnf >/dev/null 2>&1; then
die "dnf is required to install dependencies automatically. Use --nodeps to skip."
fi
run_privileged dnf install -y curl redis nginx certbot python3-certbot-nginx vim git
install_go_toolchain
if command -v go >/dev/null 2>&1; then
printf "Go ready: %s\n" "$(go version)"
else
die "Go installation failed or go not on PATH."
fi
install_swag_cli
}
INSTALL_DEPS=1
INSTALL_DEPS_TYPE="debian"
while [ "$#" -gt 0 ]; do
case "$1" in
--nodeps|-nodeps)
INSTALL_DEPS=0
shift
;;
--fedora-deps)
INSTALL_DEPS=1
INSTALL_DEPS_TYPE="fedora"
shift
;;
--help|-h)
usage
exit 0
;;
*)
die "Unknown argument: $1"
;;
esac
done
if [ "$INSTALL_DEPS" -eq 1 ]; then
if [ "$INSTALL_DEPS_TYPE" = "fedora" ]; then
install_dependencies_fedora
else
install_dependencies_debian
fi
else
printf "Skipping dependency installation (--nodeps).\n"
fi
# Setup openhack user, group, and directories (always, even with --nodeps)
setup_openhack_user_and_group
setup_directories
setup_sudoers
require_cmd curl
require_cmd install
OS=$(uname -s || true)
ARCH=$(uname -m || true)
[ "$OS" = "Linux" ] || die "Unsupported OS '$OS'; hyperctl currently targets Linux."
[ "$ARCH" = "x86_64" ] || [ "$ARCH" = "amd64" ] || die "Unsupported architecture '$ARCH'; hyperctl currently targets x86_64."
TMP_DIR=$(mktemp -d)
cleanup() { rm -rf "$TMP_DIR"; }
trap cleanup EXIT INT TERM
TMP_BIN="$TMP_DIR/$BINARY_NAME"
printf "Downloading %s to temporary folder %s...\n" "$ASSET_NAME" "$TMP_BIN"
curl -fsSL "$DOWNLOAD_URL" -o "$TMP_BIN" || die "Failed to download asset from $DOWNLOAD_URL"
chmod 0755 "$TMP_BIN"
TARGET="$INSTALL_DIR/$BINARY_NAME"
install_binary() {
install -m 0755 "$TMP_BIN" "$TARGET"
}
if [ -w "$INSTALL_DIR" ]; then
install_binary
else
if command -v sudo >/dev/null 2>&1; then
printf "Elevating privileges to write into %s...\n" "$INSTALL_DIR"
sudo install -m 0755 "$TMP_BIN" "$TARGET"
else
die "Cannot write to $INSTALL_DIR. Re-run with sudo or set INSTALL_DIR to a writable path."
fi
fi
printf "hyperctl installed to %s\n" "$TARGET"
if command -v "$BINARY_NAME" >/dev/null 2>&1; then
printf "Detected %s version: %s\n" "$BINARY_NAME" "$($BINARY_NAME version 2>/dev/null || echo 'unknown')"
else
err "Binary not found on PATH; ensure ${INSTALL_DIR} is in your PATH."
fi