nixos modules for convenient deployment of cloud resources

refactor(hetzner): improve command; make error logging better

ptr.pet 698c6ba2 4ef813c9

verified
Changed files
+37 -32
firewall
provider
hetzner
+2
.gitignore
···
+
result
+
.hetzner
+2 -5
firewall/provider/hetzner/app.nix
···
{pkgs, lib ? pkgs.lib, taggedPorts, id}: let
l = lib // (import ./rules.nix {inherit lib;});
-
firewallRules =
builtins.toFile
"hetzner-firewall-${toString id}-rules.json"
(builtins.toJSON (l.mkFirewallRuleset taggedPorts));
-
in pkgs.writers.writeNu "apply-hetzner" ''
-
let firewallId = ${toString id}
-
let rulesFile = "${firewallRules}"
-
${l.fileContents ./app.nu}
+
in pkgs.writers.writeNu "apply-hetzner-firewall-${toString id}" ''
+
nu ${./app.nu} ${toString id} ${firewallRules}
''
+33 -27
firewall/provider/hetzner/app.nu
···
use std/log
-
let authHeader = ["authorization" $"Bearer ($env.HETZNER_API_TOKEN)"]
+
def main [firewallId: number, rulesFile: path, --auth-token (-t): string] {
+
let auth_token: string = if $auth_token == null { $env.HETZNER_API_TOKEN? } else { $auth_token }
+
let authHeader: list<string> = ["authorization" $"Bearer ($auth_token)"]
-
def makeApiUrl [path: string] {
-
return $"https://api.hetzner.cloud/v1($path)"
-
}
-
def post [path: string] {
-
$in | http post -e --full -H $authHeader --content-type application/json (makeApiUrl $path)
-
}
-
def get [path: string] {
-
http get -e --full -H $authHeader (makeApiUrl $path)
-
}
+
def makeApiUrl [path: string] {
+
return $"https://api.hetzner.cloud/v1($path)"
+
}
+
def post [path: string] {
+
$in | http post -e --full -H $authHeader --content-type application/json (makeApiUrl $path)
+
}
+
def get [path: string] {
+
http get -e --full -H $authHeader (makeApiUrl $path)
+
}
-
# first fetch firewall to see if it even exists
-
let resp = get $"/firewalls/($firewallId)"
-
if $resp.status == 404 {
-
log error $"provided firewall \(id ($firewallId)\) does not exist"
-
exit 1
-
}
-
let firewall = $resp.body | get firewall
+
# first fetch firewall to see if it even exists
+
let resp = get $"/firewalls/($firewallId)"
+
if $resp.status == 404 {
+
log error $"provided firewall \(id ($firewallId)\) does not exist"
+
exit 1
+
} else if $resp.status != 200 {
+
log error $"could not get firewall \(id ($firewallId)\):\n($resp.body.error | to text -n)"
+
exit 1
+
}
+
let firewall = $resp.body | get firewall
-
# backup firewall
-
let backupPath = $".hetzner/($firewallId).json"
-
mkdir .hetzner; $firewall | to json | save -f $backupPath
-
log info $"backing up firewall ($firewallId) to ($backupPath)"
+
# backup firewall
+
let backupPath = $".hetzner/($firewallId).json"
+
mkdir .hetzner; $firewall | to json | save -f $backupPath
+
log info $"backing up firewall ($firewallId) to ($backupPath)"
-
# apply rules
-
let resp = open $rulesFile | post $"/firewalls/($firewallId)/actions/set_rules"
-
if $resp.status != 201 {
-
log error $"could not apply firewall \(id ($firewallId)\):\n($resp.body | to text)"
-
exit 2
+
# apply rules
+
let resp = open $rulesFile | post $"/firewalls/($firewallId)/actions/set_rules"
+
if $resp.status != 201 {
+
log error $"could not apply firewall \(id ($firewallId)\):\n($resp.body.error | to text -n)"
+
exit 2
+
}
+
log info $"applied firewall ($firewallId)"
}
-
log info $"applied firewall ($firewallId)"