| 1 | package main |
| 2 | |
| 3 | // The host's unhealthy automations for valley pending, read from the |
| 4 | // telemetry collector's sources operation concurrently with origin and |
| 5 | // worker check costs (checkcost.go). |
| 6 | // |
| 7 | // Each source the collector reports as failed, stale or unavailable |
| 8 | // prints as one host entry above needs you, beside main's state lines, |
| 9 | // once for the host whatever projects the listing covers: |
| 10 | // |
| 11 | // host mirrors failed · valley-publish@app.service: activating, result exit-code, status 1 |
| 12 | // see: valley telemetry read --source mirrors --logs |
| 13 | // |
| 14 | // Healthy (idle, active) and disabled sources print nothing. When several |
| 15 | // units of a source are failing, each failing unit is named on its own |
| 16 | // line under the source, up to maxHostUnits, then "and N more". |
| 17 | // |
| 18 | // A caller without the observe grant, a host with no collector, or a |
| 19 | // collector that times out or sends an incomplete answer leaves pending |
| 20 | // with no host entry and no new warning. No read outlives the command |
| 21 | // that started it. |
| 22 | |
| 23 | import ( |
| 24 | "bufio" |
| 25 | "context" |
| 26 | "encoding/json" |
| 27 | "errors" |
| 28 | "fmt" |
| 29 | "io" |
| 30 | "sync" |
| 31 | "time" |
| 32 | |
| 33 | "the-valley/telemetry" |
| 34 | ) |
| 35 | |
| 36 | // hostSourcesTimeout bounds how long valley pending waits for the |
| 37 | // collector's sources answer. |
| 38 | var hostSourcesTimeout = 10 * time.Second |
| 39 | |
| 40 | // maxHostUnits is the most failing unit names listed under one host |
| 41 | // source before summarising the rest as "and N more". |
| 42 | const maxHostUnits = 5 |
| 43 | |
| 44 | // jsonHostSource is one unhealthy host automation in valley-pending/v1's |
| 45 | // host_sources array: a telemetry source whose state is failed, stale or |
| 46 | // unavailable, with the collector's own reason and any failing units. |
| 47 | type jsonHostSource struct { |
| 48 | Source string `json:"source"` |
| 49 | State string `json:"state"` |
| 50 | Reason string `json:"reason"` |
| 51 | Units []string `json:"units"` |
| 52 | } |
| 53 | |
| 54 | // hostSources reads the serving host's telemetry sources once in the |
| 55 | // background until close. |
| 56 | type hostSources struct { |
| 57 | done chan struct{} |
| 58 | entries []jsonHostSource |
| 59 | cancel context.CancelFunc |
| 60 | wg sync.WaitGroup |
| 61 | } |
| 62 | |
| 63 | // startHostSources starts one bounded read of the serving host's |
| 64 | // telemetry sources, or returns immediately with none when no source |
| 65 | // reaches a host over ssh. |
| 66 | func startHostSources(g git, list []source) *hostSources { |
| 67 | dest, ok := hostTelemetryDest(g, list) |
| 68 | if !ok { |
| 69 | done := make(chan struct{}) |
| 70 | close(done) |
| 71 | return &hostSources{done: done, entries: []jsonHostSource{}, cancel: func() {}} |
| 72 | } |
| 73 | ctx, cancel := context.WithTimeout(context.Background(), hostSourcesTimeout) |
| 74 | h := &hostSources{done: make(chan struct{}), entries: []jsonHostSource{}, cancel: cancel} |
| 75 | h.wg.Add(1) |
| 76 | go func() { |
| 77 | defer h.wg.Done() |
| 78 | defer close(h.done) |
| 79 | if entries, err := readHostSources(ctx, dest); err == nil && len(entries) > 0 { |
| 80 | h.entries = entries |
| 81 | } |
| 82 | }() |
| 83 | return h |
| 84 | } |
| 85 | |
| 86 | // hostTelemetryDest resolves the serving host's ssh destination for |
| 87 | // telemetry: g's own telemetry remote first (honouring |
| 88 | // valley.telemetry.remote in the current checkout), else the first |
| 89 | // readable repository in list that resolves to ssh. |
| 90 | func hostTelemetryDest(g git, list []source) (sshDest, bool) { |
| 91 | if dest, err := telemetryRemote(g, ""); err == nil { |
| 92 | return dest, true |
| 93 | } |
| 94 | for _, s := range list { |
| 95 | if s.err != nil { |
| 96 | continue |
| 97 | } |
| 98 | if dest, err := telemetryRemote(s.g, ""); err == nil { |
| 99 | return dest, true |
| 100 | } |
| 101 | } |
| 102 | return sshDest{}, false |
| 103 | } |
| 104 | |
| 105 | // wait returns the unhealthy host sources once the read ends, or an empty |
| 106 | // slice when all sources are healthy or the collector could not be read. |
| 107 | func (h *hostSources) wait() []jsonHostSource { |
| 108 | if h == nil { |
| 109 | return []jsonHostSource{} |
| 110 | } |
| 111 | waited := phase("telemetry") |
| 112 | <-h.done |
| 113 | waited() |
| 114 | if h.entries == nil { |
| 115 | return []jsonHostSource{} |
| 116 | } |
| 117 | return h.entries |
| 118 | } |
| 119 | |
| 120 | // close cancels the read if it is still running and waits for its ssh to |
| 121 | // end. |
| 122 | func (h *hostSources) close() { |
| 123 | if h == nil { |
| 124 | return |
| 125 | } |
| 126 | h.cancel() |
| 127 | h.wg.Wait() |
| 128 | } |
| 129 | |
| 130 | // readHostSources runs valley-telemetry sources on dest and parses its |
| 131 | // answer. Any error, refusal, or incomplete answer is returned so the |
| 132 | // caller drops the read without warning. |
| 133 | func readHostSources(ctx context.Context, dest sshDest) ([]jsonHostSource, error) { |
| 134 | req := telemetry.Request{V: 1, Op: telemetry.OpSources} |
| 135 | ssh := sshCommand(ctx, dest, req.Command()) |
| 136 | out, err := ssh.StdoutPipe() |
| 137 | if err != nil { |
| 138 | return nil, err |
| 139 | } |
| 140 | defer context.AfterFunc(ctx, func() { out.Close() })() |
| 141 | if err := ssh.Start(); err != nil { |
| 142 | return nil, err |
| 143 | } |
| 144 | entries, err := parseHostSources(out) |
| 145 | io.Copy(io.Discard, out) |
| 146 | waitErr := ssh.Wait() |
| 147 | if err != nil { |
| 148 | return nil, err |
| 149 | } |
| 150 | if waitErr != nil { |
| 151 | return nil, waitErr |
| 152 | } |
| 153 | return entries, nil |
| 154 | } |
| 155 | |
| 156 | // parseHostSources reads one sources answer, which must end, and keeps |
| 157 | // every source whose state is failed, stale or unavailable. |
| 158 | func parseHostSources(r io.Reader) ([]jsonHostSource, error) { |
| 159 | sc := bufio.NewScanner(r) |
| 160 | sc.Buffer(make([]byte, 64<<10), telemetry.MaxResponseBytes) |
| 161 | entries := []jsonHostSource{} |
| 162 | for sc.Scan() { |
| 163 | var m struct { |
| 164 | Kind string `json:"kind"` |
| 165 | Name string `json:"name"` |
| 166 | State string `json:"state"` |
| 167 | Detail string `json:"detail"` |
| 168 | UnitStates []map[string]any `json:"unit_states"` |
| 169 | Error string `json:"error"` |
| 170 | } |
| 171 | if json.Unmarshal(sc.Bytes(), &m) != nil { |
| 172 | return nil, errors.New("an answer line is not JSON") |
| 173 | } |
| 174 | switch m.Kind { |
| 175 | case "error": |
| 176 | return nil, errors.New(telemetry.Sanitize(m.Error)) |
| 177 | case "end": |
| 178 | return entries, nil |
| 179 | case "source": |
| 180 | switch m.State { |
| 181 | case "failed", "stale", "unavailable": |
| 182 | default: |
| 183 | continue |
| 184 | } |
| 185 | name := readable(telemetry.Sanitize(m.Name)) |
| 186 | if name == "" { |
| 187 | continue |
| 188 | } |
| 189 | entries = append(entries, jsonHostSource{ |
| 190 | Source: name, |
| 191 | State: m.State, |
| 192 | Reason: readable(telemetry.Sanitize(m.Detail)), |
| 193 | Units: failingUnits(m.UnitStates), |
| 194 | }) |
| 195 | } |
| 196 | } |
| 197 | return nil, errors.New("the answer did not end") |
| 198 | } |
| 199 | |
| 200 | // failingUnits returns the unit IDs in states that are failing by the |
| 201 | // collector's own rule (telemetry/valley-telemetry/sources.go): active |
| 202 | // state failed, or a non-empty result other than success. |
| 203 | func failingUnits(states []map[string]any) []string { |
| 204 | units := []string{} |
| 205 | for _, u := range states { |
| 206 | active, _ := u["ActiveState"].(string) |
| 207 | result, _ := u["Result"].(string) |
| 208 | if active == "failed" || (result != "" && result != "success") { |
| 209 | id, _ := u["Id"].(string) |
| 210 | if id = readable(telemetry.Sanitize(id)); id != "" { |
| 211 | units = append(units, id) |
| 212 | } |
| 213 | } |
| 214 | } |
| 215 | return units |
| 216 | } |
| 217 | |
| 218 | // hostLines renders the unhealthy host sources as valley pending's host |
| 219 | // entries, beside main's state lines. |
| 220 | func (t terminal) hostLines(entries []jsonHostSource) []string { |
| 221 | var lines []string |
| 222 | for _, h := range entries { |
| 223 | colour := colourRefused |
| 224 | if h.State == "stale" { |
| 225 | colour = colourWorking |
| 226 | } |
| 227 | line := "host " + t.strong(h.Source) + " " + t.tint(colour, h.State) |
| 228 | if h.Reason != "" { |
| 229 | line += " · " + h.Reason |
| 230 | } |
| 231 | lines = append(lines, line) |
| 232 | if len(h.Units) > 1 { |
| 233 | shown := min(len(h.Units), maxHostUnits) |
| 234 | for _, u := range h.Units[:shown] { |
| 235 | lines = append(lines, " "+u) |
| 236 | } |
| 237 | if rest := len(h.Units) - shown; rest > 0 { |
| 238 | lines = append(lines, fmt.Sprintf(" and %d more", rest)) |
| 239 | } |
| 240 | } |
| 241 | see := "valley telemetry sources" |
| 242 | if telemetry.ValidSource(h.Source) { |
| 243 | see = "valley telemetry read --source " + h.Source + " --logs" |
| 244 | } |
| 245 | lines = append(lines, " see: "+see) |
| 246 | } |
| 247 | return lines |
| 248 | } |