this repo has no description
0
fork

Configure Feed

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

feat: basic status tracking with per host state and mutex

Simple implementation for now, unless we need complex state tracking
later, we don't need dedicated state manager.

Khue Doan 21d54c00 9edc7eb6

+38 -2
+27 -1
internal/hosts/config.go
··· 5 5 "fmt" 6 6 "net" 7 7 "os" 8 + "sync" 9 + ) 10 + 11 + type State int 12 + 13 + const ( 14 + // TODO maybe catch pixiecore events and/or ping to add booting/booted state 15 + StateUnknown State = iota 16 + StateInstalling 17 + StateInstalled 18 + StateFailed 8 19 ) 9 20 10 21 type Host struct { 11 22 MACAddress net.HardwareAddr `json:"mac_address"` 23 + State State `json:"-"` 24 + mu sync.RWMutex `json:"-"` 12 25 } 13 26 14 - type HostsConfig map[string]Host 27 + type HostsConfig map[string]*Host 15 28 16 29 func (h *Host) UnmarshalJSON(data []byte) error { 17 30 var aux struct { ··· 28 41 } 29 42 30 43 h.MACAddress = mac 44 + h.State = StateUnknown 31 45 return nil 46 + } 47 + 48 + func (h *Host) GetState() State { 49 + h.mu.RLock() 50 + defer h.mu.RUnlock() 51 + return h.State 52 + } 53 + 54 + func (h *Host) SetState(state State) { 55 + h.mu.Lock() 56 + defer h.mu.Unlock() 57 + h.State = state 32 58 } 33 59 34 60 func LoadHostsConfig(filename string) (HostsConfig, error) {
+11 -1
internal/serve/api.go
··· 47 47 return 48 48 } 49 49 flake := fmt.Sprintf("%s#%s", api.flake, flakeOutput) 50 + host := api.hostsConfig[flakeOutput] 51 + 52 + // TODO need better condition here 53 + if host.GetState() != hosts.StateUnknown { 54 + http.Error(w, "installation already in progress", http.StatusConflict) 55 + return 56 + } 57 + host.SetState(hosts.StateInstalling) 50 58 51 59 log.Info("installing NixOS", "host", ip, "flake", flake) 52 60 go func() { 53 61 // TODO IMPORTANT support SSH key 54 - if err := nixos.Install(api.ctx, "./examples#machine1", "root", ip, "nixos-installer", api.debug); err != nil { 62 + if err := nixos.Install(api.ctx, flake, "root", ip, "nixos-installer", api.debug); err != nil { 55 63 log.Error("failed to install NixOS", "ip", ip, "flake", flake, "error", err) 64 + host.SetState(hosts.StateInstalled) 56 65 } else { 57 66 log.Info("successfully installed NixOS", "ip", ip, "flake", flake) 67 + host.SetState(hosts.StateFailed) 58 68 } 59 69 }() 60 70