-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathinstall.sh
More file actions
2548 lines (2262 loc) · 91.7 KB
/
install.sh
File metadata and controls
2548 lines (2262 loc) · 91.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#!/usr/bin/env bash
# get.cloudstack.cloud/install.sh - Easiest Apache CloudStack Installer
# Install with this command (from your Ubuntu/EL host):
#
# curl -sSfL https://get.cloudstack.cloud/install.sh | bash
#
# Licensed to the Apache Software Foundation (ASF) under one
# or more contributor license agreements. See the NOTICE file
# distributed with this work for additional information
# regarding copyright ownership. The ASF licenses this file
# to you under the Apache License, Version 2.0 (the
# "License"); you may not use this file except in compliance
# with the License. You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing,
# software distributed under the License is distributed on an
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
# KIND, either express or implied. See the License for the
# specific language governing permissions and limitations
# under the License.
set -euo pipefail # Exit on error, undefined vars, pipe failures
# Global variables
SCRIPT_NAME="Apache CloudStack Installer"
CS_LOGFILE="$PWD/installer.log"
TRACKER_FILE="$PWD/cloudstack-installer-tracker.conf"
OS_TYPE=""
PACKAGE_MANAGER=""
SELECTED_COMPONENTS=()
ZONE_TYPE=""
MYSQL_PKG="mysql-server"
MYSQL_SERVICE=""
MYSQL_CONF_DIR=""
BRIDGE=cloudbr0
HOST_IP=
GATEWAY=
DNS="8.8.8.8"
NETMASK="255.255.255.0"
# Colors for output
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
BLUE='\033[0;34m'
NC='\033[0m' # No Color
# 1 = Prompt mode (interactive), 0 = Silent (non-interactive)
PROMPT=1
#-------------------------------------------------------------------------------
# Utility functions
#-------------------------------------------------------------------------------
# checks if prompt mode is enabled
is_interactive() { (( PROMPT )); }
is_silent() { (( !PROMPT )); }
# Log related utilities
#-------------------------------------------------------------------------------
# Clean up ANSI escape sequences from logs
strip_ansi() {
sed 's/\x1b\[[0-9;]*[a-zA-Z]//g'
}
# Logging function
log() {
echo "$(date '+%Y-%m-%d %H:%M:%S') - $1" >> "$CS_LOGFILE"
}
# Error handling function
error_exit() {
echo -e "${RED}ERROR: $1${NC}" >&2
log "ERROR: $1"
exit 1
}
# Success message function
success_msg() {
echo -e "${GREEN}SUCCESS: $1${NC}"
log "SUCCESS: $1"
}
# Warning message function
warn_msg() {
echo -e "${YELLOW}WARNING: $1${NC}"
log "WARNING: $1"
}
# Info message function
info_msg() {
echo -e "${BLUE}INFO: $1${NC}"
log "INFO: $1"
}
# Utilities for resource checks
#-------------------------------------------------------------------------------
# Check if running as root
check_root() {
if [[ $EUID -ne 0 ]]; then
error_exit "This script must be run as root. Please use 'sudo $0'"
fi
}
# Check system resources (RAM and Disk)
check_system_resources() {
MIN_RAM_KB=$((8 * 1024 * 1024)) # 8 GB in KB
TOTAL_RAM_KB=$(grep MemTotal /proc/meminfo | awk '{print $2}')
# Check if RAM is within the desired range
if [ "$TOTAL_RAM_KB" -ge "$MIN_RAM_KB" ]; then
success_msg "✓ RAM check passed: $(awk "BEGIN {printf \"%.2f\", $TOTAL_RAM_KB/1024/1024}") GB"
else
error_exit "RAM check failed: System has $(awk "BEGIN {printf \"%.2f\", $TOTAL_RAM_KB/1024/1024}") GB RAM"
fi
MIN_DISK_GB=75 # Minimum disk space in GB
TOTAL_DISK_GB=$(df / | tail -1 | awk '{print $2}' | awk '{printf "%.0f", $1/1024/1024}')
# Check if disk space is within the desired range
if [ "$TOTAL_DISK_GB" -ge "$MIN_DISK_GB" ]; then
success_msg "✓ Disk space check passed: $TOTAL_DISK_GB GB available"
else
error_exit "Disk space check failed: System has only $TOTAL_DISK_GB GB available"
fi
}
# Ensure kvm modules are loaded for KVM Agent
check_kvm_support() {
info_msg "Checking KVM prerequisites..."
if ! grep -E 'vmx|svm' /proc/cpuinfo >/dev/null; then
error_exit "CPU does not support hardware virtualization (agent)"
fi
success_msg "✓ CPU virtualization support detected"
if grep -q vmx /proc/cpuinfo; then
MODULE="kvm_intel"
elif grep -q svm /proc/cpuinfo; then
MODULE="kvm_amd"
else
error_exit "Unable to determine CPU type for KVM module."
fi
modprobe "$MODULE" 2>/dev/null || true
# Validate loaded module
if ! grep -q "^${MODULE} " /proc/modules; then
error_exit "KVM module $MODULE is not loaded. Ensure virtualization is enabled in BIOS/UEFI."
fi
success_msg "✓ KVM kernel module ($MODULE) loaded"
}
validate_selinux() {
if command -v getenforce >/dev/null 2>&1; then
SELINUX_MODE=$(getenforce)
if [ "$SELINUX_MODE" = "Enforcing" ]; then
msg="SELinux is ENFORCING. Script cannot continue."
msg+="\nFix it by:"
msg+="\n 1) Edit /etc/selinux/config → SELINUX=permissive"
msg+="\n 2) Run: setenforce 0"
msg+="\n 3) Reboot"
error_exit "$msg"
else
success_msg "✓ SELinux status: $SELINUX_MODE"
fi
else
success_msg "✓ SELinux not detected. Continuing..."
fi
}
validate_jre_version() {
if ! command -v java >/dev/null 2>&1; then
success_msg "✓ Java not found, good to go.."
return 0
fi
java_version=$(java -version 2>&1 | awk -F[\"_] 'NR==1{print $2}')
major_version=$(echo "$java_version" | cut -d. -f1)
if (( major_version < 17 )); then
error_exit "Looks like Java is pre-installed and may conflict with CloudStack. Java 17 or higher is required. Detected version: $java_version"
fi
success_msg "✓ Java version $java_version detected, good to go.."
}
# Initialize OS_TYPE, PACKAGE_MANAGER, MYSQL_SERVICE, MYSQL_CONF_DIR
detect_os() {
if [[ ! -f /etc/os-release ]]; then
error_exit "Cannot detect operating system. /etc/os-release not found."
fi
source /etc/os-release
OS_TYPE=$ID
OS_VERSION=$VERSION_ID
OS_MAJOR="${OS_VERSION%%.*}"
VERSION_CODENAME=${VERSION_CODENAME:-}
case "$OS_TYPE" in
ubuntu|debian)
PACKAGE_MANAGER="apt"
MYSQL_SERVICE="mysql"
MYSQL_CONF_DIR="/etc/mysql/mysql.conf.d"
;;
rhel|centos|ol|rocky|almalinux)
PACKAGE_MANAGER="dnf"
MYSQL_SERVICE="mysqld"
MYSQL_CONF_DIR="/etc/my.cnf.d"
if [[ ("$OS_TYPE" == "rocky" || "$OS_TYPE" == "almalinux" || "$OS_TYPE" == "ol") && "$OS_MAJOR" =~ ^(10|11)$ ]]; then
MYSQL_PKG="mysql8.4-server"
fi
validate_selinux
;;
*)
echo "Unsupported OS: $OS_TYPE"
exit 1
;;
esac
log "OS Detection: $OS_TYPE with package manager: $PACKAGE_MANAGER"
}
# Tracker file utilities
#-------------------------------------------------------------------------------
# maintains tracker values for installation and configuration steps
declare -A tracker_values
# loads tracker file data into tracker_values
load_tracker() {
if [[ ! -f "$TRACKER_FILE" ]]; then
echo "# CloudStack Installer Tracker Config" > "$TRACKER_FILE"
echo "# Created on: $(date)" >> "$TRACKER_FILE"
return 0
fi
while IFS='=' read -r key value; do
[[ -z "$key" || "$key" =~ ^# ]] && continue
tracker_values["$key"]="$value"
done < "$TRACKER_FILE"
}
get_tracker_field() {
local key="$1"
echo "${tracker_values[$key]:-}"
}
# Save or update a field in the tracker file
set_tracker_field() {
local key="$1"
local value="$2"
# Update associative array
tracker_values["$key"]="$value"
# Update or append key=value in tracker file
if grep -q "^$key=" "$TRACKER_FILE"; then
sed -i "s|^$key=.*|$key=$value|" "$TRACKER_FILE"
else
echo "$key=$value" >> "$TRACKER_FILE"
fi
}
# checks if a step is already tracked in tracker file
is_step_tracked() {
local key="$1"
[[ -n "${tracker_values[$key]:-}" ]]
}
# Utility functions for Repository setup
#-------------------------------------------------------------------------------
# Map Debian codenames to Ubuntu codenames for CloudStack repo
get_ubuntu_codename_for_debian() {
case "$1" in
buster|bullseye)
echo "focal"
;;
bookworm|trixie)
echo "jammy"
;;
*)
echo "ERROR: Unsupported Debian codename '$1'" >&2
return 1
;;
esac
}
# Determine repo_path for CloudStack repository based on OS type and version
determine_rpm_distro_version() {
# Extract major version (8 or 9) from version string
local major_version=${OS_VERSION%%.*}
case "$OS_TYPE" in
centos)
echo "centos/$major_version"
;;
rhel)
echo "rhel/$major_version"
;;
rocky|almalinux|ol)
echo "el/$major_version"
;;
*)
error_exit "Unsupported OS type: $OS_TYPE"
;;
esac
}
# Validates repository entry format
validate_repo_entry() {
local os_type="$1"
local entry="$2"
# Basic check: not empty
if [[ -z "$entry" ]]; then
error_exit "CloudStack Repository entry cannot be empty."
return 1
fi
# Debian/Ubuntu repo line example:
# deb [signed-by=...] https://download.cloudstack.org/ubuntu noble 4.20
if [[ "$os_type" =~ ^(ubuntu|debian)$ ]]; then
if [[ ! "$entry" =~ https?:// ]]; then
error_exit "Invalid Repository entry must include a valid URL (http or https)."
return 1
fi
fi
# RHEL-family example:
# https://download.cloudstack.org/centos/9/4.20/
if [[ "$os_type" =~ ^(rhel|centos|rocky|almalinux|ol)$ ]]; then
if [[ ! "$entry" =~ ^https?:// ]]; then
error_exit "Invalid Repository baseurl must start with http:// or https://."
return 1
fi
fi
# Optional: check version (warn, not fatal)
if [[ ! "$entry" =~ 4\.([1-9][0-9]) ]]; then
dialog --backtitle "$SCRIPT_NAME" \
--title "Warning" \
--msgbox "The repository entry does not appear to contain a known CloudStack version (4.xx). Please verify before proceeding." 8 70
fi
return 0
}
# Update system packages once repository is configured
update_system_packages() {
local percent=1
{
case "$PACKAGE_MANAGER" in
apt)
apt-get update 2>&1 | while IFS= read -r line; do
percent=$((percent + 1))
[ $percent -gt 50 ] && percent=50
update_progress_bar "$percent" "# Updating package lists...\n\n$line"
done
apt-get upgrade -y 2>&1 | while IFS= read -r line; do
percent=$((percent + 1))
[ $percent -gt 80 ] && percent=80
update_progress_bar "$percent" "# Installing updates...\n\n$line"
done
;;
dnf)
dnf clean all 2>&1 | while IFS= read -r line; do
percent=$((percent + 1))
[ $percent -gt 20 ] && percent=20
update_progress_bar "$percent" "# Cleaning package cache...\n\n$line"
done
dnf makecache 2>&1 | while IFS= read -r line; do
percent=$((percent + 1))
[ $percent -gt 60 ] && percent=60
update_progress_bar "$percent" "# Updating package cache...\n\n$line"
done
dnf update -y 2>&1 | while IFS= read -r line; do
percent=$((percent + 1))
[ $percent -gt 80 ] && percent=80
update_progress_bar "$percent" "# Installing system updates...\n\n$line"
done
;;
esac
update_progress_bar "100" "# System update complete!"
sleep 2
} | dialog --backtitle "$SCRIPT_NAME" \
--title "System Update" \
--gauge "Updating system packages..." 15 80 0
# Verify update success
if [[ $? -eq 0 ]]; then
show_dialog info "System Update" "System packages have been successfully updated." 2
else
dialog --backtitle "$SCRIPT_NAME" \
--title "Error" \
--msgbox "Failed to update system packages. Please check the logs." 6 50
return 1
fi
}
# Installation related utilities
#-------------------------------------------------------------------------------
is_package_installed() {
local pkgs=("$@") # Accept multiple packages
local pkg
for pkg in "${pkgs[@]}"; do
case "$PACKAGE_MANAGER" in
apt)
if ! dpkg -s "$pkg" &>/dev/null; then
return 1 # one package missing -> not installed
fi
;;
dnf)
if ! dnf list installed "$pkg" &>/dev/null; then
return 1
fi
;;
esac
done
return 0 # all packages installed
}
install_package() {
local packages=("$@") # Capture all arguments (1..N)
case "$PACKAGE_MANAGER" in
apt)
DEBIAN_FRONTEND=noninteractive apt-get install -y "${packages[@]}"
;;
dnf)
dnf install -y "${packages[@]}"
;;
*)
echo "Unsupported package manager: $PACKAGE_MANAGER" >&2
return 1
;;
esac
}
# Generic function, performs package installation with progress dialog
install_pkg_with_progress_bar() {
local title="$1"
local package_name="$2"
local tracker_key="$3"
# Skip if already configured
if is_step_tracked "$tracker_key"; then
log "$title is already installed. Skipping."
show_dialog "info" "$title Installation" "$title is already installed. Skipping installation."
return 0
fi
# Skip if package already installed
if is_package_installed "$package_name"; then
log "$title package already present. Skipping installation."
set_tracker_field "$tracker_key" "yes"
show_dialog "info" "$title Installation" "$title package already present. Skipping installation."
return 0
fi
log "Installing $title..."
# Temporary log file
local TMP_LOG
TMP_LOG=$(mktemp /tmp/install_${tracker_key}.XXXXXX.log)
# Start installation in the background
install_package $package_name >> "$TMP_LOG" 2>&1 &
local INSTALL_PID=$!
local percent=0
local start_msg="Installing $title..."
{
update_progress_bar "$percent" "# $start_msg"
while kill -0 "$INSTALL_PID" 2>/dev/null; do
local tail_output
tail_output=$(tail -n 5 "$TMP_LOG" | strip_ansi | tr -d '\r')
# Add left padding, truncate width, and wrap safely
tail_output=$(echo "$tail_output" \
| sed 's/^/ /' \
| fold -s -w 80 \
| tail -n 5)
echo "$percent"
echo "XXX"
echo "# $start_msg"
echo
echo "$tail_output"
echo "XXX"
percent=$((percent + 1))
[ $percent -gt 90 ] && percent=90
sleep 1
done
} | dialog --backtitle "$SCRIPT_NAME" --title "$title Installation" --gauge "Installing $title..." 15 80 0
wait "$INSTALL_PID"
local status=$?
if [ $status -eq 0 ]; then
set_tracker_field "$tracker_key" "yes"
log "$title installed successfully."
rm -f "$TMP_LOG"
return 0
else
log "Failed to install $title. Check $TMP_LOG"
error_exit "Failed to install $title."
fi
}
# function to update progress bar in the dialog
update_progress_bar() {
local percent="$1"
local msg="$2"
echo "XXX"
echo "$percent"
echo -e "$msg"
echo "XXX"
}
# utility function to display dialog for info or message purpose
show_dialog() {
local mode="$1"
local title="$2"
local msg="$3"
local seconds="${4:-3}"
local height="${5:-7}"
local width="${6:-60}"
case "$mode" in
info)
dialog --backtitle "$SCRIPT_NAME" \
--title "$title" \
--infobox "$msg" $height $width
sleep "$seconds"
return 0
;;
msg)
dialog --backtitle "$SCRIPT_NAME" \
--title "$title" \
--msgbox "$msg" $height $width
return 0
;;
*)
echo "Unknown mode: $mode"
return 1
;;
esac
}
# Attempts to start service_name
start_service_with_progress() {
local service_name="$1"
local display_name="${2:-$service_name}"
local start_percent="${3:-60}"
local end_percent="${4:-90}"
local verify="${5:-verify}"
if ! systemctl is-active --quiet "$service_name"; then
{
update_progress_bar "$start_percent" "# Starting $display_name service..."
systemctl start "$service_name" >/dev/null 2>&1 &
local pid=$!
local i
for ((i=start_percent; i<=end_percent; i++)); do
update_progress_bar "$i" "# Waiting for $display_name to start..."
sleep 0.2
done
wait "$pid"
update_progress_bar "$end_percent" "# $display_name service started successfully."
sleep 1
} | dialog --backtitle "$SCRIPT_NAME" \
--title "Starting $display_name Service" \
--gauge "Starting $display_name service..." 15 70 0
systemctl enable "$service_name" >/dev/null 2>&1
if [[ "$verify" == "verify" ]]; then
if systemctl is-active --quiet "$service_name"; then
log "$display_name service started successfully."
else
error_exit "Failed to start $display_name service."
fi
else
log "$display_name service started (verification skipped)."
fi
else
log "$display_name service is already running."
fi
}
# CloudStack Banner gets displayed at the end of Zone Deployment
show_cloudstack_banner() {
local banner="
█████████████████████████████████████████████████████████████
█─▄▄▄─█▄─▄███─▄▄─█▄─██─▄█▄─▄▄▀█─▄▄▄▄█─▄─▄─██▀▄─██─▄▄▄─█▄─█─▄█
█─███▀██─██▀█─██─██─██─███─██─█▄▄▄▄─███─████─▀─██─███▀██─▄▀██
▀▄▄▄▄▄▀▄▄▄▄▄▀▄▄▄▄▀▀▄▄▄▄▀▀▄▄▄▄▀▀▄▄▄▄▄▀▀▄▄▄▀▀▄▄▀▄▄▀▄▄▄▄▄▀▄▄▀▄▄▀
CloudStack Installation Complete!
--------------------------------
Access Details:
URL: http://$HOST_IP:8080/client
Username: admin
Password: password
Note: Please change the default password after first login.
"
log "$banner"
dialog --backtitle "$SCRIPT_NAME" \
--title "Installation Complete" \
--colors \
--msgbox "\Z1$banner\Zn" 20 70
}
# Find free IP addresses in a given network range
find_free_ip_range() {
local network=$1
local start_from=$2
local count=$3
local tempfile=$(mktemp)
# Scan network to find used IPs
nmap -sn -n "$network" | grep -oE '[0-9]+\.[0-9]+\.[0-9]+\.[0-9]+' > "$tempfile"
# Get base network
local base_net="${network%.*}"
local free_ips=()
local current=$start_from
while [[ ${#free_ips[@]} -lt $count && $current -lt 255 ]]; do
if ! grep -q "$base_net.$current" "$tempfile"; then
free_ips+=("$base_net.$current")
fi
((current++))
done
rm -f "$tempfile"
echo "${free_ips[@]}"
}
#-------------------------------------------------------------------------------
# Script specific functions
#-------------------------------------------------------------------------------
is_cloudstack_pkg_selected() {
if [[ " ${SELECTED_COMPONENTS[@]} " =~ " management " || \
" ${SELECTED_COMPONENTS[@]} " =~ " agent " || \
" ${SELECTED_COMPONENTS[@]} " =~ " usage " ]]; then
return 0 # true
else
return 1 # false
fi
}
# Configure CloudStack repository for Debian/Ubuntu
#-------------------------------------------------------------------------------
_configure_deb_repo() {
local gpg_key_url="$1"
local repo_entry="$2"
{
echo "Configuring CloudStack repository..."
echo "Adding CloudStack's signing key..."
mkdir -p /etc/apt/keyrings
if curl -fsSL "$gpg_key_url" | gpg --dearmor | sudo tee /etc/apt/keyrings/cloudstack.gpg > /dev/null; then
echo "CloudStack signing key added successfully."
else
error_exit "Failed to add CloudStack signing key."
fi
echo "Adding CloudStack repository..."
if echo "deb [signed-by=/etc/apt/keyrings/cloudstack.gpg] $repo_entry" | sudo tee /etc/apt/sources.list.d/cloudstack.list > /dev/null; then
echo "CloudStack repository added successfully."
else
error_exit "Failed to add CloudStack repository."
fi
} | dialog --backtitle "$SCRIPT_NAME" \
--title "Repository Configuration" \
--programbox "Configuring CloudStack repository..." 15 70
}
_configure_rpm_repo () {
local gpg_key_url="$1"
local repo_entry="$2"
mkdir -p /etc/yum.repos.d/
{
echo "Adding CloudStack repository..."
if cat > /etc/yum.repos.d/cloudstack.repo <<EOF
[cloudstack]
name=CloudStack
baseurl=$repo_entry
enabled=1
gpgcheck=0
gpgkey=$gpg_key_url
EOF
then
echo "Repository added successfully"
else
error_exit "Failed to create CloudStack repository file"
fi
} | dialog --backtitle "$SCRIPT_NAME" \
--title "Repository Configuration" \
--programbox "Configuring CloudStack repository..." 15 70
}
configure_cloudstack_repo() {
local title="CloudStack Repository Configuration"
local tracker_key="repo_url"
local repo_file=""
local repo_entry=""
case "$OS_TYPE" in
ubuntu|debian)
repo_file="/etc/apt/sources.list.d/cloudstack.list"
if [[ -f "$repo_file" ]]; then
repo_entry=$(grep -E '^deb ' "$repo_file" | sed -E 's/^.*] //')
fi
;;
rhel|centos|ol|rocky|almalinux)
repo_file="/etc/yum.repos.d/cloudstack.repo"
if [[ -f "$repo_file" ]]; then
repo_entry=$(grep -E '^baseurl=' "$repo_file" | cut -d'=' -f2-)
fi
;;
*)
dialog --msgbox "Unsupported OS: $OS_TYPE" 6 50
exit 1
;;
esac
# If repo already exists, show info and exit gracefully for silent mode
# allow reconfiguration for interactive mode
if [[ -n "$repo_entry" ]]; then
if is_silent; then
show_dialog "info" \
"$title" \
"CloudStack repository is already configured:\n\n$repo_entry"
set_tracker_field "$tracker_key" "$repo_entry"
return 0
fi
if is_interactive; then
if dialog --backtitle "$SCRIPT_NAME" \
--title "$title" \
--yesno "CloudStack repository is already configured:\n\n$repo_entry\n\nDo you want to reconfigure it?" 12 70; then
log "User opted to reconfigure existing CloudStack repository."
else
show_dialog "info" "$title" "Skipping CloudStack repository configuration."
set_tracker_field "$tracker_key" "$repo_entry"
return 0
fi
fi
fi
# default repo_entry is required if repo_entry is not set
if [[ -z "$repo_entry" ]]; then
# Set default repo_entry based on OS
local default_repo_url="https://download.cloudstack.org"
local default_cs_version="4.21"
# Build default repo_entry depending on distro
case "$OS_TYPE" in
ubuntu|debian)
local ubuntu_codename="$VERSION_CODENAME"
if [[ "$OS_TYPE" == "debian" ]]; then
ubuntu_codename=$(get_ubuntu_codename_for_debian "$VERSION_CODENAME") || exit 1
fi
default_repo_entry="${default_repo_url}/ubuntu $ubuntu_codename $default_cs_version"
;;
rhel|centos|ol|rocky|almalinux)
local repo_path
repo_path=$(determine_rpm_distro_version)
default_repo_entry="${default_repo_url}/${repo_path}/${default_cs_version}/"
;;
esac
repo_entry="$default_repo_entry"
fi
if is_interactive; then
width=60
prompt_text="Enter the CloudStack repository url:"
if [[ "$OS_TYPE" =~ ^(ubuntu|debian)$ ]]; then
prompt_text="Enter the CloudStack repository url.\n\nSupported formats:\n• Ubuntu-style (deb ... ubuntu codename version)\n• Flat layout (deb ... /)\nExample: deb [signed-by=...] http://packages.shapeblue.com/cloudstack/upstream/debian/4.21/ /"
width=90
fi
height=$(( $(echo -e "$prompt_text" | wc -l) + 8 ))
repo_entry=$(dialog --clear \
--backtitle "$SCRIPT_NAME" \
--title "Configure CloudStack Repository" \
--inputbox "$prompt_text" "$height" "$width" "$repo_entry" \
3>&1 1>&2 2>&3)
validate_repo_entry "$OS_TYPE" "$repo_entry" || {
error_exit "Invalid repository entry provided by user."
}
fi
local repo_base_url=$(echo "$repo_entry" | sed -E 's|.*(https?://[^/ ]+).*|\1|')
local gpg_url="${repo_base_url}/release.asc"
if is_interactive; then
if ! dialog --backtitle "$SCRIPT_NAME" \
--title "Confirm Repository" \
--yesno "The following CloudStack repository will be added:\n\n$repo_entry\n\nProceed?" 12 70; then
error_exit "CloudStack repository configuration cancelled by user."
fi
else
show_dialog "info" "$title" "The following CloudStack repository will be added:\n\n$repo_entry" 4
fi
log "Configuring CS repo: $repo_entry"
case "$OS_TYPE" in
ubuntu|debian)
_configure_deb_repo "$gpg_url" "$repo_entry"
;;
rhel|centos|ol|rocky|almalinux)
_configure_rpm_repo "$gpg_url" "$repo_entry"
;;
*)
dialog --msgbox "Unsupported OS: $OS_TYPE" 6 50
error_exit "Unsupported OS: $OS_TYPE"
;;
esac
log "Configured CS repo: $repo_entry"
set_tracker_field "$tracker_key" "$repo_entry"
}
# Install base dependencies required for CloudStack
#-------------------------------------------------------------------------------
install_dialog_utility() {
log "Updating package list..."
case "$PACKAGE_MANAGER" in
apt)
apt-get update || error_exit "Failed to update package lists"
;;
dnf)
dnf makecache || error_exit "Failed to update package cache"
;;
esac
log "Installing 'dialog'..."
case "$PACKAGE_MANAGER" in
apt)
apt-get install -y dialog || error_exit "Failed to install dialog"
;;
dnf)
dnf install -y dialog || error_exit "Failed to install dialog"
;;
esac
}
update_system_time() {
case $PACKAGE_MANAGER in
dnf)
if ! systemctl is-active chronyd >/dev/null 2>&1; then
systemctl enable --now chronyd >/dev/null 2>&1
fi
chronyc makestep >/dev/null 2>&1
;;
apt)
# Detect what NTP mechanism is available
if systemctl is-active ntp >/dev/null 2>&1; then
info_msg ">>> ntp detected, forcing sync via ntpd..."
ntpd -gq >/dev/null 2>&1 || true
elif systemctl is-active chrony >/dev/null 2>&1; then
info_msg ">>> chrony detected, forcing sync via chronyc..."
chronyc makestep >/dev/null 2>&1 || true
else
info_msg ">>> Falling back to systemd-timesyncd..."
systemctl enable --now systemd-timesyncd >/dev/null 2>&1 || true
timedatectl set-ntp true >/dev/null 2>&1 || true
systemctl restart systemd-timesyncd >/dev/null 2>&1 || true
fi
;;
esac
}
install_base_dependencies() {
log "Starting base dependencies installation..."
if ! command -v dialog &>/dev/null; then
install_dialog_utility
fi
TMP_LOG=$(mktemp /tmp/install_base.XXXXXX.log)
title="Installing base dependencies (qemu-kvm, python, curl, etc.)..."
{
update_progress_bar "30" "$title"
case "$PACKAGE_MANAGER" in
apt)
DEBIAN_FRONTEND=noninteractive \
apt-get install -y qemu-kvm apt-utils gnupg curl openntpd openssh-server sshpass sudo wget jq htop tar nmap bridge-utils util-linux >> "$TMP_LOG" 2>&1 &
;;
dnf)
dnf install -y curl openssh-server chrony sshpass sudo wget jq tar nmap util-linux >> "$TMP_LOG" 2>&1 &
;;
esac
INSTALL_PID=$!
PERCENT=31
while kill -0 "$INSTALL_PID" 2>/dev/null; do
tail_output=$(tail -n 5 "$TMP_LOG" | strip_ansi | tr -d '\r')
# Add left padding, truncate width, and wrap safely
tail_output=$(echo "$tail_output" \
| sed 's/^/ /' \
| fold -s -w 80 \
| tail -n 5)
update_progress_bar "$PERCENT" "$title\n\n$tail_output"
PERCENT=$((PERCENT + 1))
[ "$PERCENT" -ge 90 ] && PERCENT=90
sleep 1
done
wait "$INSTALL_PID" || error_exit "Base dependency installation failed"
update_progress_bar "100" "Base dependencies installed successfully"
} | dialog --backtitle "$SCRIPT_NAME" \
--title "Installing Dependencies" \
--gauge "Preparing system..." 15 80 0
show_dialog "info" "Dependencies Installation" "All Base dependencies installed successfully"
log "Base dependencies installed successfully"
rm -f "$TMP_LOG"
}
# Function to show available component versions from repository for SELECTED_COMPONENTS
show_components_versions() {
local versions=()
local component version_info
if [[ " ${SELECTED_COMPONENTS[*]} " =~ " management " ]] && [[ ! " ${SELECTED_COMPONENTS[*]} " =~ " mysql " ]]; then
SELECTED_COMPONENTS+=("mysql")
fi
for component in "${SELECTED_COMPONENTS[@]}"; do
case "$component" in
nfs)
if [[ "$PACKAGE_MANAGER" == "apt" ]]; then
version_info=$(apt-cache policy nfs-kernel-server 2>/dev/null | awk '/Candidate:/ {print $2}')
elif [[ "$PACKAGE_MANAGER" == "dnf" ]]; then
version_info=$($PACKAGE_MANAGER info nfs-utils 2>/dev/null | awk -F':' '/Version/ {gsub(/ /,"",$2); print $2}')
fi
versions+=("NFS Server: ${version_info:-Not Available}\n")
;;
mysql)
if [[ "$PACKAGE_MANAGER" == "apt" ]]; then
version_info=$(apt-cache policy "$MYSQL_PKG" 2>/dev/null | awk '/Candidate:/ {print $2}')
elif [[ "$PACKAGE_MANAGER" == "dnf" || "$PACKAGE_MANAGER" == "yum" ]]; then
version_info=$($PACKAGE_MANAGER info "$MYSQL_PKG" 2>/dev/null | awk -F':' '/Version/ {gsub(/ /,"",$2); print $2}')
fi
versions+=("MySQL Server: ${version_info:-Not Available}\n")
;;
management)
if [[ "$PACKAGE_MANAGER" == "apt" ]]; then
version_info=$(apt-cache policy cloudstack-management 2>/dev/null | awk '/Candidate:/ {print $2}')
elif [[ "$PACKAGE_MANAGER" == "dnf" ]]; then
version_info=$($PACKAGE_MANAGER info cloudstack-management 2>/dev/null | awk -F':' '/Version/ {gsub(/ /,"",$2); print $2}')
fi
versions+=("CloudStack Management Server: ${version_info:-Not Available}\n")
;;
agent)
if [[ "$PACKAGE_MANAGER" == "apt" ]]; then
version_info=$(apt-cache policy cloudstack-agent 2>/dev/null | awk '/Candidate:/ {print $2}')
elif [[ "$PACKAGE_MANAGER" == "dnf" ]]; then
version_info=$($PACKAGE_MANAGER info cloudstack-agent 2>/dev/null | awk -F':' '/Version/ {gsub(/ /,"",$2); print $2}')
fi
versions+=("CloudStack KVM Agent: ${version_info:-Not Available}\n")
;;
usage)
if [[ "$PACKAGE_MANAGER" == "apt" ]]; then
version_info=$(apt-cache policy cloudstack-usage 2>/dev/null | awk '/Candidate:/ {print $2}')
elif [[ "$PACKAGE_MANAGER" == "dnf" ]]; then
version_info=$($PACKAGE_MANAGER info cloudstack-usage 2>/dev/null | awk -F':' '/Version/ {gsub(/ /,"",$2); print $2}')
fi
versions+=("CloudStack Usage Server: ${version_info:-Not Available}\n")
;;
esac
done
show_dialog info "Component Versions" "Available versions from repository:\n\n$(printf '%s\n' "${versions[@]}")" 6 10 55