Skip to content
quarry Artifact [0475cef5d8]
Login

Artifact [0475cef5d8]

Artifact 0475cef5d8d70ee4a1eef2234e94dea9be645725c1d0a9348fcaa0d88c2aa122:


package install

import (
	"bufio"
	"context"
	"encoding/json"
	"errors"
	"flag"
	"fmt"
	"io"
	"io/fs"
	"net/http"
	"net/url"
	"os"
	"sort"
	"strings"
	"time"

	"craftdesign.group/quarry/internal/ci"
	"craftdesign.group/quarry/internal/cibridge"
)

// 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(e Env, args []string) (string, error) {
	s := e.Sys
	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:
		var found []string
		if e.GOOS == "darwin" {
			found = macNames(s)
		} else {
			names, _ := s.ReadDir(confDir)
			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"}
}

// doctorWarnExit is doctor's exit code when nothing failed but something warned.
const doctorWarnExit = 3

// bridgeDeadline bounds each request of the bridge check.
var bridgeDeadline = 10 * time.Second

// 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, 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 = doctorWarnExit
		}
	}
	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 {
	if e.GOOS == "darwin" {
		return runChecksDarwin(ctx, e, name)
	}
	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) {
	want, err := e.Sys.Sum256(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)
	}
	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 forge's own answer to the token, from
// GET /check, which registers nothing and does not reach the engine. A
// forge that predates the route (404) is asked the old way, a connect;
// a connect that does not answer in time is a warning, not a failure.
func bridge(ctx context.Context, e Env, m *Manifest, token string) (string, string) {
	base, err := m.bridge()
	if err != nil {
		return statFail, err.Error()
	}
	status, reason, repo, err := bridgeAsk(ctx, e, "GET", base+"/check", nil, token)
	if err != nil {
		return statFail, firstLine(err.Error())
	}
	if status == http.StatusNotFound && reason == "" {
		host, _ := e.Sys.Hostname()
		body := fmt.Sprintf(`{"worker_id":%q,"task_types":[%q],"max_tasks":1}`, "doctor-"+host, ci.GroupFor(m.Repo)+".job")
		status, reason, repo, err = bridgeAsk(ctx, e, "POST", base+"/v1/workers/connect", strings.NewReader(body), token)
		if errors.Is(err, context.DeadlineExceeded) {
			return statWarn, "could not confirm: the forge's bridge did not answer in time"
		}
		if err != nil {
			return statFail, firstLine(err.Error())
		}
	}
	switch {
	case reason == cibridge.ReasonOtherRepo && repo != "":
		return statFail, "this token belongs to repository " + repo
	case status == http.StatusOK:
		if repo != "" {
			return statOK, "connected to repository " + repo
		}
		return statOK, ""
	case status == http.StatusUnauthorized:
		return statFail, "revoked, not a worker token, or minted before this forge records tokens (re-run install to get a new one)"
	case status == http.StatusForbidden || status == http.StatusNotFound:
		return statFail, "revoked or wrong repository"
	}
	return statFail, fmt.Sprintf("answered %d", status)
}

// bridgeAsk makes one bridge request under bridgeDeadline and returns
// the status, the bridge's reason header and the repository its JSON
// body names. The headers are enough for a connect; its stream is not read.
func bridgeAsk(ctx context.Context, e Env, method, to string, body io.Reader, token string) (int, string, string, error) {
	ctx, cancel := context.WithTimeout(ctx, bridgeDeadline)
	defer cancel()
	req, err := http.NewRequestWithContext(ctx, method, to, body)
	if err != nil {
		return 0, "", "", err
	}
	req.Header.Set("Authorization", "Bearer "+token)
	if body != nil {
		req.Header.Set("Content-Type", "application/json")
	}
	resp, err := e.client().Do(req)
	if err != nil {
		return 0, "", "", err
	}
	defer resp.Body.Close()
	reason := resp.Header.Get(cibridge.ReasonHeader)
	var named struct {
		Repository string `json:"repository"`
	}
	if method == "GET" || reason == cibridge.ReasonOtherRepo {
		json.NewDecoder(io.LimitReader(resp.Body, 4096)).Decode(&named)
	}
	return resp.StatusCode, reason, named.Repository, nil
}

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
}