nix-config/hosts/bind/default.nix

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

206 lines
5.4 KiB
Nix
Raw Normal View History

2022-01-16 13:26:37 +01:00
{ zentralwerk, config, pkgs, ... }:
2021-10-15 02:07:50 +02:00
let
systemctl = "${pkgs.systemd}/bin/systemctl";
deployCommand = "${systemctl} start deploy-c3d2-dns";
2022-11-16 02:15:04 +01:00
# wrap reload in freeze/thaw so that zones are reloaded that had
# been updated by dyndns
reloadCommand = with pkgs; writeScript "reload-bind" ''
#! ${runtimeShell}
rndc() {
${bind}/sbin/rndc -k /etc/bind/rndc.key $@
}
rndc freeze
rndc reload
rndc thaw
'';
2021-10-15 02:07:50 +02:00
in
{
c3d2 = {
isInHq = false;
hq.statistics.enable = true;
deployment.server = "server10";
2021-10-15 02:07:50 +02:00
};
system.stateVersion = "22.05";
networking = {
hostName = "bind";
firewall = {
allowedTCPPorts = [
53 # DNS
80 443 # HTTP(s)
];
allowedUDPPorts = [
53 # DNS
];
};
};
2021-10-15 02:07:50 +02:00
2021-10-18 04:04:40 +02:00
# DNS server
2021-10-15 02:07:50 +02:00
services.bind = {
enable = true;
extraConfig = ''
include "${config.users.users.c3d2-dns.home}/c3d2-dns/zones.conf";
include "${zentralwerk.packages.${pkgs.system}.dns-slaves}";
2021-10-16 01:51:27 +02:00
# for collectd
statistics-channels {
inet 127.0.0.1 port 8053;
};
2021-10-15 02:07:50 +02:00
'';
};
2021-10-18 04:04:40 +02:00
systemd.services.bind = {
serviceConfig = {
Restart = "always";
RestartSec = "1s";
};
};
# BIND statistics in Grafana
2021-10-16 01:51:27 +02:00
services.collectd.plugins.bind = ''
URL "http://127.0.0.1:8053/";
ParseTime false
OpCodes true
QTypes true
ServerStats true
ZoneMaintStats true
ResolverStats false
MemoryStats true
'';
2021-10-15 02:07:50 +02:00
# Build user
users.groups.c3d2-dns = {};
users.users.c3d2-dns = {
isSystemUser = true;
group = "c3d2-dns";
home = "/var/lib/c3d2-dns";
};
systemd.tmpfiles.rules = [
"d ${config.users.users.c3d2-dns.home} 0755 c3d2-dns ${config.users.users.c3d2-dns.group} - -"
2021-10-16 01:51:39 +02:00
"d /var/lib/bind/slave 0755 named nogroup - -"
2021-10-15 02:07:50 +02:00
];
# Build script
systemd.services.deploy-c3d2-dns = let
2021-10-15 19:14:17 +02:00
inherit (pkgs.bind-secrets) giteaToken sshPrivkey;
2021-10-15 02:07:50 +02:00
in {
wantedBy = [ "multi-user.target" ];
before = [ "bind.service" ];
after = [ "network-online.target" ];
2022-06-18 02:43:11 +02:00
path = with pkgs; [ git nix curl openssh ];
2021-10-15 02:07:50 +02:00
script = ''
mkdir -p .ssh
cp ${builtins.toFile "id_ed25519" sshPrivkey} .ssh/id_ed25519
2022-01-16 13:26:37 +01:00
echo "gitea.c3d2.de ${config.c3d2.hosts.gitea.publicKey}" > .ssh/known_hosts
2021-10-15 02:07:50 +02:00
chmod 0600 .ssh/id_ed25519
# Build at least once
touch deploy-pending
2022-06-19 02:35:26 +02:00
status() {
curl -X POST \
"https://gitea.c3d2.de/api/v1/repos/c3d2-admins/c3d2-dns/statuses/$REV?token=${giteaToken}" \
-H "accept: application/json" \
-H "Content-Type: application/json" \
-d "$1"
}
2021-10-15 02:07:50 +02:00
[ -d c3d2-dns ] || git clone --depth=1 gitea@gitea.c3d2.de:c3d2-admins/c3d2-dns.git
cd c3d2-dns
# Loop in case the webhook was called while we were building
while [ -e ../deploy-pending ]; do
rm ../deploy-pending
git checkout .
git pull
REV=$(git rev-parse HEAD)
set +e
2022-06-19 02:35:26 +02:00
status "{ \"context\": \"c3d2-dns\", \"description\": \"reloading...\", \"state\": \"pending\"}"
2021-10-15 02:07:50 +02:00
2021-10-18 04:04:40 +02:00
# Fix legacy paths (TODO)
2021-10-15 02:07:50 +02:00
for f in *.conf ; do
sed -e 's#/home/git/#${config.users.users.c3d2-dns.home}/#g' -i $f
done
2021-10-18 04:04:40 +02:00
# Allow creation of .jnl files by BIND for DynDNS
chmod a+w zones
2022-02-24 20:45:44 +01:00
# Clean up .jnl files
rm -f zones/*.jnl
2021-10-18 04:04:40 +02:00
# Take action
if systemctl is-active -q bind; then
/run/wrappers/bin/sudo ${reloadCommand}
fi
2021-10-15 02:07:50 +02:00
if [ $? = 0 ]; then
2022-11-16 02:15:04 +01:00
status "{ \"context\": \"c3d2-dns\", \"description\": \"reloaded\", \"state\": \"success\"}"
2021-10-15 02:07:50 +02:00
else
2022-11-16 02:15:04 +01:00
status "{ \"context\": \"c3d2-dns\", \"description\": \"reload failure\", \"state\": \"failure\"}"
2021-10-15 02:07:50 +02:00
fi
set -e
done
'';
serviceConfig = {
User = "c3d2-dns";
Group = config.users.users.c3d2-dns.group;
PrivateTmp = true;
ProtectSystem = "full";
ReadWritePaths = config.users.users.c3d2-dns.home;
WorkingDirectory = config.users.users.c3d2-dns.home;
};
};
2021-10-18 04:04:40 +02:00
# Privileged commands triggered by webhook/deploy-c3d2-dns
2021-10-15 02:07:50 +02:00
security.sudo.extraRules = [ {
users = [ "c3d2-dns" ];
commands = [ {
command = deployCommand;
options = [ "NOPASSWD" ];
} {
2022-11-16 02:15:04 +01:00
command = toString reloadCommand;
2021-10-15 02:07:50 +02:00
options = [ "NOPASSWD" ];
} ];
} ];
2021-10-18 04:04:40 +02:00
# Web server just for the webhook
services.nginx = {
enable = true;
virtualHosts = {
# hooks, logs
"bind.serv.zentralwerk.org" = {
default = true;
enableACME = true;
forceSSL = true;
locations."/hooks/".proxyPass = "http://localhost:9000/hooks/";
};
};
};
# Webhook service
2021-10-15 02:07:50 +02:00
systemd.services.webhook =
let
hooksJson = pkgs.writeText "hooks.json" (builtins.toJSON [ {
id = "deploy-c3d2-dns";
execute-command = pkgs.writeShellScript "deploy-c3d2-dns" ''
# Request (re-)deployment
touch ${config.users.users.c3d2-dns.home}/deploy-pending
# Start deploy-c3d2-dns.service if not already running
exec /run/wrappers/bin/sudo ${deployCommand}
'';
} ]);
in {
wantedBy = [ "multi-user.target" ];
serviceConfig = {
ExecStart = "${pkgs.webhook}/bin/webhook -hooks ${hooksJson} -verbose -ip 127.0.0.1";
User = "c3d2-dns";
Group = config.users.users.c3d2-dns.group;
PrivateTmp = true;
ProtectSystem = "full";
};
};
}