-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcore.func
More file actions
853 lines (763 loc) · 28.2 KB
/
Copy pathcore.func
File metadata and controls
853 lines (763 loc) · 28.2 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
#!/usr/bin/env bash
# Copyright (c) 2021-2026 community-scripts ORG
# Author: MickLesk
# License: MIT | https://github.com/community-scripts/ProxmoxVED/raw/main/LICENSE
# ==============================================================================
# INCUS-CORE.FUNC - CORE UTILITIES FOR INCUS CONTAINER MANAGEMENT
# ==============================================================================
#
# This file provides core utility functions for Incus container management
# including colors, formatting, validation checks, message output, and
# execution helpers - adapted from Proxmox LXC patterns for Incus.
#
# Key differences from Proxmox LXC (core.func):
# - Incus CLI instead of pct/pvesm
# - incus profiles instead of CT templates
# - incus storage pools instead of Proxmox storage
# - incus networks instead of vmbr bridges
# - No pveam (uses incus image copy/list)
#
# Usage:
# source <(curl -fsSL https://raw.githubusercontent.com/community-scripts/core/main/incus/core.func)
# load_incus_functions
#
# ==============================================================================
[[ -n "${_INCUS_CORE_FUNC_LOADED:-}" ]] && return
_INCUS_CORE_FUNC_LOADED=1
INCUS_COMMUNITY_SCRIPTS_URL="${COMMUNITY_SCRIPTS_URL:-https://raw.githubusercontent.com/community-scripts/ProxmoxVED/main}"
# Load upstream misc/core.func for full helper parity (prompt_*, dev-mode, etc.).
# Incus-specific behavior is provided through incus_* functions and aliases below.
if [[ -z "${_CORE_FUNC_LOADED:-}" ]]; then
_cs_source_func "core/core.func"
fi
# ==============================================================================
# SECTION 1: INITIALIZATION & SETUP
# ==============================================================================
# ------------------------------------------------------------------------------
# load_incus_functions()
#
# - Initializes all core utility groups (colors, formatting, icons, defaults)
# - Ensures functions are loaded only once via __INCUS_FUNCTIONS_LOADED flag
# - Must be called at start of any Incus script
# ------------------------------------------------------------------------------
load_incus_functions() {
[[ -n "${__INCUS_FUNCTIONS_LOADED:-}" ]] && return
__INCUS_FUNCTIONS_LOADED=1
incus_color
incus_formatting
incus_icons
incus_default_vars
incus_set_std_mode
}
# ------------------------------------------------------------------------------
# incus_color()
#
# - Sets ANSI color codes for styled terminal output
# - Variables: YW (yellow), YWB (yellow bright), BL (blue), RD (red)
# GN (green), DGN (dark green), BGN (background green), CL (clear)
# ------------------------------------------------------------------------------
incus_color() {
YW=$(echo "\033[33m")
YWB=$'\e[93m'
BL=$(echo "\033[36m")
RD=$(echo "\033[01;31m")
BGN=$(echo "\033[4;92m")
GN=$(echo "\033[1;92m")
DGN=$(echo "\033[32m")
CL=$(echo "\033[m")
# Same as core/core.func: a dev run is red, so it never reads as a normal one.
if [[ -n "${dev_mode:-}" ]]; then
BGN=$(echo "\033[4;91m")
GN=$(echo "\033[1;91m")
DGN=$(echo "\033[31m")
fi
}
# ------------------------------------------------------------------------------
# incus_color_spinner()
#
# - Sets ANSI color codes specifically for spinner animation
# ------------------------------------------------------------------------------
incus_color_spinner() {
CS_YW=$'\033[33m'
CS_YWB=$'\033[93m'
CS_CL=$'\033[m'
}
# ------------------------------------------------------------------------------
# incus_formatting()
#
# - Defines formatting helpers for terminal output
# ------------------------------------------------------------------------------
incus_formatting() {
BFR="\\r\\033[K"
BOLD=$(echo "\033[1m")
HOLD=" "
TAB=" "
TAB3=" "
}
# ------------------------------------------------------------------------------
# incus_icons()
#
# - Sets symbolic emoji icons used throughout user feedback
# ------------------------------------------------------------------------------
incus_icons() {
CM="${TAB}✔️${TAB}"
CROSS="${TAB}✖️${TAB}"
DNSOK="✔️ "
DNSFAIL="${TAB}✖️${TAB}"
INFO="${TAB}💡${TAB}${CL}"
OS="${TAB}🖥️${TAB}${CL}"
OSVERSION="${TAB}🌟${TAB}${CL}"
CONTAINERTYPE="${TAB}📦${TAB}${CL}"
DISKSIZE="${TAB}💾${TAB}${CL}"
CPUCORE="${TAB}🧠${TAB}${CL}"
RAMSIZE="${TAB}🛠️${TAB}${CL}"
SEARCH="${TAB}🔍${TAB}${CL}"
VERBOSE_CROPPED="🔍${TAB}"
VERIFYPW="${TAB}🔐${TAB}${CL}"
CONTAINERID="${TAB}🆔${TAB}${CL}"
HOSTNAME="${TAB}🏠${TAB}${CL}"
BRIDGE="${TAB}🌉${TAB}${CL}"
NETWORK="${TAB}📡${TAB}${CL}"
GATEWAY="${TAB}🌐${TAB}${CL}"
ICON_DISABLEIPV6="${TAB}🚫${TAB}${CL}"
DEFAULT="${TAB}⚙️${TAB}${CL}"
MACADDRESS="${TAB}🔗${TAB}${CL}"
VLANTAG="${TAB}🏷️${TAB}${CL}"
ROOTSSH="${TAB}🔑${TAB}${CL}"
CREATING="${TAB}🚀${TAB}${CL}"
ADVANCED="${TAB}🧩${TAB}${CL}"
FUSE="${TAB}🗂️${TAB}${CL}"
GPU="${TAB}🎮${TAB}${CL}"
CLOUD="${TAB}☁️${TAB}"
VTPM="${TAB}🔐${TAB}"
DISKBUS="${TAB}🔌${TAB}"
SNAPSHOT="${TAB}📸${TAB}"
HOURGLASS="${TAB}⏳${TAB}"
}
# ------------------------------------------------------------------------------
# incus_default_vars()
#
# - Sets default retry and wait variables used for system actions
# ------------------------------------------------------------------------------
incus_default_vars() {
RETRY_NUM=10
RETRY_EVERY=3
i=$RETRY_NUM
}
# ------------------------------------------------------------------------------
# incus_set_std_mode()
#
# - Sets default verbose mode for script and OS execution
# - If VERBOSE=yes: STD="" (show all output)
# - If VERBOSE=no: STD="silent" (suppress output via silent() wrapper)
# ------------------------------------------------------------------------------
incus_set_std_mode() {
if [ "${VERBOSE:-no}" = "yes" ]; then
STD=""
else
STD="silent"
fi
# Enable bash tracing if trace mode active
if [[ "${DEV_MODE_TRACE:-false}" == "true" ]]; then
set -x
# BASH_SOURCE is empty at the top level of `bash -c` and inside a
# `source /dev/stdin`, which is exactly how the install script runs in the
# container. Unguarded, set -u aborts the run the moment PS4 is expanded.
export PS4='+(${BASH_SOURCE:-?}:${LINENO}): ${FUNCNAME[0]:+${FUNCNAME[0]}(): }'
fi
}
# ==============================================================================
# SECTION 2: VALIDATION CHECKS (Incus-specific)
# ==============================================================================
# ------------------------------------------------------------------------------
# incus_shell_check()
#
# - Verifies that the script is running under Bash shell
# ------------------------------------------------------------------------------
incus_shell_check() {
if [[ "$(ps -p $$ -o comm=)" != "bash" ]]; then
clear
incus_msg_error "Your default shell is currently not set to Bash. To use these scripts, please switch to the Bash shell."
echo -e "\nExiting..."
sleep 2
exit
fi
}
# ------------------------------------------------------------------------------
# incus_root_check()
#
# - Verifies script is running with root privileges
# ------------------------------------------------------------------------------
incus_root_check() {
# Incus can be used by non-root users in the 'incus-admin' group
if incus info &>/dev/null; then
return 0
fi
# Fallback: root always works
if [[ "$(id -u)" -eq 0 ]]; then
return 0
fi
clear
incus_msg_error "Cannot access Incus. Either:"
incus_msg_error " - Run as root (sudo), or"
incus_msg_error " - Add your user to the incus-admin group: sudo usermod -aG incus-admin \$USER"
echo -e "\nExiting..."
sleep 2
exit
}
# ------------------------------------------------------------------------------
# incus_check()
#
# - Validates that Incus is installed and accessible
# - Checks incus daemon is running
# - Exits with error message if Incus is not available
# ------------------------------------------------------------------------------
incus_check() {
if ! command -v incus &>/dev/null; then
incus_msg_error "Incus is not installed on this system."
incus_msg_error "Install Incus first: https://linuxcontainers.org/incus/docs/main/installing/"
exit 1
fi
# Check if incus daemon is accessible
if ! incus info &>/dev/null; then
incus_msg_error "Incus daemon is not running or not accessible."
incus_msg_error "Run: systemctl start incus"
exit 1
fi
# Check if we have any storage pools
local pool_count
pool_count=$(incus storage list -f csv,noheader 2>/dev/null | wc -l)
if [[ "$pool_count" -eq 0 ]]; then
incus_msg_error "No Incus storage pools found."
incus_msg_error "Create one with: incus storage create default dir"
exit 1
fi
}
# Incus handles cross-architecture natively (e.g. x86 host running ARM containers).
# No arch_check needed - removed from Incus scripts.
# ==============================================================================
# SECTION 3: EXECUTION HELPERS
# ==============================================================================
# ------------------------------------------------------------------------------
# get_active_incus_logfile()
#
# - Returns the appropriate log file based on execution context
# ------------------------------------------------------------------------------
get_active_incus_logfile() {
if [[ -n "${_INCUS_HOST_LOGFILE:-}" ]]; then
echo "$_INCUS_HOST_LOGFILE"
elif [[ -n "${INCUS_INSTALL_LOG:-}" ]]; then
echo "$INCUS_INSTALL_LOG"
elif [[ -n "${INCUS_BUILD_LOG:-}" ]]; then
echo "$INCUS_BUILD_LOG"
else
echo "/tmp/incus-build-$(date +%Y%m%d_%H%M%S).log"
fi
}
INCUS_SILENT_LOGFILE="$(get_active_incus_logfile)"
# ------------------------------------------------------------------------------
# incus_strip_ansi()
#
# - Removes ANSI escape sequences from input text
# ------------------------------------------------------------------------------
incus_strip_ansi() {
if [[ $# -gt 0 ]]; then
echo -e "$*" | sed 's/\x1b\[[0-9;]*m//g; s/\x1b\[[0-9;]*[a-zA-Z]//g'
else
sed 's/\x1b\[[0-9;]*m//g; s/\x1b\[[0-9;]*[a-zA-Z]//g'
fi
}
# ------------------------------------------------------------------------------
# incus_log_msg()
#
# - Writes message to active log file without ANSI codes
# - Adds timestamp prefix for log correlation
# ------------------------------------------------------------------------------
incus_log_msg() {
local msg="$*"
local logfile
logfile="$(get_active_incus_logfile)"
[[ -z "$msg" ]] && return
[[ -z "$logfile" ]] && return
mkdir -p "$(dirname "$logfile")" 2>/dev/null || true
local clean_msg
clean_msg=$(incus_strip_ansi "$msg")
echo "[$(date '+%Y-%m-%d %H:%M:%S')] $clean_msg" >>"$logfile"
}
# ------------------------------------------------------------------------------
# incus_log_section()
#
# - Writes a section header to the log file
# ------------------------------------------------------------------------------
incus_log_section() {
local section="$1"
local logfile
logfile="$(get_active_incus_logfile)"
[[ -z "$logfile" ]] && return
mkdir -p "$(dirname "$logfile")" 2>/dev/null || true
{
echo ""
echo "================================================================================"
echo "[$(date '+%Y-%m-%d %H:%M:%S')] $section"
echo "================================================================================"
} >>"$logfile"
}
# ------------------------------------------------------------------------------
# incus_silent()
#
# - Executes command with output redirected to active log file
# - On error: displays last 20 lines of log and exits with original exit code
# ------------------------------------------------------------------------------
incus_silent() {
local cmd="$*"
local caller_line="${BASH_LINENO[0]:-unknown}"
local logfile="$(get_active_incus_logfile)"
set +Eeuo pipefail
trap - ERR
"$@" >>"$logfile" 2>&1
local rc=$?
set -Eeuo pipefail
trap 'incus_error_handler' ERR
if [[ $rc -ne 0 ]]; then
printf "\e[?25h"
incus_msg_error "in line ${caller_line}: exit code ${rc}"
incus_msg_custom "→" "${YWB}" "${cmd}"
if [[ -s "$logfile" ]]; then
echo -e "\n${TAB}--- Last 20 lines of log ---"
tail -n 20 "$logfile"
echo -e "${TAB}-----------------------------------"
echo -e "${TAB}📋 Full log: ${logfile}\n"
fi
exit "$rc"
fi
}
# ------------------------------------------------------------------------------
# incus_spinner() / incus_stop_spinner()
#
# - Animated spinner for long-running operations
# ------------------------------------------------------------------------------
incus_spinner() {
hash -r 2>/dev/null || true
local chars=(⠋ ⠙ ⠹ ⠸ ⠼ ⠴ ⠦ ⠧ ⠇ ⠏)
local msg="${SPINNER_MSG:-Processing...}"
local i=0
while true; do
local index=$((i++ % ${#chars[@]}))
printf "\r\033[2K%s %b" "${CS_YWB}${chars[$index]}${CS_CL}" "${CS_YWB}${msg}${CS_CL}"
sleep 0.1 2>/dev/null || command sleep 0.1 2>/dev/null || break
done
}
incus_clear_line() {
tput cr 2>/dev/null || echo -en "\r"
tput el 2>/dev/null || echo -en "\033[K"
}
incus_stop_spinner() {
local pid="${SPINNER_PID:-}"
[[ -z "$pid" && -f /tmp/.incus_spinner.pid ]] && pid=$(</tmp/.incus_spinner.pid)
if [[ -n "$pid" && "$pid" =~ ^[0-9]+$ ]]; then
if kill "$pid" 2>/dev/null; then
sleep 0.05
kill -9 "$pid" 2>/dev/null || true
wait "$pid" 2>/dev/null || true
fi
rm -f /tmp/.incus_spinner.pid
fi
unset SPINNER_PID SPINNER_MSG
stty sane 2>/dev/null || true
}
# ==============================================================================
# SECTION 4: MESSAGE OUTPUT (Incus-prefixed for namespace safety)
# ==============================================================================
# ------------------------------------------------------------------------------
# incus_msg_info()
#
# - Displays informational message with spinner animation
# ------------------------------------------------------------------------------
incus_msg_info() {
local msg="$1"
[[ -z "$msg" ]] && return
if ! declare -p INCUS_MSG_INFO_SHOWN &>/dev/null || ! declare -A INCUS_MSG_INFO_SHOWN &>/dev/null; then
declare -gA INCUS_MSG_INFO_SHOWN=()
fi
local sanitized_key
sanitized_key=$(printf '%s' "$msg" | sed 's/\x1b\[[0-9;]*m//g; s/[^a-zA-Z0-9_]/_/g')
[[ -n "${INCUS_MSG_INFO_SHOWN["$sanitized_key"]+x}" ]] && return
INCUS_MSG_INFO_SHOWN["$sanitized_key"]=1
incus_log_msg "[INFO] $msg"
incus_stop_spinner
SPINNER_MSG="$msg"
if incus_is_verbose_mode; then
local HOURGLASS="${TAB}⏳${TAB}"
printf "\r\e[2K%s %b" "$HOURGLASS" "${YW}${msg}${CL}" >&2
if [[ "${DEV_MODE_PAUSE:-false}" == "true" ]]; then
echo -en "\n${YWB}[PAUSE]${CL} Press Enter to continue..." >&2
read -r </dev/tty
fi
return
fi
incus_color_spinner
incus_spinner &
SPINNER_PID=$!
echo "$SPINNER_PID" >/tmp/.incus_spinner.pid
disown "$SPINNER_PID" 2>/dev/null || true
}
# ------------------------------------------------------------------------------
# incus_msg_ok()
# ------------------------------------------------------------------------------
incus_msg_ok() {
local msg="$1"
[[ -z "$msg" ]] && return
incus_stop_spinner
incus_clear_line
# Match core.func: CM already includes TAB before/after the icon — no extra space.
echo -e "$CM${GN}${msg}${CL}"
incus_log_msg "[OK] $msg"
local sanitized_key
sanitized_key=$(printf '%s' "$msg" | sed 's/\x1b\[[0-9;]*m//g; s/[^a-zA-Z0-9_]/_/g')
unset 'INCUS_MSG_INFO_SHOWN['"$sanitized_key"']' 2>/dev/null || true
}
# ------------------------------------------------------------------------------
# incus_msg_error()
# ------------------------------------------------------------------------------
incus_msg_error() {
incus_stop_spinner
local msg="$1"
echo -e "${BFR:-}${CROSS:-✖️}${RD}${msg}${CL}" >&2
incus_log_msg "[ERROR] $msg"
}
# ------------------------------------------------------------------------------
# incus_msg_warn()
# ------------------------------------------------------------------------------
incus_msg_warn() {
incus_stop_spinner
local msg="$1"
# Same layout as core.func msg_warn (INFO already includes TAB padding).
echo -e "${BFR:-}${INFO:-ℹ️} ${YWB}${msg}${CL}" >&2
incus_log_msg "[WARN] $msg"
}
# ------------------------------------------------------------------------------
# incus_msg_custom()
# ------------------------------------------------------------------------------
incus_msg_custom() {
local symbol="${1:-"[*]"}"
local color="${2:-"\e[36m"}"
local msg="${3:-}"
[[ -z "$msg" ]] && return
incus_stop_spinner
# Same indentation as CM/CROSS: TAB + icon + TAB + message
echo -e "${BFR:-}${TAB:- }${symbol}${TAB:- }${color}${msg}${CL:-\e[0m}"
incus_log_msg "$msg"
}
# ------------------------------------------------------------------------------
# incus_msg_debug()
# ------------------------------------------------------------------------------
incus_msg_debug() {
if [[ "${var_full_verbose:-0}" == "1" ]]; then
[[ "${var_verbose:-0}" != "1" ]] && var_verbose=1
echo -e "${YWB}[$(date '+%F %T')] [DEBUG]${CL} $*"
fi
}
# ------------------------------------------------------------------------------
# incus_msg_dev()
# ------------------------------------------------------------------------------
incus_msg_dev() {
if [[ -n "${dev_mode:-}" ]]; then
echo -e "${SEARCH}${BOLD}${DGN}🔧 [DEV]${CL} $*"
fi
}
# ------------------------------------------------------------------------------
# incus_is_verbose_mode()
# ------------------------------------------------------------------------------
incus_is_verbose_mode() {
local verbose="${VERBOSE:-${var_verbose:-no}}"
[[ "$verbose" != "no" || ! -t 2 ]]
}
# ------------------------------------------------------------------------------
# incus_is_unattended()
# ------------------------------------------------------------------------------
incus_is_unattended() {
local mode="${MODE:-${mode:-}}"
mode="${mode,,}"
case "$mode" in
default | 1 | mydefaults | userdefaults | 3 | appdefaults | 4)
return 0
;;
advanced | 2)
# On Incus host: advanced remains interactive.
# Inside container install phase (incus command unavailable): unattended.
if command -v incus >/dev/null 2>&1; then
return 1
fi
return 0
;;
esac
[[ "${PHS_SILENT:-0}" == "1" ]] && return 0
[[ "${var_unattended:-}" =~ ^(yes|true|1)$ ]] && return 0
[[ "${UNATTENDED:-}" =~ ^(yes|true|1)$ ]] && return 0
[[ ! -t 0 ]] && return 0
return 1
}
# ------------------------------------------------------------------------------
# incus_fatal()
# ------------------------------------------------------------------------------
incus_fatal() {
incus_msg_error "$1"
# kill -INT $$ alone does not stop a non-interactive bash script: the signal
# is delivered but execution continues, so every caller that assumed "fatal
# ends here" carried on into a broken state. Observed on a real Incus host --
# a hostname conflict was reported and the create was attempted anyway.
# Signal first (reaches the main shell from a subshell), then exit for real.
kill -INT $$ 2>/dev/null || true
exit 1
}
# ==============================================================================
# SECTION 5: INCUS-SPECIFIC UTILITIES
# ==============================================================================
# ------------------------------------------------------------------------------
# incus_get_storage_pools()
#
# - Returns list of available Incus storage pools
# ------------------------------------------------------------------------------
incus_get_storage_pools() {
incus storage list -f csv,noheader 2>/dev/null | cut -d, -f1
}
# ------------------------------------------------------------------------------
# incus_get_networks()
#
# - Returns list of available Incus networks
# ------------------------------------------------------------------------------
incus_get_networks() {
incus network list -f csv,noheader 2>/dev/null | grep -v '^lo$' | cut -d, -f1
}
# ------------------------------------------------------------------------------
# incus_get_profiles()
#
# - Returns list of available Incus profiles
# ------------------------------------------------------------------------------
incus_get_profiles() {
incus profile list -f csv,noheader 2>/dev/null | cut -d, -f1
}
# ------------------------------------------------------------------------------
# incus_get_images()
#
# - Returns list of locally cached Incus images
# ------------------------------------------------------------------------------
incus_get_images() {
incus image list -f csv,noheader 2>/dev/null | cut -d, -f1,2
}
# ------------------------------------------------------------------------------
# incus_get_os_info()
#
# - Returns OS info from /etc/os-release
# - Usage: incus_get_os_info id|codename|version
# ------------------------------------------------------------------------------
incus_get_os_info() {
local field="$1"
case "$field" in
id)
. /etc/os-release 2>/dev/null && echo "${ID:-debian}"
;;
codename)
. /etc/os-release 2>/dev/null && echo "${VERSION_CODENAME:-bookworm}"
;;
version)
. /etc/os-release 2>/dev/null && echo "${VERSION_ID:-12}"
;;
*)
echo ""
;;
esac
}
# ------------------------------------------------------------------------------
# incus_get_ip()
#
# - Returns current container IP
# ------------------------------------------------------------------------------
incus_get_ip() {
local ip=""
# Try IPv4 first
ip=$(hostname -I 2>/dev/null | tr ' ' '\n' | grep -E '^[0-9]+\.[0-9]+\.[0-9]+\.[0-9]+$' | head -n1)
# Fallback to IPv6
if [[ -z "$ip" ]]; then
ip=$(hostname -I 2>/dev/null | tr ' ' '\n' | grep -E ':' | head -n1)
fi
[[ -z "$ip" ]] && ip="Unknown"
echo "$ip"
}
# ------------------------------------------------------------------------------
# incus_ensure_profile_loaded()
#
# - Sources /etc/profile.d/*.sh scripts if not already loaded
# - Fixes PATH issues when running via incus exec (non-login shells)
# ------------------------------------------------------------------------------
incus_ensure_profile_loaded() {
[[ -n "${_INCUS_PROFILE_LOADED:-}" ]] && return
command -v incusd &>/dev/null && return
if [[ -d /etc/profile.d ]]; then
for script in /etc/profile.d/*.sh; do
[[ -r "$script" ]] && source "$script"
done
fi
if [[ ":$PATH:" != *":/usr/local/bin:"* ]]; then
export PATH="/usr/local/bin:$PATH"
fi
export _INCUS_PROFILE_LOADED=1
}
# ==============================================================================
# SECTION 6: ERROR HANDLER (minimal)
# ==============================================================================
# ------------------------------------------------------------------------------
# incus_error_handler()
#
# - Basic error trap for Incus scripts
# ------------------------------------------------------------------------------
incus_error_handler() {
local exit_code=$?
local line_no=${1:-${BASH_LINENO[0]:-unknown}}
incus_msg_error "Unexpected error at line $line_no (exit code: $exit_code)"
incus_msg_custom "→" "${YWB}" "Check log: $(get_active_incus_logfile)"
exit "$exit_code"
}
# ------------------------------------------------------------------------------
# incus_catch_errors()
#
# - Set up error trapping for Incus scripts
# ------------------------------------------------------------------------------
incus_catch_errors() {
set -Eeuo pipefail
trap 'incus_error_handler ${LINENO}' ERR
}
# ==============================================================================
# SECTION 7: COMPATIBILITY ALIASES
# ==============================================================================
#
# These aliases allow existing install scripts to use msg_info/msg_ok/etc
# without modification, by mapping them to incus_* equivalents when running
# in Incus context.
#
# Usage in Incus install scripts:
# source /dev/stdin <<< "$INCUS_FUNCTIONS"
# # After this, msg_info/msg_ok/etc all work as normal
# ==============================================================================
incus_install_compat_aliases() {
# Map standard function names to Incus variants
# Using eval to create function aliases
eval 'msg_info() { incus_msg_info "$@"; }'
eval 'msg_ok() { incus_msg_ok "$@"; }'
eval 'msg_error() { incus_msg_error "$@"; }'
eval 'msg_warn() { incus_msg_warn "$@"; }'
eval 'msg_custom() { incus_msg_custom "$@"; }'
eval 'msg_debug() { incus_msg_debug "$@"; }'
eval 'msg_dev() { incus_msg_dev "$@"; }'
eval 'silent() { incus_silent "$@"; }'
eval 'log_msg() { incus_log_msg "$@"; }'
eval 'get_active_logfile() { get_active_incus_logfile; }'
eval 'ensure_profile_loaded() { incus_ensure_profile_loaded; }'
eval 'catch_errors() { incus_catch_errors; }'
eval 'error_handler() { incus_error_handler "$@"; }'
eval 'fatal() { incus_fatal "$@"; }'
eval 'color() { incus_color; }'
eval 'is_verbose_mode() { incus_is_verbose_mode; }'
eval 'set_std_mode() { incus_set_std_mode; }'
eval 'formatting() { incus_formatting; }'
eval 'icons() { incus_icons; }'
eval 'default_vars() { incus_default_vars; }'
eval 'color_spinner() { incus_color_spinner; }'
eval 'strip_ansi() { incus_strip_ansi "$@"; }'
eval 'log_section() { incus_log_section "$@"; }'
eval 'spinner() { incus_spinner; }'
eval 'clear_line() { incus_clear_line; }'
eval 'stop_spinner() { incus_stop_spinner; }'
eval 'shell_check() { incus_shell_check; }'
eval 'root_check() { incus_root_check; }'
eval 'check() { incus_check; }'
eval 'pve_check() { incus_check; }'
eval 'load_functions() { load_incus_functions; }'
eval 'is_unattended() { incus_is_unattended; }'
eval 'get_storage_pools() { incus_get_storage_pools; }'
eval 'get_networks() { incus_get_networks; }'
eval 'get_profiles() { incus_get_profiles; }'
eval 'get_images() { incus_get_images; }'
eval 'get_os_info() { incus_get_os_info "$@"; }'
eval 'get_ip() { incus_get_ip; }'
eval 'on_hup_keepalive() { :; }'
}
# ==============================================================================
# SECTION 8: NETWORK & CONTAINER UTILITIES
# ==============================================================================
# ------------------------------------------------------------------------------
# incus_container_exists()
#
# - Checks if an Incus container exists
# - Returns 0 if exists, 1 otherwise
# ------------------------------------------------------------------------------
incus_container_exists() {
local name="$1"
incus info "$name" &>/dev/null && return 0 || return 1
}
# ------------------------------------------------------------------------------
# incus_container_running()
#
# - Checks if an Incus container is running
# - Returns 0 if running, 1 otherwise
# ------------------------------------------------------------------------------
incus_container_running() {
local name="$1"
local status
status=$(incus info "$name" 2>/dev/null | awk '/^Status:/ {print $2}') || true
[[ "$status" == "Running" ]]
}
# ------------------------------------------------------------------------------
# incus_get_next_name()
#
# - Generates next available container name from base name
# - Format: <appname>-<increment>
# ------------------------------------------------------------------------------
incus_get_next_name() {
local base="${1:-incus-ct}"
local counter=0
local name="${base}"
while incus_container_exists "$name"; do
counter=$((counter + 1))
name="${base}-${counter}"
done
echo "$name"
}
# ------------------------------------------------------------------------------
# incus_get_image_alias()
#
# - Maps var_os + var_version to incus image alias
# - Example: debian/13 -> "Debian 13 (trixie)"
# ------------------------------------------------------------------------------
incus_get_image_alias() {
local os="${1:-debian}"
local version="${2:-13}"
# Convert to lowercase
os="${os,,}"
case "$os" in
debian)
case "$version" in
12 | bookworm) echo "bookworm" ;;
13 | trixie) echo "trixie" ;;
*) echo "trixie" ;;
esac
;;
ubuntu)
case "$version" in
22.04 | 22 | jammy) echo "jammy" ;;
24.04 | 24 | noble) echo "noble" ;;
26.04 | 26 | plucky | resolute) echo "plucky" ;;
*) echo "noble" ;;
esac
;;
alpine)
case "$version" in
3.19 | 3.20 | 3.21 | 3.22 | 3.23) echo "${version}" ;;
*) echo "3.21" ;;
esac
;;
*)
echo "${version}"
;;
esac
}