1
0
Fork 0
mirror of https://github.com/mdlayher/homelab.git synced 2024-12-14 11:47:32 +00:00

nixos/lib/vargen: infer server and WAN addresses automatically

Signed-off-by: Matt Layher <mdlayher@gmail.com>
This commit is contained in:
Matt Layher 2020-05-29 16:22:40 -04:00
parent e39cbef56a
commit 6d343cab9d
No known key found for this signature in database
GPG key ID: 77BFE531397EDE94
3 changed files with 37 additions and 12 deletions

View file

@ -4,9 +4,12 @@ package main
import (
"encoding/json"
"fmt"
"io/ioutil"
"log"
"net"
"net/http"
"os"
"strings"
"github.com/mdlayher/netx/eui64"
"inet.af/netaddr"
@ -34,18 +37,23 @@ func main() {
lan0 = newSubnet("lan0", 10)
iot0 = newSubnet("iot0", 66)
tengb0 = newSubnet("tengb0", 100)
server = newHost(
"servnerr-3",
tengb0,
ip("192.168.100.5"),
mac("90:e2:ba:5b:99:80"),
)
)
// Set up the output structure and create host/infra records.
out := output{
// TODO: this is a hack, we should make a Service type or similar.
ServerIPv4: server.IPv4,
ServerIPv6: server.IPv6.GUA,
Hosts: hosts{
Servers: []host{
newHost(
"servnerr-3",
tengb0,
ip("192.168.100.5"),
mac("90:e2:ba:5b:99:80"),
),
server,
newHost(
"nerr-3",
tengb0,
@ -107,8 +115,7 @@ func main() {
// section as it has different rules.
out.Interfaces["wan0"] = iface{
Name: "enp1s0",
// TODO: compute WAN addresses automatically?
IPv4: ip("24.176.57.23"),
IPv4: wanIPv4(),
}
// Marshal human-readable JSON for nicer git diffs.
@ -119,7 +126,24 @@ func main() {
}
}
func wanIPv4() netaddr.IP {
res, err := http.Get("https://ipv4.icanhazip.com")
if err != nil {
log.Fatalf("failed to perform HTTP request: %v", err)
}
defer res.Body.Close()
b, err := ioutil.ReadAll(res.Body)
if err != nil {
log.Fatalf("failed to read HTTP body: %v", err)
}
return ip(strings.TrimSpace(string(b)))
}
type output struct {
ServerIPv4 netaddr.IP `json:"server_ipv4"`
ServerIPv6 netaddr.IP `json:"server_ipv6"`
Hosts hosts `json:"hosts"`
Interfaces map[string]iface `json:"interfaces"`
}

View file

@ -1,4 +1,6 @@
{
"server_ipv4": "192.168.100.5",
"server_ipv6": "2600:6c4a:7880:3264:92e2:baff:fe5b:9980",
"hosts": {
"servers": [
{

View file

@ -1,13 +1,12 @@
# Variables referenced two or more places in the configuration.
let
# TODO: remove and pull from generated data.
server_ipv4 = "192.168.1.4";
server_ipv6 = "2600:6c4a:7880:3200:1e1b:dff:feea:830f";
# Import computed host/interface data from vars.json.
gen = builtins.fromJSON (builtins.readFile ./vars.json);
server_ipv4 = gen.server_ipv4;
server_ipv6 = gen.server_ipv6;
hosts = gen.hosts;
interfaces = gen.interfaces;
in {
inherit server_ipv4;
inherit server_ipv6;