Files
proxmox/smom-dbis-138-proxmox/lib/validation.sh

147 lines
3.5 KiB
Bash
Executable File

#!/usr/bin/env bash
# Validation functions for deployment scripts
# Validate VMID is not already in use
validate_vmid_available() {
local vmid="$1"
if pct list 2>/dev/null | grep -q "^\s*$vmid\s"; then
log_warn "VMID $vmid is already in use"
return 1
fi
return 0
}
# Validate VMID range doesn't overlap
validate_vmid_range() {
local start_vmid="$1"
local count="$2"
local end_vmid=$((start_vmid + count - 1))
log_debug "Validating VMID range: $start_vmid to $end_vmid"
for vmid in $(seq "$start_vmid" "$end_vmid"); do
if pct list 2>/dev/null | grep -q "^\s*$vmid\s"; then
log_warn "VMID $vmid in range is already in use"
return 1
fi
done
return 0
}
# Validate install script exists
validate_install_script() {
local script_path="$1"
if [[ ! -f "$script_path" ]]; then
log_error "Install script not found: $script_path"
return 1
fi
if [[ ! -r "$script_path" ]]; then
log_error "Install script is not readable: $script_path"
return 1
fi
return 0
}
# Validate bridge exists
validate_bridge() {
local bridge="${1:-${PROXMOX_BRIDGE:-vmbr0}}"
if ! ip link show "$bridge" &>/dev/null; then
log_error "Bridge $bridge does not exist"
return 1
fi
return 0
}
# Validate storage exists
validate_storage() {
local storage="${1:-${PROXMOX_STORAGE:-local-lvm}}"
if ! pvesm status 2>/dev/null | grep -q "^$storage"; then
log_warn "Storage $storage may not exist or be accessible"
return 1
fi
return 0
}
# Validate resource availability (basic check)
validate_resources() {
local memory="$1"
local cores="$2"
local disk="$3"
# Basic validation - check if values are reasonable
if [[ $memory -lt 256 ]] || [[ $memory -gt 1048576 ]]; then
log_warn "Memory value $memory MB seems unusual (expected 256-1048576 MB)"
fi
if [[ $cores -lt 1 ]] || [[ $cores -gt 64 ]]; then
log_warn "CPU cores value $cores seems unusual (expected 1-64)"
fi
if [[ $disk -lt 1 ]] || [[ $disk -gt 10000 ]]; then
log_warn "Disk size $disk GB seems unusual (expected 1-10000 GB)"
fi
return 0
}
# Validate network configuration
validate_network_config() {
local bridge="${PROXMOX_BRIDGE:-vmbr0}"
if ! validate_bridge "$bridge"; then
return 1
fi
# Check if bridge is up
if ! ip link show "$bridge" | grep -q "state UP"; then
log_warn "Bridge $bridge is not UP"
return 1
fi
return 0
}
# Pre-deployment validation
pre_deployment_validation() {
local vmid="$1"
local memory="$2"
local cores="$3"
local disk="$4"
log_info "Running pre-deployment validation for VMID $vmid..."
# Validate VMID
if ! validate_vmid_available "$vmid"; then
log_error "VMID $vmid validation failed"
return 1
fi
# Validate resources
if ! validate_resources "$memory" "$cores" "$disk"; then
log_warn "Resource validation warnings (continuing anyway)"
fi
# Validate network
if ! validate_network_config; then
log_warn "Network validation warnings (continuing anyway)"
fi
# Validate storage
if ! validate_storage; then
log_warn "Storage validation warnings (continuing anyway)"
fi
log_success "Pre-deployment validation passed for VMID $vmid"
return 0
}