-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathctrl-panel.sh
More file actions
356 lines (330 loc) · 14.4 KB
/
ctrl-panel.sh
File metadata and controls
356 lines (330 loc) · 14.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
#!/usr/bin/env bash
# APGer Control Panel CLI
NFS_ROOT="/srv/apger-nfs"
NFS_EXPORTS="/etc/exports"
NFS_SERVICE="nfs-kernel-server"
RED='\033[0;31m'
GREEN='\033[0;32m'
GRAY='\033[0;90m'
YELLOW='\033[1;33m'
NC='\033[0m'
STATUS_NOT_CONFIGURED="${GRAY}●${NC} Not configured"
STATUS_CONFIGURED_OFF="${RED}●${NC} Configured, stopped"
STATUS_CONFIGURED_ON="${GREEN}●${NC} Configured, running"
# Cached state
_NFS_STATUS=""
_K8S_REACHABLE=""
check_root() {
[ "$EUID" -ne 0 ] && { echo "Error: run as root"; exit 1; }
# Preserve kubeconfig from calling user (sudo loses $HOME)
if [ -z "$KUBECONFIG" ]; then
local user_home; user_home=$(getent passwd "${SUDO_USER:-$USER}" | cut -d: -f6)
[ -f "$user_home/.kube/config" ] && export KUBECONFIG="$user_home/.kube/config"
fi
# Pre-check k8s reachability in background
if command -v kubectl >/dev/null 2>&1; then
kubectl cluster-info --request-timeout=2s >/dev/null 2>&1 && _K8S_REACHABLE=1 || _K8S_REACHABLE=0
else
_K8S_REACHABLE=0
fi
}
get_nfs_status() {
if [ -n "$_NFS_STATUS" ]; then echo "$_NFS_STATUS"; return; fi
local configured=0 running=0
[ -d "$NFS_ROOT" ] && grep -q "$NFS_ROOT" "$NFS_EXPORTS" 2>/dev/null && configured=1 || true
systemctl is-active --quiet "$NFS_SERVICE" 2>/dev/null && running=1 || true
if [ "$configured" = 1 ] && [ "$running" = 1 ]; then _NFS_STATUS="running"
elif [ "$configured" = 1 ]; then _NFS_STATUS="stopped"
else _NFS_STATUS="not_configured"; fi
echo "$_NFS_STATUS"
}
k8s_reachable() {
if [ -z "$_K8S_REACHABLE" ]; then
if command -v kubectl >/dev/null 2>&1 && kubectl cluster-info --request-timeout=2s >/dev/null 2>&1; then
_K8S_REACHABLE=1
else
_K8S_REACHABLE=0
fi
fi
[ "$_K8S_REACHABLE" = "1" ]
}
apply_configmap() {
local ip=$1
if k8s_reachable; then
kubectl get namespace apger >/dev/null 2>&1 || kubectl create namespace apger
kubectl create configmap nfs-config --namespace=apger \
--from-literal=nfs_server="$ip" --from-literal=nfs_path="$NFS_ROOT" \
--dry-run=client -o yaml | kubectl apply -f -
echo -e "${GREEN}✓${NC} ConfigMap applied"; _K8S_CM=1
else
echo -e "${YELLOW}!${NC} kubectl unreachable — run manually:"
echo " kubectl create configmap nfs-config --namespace=apger --from-env-file=.env.nfs"
fi
}
show_status() {
local status; status=$(get_nfs_status)
local k8s_status k8s_cm
if k8s_reachable; then
k8s_status="${GREEN}●${NC} Cluster reachable"
if [ -z "$_K8S_CM" ]; then
kubectl get configmap nfs-config --namespace=apger >/dev/null 2>&1 \
&& _K8S_CM=1 || _K8S_CM=0
fi
[ "$_K8S_CM" = "1" ] \
&& k8s_cm="${GREEN}●${NC} ConfigMap active" \
|| k8s_cm="${GRAY}●${NC} ConfigMap not found"
else
k8s_status="${GRAY}●${NC} Cluster unreachable"
k8s_cm="${GRAY}●${NC} Unknown"
fi
echo ""
case "$status" in
running)
echo -e "NFS: $STATUS_CONFIGURED_ON"
echo "Path: $NFS_ROOT"
;;
stopped)
echo -e "NFS: $STATUS_CONFIGURED_OFF"
echo "Path: $NFS_ROOT"
;;
not_configured)
echo -e "NFS: $STATUS_NOT_CONFIGURED"
;;
esac
echo -e "Kubernetes: $k8s_status"
echo -e "ConfigMap: $k8s_cm"
echo ""
}
setup_nfs() {
local status; status=$(get_nfs_status)
[ "$status" != "not_configured" ] && { echo "Already configured."; return 0; }
command -v exportfs >/dev/null 2>&1 || {
echo -e "${RED}Error: NFS server not found${NC}"
echo " Debian/Ubuntu: apt install nfs-kernel-server"
echo " Fedora/RHEL: dnf install nfs-utils"
return 1
}
echo "Setting up..."
mkdir -p "$NFS_ROOT"/{.credentials,build-logs,output-pkgs}
mkdir -p "$NFS_ROOT"/build-logs/{x86_64,aarch64,riscv64}/{core,main,extra}
chmod 777 "$NFS_ROOT"; chmod 700 "$NFS_ROOT/.credentials"
grep -q "$NFS_ROOT" "$NFS_EXPORTS" 2>/dev/null || \
echo "$NFS_ROOT *(rw,sync,no_subtree_check,no_root_squash)" >> "$NFS_EXPORTS"
exportfs -ra
local ip; ip=$(ip route get 1 | awk '{print $7; exit}')
printf "NFS_SERVER=%s\nNFS_PATH=%s\n" "$ip" "$NFS_ROOT" > .env.nfs
apply_configmap "$ip"
_NFS_STATUS=""
echo -e "${GREEN}✓${NC} Done — IP: $ip"
}
start_nfs() {
local status; status=$(get_nfs_status)
[ "$status" = "not_configured" ] && { echo "Not configured."; return 1; }
[ "$status" = "running" ] && { echo "Already running."; return 0; }
systemctl start "$NFS_SERVICE" && systemctl enable "$NFS_SERVICE"
_NFS_STATUS=""
echo -e "${GREEN}✓${NC} Started"
}
stop_nfs() {
local status; status=$(get_nfs_status)
[ "$status" = "not_configured" ] && { echo "Not configured."; return 1; }
[ "$status" = "stopped" ] && { echo "Already stopped."; return 0; }
systemctl stop "$NFS_SERVICE"
_NFS_STATUS=""
echo -e "${YELLOW}✓${NC} Stopped"
}
regenerate_env() {
local ip; ip=$(ip route get 1 | awk '{print $7; exit}')
rm -f .env.nfs
k8s_reachable && kubectl delete configmap nfs-config --namespace=apger --ignore-not-found=true
printf "NFS_SERVER=%s\nNFS_PATH=%s\n" "$ip" "$NFS_ROOT" > .env.nfs
apply_configmap "$ip"
echo -e "${GREEN}✓${NC} Regenerated — IP: $ip"
}
delete_nfs() {
local status; status=$(get_nfs_status)
[ "$status" = "not_configured" ] && { echo "Not configured."; return 0; }
echo -e "${RED}WARNING: $NFS_ROOT will be deleted permanently!${NC}"
echo -n "Type 'yes' to confirm: "; read -r confirm </dev/tty
[ "$confirm" != "yes" ] && { echo "Cancelled."; return 0; }
[ "$status" = "running" ] && systemctl stop "$NFS_SERVICE" && systemctl disable "$NFS_SERVICE"
sed -i "\|$NFS_ROOT|d" "$NFS_EXPORTS"; exportfs -ra
rm -rf "$NFS_ROOT" .env.nfs
_NFS_STATUS=""
echo -e "${GREEN}✓${NC} Deleted"
}
delete_configmap() {
rm -f .env.nfs
if k8s_reachable; then
kubectl delete configmap nfs-config --namespace=apger --ignore-not-found=true
echo -e "${GREEN}✓${NC} ConfigMap deleted"; _K8S_CM=0
else
echo -e "${YELLOW}!${NC} Cluster unreachable — only .env.nfs removed"
fi
}
print_banner() {
printf '\n'
printf ' \033[38;2;255;210;0m █████╗ ██████╗ ██████╗ ███████╗██████╗ ██████╗████████╗██████╗ ██╗ ██████╗ █████╗ ███╗ ██╗███████╗██╗ \033[0m\n'
printf ' \033[38;2;255;160;0m██╔══██╗██╔══██╗██╔════╝ ██╔════╝██╔══██╗ ██╔════╝╚══██╔══╝██╔══██╗██║ ██╔══██╗██╔══██╗████╗ ██║██╔════╝██║ \033[0m\n'
printf ' \033[38;2;255;100;0m███████║██████╔╝██║ ███╗█████╗ ██████╔╝ ██║ ██║ ██████╔╝██║ ██████╔╝███████║██╔██╗ ██║█████╗ ██║ \033[0m\n'
printf ' \033[38;2;220;80;20m██╔══██║██╔═══╝ ██║ ██║██╔══╝ ██╔══██╗ ██║ ██║ ██╔══██╗██║ ██╔═══╝ ██╔══██║██║╚██╗██║██╔══╝ ██║ \033[0m\n'
printf ' \033[38;2;200;40;40m██║ ██║██║ ╚██████╔╝███████╗██║ ██║ ╚██████╗ ██║ ██║ ██║███████╗██║ ██║ ██║██║ ╚████║███████╗███████╗\033[0m\n'
printf ' \033[38;2;180;20;20m╚═╝ ╚═╝╚═╝ ╚═════╝ ╚══════╝╚═╝ ╚═╝ ╚═════╝ ╚═╝ ╚═╝ ╚═╝╚══════╝╚═╝ ╚═╝ ╚═╝╚═╝ ╚═══╝╚══════╝╚══════╝\033[0m\n'
printf '\n'
}
ping_cluster() {
printf "\nChecking Kubernetes cluster...\n\n"
if ! command -v kubectl >/dev/null 2>&1; then
printf " ${RED}✗${NC} kubectl not found\n"
return
fi
local out err
out=$(kubectl cluster-info --request-timeout=2s 2>&1)
if [ $? -eq 0 ]; then
_K8S_REACHABLE=1
printf " ${GREEN}✓${NC} Cluster reachable\n"
printf "%s\n" "$out" | grep -E "running at" | while read -r line; do
printf " %s\n" "$line"
done
else
_K8S_REACHABLE=0
printf " ${RED}✗${NC} Cluster unreachable\n"
printf " Reason: %s\n" "$(echo "$out" | head -3)"
printf "\n Check:\n"
printf " kubectl config current-context\n"
printf " kubectl config get-contexts\n"
fi
printf "\n"
}
ping_nfs() {
local ip=""
[ -f .env.nfs ] && ip=$(grep NFS_SERVER .env.nfs | cut -d= -f2)
if [ -z "$ip" ]; then
printf "\n ${RED}✗${NC} .env.nfs not found — run Setup first\n\n"
return
fi
printf "\nChecking NFS server at %s...\n\n" "$ip"
# 1. ICMP ping
if ping -c3 -W2 "$ip" >/dev/null 2>&1; then
local ms; ms=$(ping -c3 -W2 "$ip" 2>/dev/null | tail -1 | awk -F'/' '{print $5}')
printf " ${GREEN}✓${NC} Host reachable (avg ping: %s ms)\n" "$ms"
else
printf " ${RED}✗${NC} Host unreachable (ping failed)\n\n"
return
fi
# 2. NFS port 2049
if command -v nc >/dev/null 2>&1; then
if nc -z -w2 "$ip" 2049 2>/dev/null; then
printf " ${GREEN}✓${NC} NFS port 2049 open\n"
else
printf " ${RED}✗${NC} NFS port 2049 closed\n"
fi
fi
# 3. Mount test (read/write)
local mnt; mnt=$(mktemp -d)
if mount -t nfs -o ro,soft,timeo=5 "$ip:$NFS_ROOT" "$mnt" 2>/dev/null; then
printf " ${GREEN}✓${NC} NFS mount OK (read)\n"
umount "$mnt" 2>/dev/null
# write test
if mount -t nfs -o rw,soft,timeo=5 "$ip:$NFS_ROOT" "$mnt" 2>/dev/null; then
local f="$mnt/.ping_test_$$"
if echo "ok" > "$f" 2>/dev/null && rm -f "$f"; then
printf " ${GREEN}✓${NC} NFS write OK\n"
else
printf " ${YELLOW}!${NC} NFS mounted but write failed\n"
fi
umount "$mnt" 2>/dev/null
fi
else
printf " ${RED}✗${NC} NFS mount failed (server down or not exported)\n"
fi
rmdir "$mnt" 2>/dev/null
printf "\n"
}
view_logs() {
local log_dir="${BUILD_LOGS_PATH:-/output/build-logs}"
[ -d "$log_dir" ] || log_dir="$NFS_ROOT/build-logs"
local files; files=$(find "$log_dir" -name "*.log" 2>/dev/null | sort)
local count; count=$(echo "$files" | grep -c . 2>/dev/null || echo 0)
if [ "$count" -eq 0 ]; then
printf "\n ${YELLOW}!${NC} No build logs found in %s\n\n" "$log_dir"
return
fi
printf "\n Found %s log pages. Opening with less...\n\n" "$count"
echo "$files" | xargs cat 2>/dev/null | less -R
}
repodata_fmt() {
local script_dir; script_dir=$(dirname "$(realpath "$0")")
local repodata="$script_dir/repodata"
if [ ! -d "$repodata" ]; then
printf "\n ${RED}✗${NC} repodata directory not found at %s\n\n" "$repodata"
return
fi
local renamed=0 skipped=0 errors=0
while IFS= read -r -d '' file; do
local dir; dir=$(dirname "$file")
local name version arch
name=$(grep -m1 '^name\s*=' "$file" | sed 's/.*=\s*"\(.*\)"/\1/')
version=$(grep -m1 '^version\s*=' "$file" | sed 's/.*=\s*"\(.*\)"/\1/')
arch=$(grep -m1 '^architecture\s*=' "$file" | sed 's/.*=\s*"\(.*\)"/\1/')
if [ -z "$name" ] || [ -z "$version" ] || [ -z "$arch" ]; then
printf "SKIP %s (missing fields)\n" "$file"
skipped=$((skipped + 1)); continue
fi
local new_name="${name}_${arch}_${version}.toml"
local new_path="$dir/$new_name"
if [ "$file" = "$new_path" ]; then skipped=$((skipped + 1)); continue; fi
if [ -e "$new_path" ]; then
printf "ERROR %s -> %s (target exists)\n" "$(basename "$file")" "$new_name"
errors=$((errors + 1)); continue
fi
mv "$file" "$new_path"
printf "OK %s -> %s\n" "$(basename "$file")" "$new_name"
renamed=$((renamed + 1))
done < <(find "$repodata" -name "*.toml" -print0)
printf "\nDone: %s renamed, %s skipped, %s errors\n" "$renamed" "$skipped" "$errors"
if [ "$renamed" -gt 0 ]; then
git -C "$script_dir" config user.email "github-actions[bot]@users.noreply.github.com"
git -C "$script_dir" config user.name "github-actions[bot]"
git -C "$script_dir" add repodata
git -C "$script_dir" commit -m "chore: formatted $renamed packages, skipped $skipped, errors $errors"
git -C "$script_dir" push origin main
printf "\nPushed to origin/main\n"
fi
}
show_menu() {
clear 2>/dev/null || true
print_banner
show_status
local log_dir="${BUILD_LOGS_PATH:-$NFS_ROOT/build-logs}"
local log_count; log_count=$(find "$log_dir" -name "*.log" 2>/dev/null | wc -l)
printf " 1) Setup NFS\n 2) Start NFS\n 3) Stop NFS\n"
printf " 4) Re-generate .env.nfs\n 5) Delete NFS server\n"
printf " 6) Delete ConfigMap\n 7) Ping cluster\n 8) Ping NFS server\n"
printf " 9) View build logs (%s pages)\n 10) Repodata fmt\n 11) Exit\n\n" "$log_count"
printf "Select option: "
read -r choice </dev/tty
}
main() {
check_root
while true; do
show_menu
echo ""
case "$choice" in
1) setup_nfs ;;
2) start_nfs ;;
3) stop_nfs ;;
4) regenerate_env ;;
5) delete_nfs ;;
6) delete_configmap ;;
7) ping_cluster ;;
8) ping_nfs ;;
9) view_logs ;;
10) repodata_fmt ;;
11) echo "Exiting..."; exit 0 ;;
*) echo "Invalid option"; sleep 1; continue ;;
esac
echo ""; read -rp "Press Enter to continue..." </dev/tty
done
}
main