this repo has no description
0
fork

Configure Feed

Select the types of activity you want to include in your feed.

feat(api): find flake output by MAC address

Khue Doan c9124a16 699ff766

+21 -4
+1 -1
cmd/nixie/main.go
··· 72 72 log.Info("PXE server started", "address", address) 73 73 74 74 go func() { 75 - if err := serve.StartAPIServer(ctx, hostsConfig, flags.Debug); err != nil { 75 + if err := serve.StartAPIServer(ctx, hostsConfig, flags.Flake, flags.Debug); err != nil { 76 76 log.Fatal("failed to start API server", "error", err) 77 77 } 78 78 }()
+9
internal/hosts/config.go
··· 44 44 45 45 return hostsConfig, nil 46 46 } 47 + 48 + func GetFlakeOutputByMAC(macAddress string, hostsConfig HostsConfig) (string, error) { 49 + for flake, config := range hostsConfig { 50 + if config.MACAddress.String() == macAddress { 51 + return flake, nil 52 + } 53 + } 54 + return "", fmt.Errorf("unknown MAC address: %s", macAddress) 55 + }
+11 -3
internal/serve/api.go
··· 3 3 import ( 4 4 "context" 5 5 "encoding/json" 6 + "fmt" 6 7 "io" 7 8 "net" 8 9 "net/http" ··· 15 16 type API struct { 16 17 ctx context.Context 17 18 hostsConfig hosts.HostsConfig 19 + flake string 18 20 debug bool 19 21 } 20 22 ··· 38 40 39 41 ip := extractClientIP(r) 40 42 log.Info("received install request from agent", "ip", ip, "request", installRequest) 41 - // TODO IMPORTANT lookup flake name based on mac address 42 - flake := "./examples#machine1" 43 + flakeOutput, err := hosts.GetFlakeOutputByMAC(installRequest.MACAddress, api.hostsConfig) 44 + if err != nil { 45 + log.Error("failed to get flake by MAC address", "err", err) 46 + http.Error(w, err.Error(), http.StatusNotFound) 47 + return 48 + } 49 + flake := fmt.Sprintf("%s#%s", api.flake, flakeOutput) 43 50 44 51 log.Info("installing NixOS", "host", ip, "flake", flake) 45 52 go func() { ··· 63 70 return mux 64 71 } 65 72 66 - func StartAPIServer(ctx context.Context, hostsConfig hosts.HostsConfig, debug bool) error { 73 + func StartAPIServer(ctx context.Context, hostsConfig hosts.HostsConfig, flake string, debug bool) error { 67 74 api := &API{ 68 75 ctx: ctx, 69 76 hostsConfig: hostsConfig, 77 + flake: flake, 70 78 debug: debug, 71 79 } 72 80