Radish alpha
r
rad:z254T5p17bdFPmzfDojsdjo4HjpoZ
Radicle Infrastructure as Code (NixOS, OpenTofu, …)
Radicle
Git
bootstrap: Rewrite in Nu
Lorenz Leutgeb committed 1 day ago
commit b8db72c4706dbac269f397f14739b8f5ea38a5bc
parent 4bb9d9b
5 files changed +46 -39
modified flake.nix
@@ -164,6 +164,7 @@
        self.checks.${system}.pre-commit.enabledPackages
        ++ (with pkgs; [
          dnscontrol
+
          nushell
        ]);
    };

modified os/host/dev/seed/bootstrap/addresses.sql
@@ -1,12 +1,14 @@
-
select json_object(a1.node, json_group_array (a2.value))
+
select
+
	                                        node,
+
	substr(value, 0, instr(value, ':'))  as host,
+
	substr(value, instr(value, ':') + 1) as port

from
-
	           addresses a1
-
	inner join addresses a2 using (node)
+
	addresses

where
-
	a1.banned = false and
-
	a1.type != 'onion' and
-
	unixepoch() - (a1.last_success / 1000) < (60 * 60 * 24 * 7)
+
	banned = false and
+
	type == 'dns' and
+
	unixepoch() - (last_success / 1000) < (60 * 60 * 24 * 7)

-
group by a1.node
+
order by node

\ No newline at end of file
modified os/host/dev/seed/bootstrap/default.nix
@@ -8,22 +8,23 @@
    services.radicle-bootstrap = {
      serviceConfig.Type = "oneshot";
      script = ''
-
        cat "${./addresses.sql}" | sqlite3 "/var/lib/radicle/node/node.db" | \
-
          jq --slurp add | \
-
          jq \
-
            --raw-output \
-
            --arg serviceName "_radicle-node.tcp" \
-
            --from-file "${./zone.jq}" \
-
            > "/etc/knot/dns-sd.zone"
+
        nu ${./zone.nu} \
+
          --db "/var/lib/radicle/node/node.db" \
+
          --query ${./addresses.sql} \
+
          --zone "/etc/knot/dns-sd.zone"

        knotc -b zone-reload "bootstrap.radicle.network"
      '';
+
      after = ["systemd-resolved.service"];
      wants = ["knot.service"];
-
      requires = ["knot.service"];
+
      requires = [
+
        "knot.service"
+
        "systemd-resolved.service"
+
      ];
      path = with pkgs; [
-
        jq
-
        sqlite
        knot-dns
+
        nushell
+
        config.systemd.package
      ];
    };
    timers.radicle-bootstrap = {
deleted os/host/dev/seed/bootstrap/zone.jq
@@ -1,22 +0,0 @@
-
to_entries
-
| .[]
-
| .key as $nid
-
| "_radicle-node._tcp" as $serviceName
-
| "\($nid).\($serviceName)" as $name
-
| [
-
    "\($serviceName) PTR \($name)",
-

-
    "\($name) TXT \"nid=\($nid)\"",
-
    "\($name) TXT \"version=1\"",
-
    "\($name) TXT \"network=main\"",
-

-
    (.value
-
     | unique
-
     | to_entries[]
-
     | .key as $weight
-
     | (.value | capture("(?<host>[^:]+):(?<port>[0-9]+)"))
-
     | "\($nid).\($serviceName) SRV 0 \($weight) \(.port) \(.host)."),
-

-
     ""
-
]
-
| .[]
added os/host/dev/seed/bootstrap/zone.nu
@@ -0,0 +1,25 @@
+
#! /usr/bin/env nu
+

+
def main [
+
  --db:    path # Path of node database to query
+
  --query: path # Path of query to run against database
+
  --zone:  path # Path of the zone file to (over)write
+
] {
+
  open $db |
+
  query db $"(open $query)" |
+
  where {|row|
+
    (^resolvectl query --cname=false $row.host | complete).exit_code == 0
+
  } |
+
  each {|row|
+
    let serviceName = "_radicle-node._tcp"
+
    let name = $"($row.node).($serviceName)"
+
    $"
+
($serviceName) PTR ($name)
+
($name) TXT \"nid=($row.node)\"
+
($name) TXT \"version=1\"
+
($name) TXT \"network=main\"
+
($name) SRV 0 1 ($row.port) ($row.host)."
+
  } |
+
  str join "\n" |
+
  save -f $zone
+
}