ansble-terraform-jsonnet/services/terraform.libsonnet

86 lines
3.1 KiB
Plaintext
Raw Permalink Normal View History

2021-10-12 00:13:21 +02:00
local domains = import "../config/domains.libsonnet";
local servers = import "../config/servers.libsonnet";
local credentials = import "../credentials.libsonnet";
local terraform = import "../lib/terraform.libsonnet";
local defaults = import "../config/defaults.libsonnet";
local domainToName (domain) = std.strReplace(domain, '.', '_');
local splitDomainName (domain, subdomaine = []) =
local found = std.find(domain, domains);
local split = std.split(domain, '.');
if std.length(found) > 0
then { subdomain: std.join('.', subdomaine), zone: domainToName(domains[found[0]]) }
else splitDomainName(std.join('.', split[1:]), subdomaine + [split[0]]);
local domainEntries(domain, server) =
local split = splitDomainName(domain);
local record = terraform.HdnsRecord { zone_id: "${hetznerdns_zone." + split.zone + ".id}", name: "" };
local hostAttr (attr) = "${hcloud_server." + server.name + "." + attr + "}";
if split.subdomain == ""
then {
["deployment_" + domainToName(domain) + "_A"]: record { value: hostAttr('ipv4_address'), type: "A" },
["deployment_" + domainToName(domain) + "_AAAA"]: record { value: hostAttr('ipv6_address'), type: "AAAA" }
}
else { ["deployment_" + domainToName(domain) + "_CNAME"]: record { name: split.subdomain, value: server.networking.publicDomain, type: 'CNAME' } };
{
terraform: {
required_providers: {
hcloud: {
source: "hetznercloud/hcloud",
version: "1.30.0"
},
hetznerdns: {
source: "timohirt/hetznerdns",
version: "1.1.1"
}
}
},
provider: {
hcloud: {
token: credentials.hcloudToken
},
hetznerdns: {
apitoken: credentials.hdnsToken
}
},
resource: {
hcloud_ssh_key: {
[k]: terraform.HcloudSSHKey { name: k, public_key: defaults.defaultSshKeys[k] }
for k in std.objectFields(defaults.defaultSshKeys)
},
hcloud_server: {
[s]: servers[s].instance
for s in std.objectFields(servers)
},
hetznerdns_zone: {
infra: { name: defaults.infraDomain, ttl: defaults.defaultZoneTTL },
} + {
[domainToName(domain)]: { name: domain, ttl: defaults.defaultZoneTTL }
for domain in domains
},
# Default records for every host (v4, v6, VPN)
local hostRecords = std.foldl(function (a, b) a + b, [
terraform.serverDnsRecords(servers[s])
for s in std.objectFields(servers)
], {}),
# DNS records for deployed apps
local appRecords = std.foldl(
function (records, server)
records + std.foldl(
function (entries, domain) entries + domainEntries(domain, servers[server]),
servers[server].deployment_vars.domains, {}
),
std.objectFields(servers), {}
),
hetznerdns_record: hostRecords + appRecords
}
}