sureshanaparti commented on code in PR #1: URL: https://github.com/apache/cloudstack-installer/pull/1#discussion_r2469261924
########## 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 +} + + Review Comment: ```suggestion ``` -- 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]
