feat: fixed zone creation

This commit is contained in:
Jan-Ole Hübner 2025-02-05 21:58:39 +01:00
parent 3fe945f4d2
commit 3f960fe68f
3 changed files with 29 additions and 63 deletions

View file

@ -1,47 +0,0 @@
#!/usr/bin/env zsh
set -e
set -o pipefail
set -u
echo "🔍 Running Tofu plan..."
if ! tofu plan -out=tfplan 2>&1 | tee plan.log; then
echo "❌ Plan failed. Checking for missing zones..."
# Check if the error is related to a missing DNS zone
if grep -q "\-target planning" plan.log; then
echo "⚠️ Missing DNS zones detected. Creating them first..."
# Apply only the zone creation step
tofu apply -target=hetznerdns_zone.zone
echo "🔄 Re-running full plan after zone creation..."
if ! tofu plan -out=tfplan 2>&1 | tee plan.log; then
echo "❌ Plan failed even after zone creation. Exiting."
exit 1
fi
else
echo "❌ Plan failed due to an unknown issue. Exiting."
exit 1
fi
fi
# Check if no changes are required
if grep -q "No changes" plan.log; then
echo "✅ No changes detected. Exiting."
exit 0
fi
#applying a planfile skips confirmations...
echo "✅ Plan successful. Ready to apply changes."
echo "Do you want to apply these changes? (y/n)"
read -r apply_confirmation
if [[ "$apply_confirmation" =~ ^[Yy]$ ]]; then
echo "🔄 Applying changes..."
tofu apply tfplan
echo "🎉 Apply complete!"
else
echo "❌ Apply aborted by user."
exit 1
fi

43
main.tf
View file

@ -1,7 +1,25 @@
locals {
api = yamldecode(file("${path.module}/auth.yaml"))
zones = yamldecode(file("${path.module}/zones.yaml"))
nameservers = data.hetznerdns_nameservers.primary.ns
ns_data = flatten([
for zone_name, records in local.zones : [
for ns_entry in local.nameservers : {
zone_name = zone_name
record_type = "NS"
record_value = ns_entry.name
record_name = "@"
extra_data = ""
}
]
])
zone_data = flatten([
for zone_name, records in local.zones : [
@ -20,6 +38,10 @@ locals {
])
}
data "hetznerdns_nameservers" "primary" {
type = "authoritative"
}
resource "hetznerdns_zone" "zone" {
for_each = local.zones
name = each.key
@ -38,23 +60,16 @@ for_each = {
}
resource "hetznerdns_record" "ns" {
for_each = {
for entry in flatten([
for zone in hetznerdns_zone.zone : [
for ns_entry in zone.ns : {
zone_id = zone.id
ns_entry = ns_entry
key = "${zone.id}-${ns_entry}"
}
]
]) : entry.key => entry
for record in local.ns_data :
"${record.record_type}-${record.record_name}-${record.zone_name}-${record.record_value}" => record
}
zone_id = each.value.zone_id
name = "@"
type = "NS"
value = each.value.ns_entry
zone_id = hetznerdns_zone.zone[each.value.zone_name].id
name = each.value.record_name
type = each.value.record_type
value = each.value.record_value
ttl = 300
}

View file

@ -4,8 +4,6 @@ jan-ole.de:
#server0--------------
server0: 185.239.239.162
"*.server0": 185.239.239.162
pve: 185.239.239.162
"*.pve": 185.239.239.162
#server1--------------
server1: 147.189.171.39