|
| 1 | +#!/usr/bin/env bash |
| 2 | +set -euox pipefail |
| 3 | + |
| 4 | +# Color definitions |
| 5 | +RED='\033[0;31m' |
| 6 | +GREEN='\033[0;32m' |
| 7 | +BLUE='\033[0;34m' |
| 8 | +NC='\033[0m' # No Color |
| 9 | + |
| 10 | +# Logging functions |
| 11 | +log_info() { echo -e "${BLUE}[INFO]${NC} $*"; } |
| 12 | +log_success() { echo -e "${GREEN}[SUCCESS]${NC} $*"; } |
| 13 | + |
| 14 | +# Network configuration |
| 15 | +NETWORK_NAME="integration" |
| 16 | +NETWORK_UUID="1c8fe98c-b53a-4ca4-bbdb-deb0f26b3579" |
| 17 | +NETWORK_BRIDGE="integration" |
| 18 | +NETWORK_SUBNET="192.168.100.0/24" |
| 19 | +NETWORK_GATEWAY="192.168.100.1" |
| 20 | + |
| 21 | +# Package management |
| 22 | +install_packages() { |
| 23 | + log_info "Installing required packages" |
| 24 | + |
| 25 | + packages=( |
| 26 | + make golang podman jq qemu-img httpd firewalld |
| 27 | + qemu-kvm libvirt-client libvirt-daemon-kvm |
| 28 | + libvirt-daemon virt-install ansible-core |
| 29 | + cargo lorax lsof |
| 30 | + ) |
| 31 | + |
| 32 | + dnf install -y "${packages[@]}" |
| 33 | +} |
| 34 | + |
| 35 | +# Function to check and fix key permissions |
| 36 | +check_key_permissions() { |
| 37 | + local key_path="key/ostree_key" |
| 38 | + |
| 39 | + if [[ -f "${key_path}" ]]; then |
| 40 | + local key_perms |
| 41 | + key_perms=$(stat -L -c "%a" "${key_path}") |
| 42 | + |
| 43 | + if [[ "${key_perms}" != "600" ]]; then |
| 44 | + log_info "File permissions too open (${key_perms}), changing to 600" |
| 45 | + chmod 600 "${key_path}" |
| 46 | + fi |
| 47 | + else |
| 48 | + log_info "Key file not found: ${key_path}" |
| 49 | + fi |
| 50 | +} |
| 51 | + |
| 52 | +# Function to configure services |
| 53 | +configure_services() { |
| 54 | + log_info "Configuring services" |
| 55 | + |
| 56 | + # Enable and start services |
| 57 | + sudo systemctl enable --now httpd.service |
| 58 | + sudo systemctl enable --now firewalld |
| 59 | + |
| 60 | + # Configure libvirt permissions |
| 61 | + log_info "Configuring libvirt permissions" |
| 62 | + sudo tee /etc/polkit-1/rules.d/50-libvirt.rules > /dev/null << 'EOF' |
| 63 | +polkit.addRule(function(action, subject) { |
| 64 | + if (action.id == "org.libvirt.unix.manage" && |
| 65 | + subject.isInGroup("adm")) { |
| 66 | + return polkit.Result.YES; |
| 67 | + } |
| 68 | +}); |
| 69 | +EOF |
| 70 | + |
| 71 | + # Start libvirtd |
| 72 | + log_info "Starting libvirt daemon" |
| 73 | + sudo systemctl start libvirtd |
| 74 | + |
| 75 | + # Verify libvirt is working |
| 76 | + if ! sudo virsh list --all > /dev/null; then |
| 77 | + echo "Failed to connect to libvirt" >&2 |
| 78 | + return 1 |
| 79 | + fi |
| 80 | +} |
| 81 | + |
| 82 | +# Function to setup libvirt network |
| 83 | +setup_libvirt_network() { |
| 84 | + local network_xml="/tmp/integration.xml" |
| 85 | + |
| 86 | + log_info "Setting up libvirt network" |
| 87 | + |
| 88 | + # Create network configuration |
| 89 | + sudo tee "${network_xml}" > /dev/null << 'EOF' |
| 90 | +<network xmlns:dnsmasq='http://libvirt.org/schemas/network/dnsmasq/1.0'> |
| 91 | + <name>integration</name> |
| 92 | + <uuid>1c8fe98c-b53a-4ca4-bbdb-deb0f26b3579</uuid> |
| 93 | + <forward mode='nat'> |
| 94 | + <nat> |
| 95 | + <port start='1024' end='65535'/> |
| 96 | + </nat> |
| 97 | + </forward> |
| 98 | + <bridge name='integration' zone='trusted' stp='on' delay='0'/> |
| 99 | + <mac address='52:54:00:36:46:ef'/> |
| 100 | + <ip address='192.168.100.1' netmask='255.255.255.0'> |
| 101 | + <dhcp> |
| 102 | + <range start='192.168.100.2' end='192.168.100.254'/> |
| 103 | + <host mac='34:49:22:B0:83:30' name='vm-1' ip='192.168.100.50'/> |
| 104 | + <host mac='34:49:22:B0:83:31' name='vm-2' ip='192.168.100.51'/> |
| 105 | + <host mac='34:49:22:B0:83:32' name='vm-3' ip='192.168.100.52'/> |
| 106 | + </dhcp> |
| 107 | + </ip> |
| 108 | + <dnsmasq:options> |
| 109 | + <dnsmasq:option value='dhcp-vendorclass=set:efi-http,HTTPClient:Arch:00016'/> |
| 110 | + <dnsmasq:option value='dhcp-option-force=tag:efi-http,60,HTTPClient'/> |
| 111 | + <dnsmasq:option value='dhcp-boot=tag:efi-http,"http://192.168.100.1/httpboot/EFI/BOOT/BOOTX64.EFI"'/> |
| 112 | + </dnsmasq:options> |
| 113 | +</network> |
| 114 | +EOF |
| 115 | + |
| 116 | + # Define network if it doesn't exist |
| 117 | + if ! sudo virsh net-info integration > /dev/null 2>&1; then |
| 118 | + sudo virsh net-define "${network_xml}" |
| 119 | + fi |
| 120 | + |
| 121 | + # Start network if not active |
| 122 | + if [[ $(sudo virsh net-info integration | awk '/Active/ {print $2}') == "no" ]]; then |
| 123 | + sudo virsh net-start integration |
| 124 | + fi |
| 125 | +} |
| 126 | + |
| 127 | +# Function to check and free port 8081 if needed |
| 128 | +check_port_8081() { |
| 129 | + log_info "Checking port 8081 availability" |
| 130 | + |
| 131 | + if lsof -Pi :8081 -sTCP:LISTEN -t >/dev/null; then |
| 132 | + log_info "Port 8081 is in use, attempting to free it" |
| 133 | + sudo fuser -k 8081/tcp || true |
| 134 | + sleep 2 |
| 135 | + fi |
| 136 | +} |
| 137 | + |
| 138 | + |
| 139 | +log_info "Starting CI environment setup" |
| 140 | + |
| 141 | +# Get OS data |
| 142 | +source /etc/os-release |
| 143 | +log_info "Detected OS: ${ID} ${VERSION_ID}" |
| 144 | + |
| 145 | +# Install required packages |
| 146 | +install_packages |
| 147 | + |
| 148 | +# Check and fix key permissions |
| 149 | +check_key_permissions |
| 150 | + |
| 151 | +# Configure services |
| 152 | +configure_services |
| 153 | + |
| 154 | +# Setup libvirt network |
| 155 | +setup_libvirt_network |
| 156 | + |
| 157 | +# Check port 8081 |
| 158 | +check_port_8081 |
| 159 | + |
| 160 | +log_success "CI environment setup completed successfully" |
0 commit comments