this repo has no description
0
fork

Configure Feed

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

feat: shutdown once all hosts are installed

Khue Doan cbb3853e 89cbef76

+32 -4
+9 -3
cmd/nixie/main.go
··· 71 71 }() 72 72 log.Info("PXE server started", "address", address) 73 73 74 + doneCh := make(chan struct{}, 1) 74 75 go func() { 75 - if err := serve.StartAPIServer(ctx, hostsConfig, flags.Flake, flags.Debug); err != nil { 76 + if err := serve.StartAPIServer(ctx, hostsConfig, flags.Flake, flags.Debug, doneCh); err != nil { 76 77 log.Fatal("failed to start API server", "error", err) 77 78 } 78 79 }() ··· 80 81 sigCh := make(chan os.Signal, 1) 81 82 signal.Notify(sigCh, syscall.SIGINT, syscall.SIGTERM) 82 83 83 - sig := <-sigCh 84 - log.Info("signal received, shutting down", "signal", sig) 84 + select { 85 + case sig := <-sigCh: 86 + log.Info("signal received, shutting down", "signal", sig) 87 + case <-doneCh: 88 + log.Info("all hosts installed, shutting down") 89 + } 90 + 85 91 pxeServer.Shutdown() 86 92 87 93 log.Info("nixie stopped gracefully")
+10
internal/hosts/config.go
··· 79 79 } 80 80 return "", fmt.Errorf("unknown MAC address: %s", macAddress) 81 81 } 82 + 83 + func AllInstalled(hostsConfig HostsConfig) bool { 84 + for _, host := range hostsConfig { 85 + if host.GetState() != StateInstalled { 86 + return false 87 + } 88 + } 89 + 90 + return true 91 + }
+13 -1
internal/serve/api.go
··· 18 18 hostsConfig hosts.HostsConfig 19 19 flake string 20 20 debug bool 21 + doneCh chan struct{} 21 22 } 22 23 23 24 type InstallRequest struct { ··· 66 67 log.Info("successfully installed NixOS", "ip", ip, "flake", flake) 67 68 host.SetState(hosts.StateInstalled) 68 69 } 70 + 71 + if hosts.AllInstalled(api.hostsConfig) { 72 + log.Debug("all hosts installed, signaling completion") 73 + select { 74 + case api.doneCh <- struct{}{}: 75 + log.Debug("completion signal sent", "channel", "doneCh") 76 + default: 77 + log.Debug("completion already signaled, skipping") 78 + } 79 + } 69 80 }() 70 81 71 82 w.WriteHeader(http.StatusAccepted) ··· 80 91 return mux 81 92 } 82 93 83 - func StartAPIServer(ctx context.Context, hostsConfig hosts.HostsConfig, flake string, debug bool) error { 94 + func StartAPIServer(ctx context.Context, hostsConfig hosts.HostsConfig, flake string, debug bool, doneCh chan struct{}) error { 84 95 api := &API{ 85 96 ctx: ctx, 86 97 hostsConfig: hostsConfig, 87 98 flake: flake, 88 99 debug: debug, 100 + doneCh: doneCh, 89 101 } 90 102 91 103 server := &http.Server{