add some DRY to initialization scripts

This commit is contained in:
Bartal Læarsson
2025-02-28 15:56:42 +00:00
parent b225b989b0
commit 262ed46c9b
6 changed files with 445 additions and 102 deletions
+35 -4
View File
@@ -1,8 +1,39 @@
#!/bin/bash
# cleanup-kubernetes.sh - Removes Kubernetes testing environment
set -e # Exit on error
# Set variables
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
source "$SCRIPT_DIR/common-lib.sh"
# Define VMs to clean up
VM_NAMES=("controlplane" "node01" "node02" "node03")
echo "Checking for multipass..."
if ! command -v multipass &> /dev/null; then
echo -e "${RED}✗ Error:${NC} multipass is not installed. Cannot clean up VMs."
echo " Please install Multipass from: https://canonical.com/multipass/install"
exit 1
fi
echo "The following VMs will be deleted:"
multipass list | grep -E "controlplane|node01|node02|node03" || echo " No Kubernetes VMs found"
echo -e "${YELLOW}⚠${NC} This will delete all Kubernetes-related VMs (controlplane, node01, node02, node03)."
echo -e "${YELLOW}⚠${NC} All deployments, configurations, and data on these VMs will be lost."
echo "Continue? (y/n)"
read -r response
if [[ ! "$response" =~ ^[Yy]$ ]]; then
echo "Operation cancelled."
exit 0
fi
echo "Deleting Kubernetes VMs..."
multipass delete controlplane node01 node02 node03
multipass purge
multipass delete "${VM_NAMES[@]}" 2>/dev/null || true
echo -e "${GREEN}✓${NC} VMs deleted"
echo "Cleanup complete."
echo "Purging deleted VMs..."
multipass purge
echo -e "${GREEN}✓${NC} Deleted VMs purged"
echo -e "${GREEN}✓${NC} Cleanup complete."
+35 -4
View File
@@ -1,8 +1,39 @@
#!/bin/bash
# cleanup-postgres.sh - Removes PostgreSQL testing environment
set -e # Exit on error
# Set variables
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
source "$SCRIPT_DIR/common-lib.sh"
# Define VMs to clean up
VM_NAMES=("pg-primary" "pg-replica1" "pg-replica2" "pg-lb")
echo "Checking for multipass..."
if ! command -v multipass &> /dev/null; then
echo -e "${RED}✗ Error:${NC} multipass is not installed. Cannot clean up VMs."
echo " Please install Multipass from: https://canonical.com/multipass/install"
exit 1
fi
echo "The following VMs will be deleted:"
multipass list | grep -E "pg-primary|pg-replica1|pg-replica2|pg-lb" || echo " No PostgreSQL VMs found"
echo -e "${YELLOW}⚠${NC} This will delete all PostgreSQL-related VMs (pg-primary, pg-replica1, pg-replica2, pg-lb)."
echo -e "${YELLOW}⚠${NC} All data on these VMs will be lost."
echo "Continue? (y/n)"
read -r response
if [[ ! "$response" =~ ^[Yy]$ ]]; then
echo "Operation cancelled."
exit 0
fi
echo "Deleting PostgreSQL VMs..."
multipass delete pg-primary pg-replica1 pg-replica2 pg-lb
multipass purge
multipass delete "${VM_NAMES[@]}" 2>/dev/null || true
echo -e "${GREEN}✓${NC} VMs deleted"
echo "Cleanup complete."
echo "Purging deleted VMs..."
multipass purge
echo -e "${GREEN}✓${NC} Deleted VMs purged"
echo -e "${GREEN}✓${NC} Cleanup complete."
+167
View File
@@ -0,0 +1,167 @@
#!/bin/bash
# Colors for output
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[0;33m'
NC='\033[0m' # No Color
# Function to check dependencies
check_dependencies() {
echo "Checking dependencies..."
# Check if multipass is installed
if command -v multipass &> /dev/null; then
MULTIPASS_VERSION=$(multipass version | head -n1)
echo -e "${GREEN}✓${NC} Multipass is installed: $MULTIPASS_VERSION"
else
echo -e "${RED}✗ Error:${NC} multipass is not installed."
echo " Please install Multipass from the official site:"
echo " https://canonical.com/multipass/install"
return 1
fi
# Check if ansible is installed
if command -v ansible-playbook &> /dev/null; then
ANSIBLE_VERSION=$(ansible --version | head -n1)
echo -e "${GREEN}✓${NC} Ansible is installed: $ANSIBLE_VERSION"
else
echo -e "${RED}✗ Error:${NC} ansible is not installed."
echo " Please install Ansible from the official documentation:"
echo " https://docs.ansible.com/ansible/latest/installation_guide/index.html"
return 1
fi
# Check if ssh-keygen is available
if command -v ssh-keygen &> /dev/null; then
SSH_VERSION=$(ssh -V 2>&1)
echo -e "${GREEN}✓${NC} SSH tools are installed: $SSH_VERSION"
else
echo -e "${RED}✗ Error:${NC} ssh-keygen is not available."
echo " Please install OpenSSH client tools."
echo " For Ubuntu/Debian: sudo apt install openssh-client -y"
return 1
fi
# Check for Python3 (needed by Ansible)
if command -v python3 &> /dev/null; then
PYTHON_VERSION=$(python3 --version)
echo -e "${GREEN}✓${NC} Python3 is installed: $PYTHON_VERSION"
else
echo -e "${RED}✗ Error:${NC} python3 is not installed."
echo " Python3 is required for Ansible to function properly."
echo " For Ubuntu/Debian: sudo apt install python3 -y"
return 1
fi
# Verify multipass can run (sometimes requires virtualization support)
if multipass version &> /dev/null; then
echo -e "${GREEN}✓${NC} Multipass is functioning correctly"
else
echo -e "${RED}✗ Error:${NC} multipass is installed but may not be functioning correctly."
echo " Please ensure virtualization is enabled in your BIOS settings."
echo " For more information, visit: https://canonical.com/multipass/docs/troubleshooting"
return 1
fi
echo -e "${GREEN}✓${NC} All dependencies are satisfied!"
echo
return 0
}
# Function to ensure SSH key exists
ensure_ssh_key() {
local ssh_key_path=$1
local ssh_pub_key_path="$ssh_key_path.pub"
echo "Checking for provisioning SSH key..."
# Create .ssh directory if it doesn't exist
if [ ! -d "$HOME/.ssh" ]; then
echo "Creating .ssh directory..."
mkdir -p "$HOME/.ssh"
chmod 700 "$HOME/.ssh"
echo -e "${GREEN}✓${NC} Created .ssh directory with secure permissions"
else
echo -e "${GREEN}✓${NC} .ssh directory exists"
fi
# Generate provisioning SSH key if it doesn't exist
if [ ! -f "$ssh_key_path" ]; then
echo "Generating new SSH key for provisioning at $ssh_key_path"
ssh-keygen -t rsa -b 4096 -f "$ssh_key_path" -N "" -C "multipass_provisioning_key"
# Verify key generation succeeded
if [ ! -f "$ssh_key_path" ] || [ ! -f "$ssh_pub_key_path" ]; then
echo -e "${RED}✗ Error:${NC} SSH key generation failed."
return 1
fi
echo -e "${GREEN}✓${NC} Generated new SSH key for provisioning"
else
echo -e "${GREEN}✓${NC} Using existing SSH key at $ssh_key_path"
fi
# Ensure correct permissions
chmod 600 "$ssh_key_path"
chmod 644 "$ssh_pub_key_path"
echo -e "${GREEN}✓${NC} SSH key permissions set correctly"
echo
return 0
}
# Function to prepare cloud-init file from template
prepare_cloud_init() {
local template_path=$1
local processed_path=$2
local ssh_pub_key_path=$3
local ssh_key_dir=$(dirname "$template_path")
echo "Preparing cloud-init configuration..."
# Create directory for cloud-init files if it doesn't exist
mkdir -p "$ssh_key_dir"
# Copy SSH public key to cloud-init directory
cp "$ssh_pub_key_path" "$ssh_key_dir/ssh_key.txt"
echo -e "${GREEN}✓${NC} SSH public key copied to cloud-init directory"
# Read SSH key
SSH_KEY=$(cat "$ssh_key_dir/ssh_key.txt")
# Process the cloud-init template and replace the placeholder
sed "s|\$SSH_PUBLIC_KEY|$SSH_KEY|g" "$template_path" > "$processed_path"
echo -e "${GREEN}✓${NC} Cloud-init file prepared at $processed_path"
return 0
}
# Function to extract IP address from multipass VM
get_vm_ip() {
local vm_name=$1
# Extract IP using multipass info command
local ip=$(multipass info "$vm_name" | grep "IPv4" | head -n 1 | awk '{print $2}')
# Verify IP was extracted successfully
if [ -z "$ip" ]; then
echo -e "${RED}✗ Error:${NC} Failed to extract IP address for VM: $vm_name"
return 1
fi
echo "$ip"
return 0
}
# Function to check if a VM exists
check_vm_exists() {
local vm_name=$1
if multipass info "$vm_name" &>/dev/null; then
echo -e "${GREEN}✓${NC} VM '$vm_name' exists"
return 0
else
echo -e "${YELLOW}⚠${NC} VM '$vm_name' does not exist"
return 1
fi
}
+123 -47
View File
@@ -4,15 +4,33 @@
set -e # Exit on error
# Set variables
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
source "$SCRIPT_DIR/common-lib.sh"
# Kubernetes VM Names
CONTROL_PLANE="controlplane"
NODE01="node01"
NODE02="node02"
NODE03="node03"
SSH_KEY_FILE="$(pwd)/multipass/cloud-init/ssh_key.txt"
CLOUD_INIT_TEMPLATE="$(pwd)/multipass/cloud-init/common.yaml"
CLOUD_INIT_PROCESSED="$(pwd)/multipass/cloud-init/common_processed.yaml"
INVENTORY_FILE="$(pwd)/ansible/inventories/kubernetes.yml"
DEFAULTS_FILE="$(pwd)/ansible/defaults/kubernetes.yml"
# SSH Key paths
SSH_KEY_NAME="id_rsa_provisioning"
SSH_KEY_PATH="$HOME/.ssh/$SSH_KEY_NAME"
SSH_PUB_KEY_PATH="$SSH_KEY_PATH.pub"
# Cloud-init and inventory paths
CLOUD_INIT_TEMPLATE="$SCRIPT_DIR/../multipass/cloud-init/common.yaml"
CLOUD_INIT_PROCESSED="$SCRIPT_DIR/../multipass/cloud-init/common_processed.yaml"
INVENTORY_FILE="$SCRIPT_DIR/../ansible/inventories/kubernetes.yml"
DEFAULTS_FILE="$SCRIPT_DIR/../ansible/defaults/kubernetes.yml"
# Default resource values
CP_CPU=2
CP_MEM="2G"
CP_DISK="20G"
WORKER_CPU=2
WORKER_MEM="2G"
WORKER_DISK="20G"
# Load resource values from defaults file if it exists
if [ -f "$DEFAULTS_FILE" ] && command -v python3 >/dev/null && python3 -c "import yaml" 2>/dev/null; then
@@ -38,51 +56,82 @@ except Exception as e:
echo "Control plane resources: $CP_CPU CPUs, $CP_MEM memory, $CP_DISK disk"
echo "Worker node resources: $WORKER_CPU CPUs, $WORKER_MEM memory, $WORKER_DISK disk"
else
# Default values if we can't load from the defaults file
CP_CPU=2
CP_MEM="2G"
CP_DISK="20G"
WORKER_CPU=2
WORKER_MEM="2G"
WORKER_DISK="20G"
echo "Using default VM resource settings"
echo "Control plane resources: $CP_CPU CPUs, $CP_MEM memory, $CP_DISK disk"
echo "Worker node resources: $WORKER_CPU CPUs, $WORKER_MEM memory, $WORKER_DISK disk"
fi
# Check if any VMs already exist
VM_EXISTS=false
if multipass info $CONTROL_PLANE &>/dev/null || \
multipass info $NODE01 &>/dev/null || \
multipass info $NODE02 &>/dev/null || \
multipass info $NODE03 &>/dev/null; then
VM_EXISTS=true
# Check dependencies
check_dependencies || exit 1
# Ensure SSH key exists
ensure_ssh_key "$SSH_KEY_PATH" || exit 1
# Process cloud-init template
prepare_cloud_init "$CLOUD_INIT_TEMPLATE" "$CLOUD_INIT_PROCESSED" "$SSH_PUB_KEY_PATH" || exit 1
# Check if VMs exist
echo "Checking for existing VMs..."
CP_EXISTS=false
NODE01_EXISTS=false
NODE02_EXISTS=false
NODE03_EXISTS=false
if multipass info "$CONTROL_PLANE" &>/dev/null; then
echo -e "${GREEN}✓${NC} VM '$CONTROL_PLANE' exists"
CP_EXISTS=true
else
echo -e "${YELLOW}⚠${NC} VM '$CONTROL_PLANE' does not exist"
fi
if [ "$VM_EXISTS" = true ]; then
echo "Some Kubernetes VMs already exist, proceeding to Ansible configuration..."
if multipass info "$NODE01" &>/dev/null; then
echo -e "${GREEN}✓${NC} VM '$NODE01' exists"
NODE01_EXISTS=true
else
# Verify SSH key file exists
if [ ! -f "$SSH_KEY_FILE" ]; then
echo "Error: SSH key file not found at $SSH_KEY_FILE"
echo "Please create this file with your public SSH key."
exit 1
fi
echo -e "${YELLOW}⚠${NC} VM '$NODE01' does not exist"
fi
# Read SSH key
SSH_KEY=$(cat "$SSH_KEY_FILE")
if multipass info "$NODE02" &>/dev/null; then
echo -e "${GREEN}✓${NC} VM '$NODE02' exists"
NODE02_EXISTS=true
else
echo -e "${YELLOW}⚠${NC} VM '$NODE02' does not exist"
fi
# Process the cloud-init template and replace the placeholder
sed "s|\$SSH_PUBLIC_KEY|$SSH_KEY|g" "$CLOUD_INIT_TEMPLATE" > "$CLOUD_INIT_PROCESSED"
if multipass info "$NODE03" &>/dev/null; then
echo -e "${GREEN}✓${NC} VM '$NODE03' exists"
NODE03_EXISTS=true
else
echo -e "${YELLOW}⚠${NC} VM '$NODE03' does not exist"
fi
# Create VMs if they don't exist
if [[ "$CP_EXISTS" == "false" || "$NODE01_EXISTS" == "false" ||
"$NODE02_EXISTS" == "false" || "$NODE03_EXISTS" == "false" ]]; then
echo "Creating Kubernetes VMs with Multipass..."
# Control plane gets more resources
multipass launch --name $CONTROL_PLANE --cpus $CP_CPU --memory $CP_MEM --disk $CP_DISK --cloud-init "$CLOUD_INIT_PROCESSED"
multipass launch --name $NODE01 --cpus $WORKER_CPU --memory $WORKER_MEM --disk $WORKER_DISK --cloud-init "$CLOUD_INIT_PROCESSED"
multipass launch --name $NODE02 --cpus $WORKER_CPU --memory $WORKER_MEM --disk $WORKER_DISK --cloud-init "$CLOUD_INIT_PROCESSED"
multipass launch --name $NODE03 --cpus $WORKER_CPU --memory $WORKER_MEM --disk $WORKER_DISK --cloud-init "$CLOUD_INIT_PROCESSED"
echo "This may take several minutes. Please be patient..."
# Create VMs that don't exist
if [[ "$CP_EXISTS" == "false" ]]; then
echo "Creating control plane VM: $CONTROL_PLANE ($CP_CPU CPUs, $CP_MEM memory, $CP_DISK disk)"
multipass launch --name "$CONTROL_PLANE" --cpus "$CP_CPU" --memory "$CP_MEM" --disk "$CP_DISK" --cloud-init "$CLOUD_INIT_PROCESSED"
fi
if [[ "$NODE01_EXISTS" == "false" ]]; then
echo "Creating worker node VM: $NODE01 ($WORKER_CPU CPUs, $WORKER_MEM memory, $WORKER_DISK disk)"
multipass launch --name "$NODE01" --cpus "$WORKER_CPU" --memory "$WORKER_MEM" --disk "$WORKER_DISK" --cloud-init "$CLOUD_INIT_PROCESSED"
fi
if [[ "$NODE02_EXISTS" == "false" ]]; then
echo "Creating worker node VM: $NODE02 ($WORKER_CPU CPUs, $WORKER_MEM memory, $WORKER_DISK disk)"
multipass launch --name "$NODE02" --cpus "$WORKER_CPU" --memory "$WORKER_MEM" --disk "$WORKER_DISK" --cloud-init "$CLOUD_INIT_PROCESSED"
fi
if [[ "$NODE03_EXISTS" == "false" ]]; then
echo "Creating worker node VM: $NODE03 ($WORKER_CPU CPUs, $WORKER_MEM memory, $WORKER_DISK disk)"
multipass launch --name "$NODE03" --cpus "$WORKER_CPU" --memory "$WORKER_MEM" --disk "$WORKER_DISK" --cloud-init "$CLOUD_INIT_PROCESSED"
fi
# Remove processed file after use
rm "$CLOUD_INIT_PROCESSED"
@@ -92,11 +141,18 @@ else
fi
echo "Extracting IP addresses..."
# Extract IPs correctly
CP_IP=$(multipass info $CONTROL_PLANE | grep "IPv4" | head -n 1 | awk '{print $2}')
NODE01_IP=$(multipass info $NODE01 | grep "IPv4" | head -n 1 | awk '{print $2}')
NODE02_IP=$(multipass info $NODE02 | grep "IPv4" | head -n 1 | awk '{print $2}')
NODE03_IP=$(multipass info $NODE03 | grep "IPv4" | head -n 1 | awk '{print $2}')
# Extract IPs
CP_IP=$(multipass info "$CONTROL_PLANE" | grep "IPv4" | head -n 1 | awk '{print $2}')
NODE01_IP=$(multipass info "$NODE01" | grep "IPv4" | head -n 1 | awk '{print $2}')
NODE02_IP=$(multipass info "$NODE02" | grep "IPv4" | head -n 1 | awk '{print $2}')
NODE03_IP=$(multipass info "$NODE03" | grep "IPv4" | head -n 1 | awk '{print $2}')
# Verify IPs were extracted successfully
if [ -z "$CP_IP" ] || [ -z "$NODE01_IP" ] || [ -z "$NODE02_IP" ] || [ -z "$NODE03_IP" ]; then
echo -e "${RED}✗ Error:${NC} Failed to extract IP addresses from multipass."
echo "Please verify that VMs are running using 'multipass list'"
exit 1
fi
# Print the IPs to verify
echo "Control Plane IP: $CP_IP"
@@ -105,6 +161,9 @@ echo "Node02 IP: $NODE02_IP"
echo "Node03 IP: $NODE03_IP"
echo "Generating Ansible inventory..."
# Create directory for inventory file if it doesn't exist
mkdir -p "$(dirname "$INVENTORY_FILE")"
# Create inventory file
cat > "$INVENTORY_FILE" << EOF
---
@@ -127,17 +186,34 @@ all:
vars:
ansible_user: ubuntu
ansible_become: yes
ansible_ssh_private_key_file: ~/.ssh/id_rsa
ansible_ssh_private_key_file: $SSH_KEY_PATH
ansible_ssh_common_args: '-o StrictHostKeyChecking=no'
EOF
echo -e "${GREEN}✓${NC} Ansible inventory created at $INVENTORY_FILE"
# Verify inventory
echo "Verifying inventory file contents:"
cat "$INVENTORY_FILE"
echo "Running Ansible playbook for Kubernetes deployment..."
ansible-playbook -i "$INVENTORY_FILE" ansible/playbooks/kubernetes.yml
# Run Ansible playbook
PLAYBOOK="$SCRIPT_DIR/../ansible/playbooks/kubernetes.yml"
if [ ! -f "$PLAYBOOK" ]; then
echo -e "${RED}✗ Error:${NC} Ansible playbook not found at $PLAYBOOK"
exit 1
fi
echo "Kubernetes environment setup complete."
echo "Running Ansible playbook for Kubernetes deployment..."
echo "This will take some time. Please be patient..."
if ansible-playbook -i "$INVENTORY_FILE" "$PLAYBOOK"; then
echo -e "${GREEN}✓${NC} Ansible playbook completed successfully"
else
echo -e "${RED}✗ Error:${NC} Ansible playbook failed"
exit 1
fi
echo -e "${GREEN}✓${NC} Kubernetes environment setup complete."
echo "Control Plane: $CP_IP"
echo "Workers: $NODE01_IP, $NODE02_IP, $NODE03_IP"
echo ""
+80 -42
View File
@@ -1,47 +1,68 @@
#!/bin/bash
# provision-postgres.sh - Creates and configures a PostgreSQL environment
set -e # Exit on error
# Set variables
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
source "$SCRIPT_DIR/common-lib.sh"
# PostgreSQL VM Names
PRIMARY_NAME="pg-primary"
REPLICA1_NAME="pg-replica1"
REPLICA2_NAME="pg-replica2"
LB_NAME="pg-lb"
SSH_KEY_FILE="$(pwd)/multipass/cloud-init/ssh_key.txt"
CLOUD_INIT_TEMPLATE="$(pwd)/multipass/cloud-init/common.yaml"
CLOUD_INIT_PROCESSED="$(pwd)/multipass/cloud-init/common_processed.yaml"
INVENTORY_FILE="$(pwd)/ansible/inventories/postgres.yml"
# Check if any VMs already exist
VM_EXISTS=false
if multipass info $PRIMARY_NAME &>/dev/null || \
multipass info $REPLICA1_NAME &>/dev/null || \
multipass info $REPLICA2_NAME &>/dev/null || \
multipass info $LB_NAME &>/dev/null; then
VM_EXISTS=true
fi
# SSH Key paths
SSH_KEY_NAME="id_rsa_provisioning"
SSH_KEY_PATH="$HOME/.ssh/$SSH_KEY_NAME"
SSH_PUB_KEY_PATH="$SSH_KEY_PATH.pub"
if [ "$VM_EXISTS" = true ]; then
echo "Some PostgreSQL VMs already exist, proceeding to Ansible configuration..."
else
# Verify SSH key file exists
if [ ! -f "$SSH_KEY_FILE" ]; then
echo "Error: SSH key file not found at $SSH_KEY_FILE"
echo "Please create this file with your public SSH key."
exit 1
fi
# Cloud-init and inventory paths
CLOUD_INIT_TEMPLATE="$SCRIPT_DIR/../multipass/cloud-init/common.yaml"
CLOUD_INIT_PROCESSED="$SCRIPT_DIR/../multipass/cloud-init/common_processed.yaml"
INVENTORY_FILE="$SCRIPT_DIR/../ansible/inventories/postgres.yml"
# Read SSH key
SSH_KEY=$(cat "$SSH_KEY_FILE")
# Check dependencies
check_dependencies || exit 1
# Process the cloud-init template and replace the placeholder
sed "s|\$SSH_PUBLIC_KEY|$SSH_KEY|g" "$CLOUD_INIT_TEMPLATE" > "$CLOUD_INIT_PROCESSED"
# Ensure SSH key exists
ensure_ssh_key "$SSH_KEY_PATH" || exit 1
# Process cloud-init template
prepare_cloud_init "$CLOUD_INIT_TEMPLATE" "$CLOUD_INIT_PROCESSED" "$SSH_PUB_KEY_PATH" || exit 1
# Check if VMs already exist
PRIMARY_EXISTS=$(check_vm_exists "$PRIMARY_NAME" && echo "true" || echo "false")
REPLICA1_EXISTS=$(check_vm_exists "$REPLICA1_NAME" && echo "true" || echo "false")
REPLICA2_EXISTS=$(check_vm_exists "$REPLICA2_NAME" && echo "true" || echo "false")
LB_EXISTS=$(check_vm_exists "$LB_NAME" && echo "true" || echo "false")
# Create VMs if they don't exist
if [[ "$PRIMARY_EXISTS" == "false" || "$REPLICA1_EXISTS" == "false" ||
"$REPLICA2_EXISTS" == "false" || "$LB_EXISTS" == "false" ]]; then
echo "Creating PostgreSQL VMs with Multipass..."
multipass launch --name $PRIMARY_NAME --cpus 1 --mem 1G --disk 5G --cloud-init "$CLOUD_INIT_PROCESSED"
multipass launch --name $REPLICA1_NAME --cpus 1 --mem 1G --disk 5G --cloud-init "$CLOUD_INIT_PROCESSED"
multipass launch --name $REPLICA2_NAME --cpus 1 --mem 1G --disk 5G --cloud-init "$CLOUD_INIT_PROCESSED"
multipass launch --name $LB_NAME --cpus 1 --mem 1G --disk 5G --cloud-init "$CLOUD_INIT_PROCESSED"
# Create VMs that don't exist
if [[ "$PRIMARY_EXISTS" == "false" ]]; then
echo "Creating VM: $PRIMARY_NAME (1 CPU, 1G memory, 5G disk)"
multipass launch --name "$PRIMARY_NAME" --cpus 1 --memory 1G --disk 5G --cloud-init "$CLOUD_INIT_PROCESSED"
fi
if [[ "$REPLICA1_EXISTS" == "false" ]]; then
echo "Creating VM: $REPLICA1_NAME (1 CPU, 1G memory, 5G disk)"
multipass launch --name "$REPLICA1_NAME" --cpus 1 --memory 1G --disk 5G --cloud-init "$CLOUD_INIT_PROCESSED"
fi
if [[ "$REPLICA2_EXISTS" == "false" ]]; then
echo "Creating VM: $REPLICA2_NAME (1 CPU, 1G memory, 5G disk)"
multipass launch --name "$REPLICA2_NAME" --cpus 1 --memory 1G --disk 5G --cloud-init "$CLOUD_INIT_PROCESSED"
fi
if [[ "$LB_EXISTS" == "false" ]]; then
echo "Creating VM: $LB_NAME (1 CPU, 1G memory, 5G disk)"
multipass launch --name "$LB_NAME" --cpus 1 --memory 1G --disk 5G --cloud-init "$CLOUD_INIT_PROCESSED"
fi
# Remove processed file after use
rm "$CLOUD_INIT_PROCESSED"
@@ -51,11 +72,11 @@ else
fi
echo "Extracting IP addresses..."
# Extract IPs correctly
PRIMARY_IP=$(multipass info pg-primary | grep "IPv4" | head -n 1 | awk '{print $2}')
REPLICA1_IP=$(multipass info pg-replica1 | grep "IPv4" | head -n 1 | awk '{print $2}')
REPLICA2_IP=$(multipass info pg-replica2 | grep "IPv4" | head -n 1 | awk '{print $2}')
LB_IP=$(multipass info pg-lb | grep "IPv4" | head -n 1 | awk '{print $2}')
# Extract IPs
PRIMARY_IP=$(get_vm_ip "$PRIMARY_NAME") || exit 1
REPLICA1_IP=$(get_vm_ip "$REPLICA1_NAME") || exit 1
REPLICA2_IP=$(get_vm_ip "$REPLICA2_NAME") || exit 1
LB_IP=$(get_vm_ip "$LB_NAME") || exit 1
# Print the IPs to verify
echo "Primary IP: $PRIMARY_IP"
@@ -64,7 +85,10 @@ echo "Replica2 IP: $REPLICA2_IP"
echo "Load Balancer IP: $LB_IP"
echo "Generating Ansible inventory..."
# Create inventory file - using a simpler format to avoid issues
# Create directory for inventory file if it doesn't exist
mkdir -p "$(dirname "$INVENTORY_FILE")"
# Create inventory file
cat > "$INVENTORY_FILE" << EOF
[postgres_primary]
pg-primary ansible_host=$PRIMARY_IP
@@ -83,18 +107,32 @@ postgres_lb
[all:vars]
ansible_user=ubuntu
ansible_ssh_private_key_file=~/.ssh/id_rsa
ansible_ssh_private_key_file=$SSH_KEY_PATH
ansible_python_interpreter=/usr/bin/python3
ansible_ssh_common_args='-o StrictHostKeyChecking=no'
EOF
echo -e "${GREEN}✓${NC} Ansible inventory created at $INVENTORY_FILE"
# Verify inventory
echo "Verifying inventory file contents:"
cat "$INVENTORY_FILE"
echo "Running Ansible playbook..."
ansible-playbook -i "$INVENTORY_FILE" ansible/playbooks/postgres.yml
# Run Ansible playbook
PLAYBOOK="$SCRIPT_DIR/../ansible/playbooks/postgres.yml"
if [ ! -f "$PLAYBOOK" ]; then
echo -e "${RED}✗ Error:${NC} Ansible playbook not found at $PLAYBOOK"
exit 1
fi
echo "PostgreSQL environment is ready!"
echo "Running Ansible playbook..."
if ansible-playbook -i "$INVENTORY_FILE" "$PLAYBOOK"; then
echo -e "${GREEN}✓${NC} Ansible playbook completed successfully"
else
echo -e "${RED}✗ Error:${NC} Ansible playbook failed"
exit 1
fi
echo -e "${GREEN}✓${NC} PostgreSQL environment is ready!"
echo "Primary: $PRIMARY_IP"
echo "Replicas: $REPLICA1_IP, $REPLICA2_IP"
echo "Load Balancer: $LB_IP"