Repository navigation
Expand file tree
/
Copy pathinstall.sh
More file actions
executable file
·300 lines (300 loc) · 15.9 KB
/
Copy pathinstall.sh
File metadata and controls
executable file
·300 lines (300 loc) · 15.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
#!/usr/bin/env bash
# Pinned, no-git SpaceWorks installer.
set -euo pipefail
say() { printf '[SpaceWorks installer] %s\n' "$*"; }
die() { printf '[SpaceWorks installer] ERROR: %s\n' "$*" >&2; exit 1; }
REPOSITORY="${SPACEWORKS_REPOSITORY:-SpaceWorks-HQ/SpaceWorks}"
[[ "$REPOSITORY" =~ ^[A-Za-z0-9_.-]+/[A-Za-z0-9_.-]+$ ]] || die "SPACEWORKS_REPOSITORY must look like owner/repo."
RELEASE_API="https://api.github.com/repos/$REPOSITORY/releases/latest"
INSTALL_DIR="${SPACEWORKS_DIR:-/opt/spaceworks}"
MIN_FREE_KB=$((8 * 1024 * 1024))
RUN_ROOT=()
USE_DOCKER_GROUP=0
RUN_INSTALLED_AS_ROOT=0
ACTION=fresh
RELEASE_TAG=""
VERSION=""
STAGE_DIR=""
ARCHIVE=""
CREATED_PARENT=0
prompt() {
local reply
[[ -r /dev/tty ]] || die "An interactive terminal is required."
printf '%s' "$1" > /dev/tty
IFS= read -r reply < /dev/tty || reply=""
printf '%s' "$reply"
}
cleanup() {
if [[ -n "$STAGE_DIR" && "$STAGE_DIR" == */.spaceworks-stage.* ]]; then
"${RUN_ROOT[@]}" rm -rf -- "$STAGE_DIR" 2>/dev/null || true
fi
[[ -z "$ARCHIVE" ]] || rm -f -- "$ARCHIVE"
if [[ "$CREATED_PARENT" == 1 ]]; then
"${RUN_ROOT[@]}" rmdir -- "$(dirname "$INSTALL_DIR")" 2>/dev/null || true
fi
}
trap cleanup EXIT
case "$INSTALL_DIR" in
""|/|*"'"*) die "SPACEWORKS_DIR must be a specific path and cannot contain a single quote." ;;
esac
say "Preflight only—nothing will be installed until all checks pass."
say "Checking architecture, operating system, dependencies, Docker, release availability, ports, disk space, and existing state."
machine="$(uname -m)"
case "$machine" in
x86_64|amd64) ARCH=x86_64 ;;
aarch64|arm64) ARCH=aarch64 ;;
*) die "Unsupported architecture '$machine'. SpaceWorks release images support only x86_64 and aarch64." ;;
esac
kernel="$(uname -s)"
PACKAGE_FAMILY=""
case "$kernel" in
Linux)
[[ -r /etc/os-release ]] || die "Linux distribution metadata /etc/os-release is missing."
ID=""; ID_LIKE=""
source /etc/os-release
distro_words=" ${ID:-} ${ID_LIKE:-} "
case "$distro_words" in
*" debian "*|*" ubuntu "*|*" linuxmint "*) PACKAGE_FAMILY=apt ;;
*" fedora "*|*" rhel "*|*" centos "*|*" rocky "*|*" almalinux "*) PACKAGE_FAMILY=dnf ;;
*" arch "*|*" manjaro "*) PACKAGE_FAMILY=pacman ;;
*" suse "*|*" opensuse "*|*" sles "*) PACKAGE_FAMILY=zypper ;;
*) die "Unsupported Linux distribution '${ID:-unknown}' (ID_LIKE='${ID_LIKE:-}'). Supported families: Debian/Ubuntu, RHEL/Fedora/Alma/Rocky, Arch, and SUSE." ;;
esac
HOST_KIND=linux
;;
MINGW*|MSYS*|CYGWIN*) HOST_KIND=windows ;;
Darwin) HOST_KIND=macos ;;
*) die "Unsupported host '$kernel'. Use Linux (on Windows, a WSL2 shell) or macOS with Docker Desktop." ;;
esac
say "Architecture: $ARCH; host: $HOST_KIND${PACKAGE_FAMILY:+/$PACKAGE_FAMILY}."
missing=()
MISSING_DOCKER=0
MISSING_COMPOSE=0
for tool in curl tar python3; do command -v "$tool" >/dev/null 2>&1 || missing+=("$tool"); done
if ! command -v docker >/dev/null 2>&1; then
missing+=(docker-engine docker-compose-v2)
MISSING_DOCKER=1
MISSING_COMPOSE=1
elif ! docker compose version >/dev/null 2>&1; then
missing+=(docker-compose-v2)
MISSING_COMPOSE=1
fi
if ((${#missing[@]} > 0)); then
if [[ "$HOST_KIND" != linux ]]; then
die "Missing: ${missing[*]}. Install Docker Desktop (with Compose V2), curl, tar, and python3, then rerun."
fi
if [[ "$(id -u)" != 0 ]]; then
command -v sudo >/dev/null 2>&1 || die "Missing ${missing[*]} and sudo is unavailable. Install them as root, then rerun."
RUN_ROOT=(sudo)
fi
say "After preflight, the $PACKAGE_FAMILY family installer will install/ensure: curl, tar, python3, Docker Engine, and Docker Compose V2."
else
say "Dependencies are already present."
if [[ "$HOST_KIND" == linux && "$(id -u)" != 0 && ! -w "$(dirname "$INSTALL_DIR")" ]]; then
command -v sudo >/dev/null 2>&1 || die "Writing $INSTALL_DIR requires root and sudo is unavailable."
RUN_ROOT=(sudo)
fi
fi
if command -v docker >/dev/null 2>&1 && ! docker info >/dev/null 2>&1; then
if [[ "$HOST_KIND" == linux ]]; then
say "Docker is installed but stopped/inaccessible; after preflight the installer will start it or configure docker-group access."
else
die "Docker Desktop is installed but not running. Start it and wait until it is ready, then rerun; no state was changed."
fi
fi
if [[ -f "$INSTALL_DIR/.spaceworks-version" ]]; then
[[ ! ${SPACEWORKS_REPOSITORY+x} ]] || { say "SPACEWORKS_REPOSITORY from the shell is ignored for an existing install; SPACEWORKS_REPOSITORY in $INSTALL_DIR/.env decides where updates come from."; unset SPACEWORKS_REPOSITORY; }
ACTION="$(prompt $'Existing SpaceWorks install detected.\n 1) Update to latest release\n 2) Change modules\n 3) Update and change modules\n 4) Set up automatic deploys (GitHub runner)\n 5) Cancel\nChoose [5]: ')"
ACTION="${ACTION:-5}"
case "$ACTION" in
1) ACTION=update ;;
2) ACTION=modules ;;
3) ACTION=both ;;
4) ACTION=runner ;;
5) say "Cancelled; existing deployment was not changed."; exit 0 ;;
*) die "Invalid menu choice; existing deployment was not changed." ;;
esac
[[ -f "$INSTALL_DIR/scripts/update.sh" ]] || die "The version marker exists but scripts/update.sh is missing. Repair the install manually."
if [[ "$(id -u)" != 0 && ! -w "$INSTALL_DIR" ]]; then
command -v sudo >/dev/null 2>&1 || die "The existing install is not writable and sudo is unavailable."
RUN_ROOT=(sudo)
RUN_INSTALLED_AS_ROOT=1
fi
REPOSITORY="$("${RUN_ROOT[@]}" bash -c 'source "$1/scripts/env-file.sh"; source "$1/scripts/host-image.sh"; resolve_spaceworks_repository "$1"' _ "$INSTALL_DIR")" || die "Invalid installed repository."
RELEASE_API="https://api.github.com/repos/$REPOSITORY/releases/latest"
elif [[ -e "$INSTALL_DIR" ]]; then
if [[ -d "$INSTALL_DIR" && -z "$(find "$INSTALL_DIR" -mindepth 1 -maxdepth 1 -print -quit 2>/dev/null)" ]]; then
EMPTY_INSTALL_DIR=1
else
die "$INSTALL_DIR already contains state but has no .spaceworks-version marker. Refusing to reuse or overwrite it."
fi
else
EMPTY_INSTALL_DIR=0
fi
port_in_use() {
local port="$1" hex file
if [[ "$HOST_KIND" == linux ]]; then
printf -v hex '%04X' "$port"
for file in /proc/net/tcp /proc/net/tcp6; do
[[ -r "$file" ]] || continue
awk -v wanted="$hex" 'NR > 1 { split($2, address, ":"); if (toupper(address[2]) == wanted && $4 == "0A") exit 7 }' "$file" || return 0
done
return 1
fi
netstat -an 2>/dev/null | awk -v pattern="[:.]${port}[[:space:]]" \
'toupper($0) ~ /LISTEN/ && $0 ~ pattern { found=1 } END { exit !found }'
}
if [[ "$ACTION" == fresh ]]; then
for port in 80 9000 9001; do
port_in_use "$port" && die "TCP port $port is already in use. Free it or configure a manual install before running setup."
done
say "Required TCP ports 80, 9000, and 9001 are free."
fi
disk_path="$INSTALL_DIR"
while [[ ! -e "$disk_path" && "$disk_path" != / ]]; do disk_path="$(dirname "$disk_path")"; done
available_kb="$(df -Pk "$disk_path" | awk 'NR == 2 {print $4}')"
[[ "$available_kb" =~ ^[0-9]+$ ]] || die "Could not determine free disk space for $INSTALL_DIR."
((available_kb >= MIN_FREE_KB)) || die "At least 8 GiB free is required; $disk_path has $((available_kb / 1024)) MiB."
say "Disk-space check passed."
if [[ "$ACTION" != modules && "$ACTION" != runner ]]; then
release_json="$(curl --fail --silent --show-error --location \
--header 'Accept: application/vnd.github+json' \
--header 'User-Agent: spaceworks-curl-installer' "$RELEASE_API")" \
|| die "Could not read the latest tagged GitHub release."
RELEASE_TAG="$(printf '%s' "$release_json" | sed -n 's/.*"tag_name"[[:space:]]*:[[:space:]]*"\([^"]*\)".*/\1/p' | head -n 1)"
VERSION="${RELEASE_TAG#v}"
[[ "$VERSION" =~ ^[0-9]+\.[0-9]+\.[0-9]+-main\.[0-9]+\.[0-9a-f]{12}$ ]] \
|| die "GitHub returned an unexpected latest release tag: ${RELEASE_TAG:-<empty>}"
say "Pinned release: $RELEASE_TAG."
setup_command=""; MAKERSPACE_IMAGE_TAG="$VERSION"
if [[ ${SPACEWORKS_REPOSITORY+x} ]]; then
owner="$(printf '%s' "${REPOSITORY%%/*}" | tr '[:upper:]' '[:lower:]')"
MAKERSPACE_BACKEND_IMAGE="${MAKERSPACE_BACKEND_IMAGE-ghcr.io/$owner/spaceworks-backend}"
MAKERSPACE_FRONTEND_IMAGE="${MAKERSPACE_FRONTEND_IMAGE-ghcr.io/$owner/spaceworks-frontend}"
fi
for name in MAKERSPACE_IMAGE_TAG SPACEWORKS_REPOSITORY MAKERSPACE_BACKEND_IMAGE MAKERSPACE_FRONTEND_IMAGE; do
[[ ${!name+x} ]] || continue
value="${!name}"; [[ "$value" != *$'\r'* && "$value" != *$'\n'* ]] || die "$name cannot contain CR or LF."
printf -v assignment '%s=%q ' "$name" "$value"; setup_command+="$assignment"
done
setup_command+="bash setup.sh"
fi
say "All preflight checks passed; state-changing work starts now."
install_linux_dependencies() {
case "$PACKAGE_FAMILY" in
apt)
"${RUN_ROOT[@]}" apt-get update
"${RUN_ROOT[@]}" env DEBIAN_FRONTEND=noninteractive apt-get install -y curl tar python3
[[ "$MISSING_DOCKER" == 0 ]] \
|| "${RUN_ROOT[@]}" env DEBIAN_FRONTEND=noninteractive apt-get install -y docker.io
if [[ "$MISSING_COMPOSE" == 1 ]]; then
"${RUN_ROOT[@]}" env DEBIAN_FRONTEND=noninteractive apt-get install -y docker-compose-v2 \
|| "${RUN_ROOT[@]}" env DEBIAN_FRONTEND=noninteractive apt-get install -y docker-compose-plugin \
|| true
fi
;;
dnf)
manager=dnf; command -v dnf >/dev/null 2>&1 || manager=yum
"${RUN_ROOT[@]}" "$manager" install -y curl tar python3
if [[ "$MISSING_DOCKER" == 1 ]]; then
repo_os=centos
[[ "${ID:-}" == fedora ]] && repo_os=fedora
[[ "${ID:-}" == rhel ]] && repo_os=rhel
if [[ "$manager" == dnf ]]; then
"${RUN_ROOT[@]}" dnf install -y dnf-plugins-core
"${RUN_ROOT[@]}" dnf config-manager --add-repo "https://download.docker.com/linux/$repo_os/docker-ce.repo" \
|| "${RUN_ROOT[@]}" dnf config-manager addrepo --from-repofile="https://download.docker.com/linux/$repo_os/docker-ce.repo"
else
"${RUN_ROOT[@]}" yum install -y yum-utils
"${RUN_ROOT[@]}" yum-config-manager --add-repo "https://download.docker.com/linux/$repo_os/docker-ce.repo"
fi
"${RUN_ROOT[@]}" "$manager" install -y docker-ce docker-ce-cli containerd.io docker-buildx-plugin docker-compose-plugin
else
"${RUN_ROOT[@]}" "$manager" install -y docker-compose-plugin || true
fi
;;
pacman) "${RUN_ROOT[@]}" pacman -Sy --noconfirm --needed curl tar python docker docker-compose ;;
zypper) "${RUN_ROOT[@]}" zypper --non-interactive install curl tar python3 docker docker-compose ;;
esac
}
if ((${#missing[@]} > 0)); then install_linux_dependencies; fi
if ! docker compose version >/dev/null 2>&1; then
compose_json="$(curl --fail --silent --show-error --location \
--header 'Accept: application/vnd.github+json' \
--header 'User-Agent: spaceworks-curl-installer' \
https://api.github.com/repos/docker/compose/releases/latest)"
compose_tag="$(printf '%s' "$compose_json" | sed -n 's/.*"tag_name"[[:space:]]*:[[:space:]]*"\([^"]*\)".*/\1/p' | head -n 1)"
[[ "$compose_tag" =~ ^v[0-9]+\.[0-9]+\.[0-9]+$ ]] || die "Could not resolve a Docker Compose V2 plugin release."
"${RUN_ROOT[@]}" install -m 0755 -d /usr/local/lib/docker/cli-plugins
"${RUN_ROOT[@]}" curl --fail --silent --show-error --location \
"https://github.com/docker/compose/releases/download/$compose_tag/docker-compose-linux-$ARCH" \
-o /usr/local/lib/docker/cli-plugins/docker-compose
"${RUN_ROOT[@]}" chmod 755 /usr/local/lib/docker/cli-plugins/docker-compose
fi
if ! docker info >/dev/null 2>&1; then
if [[ "$HOST_KIND" == linux ]] && command -v systemctl >/dev/null 2>&1; then
"${RUN_ROOT[@]}" systemctl enable --now docker >/dev/null 2>&1 || true
fi
fi
if ! docker info >/dev/null 2>&1; then
if [[ "$HOST_KIND" == linux && "$(id -u)" != 0 ]] && "${RUN_ROOT[@]}" docker info >/dev/null 2>&1; then
operator="$(id -un)"
"${RUN_ROOT[@]}" usermod -aG docker "$operator"
command -v sg >/dev/null 2>&1 || die "Docker was installed, but group membership needs a new login. Sign out, sign in, and rerun this installer."
USE_DOCKER_GROUP=1
say "Added $operator to the docker group; setup will use that group for this run."
else
die "Docker is installed but not running or not accessible. Start Docker, then rerun."
fi
fi
docker compose version >/dev/null 2>&1 || die "Docker Compose V2 is still unavailable after dependency setup."
run_installed() {
local command="$1"
if [[ "$RUN_INSTALLED_AS_ROOT" == 1 ]]; then
"${RUN_ROOT[@]}" bash -c "cd '$INSTALL_DIR' && $command" < /dev/tty
elif [[ "$USE_DOCKER_GROUP" == 1 ]]; then
sg docker -c "cd '$INSTALL_DIR' && $command" < /dev/tty
else
(cd "$INSTALL_DIR" && bash -c "$command" < /dev/tty)
fi
}
case "$ACTION" in modules) run_installed "bash scripts/update.sh --modules-only"; exit 0 ;; runner) [[ -f "$INSTALL_DIR/scripts/install-deploy-runner.sh" ]] || die "This install predates runner deploys; choose 1 (update) first, then 4."; run_installed "bash scripts/install-deploy-runner.sh"; exit 0 ;; esac
ARCHIVE="$(mktemp)"
curl --fail --silent --show-error --location \
"https://github.com/$REPOSITORY/archive/refs/tags/$RELEASE_TAG.tar.gz" -o "$ARCHIVE"
parent="$(dirname "$INSTALL_DIR")"
if [[ ! -d "$parent" ]]; then "${RUN_ROOT[@]}" mkdir -p -- "$parent"; CREATED_PARENT=1; fi
STAGE_DIR="$("${RUN_ROOT[@]}" mktemp -d "$parent/.spaceworks-stage.XXXXXX")"
"${RUN_ROOT[@]}" tar -xzf "$ARCHIVE" -C "$STAGE_DIR" --strip-components=1 --no-same-owner
required_files=(setup.sh docker-compose.prod.yml scripts/update.sh scripts/spaceworks-compose.sh
scripts/module-selection.sh scripts/request-access-selection.sh scripts/update-lock.sh
scripts/install-host-bundle.sh scripts/install-deploy-runner.sh scripts/setup-deploy-runner.sh
scripts/check-restore-host-bundle.py scripts/restore.sh scripts/import-backup.sh install.sh)
for required in "${required_files[@]}"; do
"${RUN_ROOT[@]}" test -f "$STAGE_DIR/$required" && "${RUN_ROOT[@]}" test ! -L "$STAGE_DIR/$required" \
|| die "The pinned release archive is missing regular installer file $required."
done
guarded_paths="$("${RUN_ROOT[@]}" python3 -I -B "$STAGE_DIR/scripts/check-restore-host-bundle.py" \
"$STAGE_DIR" "${required_files[@]}")" || die "The pinned release restore host bundle is incomplete or differs."
while IFS= read -r required; do "${RUN_ROOT[@]}" chmod 644 "$STAGE_DIR/$required"; done <<< "$guarded_paths"
"${RUN_ROOT[@]}" chmod 644 "$STAGE_DIR/scripts/check-restore-host-bundle.py"
"${RUN_ROOT[@]}" chmod 755 "$STAGE_DIR" "$STAGE_DIR/scripts" "$STAGE_DIR/backend" "$STAGE_DIR/backend/apps" "$STAGE_DIR/backend/apps/backup" \
"$STAGE_DIR/install.sh" "$STAGE_DIR/scripts/spaceworks-compose.sh" "$STAGE_DIR/scripts/restore.sh" "$STAGE_DIR/scripts/import-backup.sh"
if [[ "$ACTION" != fresh ]]; then
update_options="--force --no-module-changes"; [[ "$ACTION" != both ]] || update_options="--force --modules"
printf -v update_command 'SPACEWORKS_REPOSITORY=%q SPACEWORKS_PINNED_RELEASE=%q SPACEWORKS_UPDATE_INSTALL_DIR=%q bash %q %s' "$REPOSITORY" "$RELEASE_TAG" "$INSTALL_DIR" "$STAGE_DIR/scripts/update.sh" "$update_options"
for name in SPACEWORKS_COMPOSE_LAYER SPACEWORKS_COMPOSE_BUILD_LAYER; do
[[ -z "${!name:-}" ]] || { printf -v assignment '%s=%q ' "$name" "${!name}"; update_command="$assignment$update_command"; }
done
run_installed "$update_command"
exit 0
fi
if [[ "${EMPTY_INSTALL_DIR:-0}" == 1 ]]; then "${RUN_ROOT[@]}" rmdir -- "$INSTALL_DIR"; fi
"${RUN_ROOT[@]}" mv -- "$STAGE_DIR" "$INSTALL_DIR"
STAGE_DIR=""
if [[ "$(id -u)" != 0 && ${#RUN_ROOT[@]} -gt 0 ]]; then
"${RUN_ROOT[@]}" chown -R "$(id -u):$(id -g)" "$INSTALL_DIR"
fi
setup_status=0; run_installed "$setup_command" || setup_status=$?; [[ "$setup_status" =~ ^[03]$ ]] || exit "$setup_status"
printf '%s\n' "$VERSION" > "$INSTALL_DIR/.spaceworks-version"
[[ "$setup_status" == 0 ]] || die "Installed SpaceWorks $VERSION, but setup exited 3: the module selection was not applied. Re-run this installer and choose module changes."; say "Installed SpaceWorks $VERSION in $INSTALL_DIR."