-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathopskernel.sh
More file actions
executable file
·1041 lines (914 loc) · 35.3 KB
/
opskernel.sh
File metadata and controls
executable file
·1041 lines (914 loc) · 35.3 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
# ============================================================================
# OpsKernel - Interactive Management Script (whiptail TUI)
# Equivalent to the original Makefile commands
# ============================================================================
set -e
# Colors for non-whiptail output
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
NC='\033[0m' # No Color
# Configuration
COMPOSE_FILE="docker/docker-compose.yml"
PLUGIN_COMPOSE_FILE="docker/docker-compose.plugins.yml"
ENV_FILE=".env"
AVAILABLE_PLUGINS=("webshell" "filemanager" "db-explorer" "perf-report")
CORE_SERVICES=("opskernel" "docker-socket-proxy")
PLUGIN_SERVICES=("plugin-webshell" "plugin-filemanager" "plugin-db-explorer" "plugin-perf-report")
UI_MODE=0
# ============================================================================
# Helper Functions
# ============================================================================
check_whiptail() {
if ! command -v whiptail &> /dev/null; then
echo -e "${RED}Error: whiptail is not installed.${NC}"
echo "Install it with: sudo apt install whiptail"
exit 1
fi
}
check_docker() {
if ! command -v docker &> /dev/null; then
echo -e "${RED}Error: docker is not installed.${NC}"
exit 1
fi
}
ui_success() {
if [[ "${UI_MODE}" -eq 1 ]] && command -v whiptail &> /dev/null; then
whiptail --title "Success" --msgbox "$1" 8 72
else
echo -e "${GREEN}$1${NC}"
fi
}
ui_error() {
if [[ "${UI_MODE}" -eq 1 ]] && command -v whiptail &> /dev/null; then
whiptail --title "Error" --scrolltext --msgbox "$1" 18 80
else
echo -e "${RED}$1${NC}" >&2
fi
}
ui_info() {
if [[ "${UI_MODE}" -eq 1 ]] && command -v whiptail &> /dev/null; then
whiptail --title "Info" --scrolltext --msgbox "$1" 18 80
else
echo -e "${YELLOW}$1${NC}"
fi
}
core_compose() {
if [[ -f "$ENV_FILE" ]]; then
docker compose -f "$COMPOSE_FILE" --env-file "$ENV_FILE" "$@"
else
docker compose -f "$COMPOSE_FILE" "$@"
fi
}
plugin_compose() {
docker compose -f "$PLUGIN_COMPOSE_FILE" "$@"
}
run_or_report() {
local action="$1"; shift
local output
if output=$("$@" 2>&1); then
return 0
else
local rc=$?
ui_error "$action failed (exit $rc)\n\nCommand: $*\n\n$output"
return $rc
fi
}
have_timeout() {
command -v timeout &> /dev/null
}
docker_reachable() {
if have_timeout; then
timeout 2 docker version &> /dev/null
else
docker version &> /dev/null
fi
}
container_id_for_service() {
local compose_kind="$1" # core|plugin
local service="$2"
if [[ "$compose_kind" == "core" ]]; then
core_compose ps -q "$service" 2>/dev/null || true
else
plugin_compose ps -q "$service" 2>/dev/null || true
fi
}
container_status_for_id() {
local id="$1"
[[ -z "$id" ]] && { echo "not_created"; return 0; }
docker inspect -f '{{.State.Status}}' "$id" 2>/dev/null || echo "unknown"
}
container_exit_code_for_id() {
local id="$1"
[[ -z "$id" ]] && { echo ""; return 0; }
docker inspect -f '{{.State.ExitCode}}' "$id" 2>/dev/null || echo ""
}
core_status_compact() {
if ! docker_reachable; then
echo "Docker: unavailable | Core: unknown | Plugins: ?/4"
return 0
fi
local ops_id ops_status
ops_id=$(container_id_for_service core "opskernel")
ops_status=$(container_status_for_id "$ops_id")
local running_count=0
for svc in "${PLUGIN_SERVICES[@]}"; do
local pid pstatus
pid=$(container_id_for_service plugin "$svc")
pstatus=$(container_status_for_id "$pid")
[[ "$pstatus" == "running" ]] && running_count=$((running_count+1))
done
local core_state
case "$ops_status" in
running) core_state="running" ;;
exited|dead) core_state="stopped" ;;
not_created) core_state="not_created" ;;
*) core_state="$ops_status" ;;
esac
echo "Docker: ok | Core: ${core_state} | Plugins: ${running_count}/4 running"
}
show_status() {
if ! docker_reachable; then
ui_error "Docker daemon is not reachable.\n\n- Is Docker running?\n- Do you have permission (docker group/root)?\n\nTry: docker info"
return 1
fi
local msg
msg="DOCKER\n Status: reachable\n\nCORE\n"
for svc in "${CORE_SERVICES[@]}"; do
local id st exitc
id=$(container_id_for_service core "$svc")
st=$(container_status_for_id "$id")
exitc=$(container_exit_code_for_id "$id")
if [[ "$st" == "not_created" ]]; then
msg+=" - ${svc}: not created\n"
elif [[ "$st" == "running" ]]; then
msg+=" - ${svc}: running\n"
elif [[ "$st" == "exited" || "$st" == "dead" ]]; then
if [[ -n "$exitc" && "$exitc" != "0" ]]; then
msg+=" - ${svc}: exited (crashed, exit=${exitc})\n"
else
msg+=" - ${svc}: exited (exit=${exitc})\n"
fi
else
msg+=" - ${svc}: ${st}\n"
fi
done
msg+="\nPLUGINS\n"
local running_count=0
local created_count=0
for svc in "${PLUGIN_SERVICES[@]}"; do
local id st exitc
id=$(container_id_for_service plugin "$svc")
st=$(container_status_for_id "$id")
exitc=$(container_exit_code_for_id "$id")
[[ "$st" != "not_created" ]] && created_count=$((created_count+1))
[[ "$st" == "running" ]] && running_count=$((running_count+1))
if [[ "$st" == "not_created" ]]; then
msg+=" - ${svc}: not created\n"
elif [[ "$st" == "running" ]]; then
msg+=" - ${svc}: running\n"
elif [[ "$st" == "exited" || "$st" == "dead" ]]; then
if [[ -n "$exitc" && "$exitc" != "0" ]]; then
msg+=" - ${svc}: stopped/crashed (exit=${exitc})\n"
else
msg+=" - ${svc}: stopped (exit=${exitc})\n"
fi
else
msg+=" - ${svc}: ${st}\n"
fi
done
msg+="\nSummary: Plugins ${running_count}/4 running (${created_count}/4 created)"
ui_info "$msg"
}
# ============================================================================
# Core Services
# ============================================================================
core_up() {
if ! docker_reachable; then
ui_error "Docker daemon is not reachable.\n\nCannot start core services."
return 1
fi
local ops_id ops_status
ops_id=$(container_id_for_service core "opskernel")
ops_status=$(container_status_for_id "$ops_id")
if [[ "$ops_status" == "running" ]]; then
ui_info "Core is already running.\n\n$(core_status_compact)"
return 0
fi
echo "Starting core services..."
run_or_report "Start core services" core_compose up -d opskernel docker-socket-proxy
ui_success "Core services started successfully!\n\n$(core_status_compact)"
}
core_down() {
if ! docker_reachable; then
ui_error "Docker daemon is not reachable.\n\nCannot stop services cleanly."
return 1
fi
echo "Stopping all services..."
run_or_report "Stop core services" core_compose down
ui_success "All services stopped and removed."
}
core_restart() {
if ! docker_reachable; then
ui_error "Docker daemon is not reachable.\n\nCannot restart core services."
return 1
fi
echo "Restarting core services..."
core_compose stop opskernel docker-socket-proxy 2>/dev/null || true
run_or_report "Restart core services" core_compose up -d opskernel docker-socket-proxy
ui_success "Core services restarted!\n\n$(core_status_compact)"
}
core_logs() {
clear
echo -e "${GREEN}Viewing core logs (Ctrl+C to exit)...${NC}"
core_compose logs -f opskernel
}
core_stats() {
clear
echo -e "${GREEN}Container stats (Ctrl+C to exit)...${NC}"
core_compose stats
}
# ============================================================================
# Preset Modes
# ============================================================================
preset_menu() {
local choice
choice=$(whiptail --title "Preset Modes" --menu "Select a preset mode:" 15 60 5 \
"1" "Minimal (CPU/Mem/Disk/Net only)" \
"2" "Server (no GPU/Power)" \
"3" "No Docker (disable Docker management)" \
"4" "Back to Main Menu" \
3>&1 1>&2 2>&3)
case $choice in
1) up_minimal ;;
2) up_server ;;
3) up_no_docker ;;
4) return ;;
esac
}
up_minimal() {
echo "Starting in minimal mode..."
run_or_report "Start minimal mode" env \
ENABLE_DOCKER=false ENABLE_GPU=false ENABLE_CRON=false ENABLE_SSH=false \
ENABLE_SYSTEMD=false ENABLE_SENSORS=false ENABLE_POWER=false ENABLE_SYSTEM=false \
docker compose -f "$COMPOSE_FILE" up -d opskernel
ui_success "Started in minimal mode (CPU/Mem/Disk/Net only)"
}
up_server() {
echo "Starting in server mode..."
run_or_report "Start server mode" env \
ENABLE_GPU=false ENABLE_POWER=false \
docker compose -f "$COMPOSE_FILE" up -d opskernel docker-socket-proxy
ui_success "Started in server mode (no GPU/Power)"
}
up_no_docker() {
echo "Starting without Docker management..."
run_or_report "Start without Docker management" env \
ENABLE_DOCKER=false \
docker compose -f "$COMPOSE_FILE" up -d opskernel
ui_success "Started without Docker management"
}
# ============================================================================
# Single Plugin Operations
# ============================================================================
select_plugin() {
local choice
choice=$(whiptail --title "Select Plugin" --menu "Choose a plugin:" 15 60 6 \
"webshell" "Terminal access via SSH" \
"filemanager" "File management via SFTP" \
"db-explorer" "Database browser (read-only)" \
"perf-report" "Performance reports" \
"back" "Back to previous menu" \
3>&1 1>&2 2>&3)
echo "$choice"
}
plugin_menu() {
while true; do
local choice
local status
status=$(core_status_compact)
choice=$(whiptail --title "Single Plugin Operations" --menu "${status}\n\nSelect operation:" 20 72 9 \
"1" "Build plugin image" \
"2" "Create container (not start)" \
"3" "Start plugin" \
"4" "Stop plugin" \
"5" "Remove plugin container" \
"6" "View plugin logs" \
"7" "Rebuild plugin (build + rm + create)" \
"8" "Back to Main Menu" \
3>&1 1>&2 2>&3)
[[ -z "$choice" ]] && return
[[ "$choice" == "8" ]] && return
local plugin
plugin=$(select_plugin)
[[ -z "$plugin" || "$plugin" == "back" ]] && continue
case $choice in
1) plugin_build "$plugin" ;;
2) plugin_create "$plugin" ;;
3) plugin_up "$plugin" ;;
4) plugin_down "$plugin" ;;
5) plugin_rm "$plugin" ;;
6) plugin_logs "$plugin" ;;
7) plugin_rebuild "$plugin" ;;
esac
done
}
plugin_build() {
local plugin="$1"
echo "Building plugin: $plugin..."
run_or_report "Build plugin $plugin" plugin_compose build "plugin-$plugin"
ui_success "Plugin $plugin built successfully!"
}
plugin_create() {
local plugin="$1"
if ! docker_reachable; then
ui_error "Docker daemon is not reachable.\n\nCannot create plugin container."
return 1
fi
echo "Creating container for plugin: $plugin..."
local id st
id=$(container_id_for_service plugin "plugin-$plugin")
st=$(container_status_for_id "$id")
if [[ "$st" != "not_created" ]]; then
ui_info "Plugin container already exists: $plugin ($st)."
return 0
fi
run_or_report "Create plugin container $plugin" plugin_compose up -d --no-start "plugin-$plugin"
ui_success "Container for $plugin created (not started)."
}
plugin_up() {
local plugin="$1"
if ! docker_reachable; then
ui_error "Docker daemon is not reachable.\n\nCannot start plugin."
return 1
fi
echo "Starting plugin: $plugin..."
local id st
id=$(container_id_for_service plugin "plugin-$plugin")
st=$(container_status_for_id "$id")
if [[ "$st" == "not_created" ]]; then
ui_error "Plugin container is not created: $plugin\n\nRun: plugin-create $plugin"
return 1
fi
if [[ "$st" == "running" ]]; then
ui_info "Plugin is already running: $plugin"
return 0
fi
run_or_report "Start plugin $plugin" plugin_compose start "plugin-$plugin"
ui_success "Plugin $plugin started!"
}
plugin_down() {
local plugin="$1"
if ! docker_reachable; then
ui_error "Docker daemon is not reachable.\n\nCannot stop plugin."
return 1
fi
echo "Stopping plugin: $plugin..."
local id st
id=$(container_id_for_service plugin "plugin-$plugin")
st=$(container_status_for_id "$id")
if [[ "$st" == "not_created" ]]; then
ui_info "Plugin container does not exist: $plugin"
return 0
fi
if [[ "$st" != "running" ]]; then
ui_info "Plugin is not running: $plugin ($st)"
return 0
fi
run_or_report "Stop plugin $plugin" plugin_compose stop "plugin-$plugin"
ui_success "Plugin $plugin stopped."
}
plugin_rm() {
local plugin="$1"
if ! docker_reachable; then
ui_error "Docker daemon is not reachable.\n\nCannot remove plugin container."
return 1
fi
local id st
id=$(container_id_for_service plugin "plugin-$plugin")
st=$(container_status_for_id "$id")
if [[ "$st" == "not_created" ]]; then
ui_info "Plugin container does not exist: $plugin"
return 0
fi
if whiptail --title "Confirm" --yesno "Remove container for plugin $plugin?" 8 60; then
echo "Removing plugin container: $plugin..."
run_or_report "Remove plugin container $plugin" plugin_compose rm -f "plugin-$plugin"
ui_success "Plugin container $plugin removed."
fi
}
plugin_logs() {
local plugin="$1"
clear
echo -e "${GREEN}Viewing logs for $plugin (Ctrl+C to exit)...${NC}"
plugin_compose logs -f "plugin-$plugin"
}
plugin_rebuild() {
local plugin="$1"
if ! docker_reachable; then
ui_error "Docker daemon is not reachable.\n\nCannot rebuild plugin."
return 1
fi
echo "Rebuilding plugin: $plugin..."
run_or_report "Build plugin $plugin" plugin_compose build "plugin-$plugin"
plugin_compose rm -f "plugin-$plugin" 2>/dev/null || true
run_or_report "Create plugin container $plugin" plugin_compose up -d --no-start "plugin-$plugin"
ui_success "Plugin $plugin rebuilt successfully!"
}
# ============================================================================
# All Plugins Operations
# ============================================================================
all_plugins_menu() {
local PLUGINS="plugin-webshell plugin-filemanager plugin-db-explorer plugin-perf-report"
local status
status=$(core_status_compact)
local choice
choice=$(whiptail --title "All Plugins Operations" --menu "${status}\n\nSelect operation for ALL plugins:" 18 72 8 \
"1" "Build all plugin images" \
"2" "Create all containers (not start)" \
"3" "Start all plugins" \
"4" "Stop all plugins" \
"5" "Remove all plugin containers" \
"6" "Rebuild all plugins" \
"7" "Back to Main Menu" \
3>&1 1>&2 2>&3)
case $choice in
1)
echo "Building all plugins..."
run_or_report "Build all plugins" plugin_compose build $PLUGINS
ui_success "All plugins built!"
;;
2)
echo "Creating all plugin containers..."
run_or_report "Create all plugin containers" plugin_compose up -d --no-start $PLUGINS
ui_success "All plugin containers created!"
;;
3)
echo "Starting all plugins..."
run_or_report "Start all plugins" plugin_compose start $PLUGINS
ui_success "All plugins started!"
;;
4)
echo "Stopping all plugins..."
run_or_report "Stop all plugins" plugin_compose stop $PLUGINS
ui_success "All plugins stopped!"
;;
5)
if whiptail --title "Confirm" --yesno "Remove ALL plugin containers?" 8 60; then
echo "Removing all plugin containers..."
run_or_report "Remove all plugin containers" plugin_compose rm -f $PLUGINS
ui_success "All plugin containers removed!"
fi
;;
6)
echo "Rebuilding all plugins..."
run_or_report "Build all plugins" plugin_compose build $PLUGINS
plugin_compose rm -f $PLUGINS 2>/dev/null || true
run_or_report "Create all plugin containers" plugin_compose up -d --no-start $PLUGINS
ui_success "All plugins rebuilt!"
;;
7) return ;;
esac
}
# ============================================================================
# Build & Development
# ============================================================================
build_menu() {
local choice
choice=$(whiptail --title "Build & Development" --menu "Select operation:" 16 60 7 \
"1" "Build core images" \
"2" "Build ALL images (core + plugins)" \
"3" "Rebuild (down + build + up)" \
"4" "Run locally (Go, no Docker)" \
"5" "Clean build artifacts" \
"6" "Quick start (core + all plugins)" \
"7" "Back to Main Menu" \
3>&1 1>&2 2>&3)
case $choice in
1)
echo "Building core images..."
run_or_report "Build core images" core_compose build opskernel docker-socket-proxy
ui_success "Core images built!"
;;
2)
echo "Building ALL images..."
run_or_report "Build all images" core_compose build
ui_success "All images built!"
;;
3)
echo "Rebuilding (down + build + up)..."
run_or_report "Down core services" core_compose down
run_or_report "Build core images" core_compose build opskernel docker-socket-proxy
run_or_report "Up core services" core_compose up -d opskernel docker-socket-proxy
ui_success "Rebuild complete!"
;;
4)
clear
echo -e "${GREEN}Running locally with Go (Ctrl+C to exit)...${NC}"
HOST_FS="" HOST_PROC="/proc" HOST_SYS="/sys" HOST_ETC="/etc" HOST_VAR="/var" HOST_RUN="/run" \
go run cmd/server/main.go
;;
5)
echo "Cleaning build artifacts..."
rm -f server cmd/server/server
ui_success "Build artifacts cleaned!"
;;
6)
quick_start
;;
7) return ;;
esac
}
quick_start() {
local PLUGINS="plugin-webshell plugin-filemanager plugin-db-explorer plugin-perf-report"
echo "Quick starting core + all plugins..."
run_or_report "Start core services" core_compose up -d opskernel docker-socket-proxy
plugin_compose start $PLUGINS 2>/dev/null || true
ui_success "Core services and all plugins started!\n\nAccess the dashboard at http://localhost:38080\n\n$(core_status_compact)"
}
# ============================================================================
# Main Menu
# ============================================================================
main_menu() {
while true; do
local choice
local status
status=$(core_status_compact)
choice=$(whiptail --title "OpsKernel Management" --menu "${status}\n\nSelect a category:" 20 72 10 \
"0" "View Status" \
"1" "Core Services (up/down/restart/logs)" \
"2" "Preset Modes (minimal/server/no-docker)" \
"3" "Single Plugin Operations" \
"4" "All Plugins Operations" \
"5" "Build & Development" \
"6" "View Container Stats" \
"7" "Quick Start (core + all plugins)" \
"8" "Help" \
"9" "Exit" \
3>&1 1>&2 2>&3)
case $choice in
0) show_status ;;
1) core_menu ;;
2) preset_menu ;;
3) plugin_menu ;;
4) all_plugins_menu ;;
5) build_menu ;;
6) core_stats ;;
7) quick_start ;;
8) show_help ;;
9|"") exit 0 ;;
esac
done
}
core_menu() {
local choice
local status
status=$(core_status_compact)
choice=$(whiptail --title "Core Services" --menu "${status}\n\nSelect operation:" 16 72 6 \
"0" "View Status" \
"1" "Start core services" \
"2" "Stop and remove all containers" \
"3" "Restart core services" \
"4" "View logs" \
"5" "Back to Main Menu" \
3>&1 1>&2 2>&3)
case $choice in
0) show_status ;;
1) core_up ;;
2) core_down ;;
3) core_restart ;;
4) core_logs ;;
5) return ;;
esac
}
show_help() {
whiptail --title "OpsKernel Help" --scrolltext --msgbox "
╔══════════════════════════════════════════════════════════════════╗
║ OpsKernel - Management Commands ║
╚══════════════════════════════════════════════════════════════════╝
CORE SERVICES
─────────────────────────────────────────────────────────────────
Start - Start core services (opskernel + docker-proxy)
Stop - Stop and remove ALL containers
Restart - Restart core services (keeps plugins)
Logs - View core service logs (Ctrl+C to exit)
Stats - View container resource usage
PRESET MODES
─────────────────────────────────────────────────────────────────
Minimal - CPU/Mem/Disk/Net only
Server - No GPU/Power modules
No Docker - Without Docker management
PLUGINS
─────────────────────────────────────────────────────────────────
Available: webshell, filemanager, db-explorer, perf-report
Single plugin operations require selecting a plugin first.
All plugins operations affect all 4 plugins at once.
BUILD & DEVELOPMENT
─────────────────────────────────────────────────────────────────
Build - Rebuild core Docker images
Build All - Build all images (core + plugins)
Rebuild - down + build + up
Dev - Run locally with Go (no Docker)
Clean - Remove build artifacts
Quick Start - Start core + all plugins in one step
COMMAND LINE USAGE
─────────────────────────────────────────────────────────────────
./opskernel.sh Interactive menu
./opskernel.sh status Show status summary
./opskernel.sh up Start core services
./opskernel.sh down Stop all services
./opskernel.sh restart Restart core
./opskernel.sh logs View logs
./opskernel.sh plugin-up NAME Start a plugin
./opskernel.sh plugins-up Start all plugins
./opskernel.sh all Quick start everything
PLUGIN DEVELOPMENT
─────────────────────────────────────────────────────────────────
./opskernel.sh plugin validate NAME Validate plugin manifest
./opskernel.sh plugin init NAME Init new plugin from template
" 30 75
}
# ============================================================================
# CLI Mode (non-interactive)
# ============================================================================
cli_mode() {
local cmd="$1"
local arg="$2"
case "$cmd" in
status)
show_status
;;
up)
core_compose up -d opskernel docker-socket-proxy
echo -e "${GREEN}✓ Core services started${NC}"
;;
down)
core_compose down
echo -e "${GREEN}✓ All services stopped${NC}"
;;
restart)
core_compose stop opskernel docker-socket-proxy 2>/dev/null || true
core_compose up -d opskernel docker-socket-proxy
echo -e "${GREEN}✓ Core services restarted${NC}"
;;
logs)
core_compose logs -f opskernel
;;
stats)
core_compose stats
;;
up-minimal)
ENABLE_DOCKER=false ENABLE_GPU=false ENABLE_CRON=false ENABLE_SSH=false \
ENABLE_SYSTEMD=false ENABLE_SENSORS=false ENABLE_POWER=false ENABLE_SYSTEM=false \
docker compose -f "$COMPOSE_FILE" up -d opskernel
echo -e "${GREEN}✓ Started in minimal mode${NC}"
;;
up-server)
ENABLE_GPU=false ENABLE_POWER=false \
docker compose -f "$COMPOSE_FILE" up -d opskernel docker-socket-proxy
echo -e "${GREEN}✓ Started in server mode${NC}"
;;
up-no-docker)
ENABLE_DOCKER=false docker compose -f "$COMPOSE_FILE" up -d opskernel
echo -e "${GREEN}✓ Started without Docker management${NC}"
;;
plugin-build)
[[ -z "$arg" ]] && { echo -e "${RED}Usage: $0 plugin-build <name>${NC}"; exit 1; }
plugin_compose build "plugin-$arg"
echo -e "${GREEN}✓ Plugin $arg built${NC}"
;;
plugin-create)
[[ -z "$arg" ]] && { echo -e "${RED}Usage: $0 plugin-create <name>${NC}"; exit 1; }
plugin_compose up -d --no-start "plugin-$arg"
echo -e "${GREEN}✓ Plugin $arg container created${NC}"
;;
plugin-up)
[[ -z "$arg" ]] && { echo -e "${RED}Usage: $0 plugin-up <name>${NC}"; exit 1; }
plugin_compose start "plugin-$arg"
echo -e "${GREEN}✓ Plugin $arg started${NC}"
;;
plugin-down)
[[ -z "$arg" ]] && { echo -e "${RED}Usage: $0 plugin-down <name>${NC}"; exit 1; }
plugin_compose stop "plugin-$arg"
echo -e "${GREEN}✓ Plugin $arg stopped${NC}"
;;
plugin-rm)
[[ -z "$arg" ]] && { echo -e "${RED}Usage: $0 plugin-rm <name>${NC}"; exit 1; }
plugin_compose rm -f "plugin-$arg"
echo -e "${GREEN}✓ Plugin $arg removed${NC}"
;;
plugin-logs)
[[ -z "$arg" ]] && { echo -e "${RED}Usage: $0 plugin-logs <name>${NC}"; exit 1; }
plugin_compose logs -f "plugin-$arg"
;;
plugin-rebuild)
[[ -z "$arg" ]] && { echo -e "${RED}Usage: $0 plugin-rebuild <name>${NC}"; exit 1; }
plugin_compose build "plugin-$arg"
plugin_compose rm -f "plugin-$arg" 2>/dev/null || true
plugin_compose up -d --no-start "plugin-$arg"
echo -e "${GREEN}✓ Plugin $arg rebuilt${NC}"
;;
plugins-build)
plugin_compose build plugin-webshell plugin-filemanager plugin-db-explorer plugin-perf-report
echo -e "${GREEN}✓ All plugins built${NC}"
;;
plugins-create)
plugin_compose up -d --no-start plugin-webshell plugin-filemanager plugin-db-explorer plugin-perf-report
echo -e "${GREEN}✓ All plugin containers created${NC}"
;;
plugins-up)
plugin_compose start plugin-webshell plugin-filemanager plugin-db-explorer plugin-perf-report
echo -e "${GREEN}✓ All plugins started${NC}"
;;
plugins-down)
plugin_compose stop plugin-webshell plugin-filemanager plugin-db-explorer plugin-perf-report
echo -e "${GREEN}✓ All plugins stopped${NC}"
;;
plugins-rm)
plugin_compose rm -f plugin-webshell plugin-filemanager plugin-db-explorer plugin-perf-report
echo -e "${GREEN}✓ All plugin containers removed${NC}"
;;
plugins-rebuild)
plugin_compose build plugin-webshell plugin-filemanager plugin-db-explorer plugin-perf-report
plugin_compose rm -f plugin-webshell plugin-filemanager plugin-db-explorer plugin-perf-report 2>/dev/null || true
plugin_compose up -d --no-start plugin-webshell plugin-filemanager plugin-db-explorer plugin-perf-report
echo -e "${GREEN}✓ All plugins rebuilt${NC}"
;;
build)
core_compose build opskernel docker-socket-proxy
echo -e "${GREEN}✓ Core images built${NC}"
;;
build-all)
core_compose build
echo -e "${GREEN}✓ All images built${NC}"
;;
rebuild)
core_compose down
core_compose build opskernel docker-socket-proxy
core_compose up -d opskernel docker-socket-proxy
echo -e "${GREEN}✓ Rebuild complete${NC}"
;;
dev)
HOST_FS="" HOST_PROC="/proc" HOST_SYS="/sys" HOST_ETC="/etc" HOST_VAR="/var" HOST_RUN="/run" \
go run cmd/server/main.go
;;
clean)
rm -f server cmd/server/server
echo -e "${GREEN}✓ Build artifacts cleaned${NC}"
;;
all)
core_compose up -d opskernel docker-socket-proxy
plugin_compose start plugin-webshell plugin-filemanager plugin-db-explorer plugin-perf-report 2>/dev/null || true
echo -e "${GREEN}✓ Core services and all plugins started!${NC}"
;;
help|--help|-h)
cat << 'EOF'
OpsKernel Management Script
Usage: ./opskernel.sh [command] [args]
Commands:
(no args) Launch interactive menu (requires whiptail)
status Show Docker/core/plugins status
Core Services:
up Start core services
down Stop and remove all containers
restart Restart core services
logs View core logs
stats View container stats
Preset Modes:
up-minimal Minimal mode (CPU/Mem/Disk/Net only)
up-server Server mode (no GPU/Power)
up-no-docker Without Docker management
Single Plugin:
plugin-build <name> Build plugin image
plugin-create <name> Create container (not start)
plugin-up <name> Start plugin
plugin-down <name> Stop plugin
plugin-rm <name> Remove plugin container
plugin-logs <name> View plugin logs
plugin-rebuild <name> Rebuild plugin
All Plugins:
plugins-build Build all plugins
plugins-create Create all containers
plugins-up Start all plugins
plugins-down Stop all plugins
plugins-rm Remove all containers
plugins-rebuild Rebuild all plugins
Plugin Development:
plugin validate <name> Validate plugin manifest (v1 or v2)
plugin init <name> Initialize new plugin from template
Build:
build Build core images
build-all Build all images
rebuild down + build + up
dev Run locally with Go
clean Clean build artifacts
Quick:
all Start core + all plugins
Available plugins: webshell, filemanager, db-explorer, perf-report
EOF
;;
plugin)
plugin_dev_cmd "$arg" "$3"
;;
*)
echo -e "${RED}Unknown command: $cmd${NC}"
echo "Run '$0 help' for usage information"
exit 1
;;
esac
}
# ============================================================================
# Plugin Development Commands
# ============================================================================
PLUGINCTL_BIN="./bin/pluginctl"
ensure_pluginctl() {
if [[ ! -x "$PLUGINCTL_BIN" ]]; then
echo -e "${YELLOW}Building pluginctl...${NC}"
mkdir -p bin
go build -o "$PLUGINCTL_BIN" ./cmd/pluginctl
if [[ $? -ne 0 ]]; then
echo -e "${RED}Failed to build pluginctl${NC}"
exit 1
fi
echo -e "${GREEN}✓ pluginctl built${NC}"
fi
}
plugin_dev_cmd() {
local subcmd="$1"
local name="$2"
case "$subcmd" in
validate)
[[ -z "$name" ]] && { echo -e "${RED}Usage: $0 plugin validate <name>${NC}"; exit 1; }
ensure_pluginctl
local plugin_dir="plugins/$name"
if [[ ! -d "$plugin_dir" ]]; then
echo -e "${RED}Plugin directory not found: $plugin_dir${NC}"
exit 1
fi
"$PLUGINCTL_BIN" validate "$plugin_dir"
;;
init)
[[ -z "$name" ]] && { echo -e "${RED}Usage: $0 plugin init <name>${NC}"; exit 1; }
plugin_init "$name"
;;
*)
echo -e "${RED}Unknown plugin subcommand: $subcmd${NC}"
echo "Available subcommands: validate, init"
exit 1
;;
esac
}
plugin_init() {
local name="$1"
local target_dir="plugins/$name"
local template_dir="plugins/_template/go"
# Validate name
if ! [[ "$name" =~ ^[a-z][a-z0-9-]*[a-z0-9]$ ]]; then
echo -e "${RED}Invalid plugin name: $name${NC}"
echo "Name must: start with letter, use lowercase a-z, 0-9, hyphen only"
exit 1
fi
if [[ -d "$target_dir" ]]; then
echo -e "${RED}Plugin directory already exists: $target_dir${NC}"
exit 1
fi
if [[ ! -d "$template_dir" ]]; then
echo -e "${RED}Template directory not found: $template_dir${NC}"
exit 1
fi
echo "Creating plugin: $name"
# Copy template
cp -r "$template_dir" "$target_dir"