sudo87 commented on code in PR #1: URL: https://github.com/apache/cloudstack-installer/pull/1#discussion_r2472262046
########## installer.sh: ########## @@ -0,0 +1,2315 @@ +#!/usr/bin/env bash +# c8k.in/installer.sh - Easiest Apache CloudStack Installer +# Install with this command (from your Ubuntu/EL host): +# +# curl -sSfL https://c8k.in/stall.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="CloudStack Installer" +CS_LOGFILE="/tmp/cloudstack-install.log" +TRACKER_FILE="$HOME/cloudstack-installer-tracker.conf" + +OS_TYPE="" +PACKAGE_MANAGER="" +SELECTED_COMPONENTS=() +ZONE_TYPE="" +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 +is_interactive() { (( PROMPT )); } +is_silent() { (( !PROMPT )); } + +# 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" +} + + +declare -A 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]:-}" +} + +is_configured() { + local key="$1" + [[ -n "${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.bak "s|^$key=.*|$key=$value|" "$TRACKER_FILE" + else + echo "$key=$value" >> "$TRACKER_FILE" + fi +} + +# 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() { + 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 +} + +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 ! lsmod | grep -q kvm; then + error_exit "KVM kernel module is not loaded" + fi + success_msg "✓ KVM kernel module loaded" +} + +# Function to detect the OS type and pkg mgr +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 + 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" + ;; + *) + echo "Unsupported OS: $OS_TYPE" + exit 1 + ;; + esac + + log "OS Detection: $OS_TYPE with package manager: $PACKAGE_MANAGER" +} + +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 +} + +update_system() { + { + echo "10" + echo "# Updating package cache..." + case "$PACKAGE_MANAGER" in + apt) + apt-get update 2>&1 | while IFS= read -r line; do + echo "XXX" + echo "30" + echo "# Updating package lists...\n\n$line" + echo "XXX" + done + + echo "# Installing system updates..." + apt-get upgrade -y 2>&1 | while IFS= read -r line; do + echo "XXX" + echo "75" + echo "# Installing updates...\n\n$line" + echo "XXX" + done + ;; + dnf) + dnf clean all 2>&1 | while IFS= read -r line; do + echo "XXX" + echo "20" + echo "# Cleaning package cache...\n\n$line" + echo "XXX" + done + + dnf makecache 2>&1 | while IFS= read -r line; do + echo "XXX" + echo "40" + echo "# Updating package cache...\n\n$line" + echo "XXX" + done + + dnf update -y 2>&1 | while IFS= read -r line; do + echo "XXX" + echo "75" + echo "# Installing system updates...\n\n$line" + echo "XXX" + done + ;; + esac + + echo "XXX" + echo "100" + echo "# System update complete!" + echo "XXX" + sleep 2 + } | dialog --backtitle "$SCRIPT_NAME" \ + --title "System Update" \ + --gauge "Updating system packages..." 15 70 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 +} + +normalize_repo_url_path() { + local url="$1" + # Remove duplicate slashes, then fix protocol + url=$(echo "$url" | sed 's#//*#/#g; s#https:/#https://#g; s#http:/#http://#g') + # Trim any trailing slash + url="${url%/}" + echo "$url" +} + +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 +} + +configure_cloudstack_repo() { + 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 + if [[ -n "$repo_entry" ]]; then + show_dialog "info" \ + "CloudStack Repository Configuration" \ + "CloudStack repository is already configured:\n\n$repo_entry" + + set_tracker_field "repo_url" "$repo_entry" + return 0 + fi + + 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" + 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 ! 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 + + 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 "repo_url" "$repo_entry" +} + +_configure_deb_repo() { + local gpg_key_url="$1" + local repo_entry="$2" + { + echo "Configuring CloudStack repository..." + echo "Adding CloudStack's signing key..." + + 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" + { + 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 +} + +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 +} + +preinstall_dialog() { + 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 +} + +strip_ansi() { + sed 's/\x1b\[[0-9;]*[a-zA-Z]//g' +} + +install_base_dependencies() { + log "Starting base dependencies installation..." + if ! command -v dialog &>/dev/null; then + preinstall_dialog + fi + + TMP_LOG=$(mktemp /tmp/install_base.XXXXXX.log) + title="Installing base dependencies (qemu-kvm, python, curl, etc.)..." + { + echo "XXX" + echo "30" + echo $title + echo "XXX" + + PERCENT=31 + case "$PACKAGE_MANAGER" in + apt) + DEBIAN_FRONTEND=noninteractive \ + apt-get install -y qemu-kvm apt-utils 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 + echo "XXX" + echo "$PERCENT" + tail_output=$(tail -n 3 "$TMP_LOG" | strip_ansi | tr '\n' ' ' | cut -c -200) + echo "$title\n\n$tail_output" + echo "XXX" + PERCENT=$((PERCENT + 1)) + [ "$PERCENT" -ge 90 ] && PERCENT=90 + sleep 1 + done + + wait "$INSTALL_PID" || error_exit "Base dependency installation failed" + echo "XXX" + echo "100" + echo "Base dependencies installed successfully" + echo "XXX" + + } | dialog --backtitle "$SCRIPT_NAME" \ + --title "Installing Dependencies" \ + --gauge "Preparing system..." 15 70 0 + show_dialog "info" "Dependencies Installation" "All Base dependencies installed successfully" + log "Base dependencies installed successfully" + + rm -f "$TMP_LOG" +} + +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 +} + + + +# Function to install CloudStack Management Server +install_management_server() { + local package_name="cloudstack-management" + local tracker_key="management_installed" + if is_configured "$tracker_key"; then + log "CloudStack Management Server is already installed. Skipping installation." + return 0 + fi + if is_package_installed "$package_name"; then + log "CloudStack Management Server is already installed." + set_tracker_field "$tracker_key" "yes" + return 0 + fi + install_with_progress "CloudStack Management Server" "$package_name" "$tracker_key" +} + +# Function to install CloudStack Usage Server +install_usage_server() { + local package_name="cloudstack-usage" + local tracker_key="usage_installed" + if is_configured "$tracker_key"; then + log "CloudStack Usage Server is already installed. Skipping installation." + return 0 + fi + if is_package_installed "$package_name"; then + log "CloudStack Usage Server is already installed." + set_tracker_field "$tracker_key" "yes" + return 0 + fi + + # check if mysql and management server are installed + if ! is_package_installed "cloudstack-management" "mysql-server"; then + error_exit "CloudStack Management Server and MySQL Server must be installed before installing the Usage Server." + fi + + install_with_progress "CloudStack Usage Server" "$package_name" "$tracker_key" +} + +# Function to install KVM Agent +install_kvm_agent() { + local package_name="cloudstack-agent" + local tracker_key="agent_installed" + if is_configured "$tracker_key"; then + log "KVM Agent is already installed. Skipping installation." + return 0 + fi + if is_package_installed "$package_name"; then + log "KVM Agent is already installed." + set_tracker_field "$tracker_key" "yes" + return 0 + fi + install_with_progress "CloudStack Agent" "$package_name" "$tracker_key" +} + +install_mysql_server() { + local package_name="mysql-server" + local tracker_key="mysql_installed" + if is_configured "$tracker_key"; then + log "MySQL is already installed. Skipping installation." + return 0 + fi + + if is_package_installed "$package_name"; then + log "MySQL Server is already installed." + set_tracker_field "$tracker_key" "yes" + return 0 + fi + install_with_progress "MySQL Server" "$package_name" "$tracker_key" + # Ensure MySQL service is running + if ! systemctl is-active --quiet "$MYSQL_SERVICE"; then + log "Starting $MYSQL_SERVICE service..." + { + echo "50" + echo "XXX" + echo "# Starting MySQL service..." + echo "XXX" + } | dialog --backtitle "$SCRIPT_NAME" --title "Installing MySQL" --gauge "Installing MySQL..." 15 70 0 + + systemctl enable --now "$MYSQL_SERVICE" >/dev/null 2>&1 + if systemctl is-active --quiet "$MYSQL_SERVICE"; then + log "MySQL service started successfully." + else + error_exit "Failed to start MySQL service." + fi + fi +} + +install_with_progress() { + local title="$1" + local package_name="$2" + local tracker_key="$3" + + # Skip if already configured + if is_configured "$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..." + + { + echo "$percent" + echo "XXX" + echo "# $start_msg" + echo "XXX" + + 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 70 \ + | 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 90 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 install NFS Server +install_nfs_server() { + local tracker_key="nfs_installed" + if is_configured "$tracker_key"; then + log "NFS Server is already installed. Skipping installation." + show_dialog "info" "NFS Server Installation" "NFS Server is already installed. Skipping installation." + return 0 + fi + + if command -v exportfs &>/dev/null; then + log "NFS Server is already installed." + set_tracker_field "$tracker_key" "yes" + show_dialog "info" "NFS Server Installation" "NFS Server is already installed. Skipping installation." + return 0 + fi + + local package_name="" + case "$PACKAGE_MANAGER" in + apt) + package_name="nfs-kernel-server nfs-common quota" + ;; + dnf) + package_name="nfs-utils quota" + ;; + esac + install_with_progress "NFS Server" "$package_name" "$tracker_key" +} + +select_components() { + local temp_file=$(mktemp) + + dialog --clear --backtitle "$SCRIPT_NAME" \ + --title "Component Selection" \ + --checklist "Select CloudStack components to install:" 15 70 6 \ + "management" "CloudStack Management Server" on \ + "usage" "CloudStack Usage Server" off \ + "agent" "KVM Agent" on \ + "nfs" "NFS Server" on \ + 2> "$temp_file" + + if [[ $? -ne 0 ]]; then + rm -f "$temp_file" + error_exit "Component selection cancelled by user" + fi + + # Read selected components + mapfile -t SELECTED_COMPONENTS < <(tr ' ' '\n' < "$temp_file" | tr -d '"') + rm -f "$temp_file" + + if [[ ${#SELECTED_COMPONENTS[@]} -eq 0 ]]; then + error_exit "No components selected" + fi + + log "Selected components: ${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-server 2>/dev/null | awk '/Candidate:/ {print $2}') + elif [[ "$PACKAGE_MANAGER" == "dnf" || "$PACKAGE_MANAGER" == "yum" ]]; then + version_info=$($PACKAGE_MANAGER info mysql-server 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 60 +} + +configure_mysql_for_cloudstack() { + local tracker_key="mysql_configured" + if is_configured "$tracker_key"; then + log "MySQL is already configured for CloudStack. Skipping configuration." + return 0 + fi + log "Starting MySQL configuration..." + local title="MySQL Configuration" + MYSQL_VERSION=$(mysql -V 2>/dev/null || echo "MySQL not found") + if [[ "$MYSQL_VERSION" == "MySQL not found" ]]; then + show_dialog "msg" "$title" "MySQL is not installed. Please install MySQL first." + return 1 + fi + + local config_file="$MYSQL_CONF_DIR/cloudstack.cnf" + if [[ -f "$config_file" ]]; then + show_dialog "info" "$title" "Configuration already exists at:\n$config_file\nSkipping MySQL setup." + set_tracker_field "$tracker_key" "yes" + return 0 + fi + + mkdir -p "$MYSQL_CONF_DIR" + + if ! systemctl is-active --quiet $MYSQL_SERVICE; then + dialog --title "$title" --msgbox "MySQL service is not running. Please start MySQL before proceeding." 6 50 + return 1 + fi + + sqlmode="$(mysql -B -e "show global variables like 'sql_mode'" 2>/dev/null | grep sql_mode | awk '{ print $2; }' | sed -e 's/ONLY_FULL_GROUP_BY,//')" + + if [[ -z "$sqlmode" ]]; then + dialog --msgbox "Failed to fetch current SQL mode. Aborting." 6 50 + return 1 + fi + + cat > "$config_file" <<EOF +[mysqld] +server_id = 1 +sql_mode = $sqlmode +innodb_rollback_on_timeout = 1 +innodb_lock_wait_timeout = 600 +max_connections = 1000 +log_bin = mysql-bin +binlog_format = ROW +EOF + + systemctl restart $MYSQL_SERVICE && \ + show_dialog "info" "$title" "MySQL has been configured and restarted successfully."|| \ + show_dialog "info" "$title" "Failed to restart MySQL. Please check the service manually." + set_tracker_field "$tracker_key" "yes" +} + +get_local_cidr() { + local_ip=$(ip -4 addr show scope global | grep inet | awk '{print $2}' | head -n1) + echo "$local_ip" # e.g., 10.1.1.53/24 +} + + +get_export_cidr() { + ip_cidr=$(get_local_cidr) + # Convert from 10.1.1.53/24 → 10.1.1.0/24 + base_ip=$(echo "$ip_cidr" | cut -d/ -f1) + prefix=$(echo "$ip_cidr" | cut -d/ -f2) + + # Use ipcalc or manual mask conversion + if command -v ipcalc &>/dev/null; then + network=$(ipcalc "$base_ip/$prefix" | grep -w 'Network' | awk '{print $2}') + else + # Fallback: simple /24 assumption + network="${base_ip%.*}.0/$prefix" + fi + echo "$network" +} + + + +configure_nfs_server() { + local tracker_key="nfs_configured" + local title="NFS Storage Configuration" + if is_configured "$tracker_key"; then + log "NFS storage is already configured. Skipping setup." + return 0 + fi + log "Starting NFS storage configuration..." + + if [[ -d "/export" ]] && grep -q "^/export " /etc/exports; then + show_dialog "info" "$title" "NFS is already configured. Skipping setup." + set_tracker_field "$tracker_key" "yes" + return 0 + fi + + local export_cidr + export_cidr=$(get_export_cidr) + # Step 1: Create exports and directories + mkdir -p /export/primary /export/secondary + if ! grep -q "^/export " /etc/exports; then + echo "/export ${export_cidr}(rw,async,no_root_squash,no_subtree_check)" >> /etc/exports + fi + + exportfs -a + + # Step 2: Configure ports and services based on distro + if [[ "$OS_TYPE" =~ ^(ubuntu|debian)$ ]]; then + sed -i -e 's/^RPCMOUNTDOPTS="--manage-gids"$/RPCMOUNTDOPTS="-p 892 --manage-gids"/g' /etc/default/nfs-kernel-server + sed -i -e 's/^STATDOPTS=$/STATDOPTS="--port 662 --outgoing-port 2020"/g' /etc/default/nfs-common + grep -q 'NEED_STATD=yes' /etc/default/nfs-common || echo "NEED_STATD=yes" >> /etc/default/nfs-common + sed -i -e 's/^RPCRQUOTADOPTS=$/RPCRQUOTADOPTS="-p 875"/g' /etc/default/quota + + systemctl restart nfs-kernel-server + SERVICE_STATUS=$? + + elif [[ "$OS_TYPE" =~ ^(rhel|centos|ol|rocky|almalinux)$ ]]; then + # Set ports in /etc/sysconfig/nfs if needed + cat <<EOF >> /etc/sysconfig/nfs +MOUNTD_PORT=892 +STATD_PORT=662 +STATD_OUTGOING_PORT=2020 +LOCKD_TCPPORT=32803 +LOCKD_UDPPORT=32769 +RQUOTAD_PORT=875 +EOF + + systemctl enable --now rpcbind nfs-server + systemctl restart nfs-server + SERVICE_STATUS=$? + + # Open firewall ports if firewalld is running + if systemctl is-active --quiet firewalld; then + firewall-cmd --permanent --add-service=nfs + firewall-cmd --permanent --add-service=mountd + firewall-cmd --permanent --add-service=rpc-bind + firewall-cmd --reload + fi + else + show_dialog "info" "$title" "Unsupported distribution: $OS_TYPE" + return 1 + fi + + # Step 3: Final result + if [[ $SERVICE_STATUS -eq 0 ]]; then + exports_list=$(exportfs) + show_dialog "info" "$title" "NFS Server configured and restarted successfully.\n\nCurrent exports:\n$exports_list" + set_tracker_field "$tracker_key" "yes" + log "NFS server configured successfully." + else + show_dialog "info" "$title" "Failed to restart NFS server. Please check the service logs." + fi +} + +setup_management_server_database() { + local title="CloudStack Database Deployment" + local tracker_key="db_deployed" + if is_configured "$tracker_key"; then + log "CloudStack database is already deployed. Skipping deployment." + return 0 + fi + log "Starting CloudStack database deployment..." + if ! systemctl is-active $MYSQL_SERVICE > /dev/null; then + show_dialog "msg" "$title" "MySQL service is not running. Please start MySQL before proceeding." Review Comment: mysql is started explicitly in install_mysql_server method, followed by management server installation, which gives sufficient time to mysql to start. -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected]
