package install
import (
"bufio"
"context"
"crypto/sha256"
"encoding/hex"
"encoding/json"
"errors"
"flag"
"fmt"
"io"
"io/fs"
"net/http"
"net/url"
"os"
"sort"
"strings"
"time"
"craftdesign.group/quarry/internal/ci"
)
// Check is one line of doctor.
type Check struct {
N int `json:"n"`
Name string `json:"name"`
Status string `json:"status"` // ok, warn or FAIL
Detail string `json:"detail,omitempty"`
}
const (
statOK = "ok"
statWarn = "warn"
statFail = "FAIL"
)
// target names the installed runner an argument or the machine points
// at: S, a repository URL, or nothing when exactly one is installed.
func target(s System, args []string) (string, error) {
switch len(args) {
case 1:
if strings.Contains(args[0], "://") {
_, repo, err := repoOf(args[0])
if err != nil {
return "", &ExitError{Code: 2, Msg: err.Error()}
}
return Name(ci.GroupFor(repo)), nil
}
if !nameShape.MatchString(args[0]) {
return "", &ExitError{Code: 2, Msg: fmt.Sprintf("%q is not a runner name or a repository URL", args[0])}
}
return args[0], nil
case 0:
names, _ := s.ReadDir(confDir)
var found []string
for _, n := range names {
if v, ok := strings.CutSuffix(n, ".json"); ok {
found = append(found, v)
}
}
sort.Strings(found)
switch len(found) {
case 1:
return found[0], nil
case 0:
return "", &ExitError{Code: 2, Msg: "no runner is installed here: name one, as S or its URL"}
}
return "", &ExitError{Code: 2, Msg: "several runners are installed (" + strings.Join(found, ", ") + "): name one, as S or its URL"}
}
return "", &ExitError{Code: 2, Msg: "name one runner, as S or its URL"}
}
// Doctor is `quarry-runner doctor [S|URL]`: the twelve checks of RFC
// 0030, one line each. The error is nil for exit 0, else an ExitError.
func Doctor(ctx context.Context, e Env, args []string) error {
fs := flag.NewFlagSet("doctor", flag.ContinueOnError)
asJSON := fs.Bool("json", false, "print the checks as JSON")
pos, err := parseArgs(fs, args)
if err != nil {
return err
}
name, err := target(e.Sys, pos)
if err != nil {
return err
}
checks := runChecks(ctx, e, name)
code := 0
for _, c := range checks {
switch {
case c.Status == statFail:
code = 1
case c.Status == statWarn && code == 0:
code = 3
}
}
if *asJSON {
out, _ := json.MarshalIndent(map[string]any{"name": name, "checks": checks, "exit": code}, "", " ")
fmt.Fprintf(e.Out, "%s\n", out)
} else {
for _, c := range checks {
detail := ""
if c.Detail != "" {
detail = ": " + c.Detail
}
fmt.Fprintf(e.Out, "%2d %s%s %s\n", c.N, c.Name, detail, c.Status)
}
}
if code != 0 {
return &ExitError{Code: code}
}
return nil
}
func runChecks(ctx context.Context, e Env, name string) []Check {
s := e.Sys
var out []Check
add := func(n int, label, status, detail string) {
out = append(out, Check{N: n, Name: label, Status: status, Detail: detail})
}
m, merr := loadManifest(s, name)
if merr != nil {
add(1, "manifest", statFail, merr.Error())
for n, label := range checkNames[1:] {
add(n+2, label, statFail, "needs the manifest")
}
return out
}
add(1, "manifest", statOK, manifestPath(name))
st2, d2 := program2(ctx, e, m)
add(2, checkNames[1], st2, d2)
// 3: users and groups.
st, detail := statOK, ""
var bad []string
for _, u := range append([]string{m.serviceUser()}, m.jobUsers()...) {
if !s.UserExists(u) {
bad = append(bad, "no user "+u)
}
if !s.GroupExists(u) {
bad = append(bad, "no group "+u)
}
}
for _, j := range m.jobUsers() {
if !s.InGroup(m.serviceUser(), j) {
bad = append(bad, m.serviceUser()+" is not in "+j)
}
}
if len(bad) > 0 {
st, detail = statFail, strings.Join(bad, ", ")
}
add(3, checkNames[2], st, detail)
// 4: sudo as the service user.
st, detail, bad = statOK, "", nil
for _, j := range m.jobUsers() {
if _, err := s.RunAs(ctx, m.serviceUser(), "sudo", "-n", "-u", j, m.Program, "job-reclaim", j); err != nil {
bad = append(bad, j+": "+firstLine(err.Error()))
}
}
if len(bad) > 0 {
st, detail = statFail, strings.Join(bad, "; ")
}
add(4, checkNames[3], st, detail)
// 5: owners and modes.
st, detail, bad = statOK, "", nil
for _, w := range []struct {
path, owner string
mode fs.FileMode
}{
{m.home(), m.serviceUser(), 0o711},
{m.work(), m.serviceUser(), 0o750},
{m.envFile(), "root", 0o600},
{sudoersDir + "/quarry-runner-" + name, "root", 0o440},
} {
info, err := s.Stat(w.path)
switch {
case err != nil:
bad = append(bad, w.path+" is missing")
case info.Owner != w.owner || info.Mode != w.mode:
bad = append(bad, fmt.Sprintf("%s is %s %04o, want %s %04o", w.path, info.Owner, info.Mode, w.owner, w.mode))
}
}
if len(bad) > 0 {
st, detail = statFail, strings.Join(bad, ", ")
}
add(5, checkNames[4], st, detail)
// 6: the token.
token := ""
if raw, err := s.ReadFile(m.envFile()); err == nil {
token = envToken(raw)
}
if token == "" {
add(6, checkNames[5], statFail, m.envFile()+" holds no token")
} else {
add(6, checkNames[5], statOK, "")
}
// 7: the unit.
st, detail = statOK, ""
if _, err := s.Run(ctx, "systemctl", "is-enabled", m.unit()); err != nil {
st, detail = statFail, m.unit()+" is not enabled"
} else if _, err := s.Run(ctx, "systemctl", "is-active", m.unit()); err != nil {
st, detail = statFail, m.unit()+" is not active"
}
add(7, checkNames[6], st, detail)
// 8, 9: the forge and the bridge.
forgeOK := false
u, uerr := url.Parse(m.URL)
if uerr != nil {
add(8, checkNames[7], statFail, uerr.Error())
add(9, checkNames[8], statFail, "needs the forge")
} else {
if status, err := get(ctx, e, u.Scheme+"://"+u.Host+"/api/v1"); err != nil {
add(8, checkNames[7], statFail, firstLine(err.Error()))
} else if status != http.StatusOK {
add(8, checkNames[7], statFail, fmt.Sprintf("answered %d", status))
} else {
forgeOK = true
add(8, checkNames[7], statOK, "")
}
switch {
case token == "":
add(9, checkNames[8], statFail, "no token to try")
case !forgeOK:
add(9, checkNames[8], statFail, "the forge did not answer")
default:
bs, bd := bridge(ctx, e, m, token)
add(9, checkNames[8], bs, bd)
}
}
// 10: trunk.
switch has, err := e.Trunk(ctx, m.URL); {
case err != nil:
add(10, checkNames[9], statWarn, "could not read trunk: "+firstLine(err.Error()))
case !has:
add(10, checkNames[9], statWarn, "nothing to run yet")
default:
add(10, checkNames[9], statOK, "")
}
// 11: zit.
if m.Checkout == "zit" {
bin := m.Zit
if bin == "" {
bin = "zit"
}
// As the service user: the binary is the manifest's word, and
// nothing it names runs as root.
if _, err := s.RunAs(ctx, m.serviceUser(), bin, "version"); err != nil {
add(11, checkNames[10], statFail, bin+" does not run")
} else {
add(11, checkNames[10], statOK, bin)
}
} else {
add(11, checkNames[10], statOK, "not used")
}
// 12: disk.
if free, err := s.DiskFree(m.work()); err != nil {
add(12, checkNames[11], statWarn, err.Error())
} else if free < 1<<30 {
add(12, checkNames[11], statWarn, fmt.Sprintf("%d MB free", free>>20))
} else {
add(12, checkNames[11], statOK, fmt.Sprintf("%d GB free", free>>30))
}
return out
}
var checkNames = []string{"manifest", "program", "users", "sudo", "files", "token", "unit", "forge", "bridge", "trunk", "zit", "disk"}
// program2 is check 2: the program is there and is a build the forge
// serves. The forge's SHA256SUMS lists its builds; a program that is
// none of them is older, or built elsewhere.
func program2(ctx context.Context, e Env, m *Manifest) (string, string) {
bin, err := e.Sys.ReadFile(m.Program)
if err != nil {
return statFail, m.Program + " is missing"
}
u, err := url.Parse(m.URL)
if err != nil {
return statWarn, "could not read the forge's SHA256SUMS"
}
req, _ := http.NewRequestWithContext(ctx, "GET", u.Scheme+"://"+u.Host+"/runner/SHA256SUMS", nil)
resp, err := e.client().Do(req)
if err != nil {
return statWarn, "could not read the forge's SHA256SUMS: " + firstLine(err.Error())
}
defer resp.Body.Close()
if resp.StatusCode != http.StatusOK {
return statWarn, fmt.Sprintf("the forge's SHA256SUMS answered %d", resp.StatusCode)
}
sum := sha256.Sum256(bin)
want := hex.EncodeToString(sum[:])
sc := bufio.NewScanner(io.LimitReader(resp.Body, 1<<20))
for sc.Scan() {
if f := strings.Fields(sc.Text()); len(f) > 0 && f[0] == want {
return statOK, m.Version
}
}
return statWarn, "not one of the forge's current builds; install again from a newer download"
}
func get(ctx context.Context, e Env, to string) (int, error) {
ctx, cancel := context.WithTimeout(ctx, 10*time.Second)
defer cancel()
req, err := http.NewRequestWithContext(ctx, "GET", to, nil)
if err != nil {
return 0, err
}
resp, err := e.client().Do(req)
if err != nil {
return 0, err
}
resp.Body.Close()
return resp.StatusCode, nil
}
// bridge is check 9: the connect route's answer to the token. The
// answer's headers are enough; the stream is not read.
func bridge(ctx context.Context, e Env, m *Manifest, token string) (string, string) {
base, err := m.bridge()
if err != nil {
return statFail, err.Error()
}
ctx, cancel := context.WithTimeout(ctx, 10*time.Second)
defer cancel()
host, _ := e.Sys.Hostname()
body := strings.NewReader(fmt.Sprintf(`{"worker_id":%q,"task_types":[%q],"max_tasks":1}`, "doctor-"+host, ci.GroupFor(m.Repo)+".job"))
req, err := http.NewRequestWithContext(ctx, "POST", base+"/v1/workers/connect", body)
if err != nil {
return statFail, err.Error()
}
req.Header.Set("Authorization", "Bearer "+token)
req.Header.Set("Content-Type", "application/json")
resp, err := e.client().Do(req)
if err != nil {
return statFail, firstLine(err.Error())
}
resp.Body.Close()
switch {
case resp.StatusCode == http.StatusOK:
return statOK, ""
case resp.StatusCode == http.StatusUnauthorized || resp.StatusCode == http.StatusForbidden || resp.StatusCode == http.StatusNotFound:
return statFail, "revoked or wrong repository"
}
return statFail, fmt.Sprintf("answered %d", resp.StatusCode)
}
func firstLine(s string) string {
s, _, _ = strings.Cut(strings.TrimSpace(s), "\n")
return s
}
// PromptSecret reads a line from the terminal without echoing it.
func PromptSecret(label string) (string, error) {
tty, err := os.OpenFile("/dev/tty", os.O_RDWR, 0)
if err != nil {
return "", errors.New("no terminal to ask on: pass --code or set QUARRY_INSTALL_CODE")
}
defer tty.Close()
fmt.Fprint(tty, label)
stty := func(arg string) {
c := execCommand("stty", arg)
c.Stdin = tty
c.Run()
}
stty("-echo")
defer func() { stty("echo"); fmt.Fprintln(tty) }()
line, err := bufio.NewReader(tty).ReadString('\n')
if err != nil && line == "" {
return "", err
}
return strings.TrimSpace(line), nil
}