Files
Multiplane/multiplane.sh
2026-01-19 14:18:23 +00:00

104 lines
2.7 KiB
Bash

#!/bin/bash
set -e
CONFIG_FILE="${1:-config.toml}"
if [[ "$EUID" -ne 0 ]]; then
echo -e "\e[31m[FATAL]\e[39m Currently this script requires being ran as root user - please try again as root."
exit 1
fi
echo -e "\n\nINSTALL LOG: $(date --rfc-3339=seconds)\n" >> /tmp/multiplane.log
info() {
echo -e "\e[34m[INFO]\e[39m $1"
echo "[INFO] $1" >> /tmp/multiplane.log
}
debug() {
if [[ ! -z "$DEBUG" ]]; then
echo -e "\e[96m[DEBUG]\e[39m $1"
fi
echo "[DEBUG] $1" >> /tmp/multiplane.log
}
warn() {
echo -e "\e[33m[WARNING]\e[39m $1"
echo "[WARNING] $1" >> /tmp/multiplane.log
}
fatal() {
echo -e "\e[31m[FATAL]\e[39m $1"
echo "[FATAL] $1" >> /tmp/multiplane.log
exit 1
}
# Function to validate MAC address
validate_mac() {
[[ $1 =~ ^([0-9A-Fa-f]{2}:){5}[0-9A-Fa-f]{2}$ ]]
}
# Function to validate IPv4 address
validate_ipv4() {
[[ $1 =~ ^([0-9]{1,3}\.){3}[0-9]{1,3}$ ]] && {
IFS='.' read -ra octets <<< "$1"
for octet in "${octets[@]}"; do
((octet > 255)) && return 1
done
return 0
}
return 1
}
yq -i -o toml ".k0s.hostname = \"$(gum input --placeholder "Cluster Domain:" --prompt "* " --width 80 --value "$(yq '.k0s.hostname' $CONFIG_FILE)")\"" "$CONFIG_FILE"
yq -i -o toml ".k0s.private_ip_range = \"$(gum input --placeholder "Node IP Range::" --prompt "* " --width 80 --value "$(yq '.k0s.private_ip_range' $CONFIG_FILE)")\"" "$CONFIG_FILE"
# Initialize array to store entries
declare -a entries
# Add worker node loop
count=0
while [ $count -lt 250 ]; do
gum style --border double --padding "1 2" --border-foreground 212 \
"Worker Node Entry ($(($count + 1))/250)"
# Get MAC address with validation loop
while true; do
mac=$(gum input --placeholder "MAC Address (e.g., BC:24:AC:76:96:DE)")
[ -z "$mac" ] && break 2
if validate_mac "$mac"; then
break
else
gum style --foreground 196 "Invalid MAC address format. Use XX:XX:XX:XX:XX:XX"
fi
done
# Get IPv4 address with validation loop
while true; do
ip=$(gum input --placeholder "IPv4 Address (e.g., 10.1.0.14)")
[ -z "$ip" ] && break 2
if validate_ipv4 "$ip"; then
break
else
gum style --foreground 196 "Invalid IPv4 address format"
fi
done
# Add entry using yq
((count++))
yq -i -o toml ".nodes.worker.$count = {\"mac\": \"$mac\", \"ip\": \"$ip\"}" "$CONFIG_FILE"
# Ask to continue
if [ $count -lt 250 ]; then
gum confirm "Add another worker node?" || break
fi
done
# Display results
if [ $count -gt 0 ]; then
gum style --border double --padding "1 2" --border-foreground 212 "✓ Configuration saved to $CONFIG_FILE"
fi