| 1 | package main |
| 2 | |
| 3 | import ( |
| 4 | "errors" |
| 5 | "fmt" |
| 6 | "io" |
| 7 | "os" |
| 8 | "time" |
| 9 | |
| 10 | "amadan.net/oficina/equipo/internal/equipo" |
| 11 | "amadan.net/oficina/equipo/internal/release" |
| 12 | "amadan.net/oficina/equipo/internal/runner" |
| 13 | "amadan.net/oficina/equipo/internal/runner/provider" |
| 14 | ) |
| 15 | |
| 16 | // enrolReport tells the person how enrolment is going and keeps the same |
| 17 | // lines in the install log (log; "" keeps none): one quiet line while it |
| 18 | // waits for approval, each failure it waits out, and at the end what went |
| 19 | // wrong in words. Details carry no secrets (runner.EnrolDetail). An |
| 20 | // installer that introduces the link names its line in link, said just |
| 21 | // before the link and only when one follows, and the line for a kept |
| 22 | // enrolment in kept ("" says nothing). |
| 23 | type enrolReport struct { |
| 24 | errout io.Writer |
| 25 | log string |
| 26 | link, kept string |
| 27 | } |
| 28 | |
| 29 | // macEnrolReport is the Mac installer's: "Open this link and add this |
| 30 | // Mac to your team:" right before the link, "Already added to your |
| 31 | // team." when the enrolment it has is kept. |
| 32 | func macEnrolReport(errout io.Writer, log string) enrolReport { |
| 33 | return enrolReport{errout: errout, log: log, link: "runner.mac.tap", kept: "runner.mac.already"} |
| 34 | } |
| 35 | |
| 36 | func (r enrolReport) say(key string, a ...any) { |
| 37 | line := fmt.Sprintf(equipo.RunnerSetupCopy(key), a...) |
| 38 | fmt.Fprintln(r.errout, line) |
| 39 | if r.log == "" { |
| 40 | return |
| 41 | } |
| 42 | if f, err := os.OpenFile(r.log, os.O_WRONLY|os.O_CREATE|os.O_APPEND, 0600); err == nil { |
| 43 | fmt.Fprintf(f, "%s %s\n", time.Now().UTC().Format(time.RFC3339), line) |
| 44 | f.Close() |
| 45 | } |
| 46 | } |
| 47 | |
| 48 | func (r enrolReport) notify(n runner.EnrolNotice) { |
| 49 | if n.Kept && r.kept != "" { |
| 50 | r.say(r.kept) |
| 51 | } |
| 52 | if n.Link && r.link != "" { |
| 53 | r.say(r.link) |
| 54 | } |
| 55 | if n.Waiting { |
| 56 | r.say("runner.enrol.waiting") |
| 57 | } |
| 58 | if n.Retry != nil { |
| 59 | r.say("runner.enrol.retrying", runner.EnrolDetail(n.Retry)) |
| 60 | } |
| 61 | } |
| 62 | |
| 63 | // failed says why enrolment ended; "Runner installation is unavailable." |
| 64 | // never comes alone. |
| 65 | func (r enrolReport) failed(err error) { |
| 66 | switch { |
| 67 | case errors.Is(err, runner.ErrEnrolmentExpired): |
| 68 | r.say("runner.enrol.expired") |
| 69 | case errors.Is(err, runner.ErrEnrolmentRefused): |
| 70 | r.say("runner.enrol.refused") |
| 71 | case errors.Is(err, provider.ErrIdentity): |
| 72 | // A saved enrolment this command does not match, in words, not |
| 73 | // as its code (provider_identity_mismatch, 2026-11-28). |
| 74 | r.say("runner.enrol.identity") |
| 75 | default: |
| 76 | detail := runner.EnrolDetail(err) |
| 77 | if detail == "" { |
| 78 | detail = "unknown" |
| 79 | } |
| 80 | r.say("runner.enrol.failed", detail) |
| 81 | r.say("runner.install.unavailable") |
| 82 | } |
| 83 | } |
| 84 | |
| 85 | // enrolledElsewhere is the line for a runner state the installer must |
| 86 | // not touch: otherKey with the other Equipo's address when it is enrolled |
| 87 | // with another Equipo than origin and instance, runner.enrol.identity |
| 88 | // when the state does not say which (or says it in something other than |
| 89 | // a plain web address, which is never printed), and runner.enrol.failed |
| 90 | // when the state cannot be read, so whose it is is not known. "" when |
| 91 | // the install can go ahead. |
| 92 | func enrolledElsewhere(state, origin, instance, otherKey string) (string, []any) { |
| 93 | other, elsewhere, err := runner.EnrolledElsewhere(state, origin, instance) |
| 94 | switch { |
| 95 | case err != nil: |
| 96 | detail := runner.EnrolDetail(err) |
| 97 | if detail == "" { |
| 98 | detail = "unknown" |
| 99 | } |
| 100 | return "runner.enrol.failed", []any{detail} |
| 101 | case !elsewhere: |
| 102 | return "", nil |
| 103 | case other == "" || !release.InstallOrigin(other): |
| 104 | return "runner.enrol.identity", nil |
| 105 | } |
| 106 | return otherKey, []any{other} |
| 107 | } |
| 108 | |