diff --git a/control_plane/.gitignore b/control-plane/.gitignore similarity index 100% rename from control_plane/.gitignore rename to control-plane/.gitignore diff --git a/control_plane/.terraform.lock.hcl b/control-plane/.terraform.lock.hcl similarity index 100% rename from control_plane/.terraform.lock.hcl rename to control-plane/.terraform.lock.hcl diff --git a/control_plane/README.md b/control-plane/README.md similarity index 100% rename from control_plane/README.md rename to control-plane/README.md diff --git a/control_plane/main.tf b/control-plane/main.tf similarity index 98% rename from control_plane/main.tf rename to control-plane/main.tf index c42b5d5..09fe938 100644 --- a/control_plane/main.tf +++ b/control-plane/main.tf @@ -94,7 +94,7 @@ resource "null_resource" "talos_cluster_patches" { command = < + dhcp: true +- op: replace + path: /machine/install/disk + value: /dev/ diff --git a/control_plane/terraform.tfvars b/control-plane/terraform.tfvars similarity index 100% rename from control_plane/terraform.tfvars rename to control-plane/terraform.tfvars diff --git a/control-plane/vm-setup/create-control-plane-vms.sh b/control-plane/vm-setup/create-control-plane-vms.sh new file mode 100755 index 0000000..74ced36 --- /dev/null +++ b/control-plane/vm-setup/create-control-plane-vms.sh @@ -0,0 +1,192 @@ +#!/bin/bash +set -e + +# Configuration +VM_DIR="${VM_DIR:-$(pwd)/control-plane-vm}" +CONTROLPLANE_NAME="talos-controlplane" +MEMORY="2048" # 2GB per VM +CPUS="2" +DISK_SIZE="10G" # 10GB disk +TALOS_ISO_URL="https://github.com/siderolabs/talos/releases/download/v1.11.0/metal-arm64.iso" + +# Colors for output +RED='\033[0;31m' +GREEN='\033[0;32m' +YELLOW='\033[1;33m' +NC='\033[0m' + +log_info() { + echo -e "${GREEN}[INFO]${NC} $1" +} + +log_warn() { + echo -e "${YELLOW}[WARN]${NC} $1" +} + +log_error() { + echo -e "${RED}[ERROR]${NC} $1" +} + +# Create VM directory +create_vm_dir() { + local vm_name=$1 + local vm_path="$VM_DIR/$vm_name" + + mkdir -p "$vm_path" + echo "$vm_path" +} + +# Download Talos ISO if not exists +download_talos_iso() { + local iso_path="$VM_DIR/talos.iso" + + if [[ ! -f "$iso_path" ]]; then + log_info "Downloading Talos ISO..." + curl -L -o "$iso_path" "$TALOS_ISO_URL" + else + log_info "Talos ISO already exists" + fi + echo "$iso_path" +} + +# Create disk image +create_disk_image() { + local vm_path=$1 + local vm_name=$2 + local disk_path="$vm_path/disk.qcow2" + + if [[ ! -f "$disk_path" ]]; then + log_info "Creating disk image for $vm_name..." + qemu-img create -f qcow2 "$disk_path" "$DISK_SIZE" + else + log_warn "Disk image already exists for $vm_name" + fi + echo "$disk_path" +} + +# Generate unique MAC address +generate_mac() { + local vm_name=$1 + local hash=$(echo "$vm_name" | md5sum | cut -c1-6) + printf "52:54:00:%s:%s:%s" "${hash:0:2}" "${hash:2:2}" "${hash:4:2}" +} + +# Create VM startup script +create_vm_script() { + local vm_path=$1 + local vm_name=$2 + local mac_address=$3 + local iso_path=$4 + local disk_path=$5 + + local script_path="$vm_path/start.sh" + + cat > "$script_path" << EOF +#!/bin/bash +qemu-system-aarch64 \\ + -name "$vm_name" \\ + -machine virt,highmem=off \\ + -accel hvf \\ + -cpu host \\ + -smp "$CPUS" \\ + -m "${MEMORY}M" \\ + -drive file="$disk_path",if=virtio,format=qcow2 \\ + -cdrom "$iso_path" \\ + -netdev user,id=net0 \\ + -device virtio-net-pci,netdev=net0,mac=$mac_address \\ + -nographic \\ + -serial mon:stdio +EOF + + chmod +x "$script_path" + echo "$script_path" +} + +# Create systemd service file (optional) +create_service_file() { + local vm_path=$1 + local vm_name=$2 + local script_path=$3 + + local service_path="$vm_path/$vm_name.service" + + cat > "$service_path" << EOF +[Unit] +Description=Talos VM - $vm_name +After=network.target + +[Service] +Type=simple +ExecStart=$script_path +WorkingDirectory=$vm_path +Restart=always +User=$USER + +[Install] +WantedBy=multi-user.target +EOF + + echo "$service_path" +} + +# Main execution +main() { + log_info "Creating Talos VMs with QEMU..." + + if ! command -v qemu-system-aarch64 &> /dev/null; then + log_error "QEMU is not installed. Install with: brew install qemu" + exit 1 + fi + + local iso_path=$(download_talos_iso) + + log_info "Creating controlplane VM..." + local controlplane_path=$(create_vm_dir "$CONTROLPLANE_NAME") + local controlplane_disk=$(create_disk_image "$controlplane_path" "$CONTROLPLANE_NAME") + local controlplane_mac=$(generate_mac "$CONTROLPLANE_NAME") + local controlplane_script=$(create_vm_script "$controlplane_path" "$CONTROLPLANE_NAME" "$controlplane_mac" "$iso_path" "$controlplane_disk") + create_service_file "$controlplane_path" "$CONTROLPLANE_NAME" "$controlplane_script" + + local manage_script="$VM_DIR/manage-vm.sh" + cat > "$manage_script" << 'EOF' +#!/bin/bash +VM_DIR="$(cd "$(dirname "$0")" && pwd)" + +start_vm() { + echo "Starting Talos VM..." + "$VM_DIR/talos-controlplane/start.sh" & +} + +stop_vm() { + echo "Stopping Talos VM..." + pkill -f "qemu-system-aarch64.*talos-controlplane" +} + +case "$1" in + start) + start_vm + ;; + stop) + stop_vm + ;; + status) + pgrep -f "qemu-system-aarch64.*talos-controlplane" > /dev/null && echo "VM is running" || echo "VM is stopped" + ;; + *) + echo "Usage: $0 {start|stop|status}" + exit 1 + ;; +esac +EOF + chmod +x "$manage_script" + + log_info "VM creation complete!" + log_info "VM files located at: $VM_DIR" + log_info "" + log_info "To start VM: $manage_script start" + log_info "To stop VM: $manage_script stop" + log_info "" + log_info "Controlplane MAC: $controlplane_mac" +} + +main "$@" \ No newline at end of file diff --git a/control_plane/cleanup.sh b/control_plane/cleanup.sh deleted file mode 100755 index 01fa3fd..0000000 --- a/control_plane/cleanup.sh +++ /dev/null @@ -1,24 +0,0 @@ -#!/bin/bash -cluster_name="$1" -echo "Starting cleanup for cluster: $cluster_name" - -sudo chown -R $(id -u):$(id -g) $HOME/.talos -sudo chown -R $(id -u):$(id -g) $HOME/.kube - -sudo --preserve-env=HOME talosctl cluster destroy \ - --provisioner=qemu \ - --name $cluster_name || true - -sudo pkill -9 -f qemu-system - -# Remove kubeconfig entries -kubectl config delete-context "$cluster_name" || true -kubectl config delete-cluster "admin@$cluster_name" || true -kubectl config delete-user "admin@$cluster_name" || true - -talosctl config remove "$cluster_name" || true - -# Remove files -sudo rm -rf "${HOME}/.talos/clusters/${cluster_name}" || truec - -echo "Cleanup completed for cluster: $cluster_name" \ No newline at end of file diff --git a/control_plane/patches/controlplane-patch.yaml b/control_plane/patches/controlplane-patch.yaml deleted file mode 100644 index 4d5a2be..0000000 --- a/control_plane/patches/controlplane-patch.yaml +++ /dev/null @@ -1,6 +0,0 @@ -# patches/controlplane-patch.yaml -- op: replace - path: /machine/network/nameservers - value: - - "1.1.1.1" - - "8.8.8.8" \ No newline at end of file diff --git a/hc_vault/.gitignore b/hc-vault/.gitignore similarity index 100% rename from hc_vault/.gitignore rename to hc-vault/.gitignore diff --git a/hc_vault/README.md b/hc-vault/README.md similarity index 100% rename from hc_vault/README.md rename to hc-vault/README.md diff --git a/hc_vault/docker-compose.yml b/hc-vault/docker-compose.yml similarity index 100% rename from hc_vault/docker-compose.yml rename to hc-vault/docker-compose.yml diff --git a/hc_vault/example.yaml b/hc-vault/example.yaml similarity index 100% rename from hc_vault/example.yaml rename to hc-vault/example.yaml diff --git a/hc_vault/hc_vault/Dockerfile b/hc-vault/hc_vault/Dockerfile similarity index 100% rename from hc_vault/hc_vault/Dockerfile rename to hc-vault/hc_vault/Dockerfile diff --git a/hc_vault/hc_vault/config/config.hcl b/hc-vault/hc_vault/config/config.hcl similarity index 100% rename from hc_vault/hc_vault/config/config.hcl rename to hc-vault/hc_vault/config/config.hcl diff --git a/hc_vault/nginx/conf/nginx.conf b/hc-vault/nginx/conf/nginx.conf similarity index 100% rename from hc_vault/nginx/conf/nginx.conf rename to hc-vault/nginx/conf/nginx.conf diff --git a/worker/patches/worker-patch-1.yaml b/worker/patches/worker-patch-1.yaml new file mode 100644 index 0000000..2f47a25 --- /dev/null +++ b/worker/patches/worker-patch-1.yaml @@ -0,0 +1,9 @@ +# patches/worker-patch-1.yaml +- op: replace + path: /machine/network/interfaces + value: + - interface: + dhcp: true +- op: replace + path: /machine/install/disk + value: /dev/ diff --git a/worker/vm-setup/create-worker-vm.sh b/worker/vm-setup/create-worker-vm.sh new file mode 100644 index 0000000..40b81e5 --- /dev/null +++ b/worker/vm-setup/create-worker-vm.sh @@ -0,0 +1,193 @@ +#!/bin/bash +set -e + +# Configuration +VM_DIR="${VM_DIR:-$(pwd)/worker-vms}" +WORKER_NAME="talos-worker" +MEMORY="2048" # 2GB per VM +CPUS="2" +DISK_SIZE="10G" # 10GB disk +TALOS_ISO_URL="https://github.com/siderolabs/talos/releases/download/v1.11.0/metal-arm64.iso" + +# Colors for output +RED='\033[0;31m' +GREEN='\033[0;32m' +YELLOW='\033[1;33m' +NC='\033[0m' + +log_info() { + echo -e "${GREEN}[INFO]${NC} $1" +} + +log_warn() { + echo -e "${YELLOW}[WARN]${NC} $1" +} + +log_error() { + echo -e "${RED}[ERROR]${NC} $1" +} + +# Create VM directory +create_vm_dir() { + local vm_name=$1 + local vm_path="$VM_DIR/$vm_name" + + mkdir -p "$vm_path" + echo "$vm_path" +} + +# Download Talos ISO if not exists +download_talos_iso() { + local iso_path="$VM_DIR/talos.iso" + + if [[ ! -f "$iso_path" ]]; then + log_info "Downloading Talos ISO..." + curl -L -o "$iso_path" "$TALOS_ISO_URL" + else + log_info "Talos ISO already exists" + fi + echo "$iso_path" +} + +# Create disk image +create_disk_image() { + local vm_path=$1 + local vm_name=$2 + local disk_path="$vm_path/disk.qcow2" + + if [[ ! -f "$disk_path" ]]; then + log_info "Creating disk image for $vm_name..." + qemu-img create -f qcow2 "$disk_path" "$DISK_SIZE" + else + log_warn "Disk image already exists for $vm_name" + fi + echo "$disk_path" +} + +# Generate unique MAC address +generate_mac() { + local vm_name=$1 + local hash=$(echo "$vm_name" | md5sum | cut -c1-6) + printf "52:54:00:%s:%s:%s" "${hash:0:2}" "${hash:2:2}" "${hash:4:2}" +} + +# Create VM startup script +create_vm_script() { + local vm_path=$1 + local vm_name=$2 + local mac_address=$3 + local iso_path=$4 + local disk_path=$5 + + local script_path="$vm_path/start.sh" + + cat > "$script_path" << EOF +#!/bin/bash +qemu-system-aarch64 \\ + -name "$vm_name" \\ + -machine virt,highmem=off \\ + -accel hvf \\ + -cpu host \\ + -smp "$CPUS" \\ + -m "${MEMORY}M" \\ + -drive file="$disk_path",if=virtio,format=qcow2 \\ + -cdrom "$iso_path" \\ + -netdev user,id=net0 \\ + -device virtio-net-pci,netdev=net0,mac=$mac_address \\ + -nographic \\ + -serial mon:stdio +EOF + + chmod +x "$script_path" + echo "$script_path" +} + +# Create systemd service file (optional) +create_service_file() { + local vm_path=$1 + local vm_name=$2 + local script_path=$3 + + local service_path="$vm_path/$vm_name.service" + + cat > "$service_path" << EOF +[Unit] +Description=Talos VM - $vm_name +After=network.target + +[Service] +Type=simple +ExecStart=$script_path +WorkingDirectory=$vm_path +Restart=always +User=$USER + +[Install] +WantedBy=multi-user.target +EOF + + echo "$service_path" +} + +# Main execution +main() { + log_info "Creating Talos VMs with QEMU..." + + if ! command -v qemu-system-aarch64 &> /dev/null; then + log_error "QEMU is not installed. Install with: brew install qemu" + exit 1 + fi + + local iso_path=$(download_talos_iso) + + # Create worker VM + log_info "Creating worker VM..." + local worker_path=$(create_vm_dir "$WORKER_NAME") + local worker_disk=$(create_disk_image "$worker_path" "$WORKER_NAME") + local worker_mac=$(generate_mac "$WORKER_NAME") + local worker_script=$(create_vm_script "$worker_path" "$WORKER_NAME" "$worker_mac" "$iso_path" "$worker_disk") + create_service_file "$worker_path" "$WORKER_NAME" "$worker_script" + + local manage_script="$VM_DIR/manage-vms.sh" + cat > "$manage_script" << 'EOF' +#!/bin/bash +VM_DIR="$(cd "$(dirname "$0")" && pwd)" + +start_vms() { + echo "Starting Talos VMs..." + "$VM_DIR/talos-worker/start.sh" & +} + +stop_vms() { + echo "Stopping Talos VMs..." + pkill -f "qemu-system-aarch64.*talos-worker" +} + +case "$1" in + start) + start_vms + ;; + stop) + stop_vms + ;; + status) + pgrep -f "qemu-system-aarch64.*talos-worker" > /dev/null && echo "VMs are running" || echo "VMs are stopped" + ;; + *) + echo "Usage: $0 {start|stop|status}" + exit 1 + ;; +esac +EOF + chmod +x "$manage_script" + + log_info "VM creation complete!" + log_info "VM files located at: $VM_DIR" + log_info "" + log_info "To start VMs: $manage_script start" + log_info "To stop VMs: $manage_script stop" + log_info "" + log_info "Worker MAC: $worker_mac" +} + +main "$@" \ No newline at end of file