-
-
Notifications
You must be signed in to change notification settings - Fork 74
Expand file tree
/
Copy pathserverkit
More file actions
1546 lines (1358 loc) · 50 KB
/
Copy pathserverkit
File metadata and controls
1546 lines (1358 loc) · 50 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
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#!/bin/bash
#
# ServerKit CLI - Management tool for ServerKit
#
# Architecture:
# - Backend: systemd service (runs directly on host)
# - Frontend: Docker container (nginx)
#
# Usage: serverkit <command> [options]
#
set -e
# Colors
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
BLUE='\033[0;34m'
NC='\033[0m'
# Truecolor ServerKit identity (degrades to plain text when not supported)
if [ -t 1 ] && [ -z "${NO_COLOR:-}" ] && [ "${TERM:-dumb}" != "dumb" ]; then
ESC=$'\033'
RST_TC="${ESC}[0m"; BLD_TC="${ESC}[1m"
paint() { printf '%s[38;2;%d;%d;%dm' "$ESC" "$1" "$2" "$3"; }
else
RST_TC=''; BLD_TC=''
paint() { :; }
fi
V1="$(paint 196 181 253)"; V2="$(paint 167 139 250)"; V3="$(paint 139 92 246)"
V4="$(paint 124 58 237)"; V5="$(paint 109 40 217)"
PAPER_TC="$(paint 237 233 254)"; ASH_TC="$(paint 165 160 190)"; FOG_TC="$(paint 113 108 140)"
HUE_OK_TC="$(paint 52 211 153)"; HUE_WARN_TC="$(paint 250 204 21)"; HUE_ERR_TC="$(paint 248 113 113)"; HUE_LINK_TC="$(paint 103 232 249)"
# Configuration
INSTALL_DIR="${SERVERKIT_DIR:-/opt/serverkit}"
VENV_DIR="${SERVERKIT_VENV_DIR:-$INSTALL_DIR/venv}"
NGINX_DIR="${SERVERKIT_NGINX_DIR:-/etc/nginx}"
BACKEND_SERVICE="serverkit"
FRONTEND_CONTAINER="serverkit-frontend"
# Helper functions
print_header() {
local ver
ver=$(get_local_version 2>/dev/null || echo "unknown")
printf '\n'
printf ' %s%s▖▌▌%s %s%sServerKit%s %sv%s%s %s•%s %s%s%s\n' \
"${BLD_TC}" "${V2}" "${RST_TC}" "${BLD_TC}" "${PAPER_TC}" "${RST_TC}" "${FOG_TC}" "$ver" "${RST_TC}" \
"${HUE_OK_TC}" "${RST_TC}" "${ASH_TC}" "Stable" "${RST_TC}"
printf ' %s%s▌▖▌%s %sSelf-hosted infrastructure, made simple.%s\n' \
"${BLD_TC}" "${V3}" "${RST_TC}" "${ASH_TC}" "${RST_TC}"
printf ' %s%s▌▌▖%s %sWeb apps · Databases · Docker · Email · DNS · Security%s\n' \
"${BLD_TC}" "${V4}" "${RST_TC}" "${FOG_TC}" "${RST_TC}"
printf ' %s%s▘▘▘%s %sPython + React, one command · serverkit.ai%s\n' \
"${BLD_TC}" "${V5}" "${RST_TC}" "${FOG_TC}" "${RST_TC}"
printf '\n'
}
print_success() { echo -e "${GREEN}✓ $1${NC}"; }
print_error() { echo -e "${RED}✗ $1${NC}"; }
print_warning() { echo -e "${YELLOW}! $1${NC}"; }
print_info() { echo -e "${BLUE}→ $1${NC}"; }
check_root() {
if [ "$EUID" -ne 0 ]; then
print_error "This command requires root privileges. Run with sudo."
exit 1
fi
}
check_installed() {
if [ ! -d "$INSTALL_DIR" ]; then
print_error "ServerKit is not installed in $INSTALL_DIR"
print_info "Run the install script first"
exit 1
fi
}
require_venv() {
# If the expected virtual environment is missing, try to locate an existing
# one from legacy/alternative layouts before giving up.
if [ ! -f "$VENV_DIR/bin/activate" ] || [ ! -x "$VENV_DIR/bin/python" ]; then
print_warning "Virtual environment missing at $VENV_DIR"
local candidate found_venv=""
for candidate in "$INSTALL_DIR/venv" "$INSTALL_DIR/backend/venv" \
"$INSTALL_DIR/backend/.venv" "$INSTALL_DIR/.venv"; do
if [ -f "$candidate/bin/activate" ] && [ -x "$candidate/bin/python" ]; then
found_venv="$candidate"
break
fi
done
if [ -n "$found_venv" ] && [ "$found_venv" != "$VENV_DIR" ]; then
print_info "Using virtual environment at $found_venv"
VENV_DIR="$found_venv"
else
print_error "No usable Python virtual environment found. Run serverkit update to rebuild it."
exit 1
fi
fi
}
run_cli() {
# Run CLI commands directly on host using the virtual environment
require_venv
cd "$INSTALL_DIR/backend"
source "$VENV_DIR/bin/activate"
python cli.py "$@"
}
# Version management
VERSION_FILE="$INSTALL_DIR/VERSION"
REMOTE_VERSION_URL="https://raw.githubusercontent.com/jhd3197/serverkit/main/VERSION"
get_local_version() {
if [ -f "$VERSION_FILE" ]; then
cat "$VERSION_FILE" | tr -d '\n\r '
else
echo "unknown"
fi
}
get_remote_version() {
curl -sf "$REMOTE_VERSION_URL" 2>/dev/null | tr -d '\n\r ' || echo "unknown"
}
compare_versions() {
# Returns: 0 if equal, 1 if v1 > v2, 2 if v1 < v2
local v1="$1"
local v2="$2"
if [ "$v1" = "$v2" ]; then
return 0
fi
# Unparsable versions used to compare as "equal" (every [ -gt ] probe below
# failed silently under 2>/dev/null), reporting "up to date" on a box with
# a missing or corrupt VERSION file. Treat them as "older" instead so
# check-update points the operator at `serverkit update`, which re-validates.
local vre='^[0-9]+\.[0-9]+(\.[0-9]+)?$'
if ! [[ "$v1" =~ $vre ]] || ! [[ "$v2" =~ $vre ]]; then
return 2
fi
# Compare version components
local v1_major=$(echo "$v1" | cut -d. -f1)
local v1_minor=$(echo "$v1" | cut -d. -f2)
local v1_patch=$(echo "$v1" | cut -d. -f3)
local v2_major=$(echo "$v2" | cut -d. -f1)
local v2_minor=$(echo "$v2" | cut -d. -f2)
local v2_patch=$(echo "$v2" | cut -d. -f3)
# X.Y versions carry no patch component — compare it as 0.
v1_patch="${v1_patch:-0}"
v2_patch="${v2_patch:-0}"
if [ "$v1_major" -gt "$v2_major" ] 2>/dev/null; then return 1; fi
if [ "$v1_major" -lt "$v2_major" ] 2>/dev/null; then return 2; fi
if [ "$v1_minor" -gt "$v2_minor" ] 2>/dev/null; then return 1; fi
if [ "$v1_minor" -lt "$v2_minor" ] 2>/dev/null; then return 2; fi
if [ "$v1_patch" -gt "$v2_patch" ] 2>/dev/null; then return 1; fi
if [ "$v1_patch" -lt "$v2_patch" ] 2>/dev/null; then return 2; fi
return 0
}
cmd_version() {
local version=$(get_local_version)
echo "ServerKit version $version"
}
cmd_check_update() {
print_info "Checking for updates..."
local local_version=$(get_local_version)
local remote_version=$(get_remote_version)
echo ""
echo " Installed version: $local_version"
echo " Latest version: $remote_version"
echo ""
if [ "$remote_version" = "unknown" ]; then
print_warning "Could not check remote version (network error?)"
return 1
fi
set +e # Temporarily disable exit on error
compare_versions "$local_version" "$remote_version"
local result=$?
set -e # Re-enable exit on error
if [ $result -eq 2 ]; then
print_success "Update available! Run 'sudo serverkit update' to upgrade."
return 0
elif [ $result -eq 0 ]; then
print_success "You are running the latest version."
return 0
else
print_info "You are running a newer version than released."
return 0
fi
}
# Commands
cmd_help() {
print_header
echo ""
echo "Usage: serverkit <command> [options]"
echo ""
echo "Service Commands:"
echo " start Start all services"
echo " stop Stop all services"
echo " restart Restart all services"
echo " status Show service status"
echo " logs [service] View logs (backend, frontend, or all)"
echo " update [options] Update ServerKit"
echo " --branch <name> Switch to specific branch (e.g., dev)"
echo " --release [vX.Y.Z] Update from release tarball"
echo " --force Force update even if already latest"
echo " --dry-run Show what would change without applying"
echo " branch Show current branch"
echo " uninstall Uninstall ServerKit (preserves data by default)"
echo " --purge / --delete-volumes also delete volumes, DB, data"
echo " --keep-data also keep /etc/serverkit"
echo " --yes skip the confirmation prompt"
echo " --dry-run show actions without changing anything"
echo ""
echo "User Management:"
echo " create-admin Create a new admin user"
echo " reset-password Reset a user's password"
echo " unlock-user Unlock a locked user account"
echo " list-users List all users"
echo " make-admin Promote user to admin"
echo " deactivate-user Deactivate a user account"
echo " activate-user Activate a user account"
echo ""
echo "Database Commands:"
echo " init-db Initialize the database"
echo " migrate-db Apply database migrations (alias: db-migrate [--no-backup])"
echo " db-status Show migration status (--json)"
echo " db-history List all migration revisions"
echo " backup-db Backup the database"
echo " restore-db Restore database from backup"
echo ""
echo "Cleanup Commands:"
echo " cleanup-apps Delete all apps and containers"
echo " factory-reset Complete reset (delete everything)"
echo ""
echo "App Management:"
echo " create-app Show how to create apps (via web UI)"
echo " deploy-template Deploy a template to local or remote server"
echo " list-servers List deployment target servers"
echo " deployment-status Show deployment job status and logs"
echo " list-apps [--all] List deployed apps (--all shows Docker containers)"
echo " start-app Start an app"
echo " stop-app Stop an app"
echo " app-logs View app logs"
echo ""
echo "Site Management:"
echo " setup-proxy Setup host nginx as reverse proxy (run once)"
echo " add-site Add a new site/domain"
echo " list-sites List all configured sites"
echo " remove-site Remove a site"
echo ""
echo "Version Commands:"
echo " version Show current version"
echo " check-update Check if updates are available"
echo ""
echo "Diagnostics:"
echo " doctor Check for common issues"
echo " doctor --fix Auto-fix detected issues"
echo ""
echo "Panel Commands (talk to the running panel API; most support --json):"
echo " panel-status Panel health and version"
echo " panel-doctor Panel-side diagnostics ([--repair] [--yes])"
echo " repair Repair a single drift finding (repair <type> <id>)"
echo " services list | restart <name> — monitored system services"
echo " apps list — applications known to the panel"
echo " manifest plan | diff | apply — a project's serverkit.yaml"
echo " login-url Mint a one-time panel login link"
echo " support-bundle Build a scrubbed diagnostic zip"
echo ""
echo "Utility Commands:"
echo " generate-keys Generate secure keys for .env"
echo " config Edit configuration"
echo " completion Print bash tab-completion (serverkit completion > /etc/bash_completion.d/serverkit)"
echo ""
echo "Examples:"
echo " sudo serverkit start"
echo " sudo serverkit create-admin"
echo " sudo serverkit add-site test.mysite.com 3000"
echo " serverkit logs backend"
echo ""
}
cmd_completion() {
# Print a bash tab-completion script for `serverkit`.
# Install: serverkit completion > /etc/bash_completion.d/serverkit
# Ad-hoc: eval "$(serverkit completion)"
# The word lists are static on purpose (no venv/Python needed to complete);
# scripts/test/test_cli.sh guards them against drifting from the dispatch.
cat <<'COMPLETION_EOF'
# bash completion for the serverkit CLI (generated by `serverkit completion`)
_serverkit() {
local cur commands
cur="${COMP_WORDS[COMP_CWORD]}"
commands="help start stop restart status logs update branch version check-update uninstall create-admin reset-password unlock-user list-users make-admin deactivate-user activate-user init-db migrate-db db-migrate db-status db-history backup-db restore-db cleanup-apps factory-reset generate-keys config setup-proxy create-app deploy-template list-servers deployment-status list-apps start-app stop-app app-logs add-site list-sites remove-site doctor manifest services apps repair login-url support-bundle panel-status panel-doctor completion"
if [ "$COMP_CWORD" -eq 1 ]; then
COMPREPLY=( $(compgen -W "$commands" -- "$cur") )
return
fi
case "${COMP_WORDS[1]}" in
manifest) COMPREPLY=( $(compgen -W "plan diff apply --project --file --json --yes" -- "$cur") ) ;;
services) COMPREPLY=( $(compgen -W "list restart --json" -- "$cur") ) ;;
apps) COMPREPLY=( $(compgen -W "list --json" -- "$cur") ) ;;
logs) COMPREPLY=( $(compgen -W "backend frontend all" -- "$cur") ) ;;
update) COMPREPLY=( $(compgen -W "--branch --release --force --dry-run" -- "$cur") ) ;;
uninstall) COMPREPLY=( $(compgen -W "--purge --delete-volumes --keep-data --yes --dry-run" -- "$cur") ) ;;
doctor) COMPREPLY=( $(compgen -W "--fix" -- "$cur") ) ;;
panel-doctor) COMPREPLY=( $(compgen -W "--repair --yes --json" -- "$cur") ) ;;
panel-status|list-users|list-servers|db-status) COMPREPLY=( $(compgen -W "--json" -- "$cur") ) ;;
cleanup-apps) COMPREPLY=( $(compgen -W "--delete-volumes --keep-db" -- "$cur") ) ;;
list-apps) COMPREPLY=( $(compgen -W "--all --json" -- "$cur") ) ;;
deployment-status) COMPREPLY=( $(compgen -W "--logs --json" -- "$cur") ) ;;
db-migrate|migrate-db) COMPREPLY=( $(compgen -W "--no-backup" -- "$cur") ) ;;
login-url) COMPREPLY=( $(compgen -W "--ttl --ip --user" -- "$cur") ) ;;
support-bundle) COMPREPLY=( $(compgen -W "--out --passphrase" -- "$cur") ) ;;
deploy-template) COMPREPLY=( $(compgen -W "--name --target --var --wait" -- "$cur") ) ;;
*) COMPREPLY=() ;;
esac
}
complete -F _serverkit serverkit
COMPLETION_EOF
}
cmd_start() {
check_root
check_installed
print_info "Starting ServerKit..."
# Start backend (systemd) — a partial install / non-systemd box must get a
# readable warning, not a raw set -e abort.
systemctl start $BACKEND_SERVICE || print_warning "Could not start backend service '$BACKEND_SERVICE' (is systemd available?)"
# Start frontend (Docker)
cd "$INSTALL_DIR"
docker compose up -d || print_warning "Could not start frontend container (is Docker running?)"
sleep 3
print_success "ServerKit started"
cmd_status_brief
}
cmd_stop() {
check_root
check_installed
print_info "Stopping ServerKit..."
# Stop backend (systemd)
systemctl stop $BACKEND_SERVICE || true
# Stop frontend (Docker)
cd "$INSTALL_DIR"
docker compose down || true
print_success "ServerKit stopped"
}
cmd_restart() {
check_root
check_installed
print_info "Restarting ServerKit..."
# Restart backend (systemd) — mirror cmd_stop's guards: a failing service
# manager is reported, never a raw abort.
systemctl restart $BACKEND_SERVICE || print_warning "Could not restart backend service '$BACKEND_SERVICE' (is systemd available?)"
# Restart frontend (Docker)
cd "$INSTALL_DIR"
docker compose restart || print_warning "Could not restart frontend container (is Docker running?)"
sleep 3
print_success "ServerKit restarted"
cmd_status_brief
}
cmd_status_brief() {
# Quick status check
echo ""
# Backend status
if systemctl is-active --quiet $BACKEND_SERVICE; then
print_success "Backend: running"
else
print_error "Backend: stopped"
fi
# Frontend status
if docker ps --format '{{.Names}}' | grep -q $FRONTEND_CONTAINER; then
print_success "Frontend: running"
else
print_error "Frontend: stopped"
fi
# API health
if curl -s http://127.0.0.1:5000/api/v1/system/health > /dev/null 2>&1; then
print_success "API: healthy"
else
print_warning "API: not responding"
fi
}
cmd_status() {
check_installed
print_header
echo ""
echo "Backend Service (systemd):"
echo "─────────────────────────────"
systemctl status $BACKEND_SERVICE --no-pager -l 2>/dev/null || print_warning "Backend service not found"
echo ""
echo "Frontend Container (Docker):"
echo "─────────────────────────────"
cd "$INSTALL_DIR"
docker compose ps 2>/dev/null || print_warning "Frontend container not found"
echo ""
cmd_status_brief
}
cmd_logs() {
check_installed
service="${1:-all}"
case "$service" in
backend)
if command -v journalctl >/dev/null 2>&1; then
journalctl -u $BACKEND_SERVICE -f || print_warning "Could not read the backend journal (is journald running?)"
else
print_warning "journalctl not found — cannot stream backend logs on this system"
fi
;;
frontend)
cd "$INSTALL_DIR"
docker compose logs -f || print_warning "Could not read frontend container logs (is Docker running?)"
;;
all|*)
print_info "Showing backend logs (Ctrl+C to switch to frontend)..."
echo ""
# Show both in split view isn't easy in bash, so show backend first.
# A journal-less box must still fall through to the Docker logs
# below instead of dying here.
if command -v journalctl >/dev/null 2>&1; then
journalctl -u $BACKEND_SERVICE -n 50 --no-pager || print_warning "Could not read the backend journal"
else
print_warning "journalctl not found — skipping backend logs"
fi
echo ""
print_info "Showing frontend logs..."
echo ""
cd "$INSTALL_DIR"
docker compose logs --tail=50 || print_warning "Could not read frontend container logs (is Docker running?)"
echo ""
print_info "For live logs, use: serverkit logs backend OR serverkit logs frontend"
;;
esac
}
cmd_branch() {
check_installed
cd "$INSTALL_DIR"
local current_branch=$(git rev-parse --abbrev-ref HEAD 2>/dev/null || echo "unknown")
local current_commit=$(git rev-parse --short HEAD 2>/dev/null || echo "unknown")
echo ""
echo " Current branch: $current_branch"
echo " Current commit: $current_commit"
echo " Version: $(get_local_version)"
echo ""
# Show available remote branches
print_info "Available branches:"
git branch -r 2>/dev/null | grep -v HEAD | sed 's/origin\// /' | head -10
echo ""
}
cmd_update() {
local update_args=()
# Parse arguments and forward them to the canonical update script.
while [[ $# -gt 0 ]]; do
case $1 in
--force|-f)
update_args+=("--force")
shift
;;
--branch|-b)
# A missing value used to make `shift 2` fail and exit the CLI
# silently under set -e. Guard it like --release below.
if [[ -n "${2:-}" ]] && [[ ! "$2" =~ ^- ]]; then
update_args+=("--branch" "$2")
shift 2
else
print_error "--branch requires a branch name (e.g. --branch dev)"
exit 1
fi
;;
--release|-r)
update_args+=("--release")
if [[ -n "${2:-}" ]] && [[ ! "$2" =~ ^- ]]; then
update_args+=("$2")
shift 2
else
shift
fi
;;
--dry-run|-n)
update_args+=("--dry-run")
shift
;;
*)
shift
;;
esac
done
check_root
check_installed
print_header
echo ""
if [ ! -x "$INSTALL_DIR/scripts/update.sh" ]; then
print_error "Updater not found at $INSTALL_DIR/scripts/update.sh"
exit 1
fi
SERVERKIT_DIR="$INSTALL_DIR" SERVERKIT_VENV_DIR="$VENV_DIR" \
bash "$INSTALL_DIR/scripts/update.sh" "${update_args[@]}"
}
cmd_uninstall() {
check_root
# Flags mirror uninstall.sh so both entry points accept the same options.
local purge=0 keep_data=0 assume_yes=0 dry_run=0
while [[ $# -gt 0 ]]; do
case "$1" in
--purge|--delete-volumes) purge=1; shift ;;
--keep-data) keep_data=1; shift ;;
--yes|-y) assume_yes=1; shift ;;
--dry-run|-n) dry_run=1; shift ;;
*) shift ;;
esac
done
print_header
echo ""
if [ "$purge" = "1" ]; then
print_warning "This will remove ServerKit AND delete all data (volumes, database, backups)."
else
print_warning "This will remove ServerKit. User data (volumes, /var/lib, backups) is preserved."
echo " Use --purge to also delete data, or --keep-data to also keep /etc/serverkit."
fi
[ "$dry_run" = "1" ] && print_warning "DRY RUN — nothing will actually be changed."
echo ""
if [ "$assume_yes" != "1" ]; then
# EOF (no tty / piped input) must not abort under set -e; an empty
# REPLY falls through to the safe "No" path below.
read -p "Are you sure? (y/N) " -n 1 -r || true
echo
if [[ ! $REPLY =~ ^[Yy]$ ]]; then
print_warning "Uninstall cancelled"
return 0
fi
fi
local lib="$INSTALL_DIR/scripts/lib/uninstall.sh"
if [ ! -f "$lib" ]; then
# Remnants-only box: the install tree (and the shared lib) is gone but
# traces remain. Don't duplicate the teardown here — point at the
# standalone uninstaller, which carries its own inline fallback.
print_error "Canonical uninstall routine not found at $lib"
echo ""
if [ -f "$INSTALL_DIR/uninstall.sh" ]; then
print_info "Run the standalone uninstaller instead:"
echo " sudo bash $INSTALL_DIR/uninstall.sh"
else
print_info "Fetch and run the standalone uninstaller instead:"
echo " curl -fsSL https://raw.githubusercontent.com/jhd3197/serverkit/main/uninstall.sh | sudo bash"
echo " (pass flags with: ... | sudo bash -s -- --purge --yes)"
fi
exit 1
fi
# Source the shared routine into memory *before* it starts deleting the
# install tree (which is where the file lives), then step out of that tree.
# shellcheck source=/dev/null
source "$lib"
cd /tmp 2>/dev/null || cd / || true
export SERVERKIT_DIR="$INSTALL_DIR"
export SERVERKIT_PURGE="$purge"
export SERVERKIT_KEEP_DATA="$keep_data"
export SERVERKIT_UNINSTALL_DRY_RUN="$dry_run"
serverkit_uninstall_core
print_success "ServerKit uninstalled"
echo ""
echo "Note: System packages (Docker, Node.js, nginx, Python) were NOT removed."
echo "Remove them manually if no longer needed."
echo ""
echo "To reinstall, run:"
echo " cd ~ && curl -fsSL https://serverkit.ai/install.sh | sudo bash"
}
cmd_create_admin() {
check_installed
print_info "Creating admin user..."
run_cli create-admin
}
cmd_reset_password() {
check_installed
print_info "Resetting password..."
run_cli reset-password
}
cmd_unlock_user() {
check_installed
run_cli unlock-user
}
cmd_list_users() {
check_installed
run_cli list-users "$@"
}
cmd_make_admin() {
check_installed
run_cli make-admin
}
cmd_deactivate_user() {
check_installed
run_cli deactivate-user
}
cmd_activate_user() {
check_installed
run_cli activate-user
}
cmd_init_db() {
check_installed
run_cli init-db
}
cmd_migrate_db() {
check_installed
# The Click verb is `db-migrate`; `migrate-db` is the legacy wrapper name.
run_cli db-migrate
}
cmd_panel_cli() {
# Forward a command verbatim to the Python admin CLI (backend/cli.py) so
# every Click verb is reachable as `serverkit <verb> …` without a wrapper
# function per command (that pattern is how migrate-db silently broke).
check_installed
run_cli "$@"
}
cmd_cleanup_apps() {
check_root
check_installed
DELETE_VOLUMES=""
KEEP_DB=""
# Parse arguments
while [[ $# -gt 0 ]]; do
case $1 in
--delete-volumes|-v)
DELETE_VOLUMES="--delete-volumes"
shift
;;
--keep-db|-k)
KEEP_DB="--keep-db"
shift
;;
*)
shift
;;
esac
done
run_cli cleanup-apps $DELETE_VOLUMES $KEEP_DB
}
cmd_factory_reset() {
check_root
check_installed
run_cli factory-reset
}
cmd_backup_db() {
check_installed
BACKUP_DIR="$INSTALL_DIR/backups"
BACKUP_FILE="$BACKUP_DIR/serverkit-$(date +%Y%m%d-%H%M%S).db"
DB_FILE="$INSTALL_DIR/backend/instance/serverkit.db"
mkdir -p "$BACKUP_DIR"
if [ -f "$DB_FILE" ]; then
cp "$DB_FILE" "$BACKUP_FILE"
print_success "Database backed up to $BACKUP_FILE"
else
print_error "Database file not found: $DB_FILE"
exit 1
fi
}
cmd_restore_db() {
check_root
check_installed
BACKUP_DIR="$INSTALL_DIR/backups"
DB_FILE="$INSTALL_DIR/backend/instance/serverkit.db"
if [ -z "$1" ]; then
echo "Available backups:"
ls -la "$BACKUP_DIR"/*.db 2>/dev/null || echo "No backups found"
echo ""
read -p "Enter backup file path: " BACKUP_FILE || true
else
BACKUP_FILE="$1"
fi
if [ ! -f "$BACKUP_FILE" ]; then
print_error "Backup file not found: $BACKUP_FILE"
exit 1
fi
print_warning "This will overwrite the current database!"
read -p "Continue? (y/N) " -n 1 -r || true
echo
if [[ $REPLY =~ ^[Yy]$ ]]; then
# Stop backend before restore
systemctl stop $BACKEND_SERVICE
cp "$BACKUP_FILE" "$DB_FILE"
# Restart backend
systemctl start $BACKEND_SERVICE
print_success "Database restored"
fi
}
cmd_generate_keys() {
print_header
echo ""
echo "Add these to your .env file:"
echo ""
SECRET_KEY=$(openssl rand -hex 32)
JWT_SECRET_KEY=$(openssl rand -hex 32)
SERVERKIT_ENCRYPTION_KEY=$(python3 -c 'import base64, os; print(base64.urlsafe_b64encode(os.urandom(32)).decode())')
echo "SECRET_KEY=$SECRET_KEY"
echo "JWT_SECRET_KEY=$JWT_SECRET_KEY"
echo "SERVERKIT_ENCRYPTION_KEY=$SERVERKIT_ENCRYPTION_KEY"
echo ""
}
cmd_config() {
check_installed
${EDITOR:-nano} "$INSTALL_DIR/.env"
print_info "Restart ServerKit to apply changes: sudo serverkit restart"
}
cmd_setup_proxy() {
check_root
check_installed
print_info "Setting up host nginx as reverse proxy..."
if [ -f "$INSTALL_DIR/scripts/setup-nginx-proxy.sh" ]; then
chmod +x "$INSTALL_DIR/scripts/setup-nginx-proxy.sh"
"$INSTALL_DIR/scripts/setup-nginx-proxy.sh"
else
print_error "Setup script not found. Run 'serverkit update' first."
exit 1
fi
}
cmd_create_app() {
print_header
echo ""
echo "To create a new app, use the ServerKit web interface:"
echo ""
echo " 1. Open ServerKit in your browser"
echo " 2. Go to Templates in the sidebar"
echo " 3. Choose from 60+ available templates:"
echo " - PHP Application (custom PHP projects)"
echo " - Python Application (Flask, FastAPI, Django)"
echo " - Node.js Application (Express, Next.js)"
echo " - WordPress, Ghost, Nextcloud, and many more"
echo " 4. Click Install and configure your app"
echo ""
echo "The web interface provides:"
echo " ✓ One-click installation"
echo " ✓ Automatic port assignment"
echo " ✓ Configuration UI"
echo " ✓ Update management"
echo ""
}
cmd_deploy_template() {
check_installed
shift
run_cli deploy-template "$@"
}
cmd_list_servers() {
check_installed
run_cli list-servers "$@"
}
cmd_deployment_status() {
check_installed
shift
run_cli deployment-status "$@"
}
cmd_list_apps() {
check_installed
# Forward flags verbatim (--all, --json, …); -a stays as the legacy alias.
local arg args=()
for arg in "$@"; do
[ "$arg" = "-a" ] && arg="--all"
args+=("$arg")
done
run_cli list-apps "${args[@]}"
}
cmd_start_app() {
check_root
NAME="$1"
if [ -z "$NAME" ]; then
echo "Usage: serverkit start-app <name>"
cmd_list_apps
exit 1
fi
APP_DIR="/var/www/$NAME"
if [ ! -d "$APP_DIR" ]; then
print_error "App not found: $NAME"
exit 1
fi
print_info "Starting $NAME..."
cd "$APP_DIR"
docker compose up -d
print_success "$NAME started"
}
cmd_stop_app() {
check_root
NAME="$1"
if [ -z "$NAME" ]; then
echo "Usage: serverkit stop-app <name>"
cmd_list_apps
exit 1
fi
APP_DIR="/var/www/$NAME"
if [ ! -d "$APP_DIR" ]; then
print_error "App not found: $NAME"
exit 1
fi
print_info "Stopping $NAME..."
cd "$APP_DIR"
docker compose down
print_success "$NAME stopped"
}
cmd_app_logs() {
NAME="$1"
if [ -z "$NAME" ]; then
echo "Usage: serverkit app-logs <name>"
cmd_list_apps
exit 1
fi
APP_DIR="/var/www/$NAME"
if [ ! -d "$APP_DIR" ]; then
print_error "App not found: $NAME"
exit 1
fi
cd "$APP_DIR"
docker compose logs -f
}
# Resolve the nginx vhost layout: Debian-style sites-available/sites-enabled
# when present, otherwise the conf.d layout used by the RHEL family. Sets
# SITE_FILE (where the vhost lives) and SITE_LINK (the sites-enabled symlink;
# empty in conf.d layouts, where the vhost file itself is live).
resolve_site_paths() {
local domain="$1"
if [ -d "$NGINX_DIR/sites-available" ]; then
SITE_FILE="$NGINX_DIR/sites-available/${domain}.conf"
SITE_LINK="$NGINX_DIR/sites-enabled/${domain}.conf"
else
SITE_FILE="$NGINX_DIR/conf.d/${domain}.conf"
SITE_LINK=""
fi
}
# Reload nginx if we can; on non-systemd or stopped-nginx boxes this is an
# observation, not a fatal error — the config is already on disk either way.
reload_nginx_or_warn() {
if systemctl reload nginx 2>/dev/null || nginx -s reload 2>/dev/null; then
return 0
fi
print_warning "Could not reload nginx — apply the change manually: systemctl reload nginx (or nginx -s reload)"
}
cmd_add_site() {
check_root
DOMAIN="$1"
PORT="${2:-3000}"
if [ -z "$DOMAIN" ]; then
echo "Usage: serverkit add-site <domain> [port]"
echo ""
echo "Examples:"
echo " serverkit add-site test.builditdesign.com"
echo " serverkit add-site myapp.builditdesign.com 3000"
exit 1
fi
resolve_site_paths "$DOMAIN"
if [ -f "$SITE_FILE" ]; then
print_warning "Site config already exists: $SITE_FILE"
read -p "Overwrite? (y/N) " -n 1 -r || true
echo
if [[ ! $REPLY =~ ^[Yy]$ ]]; then
exit 0
fi
fi
print_info "Creating site config for $DOMAIN..."
mkdir -p "$(dirname "$SITE_FILE")"
cat > "$SITE_FILE" << EOF
# Site: $DOMAIN
# Created: $(date)
server {
listen 80;
server_name $DOMAIN;
location / {
proxy_pass http://127.0.0.1:$PORT;
proxy_http_version 1.1;
proxy_set_header Host \$host;
proxy_set_header X-Real-IP \$remote_addr;
proxy_set_header X-Forwarded-For \$proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto \$scheme;
proxy_set_header Upgrade \$http_upgrade;
proxy_set_header Connection "upgrade";
}
}
EOF
# Enable the site (Debian layout only — conf.d configs are live as written)
if [ -n "$SITE_LINK" ]; then
mkdir -p "$(dirname "$SITE_LINK")"
ln -sf "$SITE_FILE" "$SITE_LINK"
fi
# Test and reload nginx. A stopped nginx (or a non-systemd box) must not
# raw-abort here — the site file is already written and valid.
if nginx -t 2>/dev/null; then
reload_nginx_or_warn
print_success "Site added: $DOMAIN → localhost:$PORT"
echo ""
echo "Next steps:"
echo " 1. Add DNS record in Cloudflare:"