Files
Webseite_Stines/terraform/main.tf
T
Sebastian Serfling 1f5abb9bc5
Deploy Staging / staging (push) Failing after 48s
feat: setup jetzt via ssh-key nach pct create
2026-05-08 15:09:29 +02:00

80 lines
2.2 KiB
Terraform

terraform {
required_providers {
proxmox = {
source = "telmate/proxmox"
version = "~> 2.9"
}
}
}
provider "proxmox" {
pm_api_url = var.proxmox_host
pm_api_token_id = var.proxmox_token_id
pm_api_token_secret = var.proxmox_token_secret
pm_tls_insecure = true
}
# Create staging LXC mit pct-Befehl
resource "null_resource" "staging_lxc" {
# SSH Public Key in Datei schreiben
provisioner "local-exec" {
command = "mkdir -p /tmp/terraform && echo '${var.ssh_public_key}' > /tmp/terraform/staging_key.pub"
}
provisioner "local-exec" {
command = <<-EOT
pct create 200 \
${var.lxc_ostemplate} \
--hostname hugo-staging \
--cores ${var.lxc_cores} \
--memory ${var.lxc_memory} \
--swap ${var.lxc_swap} \
--rootfs ${var.lxc_rootfs} \
--net0 name=eth0,bridge=${var.lxc_bridge},ip=${var.staging_ip}/24,gw=${var.staging_gw} \
--unprivileged 1 \
--start 1 \
--password "${var.root_password}" \
--ssh-public-keys /tmp/terraform/staging_key.pub
EOT
}
provisioner "local-exec" {
command = "sleep 30"
}
# SSH Key Setup
provisioner "local-exec" {
command = "mkdir -p ~/.ssh && echo '${var.ssh_private_key}' | base64 -d > ~/.ssh/staging_key && chmod 600 ~/.ssh/staging_key"
}
# Setup via SSH
provisioner "remote-exec" {
inline = [
"apt-get update -qq",
"apt-get install -y hugo nginx git rsync curl",
"systemctl enable --now nginx",
"useradd -m -s /bin/bash deploy 2>/dev/null || true",
"mkdir -p /home/deploy/.ssh",
"chmod 700 /home/deploy/.ssh",
"echo '${var.ssh_public_key}' >> /home/deploy/.ssh/authorized_keys",
"chmod 600 /home/deploy/.ssh/authorized_keys",
"chown -R deploy:deploy /home/deploy/.ssh",
"mkdir -p /var/www/html",
"chown -R deploy:deploy /var/www/html",
"echo '✅ Staging VM Setup complete!'"
]
connection {
type = "ssh"
user = "root"
private_key = var.ssh_private_key
host = var.staging_ip
timeout = "5m"
}
}
}
output "staging_ip" {
value = "${var.staging_ip}/24"
}