diff --git a/multiplane.sh b/multiplane.sh index 6a6f8d2..c5bc922 100644 --- a/multiplane.sh +++ b/multiplane.sh @@ -2,7 +2,7 @@ set -e -CONFIG_FILE="${1:-config.yaml}" +CONFIG_FILE="${1:-config-test.yaml}" 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." @@ -53,30 +53,50 @@ validate_ipv4() { # Function to get and validate MAC address get_mac_address() { + local io="${1:-}" local mac + while true; do - mac=$(gum input --placeholder "MAC Address (e.g., BC:24:AC:76:96:DE)") || exit 130 + if [[ -z "$io" ]]; then + mac=$(gum input --placeholder "MAC Address (e.g., BC:24:AC:76:96:DE)" --width 80) || exit 130 + else + if [[ -z "$(yq $io $CONFIG_FILE)" ]]; then + mac=$(gum input --placeholder "MAC Address (e.g., BC:24:AC:76:96:DE)" --width 80) || exit 130 + else + mac=$(gum input --placeholder "MAC Address (e.g., BC:24:AC:76:96:DE)" --width 80 --value "$(yq $io $CONFIG_FILE)") && yq -i -oy "$io = \"$mac\"" "$CONFIG_FILE" || exit 130 + fi + fi if validate_mac "$mac"; then echo "$mac" break else - gum style --foreground 196 "Invalid MAC address format. Use XX:XX:XX:XX:XX:XX" + gum style --foreground 196 "Invalid MAC address format. Use XX:XX:XX:XX:XX:XX"; sleep 0.5 fi done } # Function to get and validate IPv4 address get_ipv4_address() { + local io="${1:-}" local ip + while true; do - ip=$(gum input --placeholder "IPv4 Address (e.g., 10.1.0.14)") || exit 130 + if [[ -z "$io" ]]; then + ip=$(gum input --placeholder "IPv4 Address (e.g., 10.1.0.14)" --width 80) || exit 130 + else + if [[ -z "$(yq $io $CONFIG_FILE)" ]]; then + ip=$(gum input --placeholder "IPv4 Address (e.g., 10.1.0.14)" --width 80) || exit 130 + else + ip=$(gum input --placeholder "IPv4 Address (e.g., 10.1.0.14))" --width 80 --value "$(yq $io $CONFIG_FILE)") && yq -i -oy "$io = \"$ip\"" "$CONFIG_FILE" || exit 130 + fi + fi if validate_ipv4 "$ip"; then echo "$ip" break else - gum style --foreground 196 "Invalid IPv4 address format" + gum style --foreground 196 "Invalid IPv4 address format"; sleep 0.5 fi done } @@ -94,17 +114,17 @@ PUBLIC_IP_RANGE=$(gum input --placeholder "192.168.0.0/24" --prompt "* " --width declare -a entries # Proxy node entry (2 nodes required) -clear; gum style --border double --padding "1 2" --border-foreground 212 "Proxy Configuration (2 Required)"; sleep 3 +clear; gum style --border double --padding "1 2" --border-foreground 212 "Proxy Configuration (2 Required)"; sleep 0.5 trap 'clear; echo -e "\nExiting..."; exit 130' INT for i in 1 2; do clear; gum style --border double --padding "1 2" --border-foreground 212 "Proxy Node $i/2" # Get MAC address - mac=$(get_mac_address) || exit 130 + mac=$(get_mac_address ".haproxy.proxy.$i.mac") || exit 130 # Get IPv4 address - ip=$(get_ipv4_address) || exit 130 + ip=$(get_ipv4_address ".haproxy.proxy.$i.ip") || exit 130 # Add entry using yq yq -i -oy ".haproxy.proxy.$i = {\"mac\": \"$mac\", \"ip\": \"$ip\"}" "$CONFIG_FILE" @@ -115,17 +135,17 @@ clear; echo "HAProxy Kuberenetes VIP. (e.g., 10.1.0.10)"; HAPROXY_KUBE_VIP=$(gum input --placeholder "10.1.0.10" --prompt "* " --width 80 --value "$(yq '.haproxy.kube.vip' $CONFIG_FILE)") && yq -i -oy ".haproxy.kube.vip = \"$HAPROXY_KUBE_VIP\"" "$CONFIG_FILE" || exit 0 # Controller node entry (3 nodes required) -clear; gum style --border double --padding "1 2" --border-foreground 212 "Controller Node Configuration (3 Required)"; sleep 3 +clear; gum style --border double --padding "1 2" --border-foreground 212 "Controller Node Configuration (3 Required)"; sleep 0.5 trap 'clear; echo -e "\nExiting..."; exit 130' INT for i in 1 2 3; do clear; gum style --border double --padding "1 2" --border-foreground 212 "Controller Node $i/3" # Get MAC address - mac=$(get_mac_address) || exit 130 + mac=$(get_mac_address ".nodes.controller.$i.mac") || exit 130 # Get IPv4 address - ip=$(get_ipv4_address) || exit 130 + ip=$(get_ipv4_address ".nodes.controller.$i.ip") || exit 130 # Add entry using yq yq -i -oy ".nodes.controller.$i = {\"mac\": \"$mac\", \"ip\": \"$ip\"}" "$CONFIG_FILE" @@ -133,28 +153,29 @@ done trap - INT # Controller node entry (3 nodes required) -gum style --border double --padding "1 2" --border-foreground 212 "Worker Node Configuration (3 Required)"; sleep 3 +clear; gum style --border double --padding "1 2" --border-foreground 212 "Worker Node Configuration (3 Required)"; sleep 0.5 # Add worker node loop -count=0 +count=1 trap 'clear; echo -e "\nExiting..."; exit 130' INT while [ $count -lt 250 ]; do - clear; gum style --border double --padding "1 2" --border-foreground 212 "Worker Node Entry ($(($count + 1))/250)" - - # Get MAC address - mac=$(get_mac_address) || exit 130 - - # Get IPv4 address - ip=$(get_ipv4_address) || exit 130 - - # Add entry using yq - count=$((count+1)) - yq -i -oy ".nodes.worker.$count = {\"mac\": \"$mac\", \"ip\": \"$ip\"}" "$CONFIG_FILE" - # Ask to continue - if [ $count -gt 3 ] && [ $count -le 250 ]; then + if [ $count -gt 3 ]; then clear; gum confirm "Add another worker node?" || break fi + + clear; gum style --border double --padding "1 2" --border-foreground 212 "Worker Node Entry ($((count))/250)" + + # Get MAC address + mac=$(get_mac_address ".nodes.worker.$count.mac") || exit 130 + + # Get IPv4 address + ip=$(get_ipv4_address ".nodes.worker.$count.ip") || exit 130 + + # Add entry using yq + yq -i -oy ".nodes.worker.$count = {\"mac\": \"$mac\", \"ip\": \"$ip\"}" "$CONFIG_FILE" + + count=$((count + 1)) done trap - INT