Repository navigation
Expand file tree
/
Copy pathinstall.sh
More file actions
executable file
·992 lines (938 loc) · 45.1 KB
/
Copy pathinstall.sh
File metadata and controls
executable file
·992 lines (938 loc) · 45.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
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
#!/usr/bin/env bash
# =============================================================================
# install.sh — installs k3s + Helm + Open5GS (5G SA) + UERANSIM on a single
# Ubuntu 24.04 node (desktop or server). Idempotent: it can run many times;
# it only applies what changed in config.env.
#
# Usage:
# sudo ./install.sh # full installation
# sudo ./install.sh --deploy-only # only (re)generates the values and runs helm upgrade
# sudo ./install.sh --dashboard-only # only the dashboard (does not touch the core or the gNB)
# CONFIG=other.env sudo -E ./install.sh
#
# The log goes automatically to logs/install.log (no need for "| tee").
# =============================================================================
set -Eeuo pipefail
source "$(dirname "$(readlink -f "$0")")/scripts/lib/common.sh"
trap 'err "failed at line ${LINENO}: ${BASH_COMMAND}"' ERR
DEPLOY_ONLY=false
DASHBOARD_ONLY=false
usage() { sed -n '2,13p' "$0" | sed 's/^# \{0,1\}//'; exit 0; }
for a in "$@"; do
case "$a" in
--deploy-only) DEPLOY_ONLY=true ;;
--dashboard-only) DASHBOARD_ONLY=true ;;
-h|--help) usage ;;
*) die "unknown option: $a" ;;
esac
done
# ----------------------------------------------------------------------------
# 1. Packages
# ----------------------------------------------------------------------------
step_packages() {
step "System packages"
local pkgs=(curl ca-certificates git iproute2 iptables kmod lksctp-tools tar gzip coreutils bash-completion) missing=()
local p
for p in "${pkgs[@]}"; do dpkg -s "$p" &>/dev/null || missing+=("$p"); done
if (( ${#missing[@]} )); then
info "installing: ${missing[*]}"
DEBIAN_FRONTEND=noninteractive apt-get update -qq
DEBIAN_FRONTEND=noninteractive apt-get install -y -qq "${missing[@]}" >/dev/null
fi
ok "dependencies present"
}
# ----------------------------------------------------------------------------
# 2. Kernel modules (SCTP for N2/NGAP, TUN for UPF/UE, bridge for the CNI)
# ----------------------------------------------------------------------------
step_modules() {
step "Kernel modules"
if ! modinfo sctp &>/dev/null; then
if is_true "${AUTO_INSTALL_MODULES_EXTRA}"; then
info "sctp module missing in kernel $(uname -r); installing linux-modules-extra"
DEBIAN_FRONTEND=noninteractive apt-get update -qq
DEBIAN_FRONTEND=noninteractive apt-get install -y -qq "linux-modules-extra-$(uname -r)" >/dev/null \
|| die "could not install linux-modules-extra-$(uname -r). Use a 'generic' kernel."
else
die "sctp module unavailable and AUTO_INSTALL_MODULES_EXTRA=false"
fi
fi
local mods=(sctp tun br_netfilter overlay) m
printf '%s\n' "# Gerado por open5GK3S/install.sh" "${mods[@]}" >"${MODULES_FILE}"
for m in "${mods[@]}"; do modprobe "$m" || die "failed to load module $m"; done
ok "loaded and persisted in ${MODULES_FILE}: ${mods[*]}"
if command -v checksctp &>/dev/null; then
checksctp &>/dev/null && ok "SCTP working (checksctp)" || warn "checksctp failed"
fi
}
# ----------------------------------------------------------------------------
# 3. sysctl
# ----------------------------------------------------------------------------
step_sysctl() {
step "sysctl parameters"
cat >"${SYSCTL_FILE}" <<'EOF'
# Generated by InnovaNetwork install.sh
# IPv4 forwarding (pods, UPF/N6 NAT)
net.ipv4.ip_forward = 1
# Bridge traffic going through iptables (kube-proxy / flannel)
net.bridge.bridge-nf-call-iptables = 1
net.bridge.bridge-nf-call-ip6tables = 1
# Many pods (one per NF) => more inotify
fs.inotify.max_user_instances = 1024
fs.inotify.max_user_watches = 524288
# Larger buffers for GTP-U/SCTP
net.core.rmem_max = 8388608
net.core.wmem_max = 8388608
EOF
sysctl -q -p "${SYSCTL_FILE}" >/dev/null
ok "applied ${SYSCTL_FILE}"
}
# ----------------------------------------------------------------------------
# 4. Environment-specific settings (desktop x server)
# ----------------------------------------------------------------------------
step_host_profile() {
step "Settings of the '${DETECTED_PROFILE}' profile"
if ${HAS_NM}; then
# Without it NetworkManager tries to manage cni0/flannel/veth and breaks the pod network
local content
local devs="interface-name:cni0;interface-name:flannel*;interface-name:veth*"
if [[ "${NODE_IP_MODE}" == stable ]]; then devs+=";interface-name:${STABLE_IFACE}"; fi
content=$'# Gerado por open5GK3S/install.sh\n[keyfile]\nunmanaged-devices='"${devs}"$'\n'
if [[ ! -f "${NM_CONF_FILE}" ]] || [[ "$(cat "${NM_CONF_FILE}")"$'\n' != "${content}" ]]; then
mkdir -p "$(dirname "${NM_CONF_FILE}")"
printf '%s' "${content}" >"${NM_CONF_FILE}"
systemctl reload NetworkManager || true
fi
ok "NetworkManager ignora cni0/flannel*/veth*$([[ "${NODE_IP_MODE}" == stable ]] && echo "/${STABLE_IFACE}" || true)"
else
info "NetworkManager inactive (typical of server/netplan+networkd) — nothing to do"
fi
if ${UFW_ACTIVE}; then
if is_true "${CONFIGURE_UFW}"; then
ufw allow from "${K3S_CLUSTER_CIDR}" comment 'open5gs-k3s pods' >/dev/null
ufw allow from "${K3S_SERVICE_CIDR}" comment 'open5gs-k3s services' >/dev/null
ufw route allow from "${K3S_CLUSTER_CIDR}" comment 'open5gs-k3s pods' >/dev/null
ufw route allow to "${K3S_CLUSTER_CIDR}" comment 'open5gs-k3s pods' >/dev/null
if is_true "${WEBUI_ENABLED}"; then ufw allow "${WEBUI_NODEPORT}/tcp" comment 'open5gs-k3s webui' >/dev/null; fi
if is_true "${DASHBOARD_ENABLED}"; then ufw allow "${DASHBOARD_NODEPORT}/tcp" comment 'open5gs-k3s dashboard' >/dev/null; fi
if is_true "${EXPOSE_RAN}"; then ufw allow 2152/udp comment 'open5gs-k3s N3' >/dev/null; fi
ok "UFW rules applied (cluster CIDRs, WebUI$(is_true "${EXPOSE_RAN}" && echo ', N3' || true))"
if is_true "${EXPOSE_RAN}"; then warn "UFW does not filter SCTP by port: make sure SCTP/38412 is not blocked"; fi
else
warn "UFW active and CONFIGURE_UFW=false: it may block pod traffic"
fi
fi
if ${HAS_FIREWALLD}; then
if is_true "${CONFIGURE_FIREWALLD}"; then
# k3s recommendation: pod and service networks in the trusted zone
local fw=(--zone=trusted --add-source="${K3S_CLUSTER_CIDR}" --add-source="${K3S_SERVICE_CIDR}")
firewall-cmd -q --permanent "${fw[@]}" 2>/dev/null || true
if is_true "${WEBUI_ENABLED}"; then firewall-cmd -q --permanent --add-port="${WEBUI_NODEPORT}/tcp" 2>/dev/null || true; fi
if is_true "${DASHBOARD_ENABLED}"; then firewall-cmd -q --permanent --add-port="${DASHBOARD_NODEPORT}/tcp" 2>/dev/null || true; fi
if is_true "${EXPOSE_RAN}"; then
firewall-cmd -q --permanent --add-port=38412/sctp --add-port=2152/udp 2>/dev/null || true
fi
firewall-cmd -q --reload
firewall-cmd -q --zone=trusted --query-source="${K3S_CLUSTER_CIDR}" \
|| die "firewalld: could not add ${K3S_CLUSTER_CIDR} to the trusted zone"
ok "firewalld: ${K3S_CLUSTER_CIDR} and ${K3S_SERVICE_CIDR} in the trusted zone$(is_true "${WEBUI_ENABLED}" && echo ", port ${WEBUI_NODEPORT}/tcp" || true)"
else
warn "firewalld active and CONFIGURE_FIREWALLD=false: direct traffic between pods (UE<->gNB, N3) will be rejected"
fi
fi
if ${HAS_DOCKER}; then
warn "Docker active: it sets the iptables FORWARD=DROP policy. k3s/flannel creates its own"
warn "rules for ${K3S_CLUSTER_CIDR}, but if pods lose the network see the 'Docker' section of the troubleshooting guide."
fi
if ! ${HAS_AVX} && [[ -z "${MONGODB_IMAGE_REPOSITORY}" ]]; then
warn "CPU without AVX: the default MongoDB image (8.x) does NOT run. Set MONGODB_IMAGE_* to a 4.4 version."
fi
if (( MEM_MB < 3500 )); then warn "low RAM (${MEM_MB} MiB). Recommended >= 4 GiB."; fi
if (( CPUS < 2 )); then warn "only ${CPUS} vCPU. Recommended >= 2."; fi
if (( $(swapon --show --noheadings 2>/dev/null | wc -l) > 0 )); then info "swap active (k3s tolerates it; nothing to do)"; fi
}
# ----------------------------------------------------------------------------
# 4b. Stable node interface (NODE_IP_MODE=stable)
# k3s stores the node-ip in its config; if the host IP changes (DHCP, Wi-Fi x cable,
# another network) k3s does not start. A dummy interface with a fixed private IP, created
# at every boot before k3s, makes the cluster independent of the physical network.
# The way out to the internet (N6) still follows the default route, whatever it is.
# ----------------------------------------------------------------------------
step_stable_netif() {
step "Stable node interface (NODE_IP_MODE=${NODE_IP_MODE})"
if [[ "${NODE_IP_MODE}" != stable ]]; then
if [[ -f "${NETIF_UNIT_FILE}" ]]; then
systemctl disable --now open5gs-k3s-netif.service >/dev/null 2>&1 || true
rm -f "${NETIF_UNIT_FILE}" "${K3S_DROPIN_FILE}"
systemctl daemon-reload
ok "stable interface removed; k3s uses ${K3S_NODE_IFACE} (${K3S_NODE_IP})"
else
info "k3s uses the physical interface ${K3S_NODE_IFACE} (${K3S_NODE_IP}) — sensitive to IP changes"
fi
return 0
fi
local unit dropin changed=false
unit=$(cat <<EOU
# Generated by InnovaNetwork install.sh
[Unit]
Description=InnovaNetwork: stable k3s node interface (${STABLE_IFACE} ${STABLE_IP}), independent of DHCP
Before=k3s.service
[Service]
Type=oneshot
RemainAfterExit=yes
ExecStart=/bin/sh -c 'ip link show ${STABLE_IFACE} >/dev/null 2>&1 || ip link add ${STABLE_IFACE} type dummy; ip addr replace ${STABLE_IP}/32 dev ${STABLE_IFACE}; ip link set ${STABLE_IFACE} up'
[Install]
WantedBy=multi-user.target
EOU
)
dropin=$'# Generated by InnovaNetwork install.sh
[Unit]
Requires=open5gs-k3s-netif.service
After=open5gs-k3s-netif.service'
if [[ "$(cat "${NETIF_UNIT_FILE}" 2>/dev/null)" != "${unit}" ]]; then
printf '%s
' "${unit}" >"${NETIF_UNIT_FILE}"; changed=true
fi
if [[ "$(cat "${K3S_DROPIN_FILE}" 2>/dev/null)" != "${dropin}" ]]; then
mkdir -p "$(dirname "${K3S_DROPIN_FILE}")"
printf '%s
' "${dropin}" >"${K3S_DROPIN_FILE}"; changed=true
fi
if ${changed}; then systemctl daemon-reload; fi
systemctl enable open5gs-k3s-netif.service >/dev/null 2>&1
# 'restart' re-applies the IP/name if they changed in the config
if ${changed}; then systemctl restart open5gs-k3s-netif.service; else systemctl start open5gs-k3s-netif.service; fi
ip -4 -o addr show dev "${STABLE_IFACE}" 2>/dev/null | grep -q " ${STABLE_IP}/" \
|| die "could not create ${STABLE_IFACE} with ${STABLE_IP}"
ok "${STABLE_IFACE} = ${STABLE_IP} (recreated at every boot before k3s)"
if is_true "${EXPOSE_RAN}"; then
warn "EXPOSE_RAN=true: the external gNB uses the physical IP ${NODE_IP}; if it changes, run 'sudo ./install.sh --deploy-only'"
fi
}
# ----------------------------------------------------------------------------
# 5. k3s
# ----------------------------------------------------------------------------
step_k3s() {
step "k3s ${K3S_VERSION}"
local cfg restart=false
cfg=$(
echo "# Generated by InnovaNetwork install.sh from config.env"
echo "write-kubeconfig-mode: \"0600\" # root only; the user gets ~/.kube/open5gs-k3s.yaml"
echo "node-ip: \"${K3S_NODE_IP}\" # NODE_IP_MODE=${NODE_IP_MODE}"
echo "flannel-iface: \"${K3S_NODE_IFACE}\""
echo "cluster-cidr: \"${K3S_CLUSTER_CIDR}\""
echo "service-cidr: \"${K3S_SERVICE_CIDR}\""
echo "disable:"
echo " - traefik"
echo "# ~15 images downloaded at the same time: the default kubelet limit (5 QPS) causes 'pull QPS exceeded'"
echo "kubelet-arg:"
echo " - \"registry-qps=20\""
echo " - \"registry-burst=40\""
)
mkdir -p "$(dirname "${K3S_CONFIG_FILE}")"
if [[ -f "${K3S_CONFIG_FILE}" && "$(cat "${K3S_CONFIG_FILE}")" != "${cfg}" ]] \
&& [[ "$(strip_comments <"${K3S_CONFIG_FILE}")" == "$(strip_comments <<<"${cfg}")" ]]; then
printf '%s\n' "${cfg}" >"${K3S_CONFIG_FILE}" # only comments changed: no k3s restart
fi
if [[ ! -f "${K3S_CONFIG_FILE}" ]] || [[ "$(cat "${K3S_CONFIG_FILE}")" != "${cfg}" ]]; then
if [[ -f "${K3S_CONFIG_FILE}" ]] && grep -q "cluster-cidr" "${K3S_CONFIG_FILE}" \
&& ! grep -q "\"${K3S_CLUSTER_CIDR}\"" "${K3S_CONFIG_FILE}"; then
warn "K3S_CLUSTER_CIDR changed on an existing cluster: that requires './uninstall.sh --k3s' first."
fi
printf '%s\n' "${cfg}" >"${K3S_CONFIG_FILE}"
restart=true
fi
local installed="" sig_file="${INSTALL_DIR}/state/k3s.sig" sig
sig="${K3S_VERSION} ${K3S_EXTRA_ARGS[*]}"
if command -v k3s &>/dev/null; then installed=$(k3s --version 2>/dev/null | awk 'NR==1{print $3}'); fi
if [[ "${installed}" != "${K3S_VERSION}" ]] || [[ "$(cat "${sig_file}" 2>/dev/null)" != "${sig}" ]]; then
info "installing k3s ${K3S_VERSION} (current: ${installed:-none})"
curl -fsSL --retry 5 --retry-delay 3 --retry-all-errors --connect-timeout 15 https://get.k3s.io -o "${INSTALL_DIR}/state/get-k3s.sh"
INSTALL_K3S_VERSION="${K3S_VERSION}" INSTALL_K3S_EXEC="server ${K3S_EXTRA_ARGS[*]}" \
retry sh "${INSTALL_DIR}/state/get-k3s.sh" >/dev/null
echo "${sig}" >"${sig_file}"
restart=false
elif ${restart}; then
info "k3s config changed; restarting"
systemctl restart k3s
fi
systemctl enable --now k3s >/dev/null 2>&1 || true
info "waiting for the Kubernetes API..."
local t=0
until k3s_ready; do
(( t += 3 )); (( t < 300 )) || die "k3s API did not answer within 300s (journalctl -u k3s)"
sleep 3
done
# k3s applies the system manifests asynchronously: wait for them to exist before the rollout
local d
for d in coredns local-path-provisioner; do
t=0
until kc -n kube-system get "deploy/${d}" &>/dev/null; do
(( t += 3 )); (( t < 300 )) || die "deploy/${d} was not created by k3s within 300s"
sleep 3
done
done
kc wait --for=condition=Ready node --all --timeout=300s >/dev/null
kc -n kube-system rollout status deploy/coredns --timeout=300s >/dev/null
kc -n kube-system rollout status deploy/local-path-provisioner --timeout=300s >/dev/null
ok "k3s $(k3s --version | awk 'NR==1{print $3}') ready; node $(kc get nodes -o jsonpath='{.items[0].metadata.name}') Ready"
if [[ -x /usr/local/bin/kubectl && ! -L /usr/local/bin/kubectl ]]; then
info "a separate kubectl already exists in /usr/local/bin (not replaced); use 'sudo k3s kubectl' or KUBECONFIG=${KUBECONFIG_PATH}"
fi
}
# ----------------------------------------------------------------------------
# 6. Helm
# ----------------------------------------------------------------------------
step_helm() {
step "Helm"
find_helm
local cur=""
[[ -n "${HELM_BIN}" ]] && cur=$("${HELM_BIN}" version --template '{{.Version}}' 2>/dev/null || true)
# Installs the pinned helm if: there is no helm; ours is another version; or the
# system one is not v3 (Helm 4 does not accept a post-renderer as an executable)
if [[ -z "${HELM_BIN}" ]] || \
[[ "${HELM_BIN}" == "${INSTALL_DIR}/bin/helm" && "${cur}" != "${HELM_VERSION}" ]] || \
[[ "${cur}" != v3.* ]]; then
info "installing helm ${HELM_VERSION} in ${INSTALL_DIR}/bin (link in /usr/local/bin/helm)"
local tmp arch; tmp=$(mktemp -d)
case "$(uname -m)" in
x86_64) arch=amd64 ;; aarch64|arm64) arch=arm64 ;;
*) die "architecture $(uname -m) not supported by the helm download" ;;
esac
curl -fsSL --retry 5 --retry-delay 3 --retry-all-errors --connect-timeout 15 "https://get.helm.sh/helm-${HELM_VERSION}-linux-${arch}.tar.gz" -o "${tmp}/helm.tgz"
curl -fsSL --retry 5 --retry-delay 3 --retry-all-errors --connect-timeout 15 "https://get.helm.sh/helm-${HELM_VERSION}-linux-${arch}.tar.gz.sha256sum" -o "${tmp}/helm.sha"
(cd "${tmp}" && echo "$(awk '{print $1}' helm.sha) helm.tgz" | sha256sum -c --quiet) || die "invalid helm checksum"
tar xzf "${tmp}/helm.tgz" -C "${tmp}"
install -D -m 0755 "${tmp}/linux-${arch}/helm" "${INSTALL_DIR}/bin/helm"
rm -rf "${tmp}"
[[ -e /usr/local/bin/helm ]] || ln -s "${INSTALL_DIR}/bin/helm" /usr/local/bin/helm
HELM_BIN="${INSTALL_DIR}/bin/helm"
fi
ok "helm $("${HELM_BIN}" version --template '{{.Version}}') (${HELM_BIN})"
}
# ----------------------------------------------------------------------------
# 7. Gradiant charts (git, pinned ref) + dependencies
# ----------------------------------------------------------------------------
_clone_charts() {
rm -rf "${CHARTS_DIR}" # an interrupted clone leaves a half-done folder
git clone -q --filter=blob:none "${GRADIANT_CHARTS_REPO}" "${CHARTS_DIR}"
}
_fetch_charts_ref() {
git -C "${CHARTS_DIR}" fetch -q origin "${GRADIANT_CHARTS_REF}" || git -C "${CHARTS_DIR}" fetch -q --tags origin
}
step_charts() {
step "Charts Gradiant @ ${GRADIANT_CHARTS_REF:0:12}"
if [[ ! -d "${CHARTS_DIR}/.git" ]]; then
retry _clone_charts
fi
if ! git -C "${CHARTS_DIR}" cat-file -e "${GRADIANT_CHARTS_REF}^{commit}" 2>/dev/null; then
retry _fetch_charts_ref
fi
local head; head=$(git -C "${CHARTS_DIR}" rev-parse HEAD)
if [[ "${head}" != "$(git -C "${CHARTS_DIR}" rev-parse "${GRADIANT_CHARTS_REF}^{commit}")" ]]; then
git -C "${CHARTS_DIR}" checkout -q --force "${GRADIANT_CHARTS_REF}"
rm -f "${CHARTS_DIR}"/charts/*/.deps-ok
fi
retry "${HELM_BIN}" repo add bitnami https://charts.bitnami.com/bitnami --force-update >/dev/null
local c
for c in open5gs ueransim-gnb; do
if [[ ! -f "${CHARTS_DIR}/charts/${c}/.deps-ok" ]]; then
info "helm dependency build ${c}"
retry "${HELM_BIN}" dependency build "${CHARTS_DIR}/charts/${c}" >/dev/null
touch "${CHARTS_DIR}/charts/${c}/.deps-ok"
fi
done
ok "open5gs $(awk '/^version:/{print $2}' "${CHARTS_DIR}/charts/open5gs/Chart.yaml") (app $(awk -F'"' '/^appVersion:/{print $2}' "${CHARTS_DIR}/charts/open5gs/Chart.yaml")), ueransim-gnb $(awk '/^version:/{print $2}' "${CHARTS_DIR}/charts/ueransim-gnb/Chart.yaml")"
}
# ----------------------------------------------------------------------------
# 8. Deploy
# ----------------------------------------------------------------------------
# ----------------------------------------------------------------------------
# 7b. Link to the real gNB (RAN_LINK_ENABLED): Multus + ran-link network (macvlan)
# ----------------------------------------------------------------------------
step_ran_link() {
is_true "${RAN_LINK_ENABLED}" || return 0
step "Link to the gNB (Multus + macvlan on ${RAN_LINK_IFACE})"
ip link show "${RAN_LINK_IFACE}" &>/dev/null || die "RAN_LINK_IFACE '${RAN_LINK_IFACE}' does not exist on this host"
local mtu; mtu=$(cat "/sys/class/net/${RAN_LINK_IFACE}/mtu")
(( mtu >= RAN_LINK_MTU )) || warn "${RAN_LINK_IFACE} has MTU ${mtu} < RAN_LINK_MTU ${RAN_LINK_MTU}"
grep -q LOWER_UP < <(ip link show "${RAN_LINK_IFACE}") \
|| warn "${RAN_LINK_IFACE} has no physical link: AMF/UPF/simulated gNB cannot talk until the cable has signal"
# The rke2-multus chart does not ship the NetworkAttachmentDefinition CRD
kc apply -f "${PROJECT_DIR}/helm/multus/crd.yaml" >/dev/null
retry hm upgrade --install multus rke2-multus --repo https://rke2-charts.rancher.io \
--version "${MULTUS_CHART_VERSION}" --namespace kube-system \
-f "${PROJECT_DIR}/helm/multus/values.yaml" --wait --timeout 5m >/dev/null
kc -n kube-system rollout status ds/multus --timeout=180s >/dev/null
ok "Multus ${MULTUS_CHART_VERSION} ready"
kc create namespace "${K8S_NAMESPACE}" --dry-run=client -o yaml | kc apply -f - >/dev/null
render_ran_link_nad
kc apply -f "${RAN_LINK_NAD}" >/dev/null
ok "ran-link network: AMF ${AMF_N2_IP}, UPF ${UPF_N3_IP}, simulated gNB ${GNB_SIM_IP} (${RAN_LINK_CIDR}, MTU ${RAN_LINK_MTU})"
}
step_deploy_core() {
step "Open5GS (release ${OPEN5GS_RELEASE}, namespace ${K8S_NAMESPACE})"
render_open5gs_values
info "generated values: ${OPEN5GS_VALUES} (hash ${OPEN5GS_HASH})"
info "helm upgrade --install (may take several minutes the 1st time: image downloads)"
# clusterIP is immutable: the old PFCP Service (ClusterIP) must go to become headless
local cip
cip=$(kc -n "${K8S_NAMESPACE}" get svc "${OPEN5GS_RELEASE}-upf-pfcp" -o jsonpath='{.spec.clusterIP}' 2>/dev/null || true)
if [[ -n "${cip}" && "${cip}" != None ]]; then
kc -n "${K8S_NAMESPACE}" delete svc "${OPEN5GS_RELEASE}-upf-pfcp" >/dev/null
info "Service ${OPEN5GS_RELEASE}-upf-pfcp (ClusterIP ${cip}) removed: it will be recreated headless"
fi
local pr=()
if is_true "${RAN_LINK_ENABLED}"; then pr=(--post-renderer "${PROJECT_DIR}/helm/postrender/amf-ngap-net1.sh"); fi
hm upgrade --install "${OPEN5GS_RELEASE}" "${CHARTS_DIR}/charts/open5gs" \
--namespace "${K8S_NAMESPACE}" --create-namespace \
-f "${CHARTS_DIR}/charts/open5gs/values-5g.yaml" \
-f "${OPEN5GS_VALUES}" \
"${pr[@]}" \
--wait --timeout "${HELM_TIMEOUT}" >/dev/null \
|| { kc -n "${K8S_NAMESPACE}" get pods; die "Open5GS deploy failed (see the pods above)"; }
ok "Open5GS deployed"
}
step_subscribers() {
step "Subscribers (MongoDB)"
# works with the core running, partly stopped or stopped from the Control tab
if ! mongodb_ready; then
warn "MongoDB is stopped (core stopped from the Control tab?): subscribers not synced now."
warn " Start the core (Control tab) and run --deploy-only again to sync them."
return 0
fi
sync_subscribers
}
step_deploy_ueransim() {
if ! is_true "${DEPLOY_UERANSIM}"; then
step "UERANSIM disabled (DEPLOY_UERANSIM=false)"
if hm -n "${K8S_NAMESPACE}" status "${UERANSIM_RELEASE}" &>/dev/null; then
hm -n "${K8S_NAMESPACE}" uninstall "${UERANSIM_RELEASE}" >/dev/null
ok "release ${UERANSIM_RELEASE} removed"
fi
return 0
fi
step "UERANSIM gNB + ${UE_COUNT} UE(s) (release ${UERANSIM_RELEASE})"
render_ueransim_values
info "generated values: ${UERANSIM_VALUES} (hash ${UERANSIM_HASH})"
hm upgrade --install "${UERANSIM_RELEASE}" "${CHARTS_DIR}/charts/ueransim-gnb" \
--namespace "${K8S_NAMESPACE}" \
-f "${UERANSIM_VALUES}" \
--post-renderer "${PROJECT_DIR}/helm/postrender/drop-gnb-hpa.sh" \
--wait --timeout "${HELM_TIMEOUT}" >/dev/null \
|| { kc -n "${K8S_NAMESPACE}" get pods; die "UERANSIM deploy failed"; }
ok "UERANSIM deployed"
ensure_user_plane || warn "the user plane is not OK — run ./test.sh -v"
}
# ----------------------------------------------------------------------------
# 8a. Dashboard (Innova Center Dashboard) — docs/components/dashboard.md
# Read-only pod in the core namespace + (optional) gNB log agent, which
# runs on THIS machine as the install user and reads the log over SSH (tail -F).
# ----------------------------------------------------------------------------
dashboard_firewall() {
is_true "${DASHBOARD_ENABLED}" || return 0
if ${UFW_ACTIVE} && is_true "${CONFIGURE_UFW}"; then
ufw allow "${DASHBOARD_NODEPORT}/tcp" comment 'open5gs-k3s dashboard' >/dev/null
fi
if ${HAS_FIREWALLD} && is_true "${CONFIGURE_FIREWALLD}"; then
firewall-cmd -q --permanent --add-port="${DASHBOARD_NODEPORT}/tcp" 2>/dev/null && firewall-cmd -q --reload || true
fi
}
# Settings tab: administrator password (generated once, only root reads it) and permissions
dashboard_config_edit() {
DASHBOARD_ADMIN_PASSWORD="" DASHBOARD_CONFIG_GID=""
is_true "${DASHBOARD_CONFIG_EDIT}" || return 0
local cfg="${PROJECT_DIR}/config.env" pwf="${INSTALL_DIR}/state/dashboard-admin-password" gid
if [[ "$(readlink -f "${CONFIG}")" != "$(readlink -f "${cfg}")" ]]; then
warn "Settings tab off: it only edits ${cfg} (CONFIG points to ${CONFIG})"; return 0
fi
if [[ ! -s "${pwf}" ]]; then
install -m 0600 -o root -g root /dev/null "${pwf}"
head -c 32 /dev/urandom | base64 | tr -dc 'A-Za-z0-9' | cut -c1-20 >"${pwf}"
fi
DASHBOARD_ADMIN_PASSWORD=$(<"${pwf}")
gid=$(stat -c %g "${cfg}")
if (( gid == 0 )); then warn "Settings tab off: ${cfg} belongs to the root group"; DASHBOARD_ADMIN_PASSWORD=""; return 0; fi
# The pod (user 65534) writes through the group of the file owner: needs g+w
[[ $(( 0$(stat -c %a "${cfg}") & 020 )) -ne 0 ]] || { chmod g+w "${cfg}"; info "config.env: group write permission (Settings tab)"; }
install -d -m 2770 -o root -g "${gid}" "${INSTALL_DIR}/state/config-backups"
DASHBOARD_CONFIG_GID="${gid}"
}
step_dashboard() {
if ! is_true "${DASHBOARD_ENABLED}"; then
if kc -n "${K8S_NAMESPACE}" get deploy dashboard &>/dev/null; then
kc -n "${K8S_NAMESPACE}" delete deploy,svc,configmap/dashboard-app,secret,sa dashboard --ignore-not-found >/dev/null 2>&1 || true
kc -n "${K8S_NAMESPACE}" delete role,rolebinding dashboard-read --ignore-not-found >/dev/null 2>&1 || true
ok "dashboard removed (DASHBOARD_ENABLED=false)"
fi
remove_gnb_log_agent
remove_hosts_agent
remove_ru_agent
remove_gnb_ctl_agent
remove_term_agent
return 0
fi
step "Innova Center Dashboard (NodePort ${DASHBOARD_NODEPORT})"
# gNB log agent token: generated once and kept for root only
local tokf="${INSTALL_DIR}/state/dashboard-ingest-token"
if [[ ! -s "${tokf}" ]]; then
install -m 0600 -o root -g root /dev/null "${tokf}"
head -c 32 /dev/urandom | sha256sum | cut -c1-48 >"${tokf}"
fi
DASHBOARD_INGEST_TOKEN=$(<"${tokf}")
dashboard_config_edit
# Persistent history: pod folder (user 65534); stays on the host even with the dashboard off
if is_true "${DASHBOARD_PERSIST}"; then
install -d -m 0750 -o 65534 -g 65534 "${DASHBOARD_DATA_DIR}"
info "persistent history in ${DASHBOARD_DATA_DIR} (retention ${DASHBOARD_PERSIST_DAYS} days, max. ${DASHBOARD_PERSIST_MAX_GB} GB)"
fi
kc create namespace "${K8S_NAMESPACE}" --dry-run=client -o yaml | kc apply -f - >/dev/null
render_dashboard
# server-side: the dashboard files exceed 256 KB (limit of the last-applied annotation of a plain apply)
kc apply --server-side --force-conflicts --field-manager=open5gk3s -f "${DASHBOARD_MANIFEST}" >/dev/null
kc -n "${K8S_NAMESPACE}" rollout status deploy/dashboard --timeout=300s >/dev/null \
|| { kc -n "${K8S_NAMESPACE}" get pods -l app.kubernetes.io/name=dashboard; die "the dashboard did not become ready"; }
dashboard_firewall
ok "dashboard up: http://${NODE_IP:-localhost}:${DASHBOARD_NODEPORT}"
if [[ -n "${GNB_METRICS_URL}" ]]; then info "gNB metrics: ${GNB_METRICS_URL}"; fi
setup_gnb_log_agent
setup_hosts_agent
setup_ru_agent
setup_gnb_ctl_agent
setup_term_agent
}
remove_term_agent() {
[[ -f "${TERM_UNIT_FILE}" ]] || return 0
systemctl disable --now open5gs-k3s-term.service >/dev/null 2>&1 || true
rm -f "${TERM_UNIT_FILE}" "${INSTALL_DIR}/dashboard-agent/term.env"; systemctl daemon-reload
ok "unit open5gs-k3s-term removed"
}
# Terminal tab (administrator): term-agent.py opens the shells here, as the install user, and connects
# them to the dashboard (outgoing connections only). Profiles: this machine, the gNB host (GNB_CTL_SSH)
# and the DASHBOARD_HOSTS machines, over the user's SSH keys.
setup_term_agent() {
if ! is_true "${DASHBOARD_TERMINAL}" || [[ -z "${DASHBOARD_ADMIN_PASSWORD}" ]]; then remove_term_agent; return 0; fi
[[ "${TARGET_USER}" != root ]] || { warn "the Terminal tab needs a regular install user"; return 0; }
local dst="${INSTALL_DIR}/dashboard-agent" unit profiles e hn uh nnp="NoNewPrivileges=true"
profiles="$(hostname -s)|bash -l"
if [[ -n "${GNB_CTL_SSH}" ]]; then profiles+=";${GNB_CTL_SSH%%@*}|ssh -t ${GNB_CTL_SSH}"; fi
for e in "${DASHBOARD_HOSTS[@]}"; do
IFS='|' read -r hn uh _ <<<"$e"
[[ ";${profiles}" == *";${hn}|"* ]] || profiles+=";${hn}|ssh -t ${uh}"
done
install -d -m 0755 -o root -g root "${dst}"
install -m 0755 -o root -g root "${PROJECT_DIR}/dashboard/agent/term-agent.py" "${dst}/term-agent.py"
install -m 0640 -o root -g "${TARGET_GROUP}" /dev/null "${dst}/term.env"
{
echo "DASHBOARD_URL=http://${K3S_NODE_IP:-127.0.0.1}:${DASHBOARD_NODEPORT}"
echo "INGEST_TOKEN=${DASHBOARD_INGEST_TOKEN}"
echo "TERM_PROFILES=${profiles}"
echo "KUBECONFIG=${TARGET_HOME}/.kube/config"
} >"${dst}/term.env"
# sudo inside the browser terminals only with DASHBOARD_TERMINAL_SUDO=true
if is_true "${DASHBOARD_TERMINAL_SUDO}"; then nnp="NoNewPrivileges=false"; fi
unit=$(cat <<EOU
# Gerado por open5GK3S/install.sh
[Unit]
Description=InnovaNetwork: shells for the dashboard Terminal tab (administrator)
After=network-online.target k3s.service
Wants=network-online.target
[Service]
Type=simple
User=${TARGET_USER}
WorkingDirectory=${TARGET_HOME}
EnvironmentFile=${dst}/term.env
ExecStart=/usr/bin/python3 -u ${dst}/term-agent.py
Restart=always
RestartSec=5
${nnp}
[Install]
WantedBy=multi-user.target
EOU
)
if [[ "$(cat "${TERM_UNIT_FILE}" 2>/dev/null)" != "${unit}" ]]; then
printf '%s\n' "${unit}" >"${TERM_UNIT_FILE}"; systemctl daemon-reload
fi
systemctl enable open5gs-k3s-term.service >/dev/null 2>&1
systemctl restart open5gs-k3s-term.service
ok "Terminal tab: shells as ${TARGET_USER} (journalctl -u open5gs-k3s-term)"
if is_true "${DASHBOARD_TERMINAL_SUDO}"; then warn "Terminal tab: sudo allowed in the browser terminals (DASHBOARD_TERMINAL_SUDO=true)"; fi
return 0
}
remove_gnb_ctl_agent() {
[[ -f "${GNBCTL_UNIT_FILE}" ]] || return 0
systemctl disable --now open5gs-k3s-gnb-ctl.service >/dev/null 2>&1 || true
rm -f "${GNBCTL_UNIT_FILE}" "${INSTALL_DIR}/dashboard-agent/gnb-ctl.env"; systemctl daemon-reload
ok "unit open5gs-k3s-gnb-ctl removed"
}
# OCUDU gNB from the Control tab: the agent reads the gNB state (k8s pod or the ocudu-gnb@ service on
# the gNB host) and runs start/stop (gnb.sh, or systemctl authorized by polkit there). Same SSH key as the log.
setup_gnb_ctl_agent() {
if ! is_true "${DASHBOARD_CONTROL}" || [[ -z "${GNB_CTL_SSH}" ]]; then remove_gnb_ctl_agent; return 0; fi
[[ "${TARGET_USER}" != root ]] || { warn "the gNB control agent needs a regular user"; return 0; }
local dst="${INSTALL_DIR}/dashboard-agent" unit
install -d -m 0755 -o root -g root "${dst}"
install -m 0755 -o root -g root "${PROJECT_DIR}/dashboard/agent/gnb-ctl-agent.py" "${dst}/gnb-ctl-agent.py"
install -m 0640 -o root -g "${TARGET_GROUP}" /dev/null "${dst}/gnb-ctl.env"
{
echo "DASHBOARD_URL=http://${K3S_NODE_IP:-127.0.0.1}:${DASHBOARD_NODEPORT}" # fixed node IP: does not change with DHCP
echo "INGEST_TOKEN=${DASHBOARD_INGEST_TOKEN}"
echo "GNB_CTL_SSH=${GNB_CTL_SSH}"
echo "GNB_CTL_DIR=${GNB_CTL_DIR}"
echo "GNB_CTL_MODE=${GNB_CTL_MODE}"
if [[ "${GNB_CTL_MODE}" == k8s ]]; then
echo "GNB_CTL_GNBSH=${PROJECT_DIR}/ran/ocudu/gnb.sh"
echo "KUBECONFIG=${TARGET_HOME}/.kube/config"
fi
} >"${dst}/gnb-ctl.env"
unit=$(cat <<EOU
# Gerado por open5GK3S/install.sh
[Unit]
Description=InnovaNetwork: start/stop the OCUDU gNB (${GNB_CTL_SSH}) from the dashboard
After=network-online.target k3s.service
Wants=network-online.target
[Service]
Type=simple
User=${TARGET_USER}
EnvironmentFile=${dst}/gnb-ctl.env
ExecStart=/usr/bin/python3 -u ${dst}/gnb-ctl-agent.py
Restart=always
RestartSec=10
NoNewPrivileges=true
[Install]
WantedBy=multi-user.target
EOU
)
if [[ "$(cat "${GNBCTL_UNIT_FILE}" 2>/dev/null)" != "${unit}" ]]; then
printf '%s\n' "${unit}" >"${GNBCTL_UNIT_FILE}"; systemctl daemon-reload
fi
systemctl enable open5gs-k3s-gnb-ctl.service >/dev/null 2>&1
systemctl restart open5gs-k3s-gnb-ctl.service
if [[ "${GNB_CTL_MODE}" == k8s ]]; then
if su - "${TARGET_USER}" -s /bin/bash -c "KUBECONFIG=${TARGET_HOME}/.kube/config kubectl -n ocudu get deploy ocudu-gnb" &>/dev/null; then
ok "Control tab: OCUDU gNB on Kubernetes (pod ocudu-gnb; journalctl -u open5gs-k3s-gnb-ctl)"
else
warn "Control tab: the ocudu-gnb Deployment does not exist: install OCUDU (docs/installation.md, ran/ocudu/install.sh)"
fi
elif su - "${TARGET_USER}" -s /bin/bash -c "ssh -o BatchMode=yes -o ConnectTimeout=5 ${GNB_CTL_SSH} test -f /etc/systemd/system/ocudu-gnb@.service" &>/dev/null; then
ok "Control tab: OCUDU gNB controllable (${GNB_CTL_SSH}; journalctl -u open5gs-k3s-gnb-ctl)"
else
warn "Control tab: the gNB service is not installed on the gNB host (once, with sudo there):"
warn " scp scripts/ocudu/setup-gnb-service.sh ${GNB_CTL_SSH}:/tmp/ && ssh -t ${GNB_CTL_SSH} sudo bash /tmp/setup-gnb-service.sh --dir ${GNB_CTL_DIR}"
fi
}
remove_ru_agent() {
[[ -f "${RU_UNIT_FILE}" ]] || return 0
systemctl disable --now open5gs-k3s-ru.service >/dev/null 2>&1 || true
rm -f "${RU_UNIT_FILE}" "${INSTALL_DIR}/dashboard-agent/ru.env"; systemctl daemon-reload
ok "unit open5gs-k3s-ru removed"
}
# RU (RU tab): password SSH session on the management port, "show" commands only.
# The password lives in a root file (user group) created by hand — never in config.env/git.
setup_ru_agent() {
if [[ -z "${RU_MGMT_IP}" ]]; then remove_ru_agent; return 0; fi
[[ "${TARGET_USER}" != root ]] || { warn "the RU agent needs a regular user"; return 0; }
local dst="${INSTALL_DIR}/dashboard-agent" cred="${INSTALL_DIR}/state/ru-credentials" unit pre=""
local ifc="${RU_MGMT_IFACE:-${NODE_IFACE}}"
install -d -m 0755 -o root -g root "${dst}"
install -m 0755 -o root -g root "${PROJECT_DIR}/dashboard/agent/ru-agent.py" "${dst}/ru-agent.py"
install -m 0640 -o root -g "${TARGET_GROUP}" /dev/null "${dst}/ru.env"
{
echo "DASHBOARD_URL=http://${K3S_NODE_IP:-127.0.0.1}:${DASHBOARD_NODEPORT}" # fixed node IP: does not change with DHCP
echo "INGEST_TOKEN=${DASHBOARD_INGEST_TOKEN}"
echo "RU_TARGET=${RU_SSH_USER}@${RU_MGMT_IP}"
echo "RU_POLL_SECONDS=${RU_POLL_SECONDS}"
} >"${dst}/ru.env"
# extra IP of this machine on the RU management network, redone at every agent start (the "+" runs as root)
if [[ -n "${RU_MGMT_LOCAL_CIDR}" && -n "${ifc}" ]]; then
pre="ExecStartPre=+/usr/sbin/ip addr replace ${RU_MGMT_LOCAL_CIDR} dev ${ifc}"
fi
unit=$(cat <<EOU
# Gerado por open5GK3S/install.sh
[Unit]
Description=InnovaNetwork: RU state (${RU_SSH_USER}@${RU_MGMT_IP}, read only) for the dashboard
After=network-online.target k3s.service
Wants=network-online.target
ConditionPathExists=${cred}
[Service]
Type=simple
User=${TARGET_USER}
EnvironmentFile=${dst}/ru.env
EnvironmentFile=${cred}
${pre}
ExecStart=/usr/bin/python3 -u ${dst}/ru-agent.py
Restart=always
RestartSec=15
[Install]
WantedBy=multi-user.target
EOU
)
if [[ "$(cat "${RU_UNIT_FILE}" 2>/dev/null)" != "${unit}" ]]; then
printf '%s\n' "${unit}" >"${RU_UNIT_FILE}"; systemctl daemon-reload
fi
systemctl enable open5gs-k3s-ru.service >/dev/null 2>&1
if [[ ! -s "${cred}" ]]; then
warn "RU tab: the RU password file is missing. Create it with (use the real passwords):"
warn " printf 'RU_PASS=%s\\nRU_ENABLE=%s\\n' 'SSH_PASSWORD' 'ENABLE_PASSWORD' | sudo install -m 0640 -g ${TARGET_GROUP} /dev/stdin ${cred}"
warn " and then: sudo systemctl restart open5gs-k3s-ru"
return 0
fi
chown root:"${TARGET_GROUP}" "${cred}"; chmod 0640 "${cred}"
systemctl restart open5gs-k3s-ru.service
ok "RU tab: agent active (${RU_SSH_USER}@${RU_MGMT_IP} via ${ifc}${RU_MGMT_LOCAL_CIDR:+, local IP ${RU_MGMT_LOCAL_CIDR}}; journalctl -u open5gs-k3s-ru)"
}
remove_hosts_agent() {
[[ -f "${HOSTS_UNIT_FILE}" ]] || return 0
systemctl disable --now open5gs-k3s-hosts.service >/dev/null 2>&1 || true
rm -f "${HOSTS_UNIT_FILE}"; systemctl daemon-reload
ok "unit open5gs-k3s-hosts removed"
}
# Other machines (Machines tab): one SSH session per machine, from this one.
setup_hosts_agent() {
if (( ${#DASHBOARD_HOSTS[@]} == 0 )); then remove_hosts_agent; return 0; fi
[[ "${TARGET_USER}" != root ]] || { warn "the machines agent needs a regular user"; return 0; }
local dst="${INSTALL_DIR}/dashboard-agent" unit e hn tg rl hosts="" bad=()
install -d -m 0755 -o root -g root "${dst}"
install -m 0755 -o root -g root "${PROJECT_DIR}/dashboard/agent/host-agent.py" "${dst}/host-agent.py"
install -m 0644 -o root -g root "${PROJECT_DIR}/dashboard/app/hoststats.py" "${dst}/hoststats.py"
for e in "${DASHBOARD_HOSTS[@]}"; do hosts+="${hosts:+;}${e}"; done
install -m 0640 -o root -g "${TARGET_GROUP}" /dev/null "${dst}/hosts.env"
{
echo "DASHBOARD_URL=http://${K3S_NODE_IP:-127.0.0.1}:${DASHBOARD_NODEPORT}" # fixed node IP: does not change with DHCP
echo "INGEST_TOKEN=${DASHBOARD_INGEST_TOKEN}"
echo "HOSTS=${hosts}"
} >"${dst}/hosts.env"
unit=$(cat <<EOU
# Gerado por open5GK3S/install.sh
[Unit]
Description=InnovaNetwork: CPU/memory/temperature of the other machines for the dashboard
After=network-online.target k3s.service
Wants=network-online.target
[Service]
Type=simple
User=${TARGET_USER}
EnvironmentFile=${dst}/hosts.env
ExecStart=/usr/bin/python3 -u ${dst}/host-agent.py ${dst}/hoststats.py
Restart=always
RestartSec=10
NoNewPrivileges=true
[Install]
WantedBy=multi-user.target
EOU
)
if [[ "$(cat "${HOSTS_UNIT_FILE}" 2>/dev/null)" != "${unit}" ]]; then
printf '%s\n' "${unit}" >"${HOSTS_UNIT_FILE}"; systemctl daemon-reload
fi
systemctl enable open5gs-k3s-hosts.service >/dev/null 2>&1
systemctl restart open5gs-k3s-hosts.service
for e in "${DASHBOARD_HOSTS[@]}"; do
IFS='|' read -r hn tg rl <<<"$e"
su - "${TARGET_USER}" -s /bin/bash -c "ssh -o BatchMode=yes -o ConnectTimeout=5 ${tg} true" &>/dev/null || bad+=("${hn} (${tg})")
done
if (( ${#bad[@]} )); then
warn "Machines tab: ${TARGET_USER} has no passwordless SSH access yet: ${bad[*]} (see docs/components/dashboard.md)"
else
ok "Machines tab: ${#DASHBOARD_HOSTS[@]} remote machine(s) + this one (journalctl -u open5gs-k3s-hosts)"
fi
}
remove_gnb_log_agent() {
[[ -f "${GNBLOG_UNIT_FILE}" ]] || return 0
systemctl disable --now open5gs-k3s-gnb-log.service >/dev/null 2>&1 || true
rm -f "${GNBLOG_UNIT_FILE}"; rm -rf "${INSTALL_DIR}/dashboard-agent"; systemctl daemon-reload
ok "unit open5gs-k3s-gnb-log removed"
}
setup_gnb_log_agent() {
if [[ -z "${GNB_LOG_SSH}" ]]; then
remove_gnb_log_agent
info "real gNB log off (GNB_LOG_SSH empty)"
return 0
fi
[[ "${TARGET_USER}" != root ]] || { warn "the gNB log agent needs a regular user (KUBECONFIG_USER/SUDO_USER)"; return 0; }
local dst="${INSTALL_DIR}/dashboard-agent" unit
install -d -m 0755 -o root -g root "${dst}"
install -m 0755 -o root -g root "${PROJECT_DIR}/dashboard/agent/gnb-log-agent.py" "${dst}/gnb-log-agent.py"
# Token readable only by root and by the group of the user running the agent
install -m 0640 -o root -g "${TARGET_GROUP}" /dev/null "${dst}/agent.env"
{
echo "DASHBOARD_URL=http://${K3S_NODE_IP:-127.0.0.1}:${DASHBOARD_NODEPORT}" # fixed node IP: does not change with DHCP
echo "INGEST_TOKEN=${DASHBOARD_INGEST_TOKEN}"
echo "GNB_LOG_SSH=${GNB_LOG_SSH}"
echo "GNB_LOG_DROP=${GNB_LOG_DROP}"
} >"${dst}/agent.env"
unit=$(cat <<EOU
# Gerado por open5GK3S/install.sh
[Unit]
Description=InnovaNetwork: sends the gNB log (${GNB_LOG_SSH}:${GNB_LOG_FILE}) to the dashboard
After=network-online.target k3s.service
Wants=network-online.target
[Service]
Type=simple
User=${TARGET_USER}
EnvironmentFile=${dst}/agent.env
ExecStart=/usr/bin/python3 -u ${dst}/gnb-log-agent.py ${GNB_LOG_FILE}
Restart=always
RestartSec=10
NoNewPrivileges=true
[Install]
WantedBy=multi-user.target
EOU
)
if [[ "$(cat "${GNBLOG_UNIT_FILE}" 2>/dev/null)" != "${unit}" ]]; then
printf '%s\n' "${unit}" >"${GNBLOG_UNIT_FILE}"; systemctl daemon-reload
fi
systemctl enable open5gs-k3s-gnb-log.service >/dev/null 2>&1
systemctl restart open5gs-k3s-gnb-log.service
# The agent uses the user's SSH key: warn if there is no passwordless access yet
if su - "${TARGET_USER}" -s /bin/bash -c "ssh -o BatchMode=yes -o ConnectTimeout=5 ${GNB_LOG_SSH} test -r ${GNB_LOG_FILE}" &>/dev/null; then
ok "gNB log agent active (${GNB_LOG_SSH}:${GNB_LOG_FILE}; journalctl -u open5gs-k3s-gnb-log)"
else
warn "agent installed, but ${TARGET_USER} has no passwordless SSH access to ${GNB_LOG_SSH} yet (or the file does not exist)"
warn " set up the key (docs/components/dashboard.md) — the agent retries by itself"
fi
}
# ----------------------------------------------------------------------------
# 8b. Automatic recovery at boot (AUTO_HEAL)
# After a reboot the pods come back in any order and the user plane
# may get out of sync (UPF/SMF/UE). The unit runs scripts/heal.sh at every boot.
# It runs as root, so it executes a COPY in ${INSTALL_DIR} (owned by root),
# never the user's editable project files.
# ----------------------------------------------------------------------------
step_boot_heal() {
step "Boot: CORE_AUTOSTART=${CORE_AUTOSTART}, AUTO_HEAL=${AUTO_HEAL}"
local mode="--boot" desc="checks and recovers the user plane after boot"
if ! is_true "${CORE_AUTOSTART}"; then
mode="--boot-off"; desc="stops the core at boot (start it from the dashboard Control tab)"
fi
if ! is_true "${AUTO_HEAL}" && is_true "${CORE_AUTOSTART}"; then
if [[ -f "${HEAL_UNIT_FILE}" ]]; then
systemctl disable open5gs-k3s-heal.service >/dev/null 2>&1 || true
rm -f "${HEAL_UNIT_FILE}"; systemctl daemon-reload
ok "unit open5gs-k3s-heal removed"
else
info "off; after a reboot run ./scripts/heal.sh if ./test.sh fails"
fi
return 0
fi
local dst="${INSTALL_DIR}/heal" unit
install -d -m 0755 -o root -g root "${dst}" "${dst}/scripts" "${dst}/scripts/lib"
install -m 0755 -o root -g root "${PROJECT_DIR}/scripts/heal.sh" "${dst}/scripts/heal.sh"
install -m 0644 -o root -g root "${PROJECT_DIR}/scripts/lib/common.sh" "${dst}/scripts/lib/common.sh"
install -m 0600 -o root -g root "${CONFIG}" "${dst}/config.env"
unit=$(cat <<EOU
# Gerado por open5GK3S/install.sh
[Unit]
Description=InnovaNetwork: ${desc}
After=k3s.service
Wants=k3s.service
[Service]
# exec (not oneshot): does not delay the boot while waiting for the pods
Type=exec
Environment=CONFIG=${dst}/config.env HOME=/root
ExecStart=${dst}/scripts/heal.sh ${mode}
[Install]
WantedBy=multi-user.target
EOU
)
if [[ "$(cat "${HEAL_UNIT_FILE}" 2>/dev/null)" != "${unit}" ]]; then
printf '%s\n' "${unit}" >"${HEAL_UNIT_FILE}"; systemctl daemon-reload
fi
systemctl enable open5gs-k3s-heal.service >/dev/null 2>&1
if [[ "${mode}" == --boot-off ]]; then
ok "at boot the core stays STOPPED (CORE_AUTOSTART=false): start it from the Control tab (log: journalctl -u open5gs-k3s-heal)"
else
ok "unit open5gs-k3s-heal active at boot (copy in ${dst}; log: journalctl -u open5gs-k3s-heal)"
fi
}
# ----------------------------------------------------------------------------
# 9. kubeconfig for the user who called sudo (without overwriting theirs)
# ----------------------------------------------------------------------------
step_user_kubeconfig() {
step "kubeconfig and shortcuts of user '${TARGET_USER}'"
KUBECONFIG_CHANGED=false; BASHRC_CHANGED=false; KCTX_NOT_CURRENT=false
setup_user_kubeconfig "${K3S_KUBECONFIG}"
if ${KUBECONFIG_CHANGED}; then
ok "${TARGET_HOME}/.kube/config updated (context '${KCTX}', owner ${TARGET_USER}, 0600)"
else
ok "${TARGET_HOME}/.kube/config was already up to date"
fi
if ${KCTX_NOT_CURRENT}; then
warn "your current context was kept; to use this cluster: kubectl config use-context ${KCTX}"
fi
if is_true "${SHELL_EXTRAS}"; then
setup_user_bashrc
if ${BASHRC_CHANGED}; then ok "alias 'k' and kubectl completion in ${TARGET_HOME}/.bashrc (open a new terminal)"; fi
fi
# real test as the user (no sudo, no KUBECONFIG)
if su - "${TARGET_USER}" -s /bin/bash -c 'env -u KUBECONFIG kubectl get nodes' &>/dev/null; then
ok "'kubectl get nodes' works as ${TARGET_USER} without sudo"
else
warn "'kubectl get nodes' failed as ${TARGET_USER} — see docs/troubleshooting.md"
fi
}
final_summary() {
step "Done"
kc -n "${K8S_NAMESPACE}" get pods -o wide
echo
info "Test: ./test.sh"
if is_true "${WEBUI_ENABLED}"; then
info "WebUI: http://${NODE_IP:-localhost}:${WEBUI_NODEPORT} (admin / 1423; any host IP works)"
fi
if is_true "${DASHBOARD_ENABLED}"; then
info "Dashboard: http://${NODE_IP:-localhost}:${DASHBOARD_NODEPORT}$( [[ -n "${DASHBOARD_PASSWORD}" ]] && echo " (admin / DASHBOARD_PASSWORD)" || true)"
if [[ -n "${DASHBOARD_ADMIN_PASSWORD:-}" ]]; then
info " Settings tab: administrator password in: sudo cat ${INSTALL_DIR}/state/dashboard-admin-password"
fi
fi
info "kubectl: kubectl -n ${K8S_NAMESPACE} get pods (as ${TARGET_USER}, no sudo)"
if is_true "${EXPOSE_RAN}"; then
info "External gNB: AMF N2 = ${NODE_IP}:38412/SCTP UPF N3 = ${NODE_IP}:2152/UDP"
fi
info "Subscribers: sudo ./scripts/subscribers.sh list"
}
# Files created as root (logs, values) go back to the real user
fix_ownership() {
[[ -n "${TARGET_USER:-}" && "${TARGET_USER}" != "root" ]] || return 0
chown -R "${TARGET_USER}:${TARGET_GROUP}" "${LOG_DIR}" "${GENERATED_DIR}" 2>/dev/null || true
}
main() {
require_root "$@"
# Automatic log (do not use "| tee": extra words become files)
mkdir -p "${LOG_DIR}"
exec > >(tee -a "${LOG_DIR}/install.log") 2>&1
echo "===== $(date '+%F %T') install.sh $* ====="
trap fix_ownership EXIT
load_config
step "Config validation and environment detection (${CONFIG})"
validate_config
detect_env
resolve_target_user
print_env_summary
mkdir -p "${INSTALL_DIR}/state"
if ${DASHBOARD_ONLY}; then
k3s_ready || die "--dashboard-only requires k3s already installed and running"
step_dashboard
step_boot_heal # only the boot unit (CORE_AUTOSTART); does not touch the core now
return 0
fi
if ${DEPLOY_ONLY}; then
k3s_ready || die "--deploy-only requires k3s already installed and running"
find_helm; [[ -n "${HELM_BIN}" ]] || die "helm not found"
else
step_packages
step_modules
step_sysctl
step_host_profile
step_stable_netif
step_k3s
step_helm
fi
step_charts
step_ran_link
step_deploy_core
step_subscribers
step_deploy_ueransim
step_dashboard
step_boot_heal
step_user_kubeconfig
final_summary
}
main "$@"