89 lines
3.1 KiB
Ruby
89 lines
3.1 KiB
Ruby
# -*- mode: ruby -*-
|
|
# vi: set ft=ruby :
|
|
|
|
# --- USER CUSTOMIZATION ---
|
|
# Change the values below to your desired settings.
|
|
|
|
# 1. The username for the new user account with sudo permissions.
|
|
USERNAME = "pkhamre"
|
|
|
|
# 2. The absolute path to your SSH public key.
|
|
PUBLIC_KEY_PATH = File.expand_path("~/.ssh/id_ed25519.pub")
|
|
|
|
# --- VM & CLUSTER CONFIGURATION ---
|
|
|
|
# Base box for all VMs
|
|
VAGRANT_BOX = "cloud-image/ubuntu-24.04"
|
|
VAGRANT_BOX_VERSION = "20250704.0.0"
|
|
|
|
# Predefined static IP addresses and configurations for each node
|
|
NODES = [
|
|
{ hostname: "k8s-cp-1", ip: "192.168.122.101", memory: 2048, cpus: 2 },
|
|
{ hostname: "k8s-cp-2", ip: "192.168.122.102", memory: 2048, cpus: 2 },
|
|
{ hostname: "k8s-cp-3", ip: "192.168.122.103", memory: 2048, cpus: 2 },
|
|
{ hostname: "k8s-worker-1", ip: "192.168.122.111", memory: 2048, cpus: 4 },
|
|
{ hostname: "k8s-worker-2", ip: "192.168.122.112", memory: 2048, cpus: 4 }
|
|
]
|
|
|
|
Vagrant.configure("2") do |config|
|
|
config.vm.box = VAGRANT_BOX
|
|
config.vm.box_version = VAGRANT_BOX_VERSION
|
|
|
|
# Verify that the specified SSH public key file exists before proceeding.
|
|
if !File.exist?(PUBLIC_KEY_PATH)
|
|
raise "SSH public key not found at path: #{PUBLIC_KEY_PATH}. Please update the PUBLIC_KEY_PATH variable in the Vagrantfile."
|
|
end
|
|
publicKey = File.read(PUBLIC_KEY_PATH).strip
|
|
|
|
# --- DEFINE VMS FROM THE NODES LIST ---
|
|
NODES.each do |node_config|
|
|
config.vm.define node_config[:hostname] do |node|
|
|
node.vm.hostname = node_config[:hostname]
|
|
|
|
# ** CORRECTED NETWORK CONFIGURATION **
|
|
# Use 'private_network' to assign a static IP and 'libvirt__network_name'
|
|
# to connect to an existing libvirt virtual network.
|
|
node.vm.network "private_network",
|
|
ip: node_config[:ip],
|
|
libvirt__network_name: "default"
|
|
|
|
node.vm.provider "libvirt" do |libvirt|
|
|
libvirt.memory = node_config[:memory]
|
|
libvirt.cpus = node_config[:cpus]
|
|
end
|
|
end
|
|
end
|
|
|
|
# --- COMMON PROVISIONING SCRIPT ---
|
|
# This script runs on all nodes to create a user and set up SSH access.
|
|
config.vm.provision "shell", inline: <<-SHELL
|
|
echo ">>> Starting user and SSH configuration..."
|
|
|
|
# Create the user with a home directory and add to the sudo group
|
|
if ! id -u #{USERNAME} >/dev/null 2>&1; then
|
|
echo ">>> Creating user '#{USERNAME}'"
|
|
useradd #{USERNAME} --create-home --shell /bin/bash --groups sudo
|
|
else
|
|
echo ">>> User '#{USERNAME}' already exists"
|
|
fi
|
|
|
|
# Grant passwordless sudo to the new user
|
|
echo ">>> Configuring passwordless sudo for '#{USERNAME}'"
|
|
echo '#{USERNAME} ALL=(ALL) NOPASSWD:ALL' > /etc/sudoers.d/#{USERNAME}
|
|
chmod 0440 /etc/sudoers.d/#{USERNAME}
|
|
|
|
# Set up SSH key-based authentication
|
|
echo ">>> Adding SSH public key for '#{USERNAME}'"
|
|
HOME_DIR=$(eval echo ~#{USERNAME})
|
|
mkdir -p $HOME_DIR/.ssh
|
|
echo '#{publicKey}' > $HOME_DIR/.ssh/authorized_keys
|
|
|
|
# Set correct permissions for the .ssh directory and authorized_keys file
|
|
chown -R #{USERNAME}:#{USERNAME} $HOME_DIR/.ssh
|
|
chmod 700 $HOME_DIR/.ssh
|
|
chmod 600 $HOME_DIR/.ssh/authorized_keys
|
|
|
|
echo ">>> User configuration complete!"
|
|
SHELL
|
|
end
|