Refactor and add deployment support

This commit is contained in:
2021-10-12 00:13:21 +02:00
parent ae2e359225
commit 6cc0596b72
13 changed files with 218 additions and 106 deletions

22
lib/deployments.libsonnet Normal file
View File

@@ -0,0 +1,22 @@
{
Deployment:: {
roles: error "At least one role is required",
variables: error "Deployment variables missing"
},
dockerComposeApp: function (name, domain = null) self.Deployment {
roles: ['reverse-proxy', 'docker', 'docker-compose-app'],
variables: {
docker_compose_app: [ name ],
domains: [ domain ]
},
},
laravelApp: function (name, domain) self.Deployment {
roles: ['reverse-proxy', 'laravel-app'],
variables: {
laravel_apps: [ { name: name, domain: domain } ] ,
domains: [ domain ]
}
}
}

21
lib/servers.libsonnet Normal file
View File

@@ -0,0 +1,21 @@
local defaults = import "../config/defaults.libsonnet";
local utils = import "../lib/utils.libsonnet";
{
hashIp: function (name) std.substr(std.md5(name), 0, 4) + ":" + std.substr(std.md5(name), 4, 4),
meta: function (name, instance, deployments) {
[name]: {
name: name,
networking: {
publicSubdomain: name + ".infra",
internalSubdomain: name + ".i.infra",
publicDomain: self.publicSubdomain + "." + defaults.infraDomain,
internalDomain: self.internalSubdomain + "." + defaults.infraDomain,
wireguardIp: defaults.ipSubnet + ":" + $.hashIp(name),
},
instance: instance + { name: name },
roles: std.uniq(std.sort(std.foldl(function (roles, deployment) roles + deployment.roles, deployments, []))),
deployment_vars: std.foldl(function (vars, deployment) utils.merge(vars, deployment.variables), deployments, {})
}
}
}

View File

@@ -1,7 +1,6 @@
local config = import "../config/config.libsonnet";
local defaults = import "../config/defaults.libsonnet";
{
local terraform = self,
local rname (server, suffix) = "host_" + server.name + "_" + suffix,
HcloudInstance:: {
@@ -13,8 +12,7 @@ local config = import "../config/config.libsonnet";
HcloudSSHKey:: {
name: error "Key must have field: name",
public_key: error "Key must have field: public_key",
labels: { source: "terraform" }
public_key: error "Key must have field: public_key"
},
HdnsRecord:: {
@@ -22,14 +20,14 @@ local config = import "../config/config.libsonnet";
name: error "Record must have field: name",
value: error "Record must have field: value",
type: error "Record must have field: type",
ttl: config.defaultTTL
ttl: defaults.defaultTTL
},
serverDnsRecords: function (s) {
local attr (s, n) = "${hcloud_server." + s.name + "." + n + "}",
[rname(s, "A")]: terraform.HdnsRecord{ name: s.publicSubdomain, value: attr(s, "ipv4_address"), type: "A" },
[rname(s, "AAAA")]: terraform.HdnsRecord{ name: s.publicSubdomain, value: attr(s, "ipv6_address"), type: "AAAA" },
[rname(s, "VPN")]: terraform.HdnsRecord{ name: s.internalSubdomain, value: s.wireguardIp, type: "AAAA" },
[rname(s, "A")]: $.HdnsRecord{ name: s.networking.publicSubdomain, value: attr(s, "ipv4_address"), type: "A" },
[rname(s, "AAAA")]: $.HdnsRecord{ name: s.networking.publicSubdomain, value: attr(s, "ipv6_address"), type: "AAAA" },
[rname(s, "VPN")]: $.HdnsRecord{ name: s.networking.internalSubdomain, value: s.networking.wireguardIp, type: "AAAA" },
}
}

32
lib/utils.libsonnet Normal file
View File

@@ -0,0 +1,32 @@
{
# adopted from stdlib source to handle array merges
# https://github.com/google/jsonnet/blob/4e67da2c015bb316158d3e52a47376b38a29a4ef/stdlib/std.jsonnet#L1473
merge (target, patch)::
if std.isObject(patch) then
local target_object =
if std.isObject(target) then target else {};
local target_fields =
if std.isObject(target_object) then std.objectFields(target_object) else [];
local null_fields = [k for k in std.objectFields(patch) if patch[k] == null];
local both_fields = std.setUnion(target_fields, std.objectFields(patch));
{
[k]:
if !std.objectHas(patch, k) then
target_object[k]
else if !std.objectHas(target_object, k) then
$.merge(null, patch[k])
else
$.merge(target_object[k], patch[k])
for k in std.setDiff(both_fields, null_fields)
}
else if std.isArray(patch) then
if std.isArray(target) && target != [null] then
target + patch
else
patch
else
patch
}