-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinstall.sh
More file actions
executable file
·422 lines (376 loc) · 16.4 KB
/
Copy pathinstall.sh
File metadata and controls
executable file
·422 lines (376 loc) · 16.4 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
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
#!/usr/bin/env bash
# Ctrlable Provisioner — bootstrap installer
# Run on a fresh Proxmox VE 8.x host.
#
# Remote install (production — auto-enrolls into portal):
# export CTRLABLE_PORTAL_PASSWORD=your_password # avoids password in ps/history
# curl -fsSL https://raw.githubusercontent.com/ctrlable/ctrlable-provisioner/main/install.sh \
# | bash -s -- --portal-email admin@example.com
#
# Without auto-enroll (paste token manually in the Platform tab afterward):
# bash -c "$(curl -fsSL https://raw.githubusercontent.com/ctrlable/ctrlable-provisioner/main/install.sh)"
#
# Local install (development — run from the repo root on the PVE host):
# ./install.sh --local .
set -euo pipefail
# ---------------------------------------------------------------------------
# Defaults (override via flags)
# ---------------------------------------------------------------------------
VMID=900
LXC_HOSTNAME=ctrlable-orchestrator
MEMORY=2048
CORES=2
DISK_SIZE=8
BRIDGE=vmbr0
STORAGE=local-lvm
REPO_URL=https://github.com/ctrlable/ctrlable-provisioner
REPO_REF=main
LOCAL_SRC="" # path to local repo checkout (--local <path>)
ENROLL_TOKEN="" # portal.ctrlable.com enrollment token (--enroll-token TOKEN)
PORTAL_URL="https://portal.ctrlable.com" # override with --portal-url
PORTAL_EMAIL="" # required when using --portal-password; set with --portal-email
PORTAL_PASSWORD="" # read from CTRLABLE_PORTAL_PASSWORD env var or --portal-password PW
# ---------------------------------------------------------------------------
# Arg parsing
# ---------------------------------------------------------------------------
usage() {
echo "Usage: $0 [--vmid N] [--bridge BR] [--storage ST] [--local PATH] [--repo URL] [--ref REF] [--enroll-token TOKEN] [--portal-url URL] [--portal-email EMAIL] [--portal-password PW]"
exit 1
}
while [[ $# -gt 0 ]]; do
case $1 in
--vmid) VMID="$2"; shift 2 ;;
--bridge) BRIDGE="$2"; shift 2 ;;
--storage) STORAGE="$2"; shift 2 ;;
--local) LOCAL_SRC="$2"; shift 2 ;;
--repo) REPO_URL="$2"; shift 2 ;;
--ref) REPO_REF="$2"; shift 2 ;;
--enroll-token) ENROLL_TOKEN="$2"; shift 2 ;;
--portal-url) PORTAL_URL="$2"; shift 2 ;;
--portal-email) PORTAL_EMAIL="$2"; shift 2 ;;
--portal-password) PORTAL_PASSWORD="$2"; shift 2 ;;
--help|-h) usage ;;
*) echo "Unknown argument: $1"; usage ;;
esac
done
# Prefer env var over CLI flag so the password never appears in `ps aux`
[[ -n "${CTRLABLE_PORTAL_PASSWORD:-}" && -z "$PORTAL_PASSWORD" ]] \
&& PORTAL_PASSWORD="$CTRLABLE_PORTAL_PASSWORD"
# ---------------------------------------------------------------------------
# Helpers
# ---------------------------------------------------------------------------
RED='\033[0;31m'; GREEN='\033[0;32m'; CYAN='\033[0;36m'; BOLD='\033[1m'; NC='\033[0m'
log() { echo -e "${CYAN}[install]${NC} $*"; }
ok() { echo -e "${GREEN}[✓]${NC} $*"; }
die() { echo -e "${RED}[✗] $*${NC}" >&2; exit 1; }
pct_exec() { pct exec "$VMID" -- "$@"; }
pct_push() { pct push "$VMID" "$1" "$2"; }
pct_pull() { pct pull "$VMID" "$1" "$2"; }
# Secure temp dir — cleaned up on exit even if the script fails
TMPDIR_PRIV=$(mktemp -d)
chmod 700 "$TMPDIR_PRIV"
trap 'rm -rf "$TMPDIR_PRIV"' EXIT
# ---------------------------------------------------------------------------
# Preflight
# ---------------------------------------------------------------------------
preflight() {
command -v pct >/dev/null 2>&1 || die "pct not found — run this on a Proxmox VE host"
command -v pveum >/dev/null 2>&1 || die "pveum not found"
command -v pvesh >/dev/null 2>&1 || die "pvesh not found"
command -v pveam >/dev/null 2>&1 || die "pveam not found"
[[ $(id -u) -eq 0 ]] || die "must run as root"
PVE_NODE=$(hostname -s)
PVE_HOST=$(hostname -I | awk '{print $1}')
ok "PVE node: ${PVE_NODE} (${PVE_HOST})"
if [[ -n "$LOCAL_SRC" ]]; then
[[ -d "$LOCAL_SRC/backend" ]] || die "--local path does not look like the provisioner repo: $LOCAL_SRC"
LOCAL_SRC=$(realpath "$LOCAL_SRC")
ok "local source: $LOCAL_SRC"
fi
}
# ---------------------------------------------------------------------------
# Debian 12 LXC template
# ---------------------------------------------------------------------------
get_template() {
log "checking for Debian 12 LXC template"
pveam update >/dev/null 2>&1 || true
TEMPLATE=$(pveam available --section system 2>/dev/null \
| grep "debian-12-standard" | tail -1 | awk '{print $2}')
[[ -n "$TEMPLATE" ]] || die "no Debian 12 standard template found in pveam"
if ! pveam list local 2>/dev/null | grep -q "$TEMPLATE"; then
log "downloading $TEMPLATE (this may take a minute)"
pveam download local "$TEMPLATE"
fi
TEMPLATE_STOR="local:vztmpl/$TEMPLATE"
ok "template: $TEMPLATE"
}
# ---------------------------------------------------------------------------
# Create + start orchestrator LXC
# ---------------------------------------------------------------------------
create_lxc() {
if pct status "$VMID" >/dev/null 2>&1; then
log "VMID $VMID already exists — resuming"
pct start "$VMID" 2>/dev/null || true
else
log "creating orchestrator LXC (VMID=$VMID, storage=$STORAGE, bridge=$BRIDGE)"
pct create "$VMID" "$TEMPLATE_STOR" \
--hostname "$LXC_HOSTNAME" \
--memory "$MEMORY" \
--cores "$CORES" \
--rootfs "${STORAGE}:${DISK_SIZE}" \
--net0 "name=eth0,bridge=${BRIDGE},ip=dhcp" \
--unprivileged 1 \
--features nesting=1 \
--start 0
pct start "$VMID"
fi
log "waiting for DHCP"
local ip="" i
for i in $(seq 1 40); do
ip=$(pct exec "$VMID" -- hostname -I 2>/dev/null | awk '{print $1}') || true
[[ -n "$ip" ]] && break
sleep 3
done
[[ -n "$ip" ]] || die "LXC did not obtain an IP after 120 s"
ORCHESTRATOR_IP="$ip"
ok "orchestrator IP: $ORCHESTRATOR_IP"
}
# ---------------------------------------------------------------------------
# PVE API token
# ---------------------------------------------------------------------------
setup_pve_auth() {
log "creating PVE API role and token"
local privs="Sys.Audit,VM.Allocate,VM.Audit,VM.Clone,VM.Config.CPU,VM.Config.Disk,VM.Config.Memory,VM.Config.Network,VM.Config.Options,VM.PowerMgmt,Datastore.AllocateSpace,SDN.Use"
# modify if exists, add if not — avoids unreliable pveum role list grep in non-TTY
pveum role modify CtrlableProvisioner --privs "$privs" 2>/dev/null \
|| pveum role add CtrlableProvisioner --privs "$privs"
pveum user add provisioner@pve 2>/dev/null || true
# PVE 9: pveum acl modify (--users / --roles); pveum aclmod is a deprecated alias
pveum acl modify / --users provisioner@pve --roles CtrlableProvisioner
# Revoke first so re-runs don't fail on "token exists"
pveum user token remove provisioner@pve provisioner 2>/dev/null || true
local json
json=$(pveum user token add provisioner@pve provisioner --privsep=0 --output-format json)
TOKEN_SECRET=$(python3 -c "import json,sys; print(json.load(sys.stdin)['value'])" <<< "$json")
TOKEN_ID="provisioner@pve!provisioner"
ok "PVE token: $TOKEN_ID"
}
# ---------------------------------------------------------------------------
# Build-plane SSH key
# ---------------------------------------------------------------------------
setup_build_key() {
log "generating build SSH key"
mkdir -p /etc/ctrlable
chmod 700 /etc/ctrlable
if [[ ! -f /etc/ctrlable/build_key ]]; then
ssh-keygen -t ed25519 -f /etc/ctrlable/build_key -N "" \
-C "ctrlable-build@${PVE_NODE}" -q
fi
BUILD_TOKEN=$(python3 -c "import secrets; print(secrets.token_hex(16))")
# Add command=-restricted entry (idempotent)
local pubkey entry
pubkey=$(cat /etc/ctrlable/build_key.pub)
entry="command=\"/usr/local/bin/ctrlable-build\",restrict,no-pty ${pubkey}"
mkdir -p /root/.ssh
touch /root/.ssh/authorized_keys
chmod 600 /root/.ssh/authorized_keys
if ! grep -qF "ctrlable-build" /root/.ssh/authorized_keys; then
echo "$entry" >> /root/.ssh/authorized_keys
fi
ok "build SSH key installed"
}
# ---------------------------------------------------------------------------
# Inner LXC setup
# ---------------------------------------------------------------------------
setup_lxc() {
log "installing packages inside LXC (this takes a few minutes)"
pct_exec bash -c "
set -euo pipefail
export DEBIAN_FRONTEND=noninteractive
apt-get update -qq
apt-get install -y -qq python3 python3-pip python3-venv git curl ca-certificates gnupg
"
log "installing Node.js 20 LTS"
pct_exec bash -c "
set -euo pipefail
curl -fsSL https://deb.nodesource.com/gpgkey/nodesource-repo.gpg.key \
| gpg --dearmor -o /usr/share/keyrings/nodesource.gpg
echo 'deb [signed-by=/usr/share/keyrings/nodesource.gpg] https://deb.nodesource.com/node_20.x nodistro main' \
> /etc/apt/sources.list.d/nodesource.list
apt-get update -qq
apt-get install -y -qq nodejs
"
if [[ -n "$LOCAL_SRC" ]]; then
log "packing and pushing local repo"
tar czf /tmp/ctrlable-provisioner.tar.gz \
--exclude='.venv' --exclude='node_modules' --exclude='__pycache__' \
--exclude='*.pyc' --exclude='.git' --exclude='frontend/dist' \
-C "$LOCAL_SRC" .
pct_push /tmp/ctrlable-provisioner.tar.gz /tmp/ctrlable-provisioner.tar.gz
pct_exec bash -c "
mkdir -p /opt/ctrlable-provisioner
tar xzf /tmp/ctrlable-provisioner.tar.gz -C /opt/ctrlable-provisioner
"
rm /tmp/ctrlable-provisioner.tar.gz
else
log "cloning provisioner repo (${REPO_REF})"
pct_exec git clone --branch "$REPO_REF" "$REPO_URL" /opt/ctrlable-provisioner
fi
log "installing Python dependencies"
pct_exec bash -c "
cd /opt/ctrlable-provisioner
python3 -m venv .venv
.venv/bin/pip install --quiet -r backend/requirements.txt
"
log "building frontend"
pct_exec bash -c "
cd /opt/ctrlable-provisioner/frontend
npm install --silent
npm run build
"
log "installing orchestrator systemd service"
pct_exec bash -c "
install -m 644 /opt/ctrlable-provisioner/deploy/ctrlable-provisioner.service \
/etc/systemd/system/
systemctl daemon-reload
systemctl enable ctrlable-provisioner
"
ok "LXC setup complete"
}
# ---------------------------------------------------------------------------
# Write configuration
# ---------------------------------------------------------------------------
write_config() {
log "writing orchestrator .env"
local env_content
env_content=$(cat <<ENV
PVE_HOST=${PVE_HOST}
PVE_TOKEN_ID=${TOKEN_ID}
PVE_TOKEN_SECRET=${TOKEN_SECRET}
PVE_NODE=${PVE_NODE}
PVE_VERIFY_SSL=false
BUILD_KEY_PATH=/etc/ctrlable/build_key
BUILD_TOKEN=${BUILD_TOKEN}
ORCHESTRATOR_URL=http://${ORCHESTRATOR_IP}:8000
ENV
)
printf '%s\n' "$env_content" > "$TMPDIR_PRIV/ctrlable.env"
pct_push "$TMPDIR_PRIV/ctrlable.env" /opt/ctrlable-provisioner/backend/.env
pct_exec chmod 600 /opt/ctrlable-provisioner/backend/.env
# Copy build SSH private key into the orchestrator LXC so it can reach the host
pct_exec mkdir -p /etc/ctrlable
pct_push /etc/ctrlable/build_key /etc/ctrlable/build_key
pct_exec chmod 600 /etc/ctrlable/build_key
# Store portal enrollment token for auto-enrollment on first boot
if [[ -n "$ENROLL_TOKEN" ]]; then
printf '%s\n' "$ENROLL_TOKEN" > "$TMPDIR_PRIV/enroll.token"
pct_push "$TMPDIR_PRIV/enroll.token" /etc/ctrlable/enroll.token
pct_exec chmod 600 /etc/ctrlable/enroll.token
ok "enrollment token stored — orchestrator will auto-enroll on first internet access"
fi
log "writing host build.conf"
cat > /etc/ctrlable/build.conf <<JSON
{
"orchestrator_url": "http://${ORCHESTRATOR_IP}:8000",
"build_token": "${BUILD_TOKEN}"
}
JSON
chmod 600 /etc/ctrlable/build.conf
ok "configuration written"
}
# ---------------------------------------------------------------------------
# Auto-fetch enrollment token from portal (if --portal-password was given)
# ---------------------------------------------------------------------------
fetch_enroll_token() {
[[ -n "$PORTAL_PASSWORD" && -z "$ENROLL_TOKEN" ]] || return 0
[[ -n "$PORTAL_EMAIL" ]] || die "--portal-email is required when using --portal-password"
log "fetching enrollment token from ${PORTAL_URL}"
local jwt creds
# Build credentials in a variable — never interpolated into a position where
# it would appear in the process list or shell trace
creds=$(python3 -c "
import json, sys
print(json.dumps({'email': sys.argv[1], 'password': sys.argv[2]}))
" "$PORTAL_EMAIL" "$PORTAL_PASSWORD")
jwt=$(curl -fsSL -X POST "${PORTAL_URL}/api/v1/auth/login" \
-H "Content-Type: application/json" \
-d "$creds" \
| python3 -c "import sys,json; d=json.load(sys.stdin); t=d.get('access_token'); print(t) if t else exit(1)" \
2>/dev/null) || die "portal login failed — check --portal-email and --portal-password"
unset creds
ENROLL_TOKEN=$(curl -fsSL -X POST "${PORTAL_URL}/api/v1/devices/enrollment-tokens" \
-H "Authorization: Bearer ${jwt}" \
-H "Content-Type: application/json" \
-d '{"expires_hours":24,"is_appliance":true}' \
| python3 -c "import sys,json; d=json.load(sys.stdin); t=d.get('token'); print(t) if t else exit(1)" \
2>/dev/null) || die "failed to create enrollment token from portal"
unset jwt
ok "enrollment token obtained from portal"
}
# ---------------------------------------------------------------------------
# Install ctrlable-build on host
# ---------------------------------------------------------------------------
install_host_tools() {
log "installing ctrlable-build on host"
pct_pull /opt/ctrlable-provisioner/host/ctrlable-build /usr/local/bin/ctrlable-build
chmod +x /usr/local/bin/ctrlable-build
ok "ctrlable-build installed at /usr/local/bin/ctrlable-build"
}
# ---------------------------------------------------------------------------
# Start orchestrator
# ---------------------------------------------------------------------------
start_orchestrator() {
log "starting orchestrator service"
pct_exec systemctl start ctrlable-provisioner
local i
for i in $(seq 1 20); do
if pct_exec curl -sf http://localhost:8000/health >/dev/null 2>&1; then
ok "orchestrator is up"
return 0
fi
sleep 3
done
echo "Warning: orchestrator health check timed out — check 'pct exec $VMID -- journalctl -u ctrlable-provisioner'" >&2
}
# ---------------------------------------------------------------------------
# Summary
# ---------------------------------------------------------------------------
print_summary() {
echo ""
echo -e "${BOLD}=== Ctrlable Provisioner installed ===${NC}"
echo ""
echo -e " Orchestrator LXC : ${BOLD}${VMID}${NC} (${LXC_HOSTNAME})"
echo -e " Web UI : ${BOLD}http://${ORCHESTRATOR_IP}:8000${NC}"
echo -e " Build key : /etc/ctrlable/build_key"
echo ""
echo -e "${BOLD}Next steps:${NC}"
if [[ -n "$ENROLL_TOKEN" ]]; then
echo " 1. Orchestrator will auto-enroll in ${PORTAL_URL} within ~30 seconds"
else
echo " 1. Open the web UI → Platform tab → paste an enrollment token"
echo " (or re-run with --portal-password to auto-enroll)"
fi
echo " 2. Go to Releases → Build release 2026.06"
echo " 3. Wait for all LXC templates to build (~10 min)"
echo " 4. Deploy your first stack"
echo ""
echo -e " To view logs: ${CYAN}pct exec $VMID -- journalctl -fu ctrlable-provisioner${NC}"
echo ""
}
# ---------------------------------------------------------------------------
# Main
# ---------------------------------------------------------------------------
echo ""
echo -e "${BOLD}Ctrlable Provisioner — bootstrap installer${NC}"
echo ""
preflight
get_template
create_lxc
setup_pve_auth
setup_build_key
setup_lxc
fetch_enroll_token
write_config
install_host_tools
start_orchestrator
print_summary