vole
Clone
Read only, no credentials:
https://rodent.software/vole.git
Write over SSH with a registered key:
git clone ssh://git@rodent.software:2222/vole.git
A free and open-source forge that lives in your Git repo.
Read a run and its log from the CLI
92d8afc86ab7a5e8f2e56a8981e0614aa55980f1 · Keenan Nemetz · 3h ago · parent 4b8c9e1d
Read a run and its log from the CLI The queue and its logs live on the instance, so the CLI now has read verbs beside the cancel and rerun writes: run list, run show and run logs. They check the repository's read capability, scope an id to its repository, and stream a log to stdout with refusals on stderr. change show lists the runs for the head and, when the landing needs one, the combined result, so a red check carries the id needed to open it. The queue search narrows by status, branch and commit prefix without a pager. closes: 01M6YZAQZVJWTA9BA5RV2H6PQ8
server/ssh/run.go +455−33
| @@ -1,22 +1,29 @@ | |||
| 1 | 1 | package ssh | |
| 2 | 2 | ||
| 3 | // Managing a run over the SSH channel. | ||
| 3 | // Reading and managing a run over the SSH channel. | ||
| 4 | 4 | // | |
| 5 | // vole-run-list <repository> <status|-> <branch|-> <sha|-> <limit> | ||
| 6 | // vole-run-show <repository> <run-id> | ||
| 7 | // vole-run-log <repository> <run-id> <attempt|0> <step|0> | ||
| 5 | 8 | // vole-run-cancel <repository> <run-id> | |
| 6 | 9 | // vole-run-rerun <repository> <run-id> | |
| 7 | 10 | // | |
| 8 | // Canceling and rerunning are writes to the queue, and the queue is instance-side, | ||
| 9 | // so the CLI asks the server the same way `vole change merge` does. They are the | ||
| 10 | // counterparts of the browser's buttons on a run page: the same account that may act | ||
| 11 | // from the page may act here, the same status decides what the command means, and | ||
| 12 | // the audit record names the account, the repository, and the run. | ||
| 11 | // The queue and its logs are instance-side, so the CLI asks the server the same way | ||
| 12 | // `vole change merge` does. The read verbs are the CLI half of the runs page and a | ||
| 13 | // run page's log; the write verbs are the counterparts of its buttons. The same | ||
| 14 | // account that may look at a page may look here, the same status decides what a | ||
| 15 | // command means, and the audit record names the account, the repository, and the run. | ||
| 13 | 16 | // | |
| 14 | // The verbs are not a shell. Each takes one repository and one run id and answers one | ||
| 15 | // line: a queued run is retired, a running one is asked to stop at its runner's next | ||
| 16 | // heartbeat, and a finished one can be queued again with the same pinned job. | ||
| 17 | // The verbs are not a shell. Each takes one repository and, except for list, one run | ||
| 18 | // id, and answers one line or one log. A reader sees the same rows the browser shows | ||
| 19 | // for that repository; a run from another repository is not found, even when the id | ||
| 20 | // is known. | ||
| 17 | 21 | ||
| 18 | 22 | import ( | |
| 19 | 23 | "fmt" | |
| 24 | "io" | ||
| 25 | "os" | ||
| 26 | "strconv" | ||
| 20 | 27 | "strings" | |
| 21 | 28 | "time" | |
| 22 | 29 | ||
| @@ -24,41 +31,78 @@ import ( | |||
| 24 | 31 | ||
| 25 | 32 | "rodent.software/vole/ci/runs" | |
| 26 | 33 | "rodent.software/vole/core/id" | |
| 34 | "rodent.software/vole/forge/access" | ||
| 27 | 35 | "rodent.software/vole/internal/audit" | |
| 36 | "rodent.software/vole/internal/format" | ||
| 28 | 37 | ) | |
| 29 | 38 | ||
| 30 | // openRunFor resolves the target of a run-management verb. It writes the refusal to | ||
| 31 | // ch and reports false when the caller should stop. Both verbs ask the same | ||
| 32 | // questions in the same order, so a name that is refused for one is refused for the | ||
| 33 | // other with the same words: the account, the repository, the maintainer capability, | ||
| 34 | // and then the run. | ||
| 35 | func (s *Server) openRunFor(ch xssh.Channel, argv []string, user, usage string) (*runs.Run, string, bool) { | ||
| 39 | // maxRunListLimit bounds one listing. A diagnosis reads the newest few runs; a | ||
| 40 | // larger answer is a page, and the runs page already has one. | ||
| 41 | const maxRunListLimit = 200 | ||
| 42 | |||
| 43 | // openRunScope resolves the repository and capability shared by every run verb. It | ||
| 44 | // writes the refusal to out and reports false when the caller should stop. Reading a | ||
| 45 | // run is the repository's read capability, the same rule the runs page uses; managing | ||
| 46 | // one is checked separately by openRunFor, because a reader may look and may not act. | ||
| 47 | func (s *Server) openRunScope(out io.Writer, argv []string, user, usage string) (string, *access.ACL, bool) { | ||
| 36 | 48 | if len(argv) != 3 { | |
| 37 | fmt.Fprintf(ch, "vole: usage: %s\n", usage) | ||
| 38 | return nil, "", false | ||
| 49 | fmt.Fprintf(out, "vole: usage: %s\n", usage) | ||
| 50 | return "", nil, false | ||
| 39 | 51 | } | |
| 40 | 52 | name, ok := repoName(argv[1]) | |
| 41 | 53 | if !ok { | |
| 42 | fmt.Fprint(ch, "vole: invalid repository name\n") | ||
| 43 | return nil, "", false | ||
| 54 | fmt.Fprint(out, "vole: invalid repository name\n") | ||
| 55 | return "", nil, false | ||
| 44 | 56 | } | |
| 45 | 57 | runID := strings.TrimSpace(argv[2]) | |
| 46 | 58 | if !id.Valid(runID) { | |
| 47 | fmt.Fprintf(ch, "vole: %q is not a run id; use the 26-character id from the run page\n", runID) | ||
| 48 | return nil, "", false | ||
| 59 | fmt.Fprintf(out, "vole: %q is not a run id; use the 26-character id from the run page\n", runID) | ||
| 60 | return "", nil, false | ||
| 49 | 61 | } | |
| 50 | 62 | if s.cfg.Runs == nil { | |
| 51 | fmt.Fprint(ch, "vole: this instance has no CI queue\n") | ||
| 52 | return nil, "", false | ||
| 63 | fmt.Fprint(out, "vole: this instance has no CI queue\n") | ||
| 64 | return "", nil, false | ||
| 53 | 65 | } | |
| 54 | 66 | m, err := s.cfg.Repos.Open(name) | |
| 55 | 67 | if err != nil { | |
| 56 | fmt.Fprintf(ch, "vole: repository %q not found\n", name) | ||
| 57 | return nil, "", false | ||
| 68 | fmt.Fprintf(out, "vole: repository %q not found\n", name) | ||
| 69 | return "", nil, false | ||
| 58 | 70 | } | |
| 59 | 71 | a, err := m.ACL() | |
| 60 | 72 | if err != nil { | |
| 61 | fmt.Fprintf(ch, "vole: cannot read access rules for %s\n", name) | ||
| 73 | fmt.Fprintf(out, "vole: cannot read access rules for %s\n", name) | ||
| 74 | return "", nil, false | ||
| 75 | } | ||
| 76 | role := a.EffectiveRole(user, s.cfg.Identity != nil && s.cfg.Identity.IsAdmin(user)) | ||
| 77 | if !role.AtLeast(access.Read) { | ||
| 78 | fmt.Fprintf(out, "vole: %s may not read runs in %s\n", user, name) | ||
| 79 | return "", nil, false | ||
| 80 | } | ||
| 81 | return name, a, true | ||
| 82 | } | ||
| 83 | |||
| 84 | // runIn fetches the run named by argv, scoped to name. An id from another repository | ||
| 85 | // is a miss, not a cross-repository read. | ||
| 86 | func (s *Server) runIn(out io.Writer, name, runID string) (*runs.Run, bool) { | ||
| 87 | run, err := s.cfg.Runs.Get(runID) | ||
| 88 | if err != nil { | ||
| 89 | fmt.Fprintf(out, "vole: cannot read run %s: %v\n", runID, err) | ||
| 90 | return nil, false | ||
| 91 | } | ||
| 92 | if run == nil || run.Repo != name { | ||
| 93 | fmt.Fprintf(out, "vole: no run %s in %s\n", runID, name) | ||
| 94 | return nil, false | ||
| 95 | } | ||
| 96 | return run, true | ||
| 97 | } | ||
| 98 | |||
| 99 | // openRunFor resolves the target of a run-management verb. Both verbs ask the same | ||
| 100 | // questions in the same order, so a name that is refused for one is refused for the | ||
| 101 | // other with the same words: the account, the repository, the maintainer capability, | ||
| 102 | // and then the run. | ||
| 103 | func (s *Server) openRunFor(ch xssh.Channel, argv []string, user, usage string) (*runs.Run, string, bool) { | ||
| 104 | name, a, ok := s.openRunScope(ch, argv, user, usage) | ||
| 105 | if !ok { | ||
| 62 | 106 | return nil, "", false | |
| 63 | 107 | } | |
| 64 | 108 | // Managing a run is the maintainer capability, not triage of one item. This is | |
| @@ -67,18 +111,328 @@ func (s *Server) openRunFor(ch xssh.Channel, argv []string, user, usage string) | |||
| 67 | 111 | fmt.Fprintf(ch, "vole: %s may not manage runs in %s; only a maintainer or admin may manage CI\n", user, name) | |
| 68 | 112 | return nil, "", false | |
| 69 | 113 | } | |
| 70 | run, err := s.cfg.Runs.Get(runID) | ||
| 71 | if err != nil { | ||
| 72 | fmt.Fprintf(ch, "vole: cannot read run %s: %v\n", runID, err) | ||
| 73 | return nil, "", false | ||
| 74 | } | ||
| 75 | if run == nil || run.Repo != name { | ||
| 76 | fmt.Fprintf(ch, "vole: no run %s in %s\n", runID, name) | ||
| 114 | run, ok := s.runIn(ch, name, strings.TrimSpace(argv[2])) | ||
| 115 | if !ok { | ||
| 77 | 116 | return nil, "", false | |
| 78 | 117 | } | |
| 79 | 118 | return run, name, true | |
| 80 | 119 | } | |
| 81 | 120 | ||
| 121 | // openRunForRead resolves one run for a verb that only looks. It is openRunScope plus | ||
| 122 | // the run itself; the read capability was already checked. | ||
| 123 | func (s *Server) openRunForRead(out io.Writer, argv []string, user, usage string) (*runs.Run, string, *access.ACL, bool) { | ||
| 124 | name, a, ok := s.openRunScope(out, argv, user, usage) | ||
| 125 | if !ok { | ||
| 126 | return nil, "", nil, false | ||
| 127 | } | ||
| 128 | run, ok := s.runIn(out, name, strings.TrimSpace(argv[2])) | ||
| 129 | if !ok { | ||
| 130 | return nil, "", nil, false | ||
| 131 | } | ||
| 132 | return run, name, a, true | ||
| 133 | } | ||
| 134 | |||
| 135 | // serveRunList answers the runs page as lines. The fields are tab-separated and fixed | ||
| 136 | // in order because the client formats them; they are the run page's own columns. | ||
| 137 | func (s *Server) serveRunList(ch xssh.Channel, argv []string, user string) int { | ||
| 138 | if len(argv) != 6 { | ||
| 139 | fmt.Fprint(ch, "vole: usage: vole-run-list <repository> <status|-> <branch|-> <sha|-> <limit>\n") | ||
| 140 | return 129 | ||
| 141 | } | ||
| 142 | name, ok := repoName(argv[1]) | ||
| 143 | if !ok { | ||
| 144 | fmt.Fprint(ch, "vole: invalid repository name\n") | ||
| 145 | return 129 | ||
| 146 | } | ||
| 147 | if s.cfg.Runs == nil { | ||
| 148 | fmt.Fprint(ch, "vole: this instance has no CI queue\n") | ||
| 149 | return 1 | ||
| 150 | } | ||
| 151 | m, err := s.cfg.Repos.Open(name) | ||
| 152 | if err != nil { | ||
| 153 | fmt.Fprintf(ch, "vole: repository %q not found\n", name) | ||
| 154 | return 1 | ||
| 155 | } | ||
| 156 | a, err := m.ACL() | ||
| 157 | if err != nil { | ||
| 158 | fmt.Fprintf(ch, "vole: cannot read access rules for %s\n", name) | ||
| 159 | return 1 | ||
| 160 | } | ||
| 161 | role := a.EffectiveRole(user, s.cfg.Identity != nil && s.cfg.Identity.IsAdmin(user)) | ||
| 162 | if !role.AtLeast(access.Read) { | ||
| 163 | fmt.Fprintf(ch, "vole: %s may not read runs in %s\n", user, name) | ||
| 164 | return 1 | ||
| 165 | } | ||
| 166 | |||
| 167 | status := dash(argv[2]) | ||
| 168 | if status != "" && !runs.ValidStatus(status) { | ||
| 169 | fmt.Fprintf(ch, "vole: %q is not a run status; use queued, running, success, failed, canceled or error\n", status) | ||
| 170 | return 129 | ||
| 171 | } | ||
| 172 | branch := dash(argv[3]) | ||
| 173 | sha := strings.ToLower(dash(argv[4])) | ||
| 174 | if sha != "" { | ||
| 175 | if len(sha) < 4 || len(sha) > 40 || !isHex(sha) { | ||
| 176 | fmt.Fprintf(ch, "vole: %q is not a commit id or prefix\n", argv[4]) | ||
| 177 | return 129 | ||
| 178 | } | ||
| 179 | } | ||
| 180 | limit, err := strconv.Atoi(dash(argv[5])) | ||
| 181 | if err != nil || limit < 1 || limit > maxRunListLimit { | ||
| 182 | fmt.Fprintf(ch, "vole: limit must be between 1 and %d\n", maxRunListLimit) | ||
| 183 | return 129 | ||
| 184 | } | ||
| 185 | |||
| 186 | list, err := s.cfg.Runs.Search(name, runs.SearchFilter{ | ||
| 187 | Status: status, Branch: branch, SHA: sha, Limit: limit, | ||
| 188 | }) | ||
| 189 | if err != nil { | ||
| 190 | fmt.Fprintf(ch, "vole: cannot read the queue: %v\n", err) | ||
| 191 | return 1 | ||
| 192 | } | ||
| 193 | for _, r := range list { | ||
| 194 | hasLog := "no" | ||
| 195 | if r.HasLog { | ||
| 196 | hasLog = "yes" | ||
| 197 | } | ||
| 198 | fmt.Fprintf(ch, "%s\t%s\t%s\t%s\t%s\t%s\t%s\t%s\t%d\t%d\t%s\t%s\n", | ||
| 199 | r.ID, r.Status, r.Workflow, r.Job, r.Branch, r.SHA, r.Trigger, | ||
| 200 | r.TriggerTitle(), r.ExitCode, r.Attempts, hasLog, r.Created.Format(time.RFC3339)) | ||
| 201 | } | ||
| 202 | return 0 | ||
| 203 | } | ||
| 204 | |||
| 205 | // serveRunShow answers one run the way the run page does: what it ran, how it ended, | ||
| 206 | // the steps that ran, the attempts that have a log, and whether the result counts as | ||
| 207 | // a check. It is the page a failure is diagnosed on, in text. | ||
| 208 | func (s *Server) serveRunShow(ch xssh.Channel, argv []string, user string) int { | ||
| 209 | run, name, acl, ok := s.openRunForRead(ch, argv, user, "vole-run-show <repository> <run-id>") | ||
| 210 | if !ok { | ||
| 211 | return 129 | ||
| 212 | } | ||
| 213 | |||
| 214 | rec, recErr := s.cfg.Runs.NewestAttempt(run.ID) | ||
| 215 | if recErr != nil { | ||
| 216 | fmt.Fprintf(ch, "vole: cannot read the attempt record: %v\n", recErr) | ||
| 217 | } | ||
| 218 | // A requeued run is on a newer attempt. The previous attempt's steps are history, | ||
| 219 | // reachable through its log; showing them as this attempt's would be a lie. | ||
| 220 | if rec != nil && rec.Attempt != run.Attempts { | ||
| 221 | rec = nil | ||
| 222 | } | ||
| 223 | logs, err := s.cfg.Runs.AttemptLogs(run.ID) | ||
| 224 | if err != nil { | ||
| 225 | fmt.Fprintf(ch, "vole: cannot read the logs: %v\n", err) | ||
| 226 | } | ||
| 227 | arts, err := s.cfg.Runs.Artifacts(run.ID) | ||
| 228 | if err != nil { | ||
| 229 | fmt.Fprintf(ch, "vole: cannot read the artifacts: %v\n", err) | ||
| 230 | } | ||
| 231 | att, err := s.cfg.Runs.Attestation(run.ID) | ||
| 232 | if err != nil { | ||
| 233 | fmt.Fprintf(ch, "vole: cannot read who reported the run: %v\n", err) | ||
| 234 | } | ||
| 235 | |||
| 236 | fmt.Fprintf(ch, "id: %s\n", run.ID) | ||
| 237 | fmt.Fprintf(ch, "repository: %s\n", name) | ||
| 238 | fmt.Fprintf(ch, "status: %s\n", runStatusLine(run)) | ||
| 239 | if run.FailureMessage != "" { | ||
| 240 | fmt.Fprintf(ch, "failure: %s\n", run.FailureMessage) | ||
| 241 | } | ||
| 242 | fmt.Fprintf(ch, "workflow: %s\n", nameAndPath(run.WorkflowTitle(), run.Workflow)) | ||
| 243 | fmt.Fprintf(ch, "job: %s\n", nameAndPath(run.JobTitle(), run.Job)) | ||
| 244 | fmt.Fprintf(ch, "commit: %s\n", commitLine(run)) | ||
| 245 | if t := run.TriggerTitle(); t != "" { | ||
| 246 | fmt.Fprintf(ch, "trigger: %s\n", t) | ||
| 247 | } | ||
| 248 | fmt.Fprintf(ch, "attempt: %d\n", run.Attempts) | ||
| 249 | fmt.Fprintf(ch, "created: %s\n", run.Created.Format(time.RFC3339)) | ||
| 250 | if !run.Started.IsZero() { | ||
| 251 | fmt.Fprintf(ch, "started: %s\n", run.Started.Format(time.RFC3339)) | ||
| 252 | } | ||
| 253 | if !run.Finished.IsZero() { | ||
| 254 | fmt.Fprintf(ch, "finished: %s\n", run.Finished.Format(time.RFC3339)) | ||
| 255 | } | ||
| 256 | fmt.Fprintf(ch, "duration: %s\n", format.Duration(run.Duration())) | ||
| 257 | if run.LeaseOwner != "" { | ||
| 258 | fmt.Fprintf(ch, "runner: %s\n", run.LeaseOwner) | ||
| 259 | } | ||
| 260 | counts, why := runCounts(run, acl, att) | ||
| 261 | if counts { | ||
| 262 | fmt.Fprint(ch, "counts: yes\n") | ||
| 263 | } else { | ||
| 264 | fmt.Fprintf(ch, "counts: no (%s)\n", why) | ||
| 265 | } | ||
| 266 | |||
| 267 | fmt.Fprint(ch, "steps:\n") | ||
| 268 | if rec == nil { | ||
| 269 | label := "not reported" | ||
| 270 | switch run.Status { | ||
| 271 | case runs.StatusQueued: | ||
| 272 | label = "queued" | ||
| 273 | case runs.StatusRunning: | ||
| 274 | label = "waiting" | ||
| 275 | case runs.StatusCanceled: | ||
| 276 | label = "not run" | ||
| 277 | } | ||
| 278 | for i, step := range run.Steps { | ||
| 279 | fmt.Fprintf(ch, " %d. %-24s %s\n", i+1, step.Name, label) | ||
| 280 | } | ||
| 281 | } else { | ||
| 282 | for i, step := range rec.Steps { | ||
| 283 | status, label := "ok", "" | ||
| 284 | if step.Exit != 0 { | ||
| 285 | status, label = "failed", fmt.Sprintf(" (exit %d)", step.Exit) | ||
| 286 | if run.Status == runs.StatusCanceled { | ||
| 287 | status, label = "stopped", "" | ||
| 288 | } | ||
| 289 | } | ||
| 290 | log := "" | ||
| 291 | if step.LogSize > 0 { | ||
| 292 | log = ", log " + format.Bytes(step.LogSize) | ||
| 293 | } | ||
| 294 | fmt.Fprintf(ch, " %d. %-24s %-8s %s%s%s\n", i+1, step.Name, status, | ||
| 295 | format.Duration(step.Duration()), log, label) | ||
| 296 | } | ||
| 297 | for i := len(rec.Steps); i < len(run.Steps); i++ { | ||
| 298 | fmt.Fprintf(ch, " %d. %-24s not run\n", i+1, run.Steps[i].Name) | ||
| 299 | } | ||
| 300 | } | ||
| 301 | |||
| 302 | if len(logs) == 0 { | ||
| 303 | fmt.Fprint(ch, "logs: (none)\n") | ||
| 304 | } else { | ||
| 305 | fmt.Fprint(ch, "logs:\n") | ||
| 306 | for _, l := range logs { | ||
| 307 | fmt.Fprintf(ch, " attempt %d %s %s\n", l.Attempt, format.Bytes(l.Size), l.Created.Format(time.RFC3339)) | ||
| 308 | } | ||
| 309 | } | ||
| 310 | if len(arts) == 0 { | ||
| 311 | fmt.Fprint(ch, "artifacts: (none)\n") | ||
| 312 | } else { | ||
| 313 | fmt.Fprint(ch, "artifacts:\n") | ||
| 314 | for _, art := range arts { | ||
| 315 | fmt.Fprintf(ch, " %s %s\n", art.Name, format.Bytes(art.Size)) | ||
| 316 | } | ||
| 317 | } | ||
| 318 | return 0 | ||
| 319 | } | ||
| 320 | |||
| 321 | // serveRunLog streams one attempt's log, or one step's byte range inside it, as the | ||
| 322 | // bytes the runner wrote. The output is the data, so every refusal goes to the | ||
| 323 | // session's stderr; a caller that redirects stdout gets a log or an empty file, never | ||
| 324 | // an error message at the top of it. | ||
| 325 | func (s *Server) serveRunLog(ch xssh.Channel, argv []string, user string) int { | ||
| 326 | out := ch.Stderr() | ||
| 327 | if len(argv) != 5 { | ||
| 328 | fmt.Fprint(out, "vole: usage: vole-run-log <repository> <run-id> <attempt|0> <step|0>\n") | ||
| 329 | return 129 | ||
| 330 | } | ||
| 331 | name, ok := repoName(argv[1]) | ||
| 332 | if !ok { | ||
| 333 | fmt.Fprint(out, "vole: invalid repository name\n") | ||
| 334 | return 129 | ||
| 335 | } | ||
| 336 | runID := strings.TrimSpace(argv[2]) | ||
| 337 | if !id.Valid(runID) { | ||
| 338 | fmt.Fprintf(out, "vole: %q is not a run id; use the 26-character id from the run page\n", runID) | ||
| 339 | return 129 | ||
| 340 | } | ||
| 341 | attempt, attemptErr := strconv.Atoi(strings.TrimSpace(argv[3])) | ||
| 342 | step, stepErr := strconv.Atoi(strings.TrimSpace(argv[4])) | ||
| 343 | if attemptErr != nil || attempt < 0 || stepErr != nil || step < 0 { | ||
| 344 | fmt.Fprint(out, "vole: attempt and step are numbers; 0 means the default\n") | ||
| 345 | return 129 | ||
| 346 | } | ||
| 347 | if s.cfg.Runs == nil { | ||
| 348 | fmt.Fprint(out, "vole: this instance has no CI queue\n") | ||
| 349 | return 1 | ||
| 350 | } | ||
| 351 | if s.cfg.Logs == nil { | ||
| 352 | fmt.Fprint(out, "vole: this instance has no log directory\n") | ||
| 353 | return 1 | ||
| 354 | } | ||
| 355 | m, err := s.cfg.Repos.Open(name) | ||
| 356 | if err != nil { | ||
| 357 | fmt.Fprintf(out, "vole: repository %q not found\n", name) | ||
| 358 | return 1 | ||
| 359 | } | ||
| 360 | acl, err := m.ACL() | ||
| 361 | if err != nil { | ||
| 362 | fmt.Fprintf(out, "vole: cannot read access rules for %s\n", name) | ||
| 363 | return 1 | ||
| 364 | } | ||
| 365 | role := acl.EffectiveRole(user, s.cfg.Identity != nil && s.cfg.Identity.IsAdmin(user)) | ||
| 366 | if !role.AtLeast(access.Read) { | ||
| 367 | fmt.Fprintf(out, "vole: %s may not read runs in %s\n", user, name) | ||
| 368 | return 1 | ||
| 369 | } | ||
| 370 | run, ok := s.runIn(out, name, runID) | ||
| 371 | if !ok { | ||
| 372 | return 1 | ||
| 373 | } | ||
| 374 | |||
| 375 | // The default is the attempt the run is on while it runs, and the newest logged | ||
| 376 | // attempt after it has finished. That is the attempt a reader means by "the log". | ||
| 377 | logs, err := s.cfg.Runs.AttemptLogs(run.ID) | ||
| 378 | if err != nil { | ||
| 379 | fmt.Fprintf(out, "vole: cannot read the log index: %v\n", err) | ||
| 380 | return 1 | ||
| 381 | } | ||
| 382 | if attempt == 0 { | ||
| 383 | attempt = run.Attempts | ||
| 384 | if run.Status != runs.StatusRunning && run.Status != runs.StatusQueued && len(logs) > 0 { | ||
| 385 | attempt = logs[len(logs)-1].Attempt | ||
| 386 | } | ||
| 387 | } | ||
| 388 | if attempt < 1 { | ||
| 389 | fmt.Fprint(out, "vole: this run has not started, so it has no log yet\n") | ||
| 390 | return 1 | ||
| 391 | } | ||
| 392 | |||
| 393 | var offset, length int64 = 0, -1 | ||
| 394 | if step > 0 { | ||
| 395 | rec, err := s.cfg.Runs.Attempt(run.ID, attempt) | ||
| 396 | if err != nil { | ||
| 397 | fmt.Fprintf(out, "vole: cannot read attempt %d: %v\n", attempt, err) | ||
| 398 | return 1 | ||
| 399 | } | ||
| 400 | if rec == nil || step > len(rec.Steps) { | ||
| 401 | fmt.Fprintf(out, "vole: attempt %d has no step %d\n", attempt, step) | ||
| 402 | return 1 | ||
| 403 | } | ||
| 404 | sr := rec.Steps[step-1] | ||
| 405 | if sr.LogSize <= 0 || sr.LogOffset < 0 { | ||
| 406 | fmt.Fprintf(out, "vole: step %d of attempt %d has no log\n", step, attempt) | ||
| 407 | return 1 | ||
| 408 | } | ||
| 409 | offset, length = sr.LogOffset, sr.LogSize | ||
| 410 | } | ||
| 411 | |||
| 412 | f, info, err := s.cfg.Logs.Open(run.Repo, run.ID, attempt) | ||
| 413 | if err != nil { | ||
| 414 | if os.IsNotExist(err) { | ||
| 415 | fmt.Fprintf(out, "vole: attempt %d has no log (it may not have produced output, or its runner was killed)\n", attempt) | ||
| 416 | return 1 | ||
| 417 | } | ||
| 418 | fmt.Fprintf(out, "vole: cannot open the log: %v\n", err) | ||
| 419 | return 1 | ||
| 420 | } | ||
| 421 | defer f.Close() | ||
| 422 | if length < 0 { | ||
| 423 | length = info.Size() - offset | ||
| 424 | } | ||
| 425 | if offset > info.Size() || offset+length > info.Size() { | ||
| 426 | fmt.Fprintf(out, "vole: step %d of attempt %d points outside its log\n", step, attempt) | ||
| 427 | return 1 | ||
| 428 | } | ||
| 429 | if _, err := io.CopyN(ch, f, length); err != nil { | ||
| 430 | s.cfg.Log.Warn("ssh: run log copy failed", "run", run.ID, "attempt", attempt, "err", err) | ||
| 431 | return 1 | ||
| 432 | } | ||
| 433 | return 0 | ||
| 434 | } | ||
| 435 | |||
| 82 | 436 | func (s *Server) serveRunCancel(ch xssh.Channel, argv []string, user string) int { | |
| 83 | 437 | run, name, ok := s.openRunFor(ch, argv, user, "vole-run-cancel <repository> <run-id>") | |
| 84 | 438 | if !ok { | |
| @@ -158,3 +512,71 @@ func (s *Server) auditRun(action, user, repo, runID string) { | |||
| 158 | 512 | s.cfg.Log.Error("audit log write failed", "account", user, "repo", repo, "run", runID, "error", err) | |
| 159 | 513 | } | |
| 160 | 514 | } | |
| 515 | |||
| 516 | // runStatusLine is the stored status with the one fact the status alone does not | ||
| 517 | // carry. "error" is a run that never started, so it says that rather than sounding | ||
| 518 | // like a verdict about the commit. | ||
| 519 | func runStatusLine(r *runs.Run) string { | ||
| 520 | switch r.Status { | ||
| 521 | case runs.StatusError: | ||
| 522 | return "error (could not run; nothing was tested)" | ||
| 523 | case runs.StatusFailed: | ||
| 524 | return fmt.Sprintf("failed (exit %d)", r.ExitCode) | ||
| 525 | default: | ||
| 526 | return r.Status | ||
| 527 | } | ||
| 528 | } | ||
| 529 | |||
| 530 | // nameAndPath avoids repeating a display name that is the resource key itself. | ||
| 531 | func nameAndPath(name, key string) string { | ||
| 532 | if name == "" || name == key { | ||
| 533 | return key | ||
| 534 | } | ||
| 535 | return fmt.Sprintf("%s (%s)", name, key) | ||
| 536 | } | ||
| 537 | |||
| 538 | func commitLine(r *runs.Run) string { | ||
| 539 | line := format.SHA(r.SHA) | ||
| 540 | switch { | ||
| 541 | case r.Tag != "" && r.Branch != "": | ||
| 542 | line += " on " + r.Branch + ", tag " + r.Tag | ||
| 543 | case r.Tag != "": | ||
| 544 | line += " (tag " + r.Tag + ")" | ||
| 545 | case r.Branch != "": | ||
| 546 | line += " on " + r.Branch | ||
| 547 | } | ||
| 548 | return line | ||
| 549 | } | ||
| 550 | |||
| 551 | // runCounts answers the question a green run raises when a gate still refuses it, | ||
| 552 | // by the same rule the run page and the landing gate use. | ||
| 553 | func runCounts(r *runs.Run, acl *access.ACL, att *runs.Attestation) (bool, string) { | ||
| 554 | switch { | ||
| 555 | case r.Status != runs.StatusSuccess: | ||
| 556 | return false, "only a successful run counts as a check" | ||
| 557 | case att == nil: | ||
| 558 | return false, "this run has no record of who reported it" | ||
| 559 | case acl != nil && !acl.CanReport(att.Signer): | ||
| 560 | return false, fmt.Sprintf("%s is not a runner for this repository", att.Signer) | ||
| 561 | default: | ||
| 562 | return true, "" | ||
| 563 | } | ||
| 564 | } | ||
| 565 | |||
| 566 | // dash turns the "-" placeholder into the empty string a filter means by "unset". | ||
| 567 | func dash(v string) string { | ||
| 568 | v = strings.TrimSpace(v) | ||
| 569 | if v == "-" { | ||
| 570 | return "" | ||
| 571 | } | ||
| 572 | return v | ||
| 573 | } | ||
| 574 | |||
| 575 | func isHex(s string) bool { | ||
| 576 | for _, c := range s { | ||
| 577 | if (c < '0' || c > '9') && (c < 'a' || c > 'f') { | ||
| 578 | return false | ||
| 579 | } | ||
| 580 | } | ||
| 581 | return true | ||
| 582 | } | ||