Skip to content
quarry Artifact [e6c9d0a4d3]
Login

Artifact [e6c9d0a4d3]

Artifact e6c9d0a4d39dec7d1f24547569c07ea5693e034d5c740acfbd0fdb19e544da58:


package install

import (
	"bytes"
	"context"
	"crypto/sha256"
	"encoding/hex"
	"encoding/json"
	"errors"
	"flag"
	"fmt"
	"io"
	"io/fs"
	"net/http"
	"net/url"
	"sort"
	"strconv"
	"strings"
	"time"

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

const (
	programDir  = "/usr/local/lib/quarry-runner"
	program     = programDir + "/quarry-runner"
	stateDir    = "/var/lib/quarry-runner"
	confDir     = "/etc/quarry-runner"
	sudoersDir  = "/etc/sudoers.d"
	unitDir     = "/etc/systemd/system"
	nologin     = "/usr/sbin/nologin"
	redeemLabel = "runner"
)

// ExitError ends the program with Code; Msg, when set, is printed
// first. Doctor's 1, 3 and 2 are carried this way.
type ExitError struct {
	Code int
	Msg  string
}

func (e *ExitError) Error() string { return e.Msg }

// Env is what the three commands need beyond the System.
type Env struct {
	Sys  System
	HTTP *http.Client
	Out  io.Writer
	// Getenv reads the process environment.
	Getenv func(string) string
	// Prompt asks for a secret without echoing it.
	Prompt  func(label string) (string, error)
	Version string
	GOOS    string
	// Trunk says whether the repository's trunk has CI to run: a
	// workflow in ci/ or a ci/runner.json.
	Trunk func(ctx context.Context, repoURL string) (bool, error)
}

func (e Env) client() *http.Client {
	if e.HTTP != nil {
		return e.HTTP
	}
	return &http.Client{Timeout: 30 * time.Second}
}

// Name is S for a worker group: the group, cut to 20 characters with
// four hex digits of its hash appended when cut, so every user name
// made from it fits.
func Name(group string) string {
	if len(group) <= 20 {
		return group
	}
	sum := sha256.Sum256([]byte(group))
	return group[:20] + hex.EncodeToString(sum[:])[:4]
}

// Manifest is what an install records at /etc/quarry-runner/S.json: all
// uninstall removes and all doctor checks.
type Manifest struct {
	Version string `json:"version"`
	URL     string `json:"url"`
	Repo    string `json:"repo"`
	Name    string `json:"name"`
	Jobs    int    `json:"jobs"`
	// Checkout, Zit, Branch and Tasks are the flags passed through to
	// the unit.
	Checkout string `json:"checkout,omitempty"`
	Zit      string `json:"zit,omitempty"`
	Branch   string `json:"branch,omitempty"`
	Tasks    string `json:"tasks,omitempty"`
	TokenID  string `json:"token_id"`
	// Users, Groups and Paths are what this install created, and only
	// that.
	Users  []string `json:"users"`
	Groups []string `json:"groups"`
	Paths  []string `json:"paths"`
	// Program is the shared program copy: removed with the last runner.
	Program string `json:"program"`
}

func (m *Manifest) serviceUser() string { return "qr-" + m.Name }
func (m *Manifest) jobUsers() []string  { return jobUsers(m.Name, m.Jobs) }
func (m *Manifest) home() string        { return stateDir + "/" + m.Name }
func (m *Manifest) work() string        { return m.home() + "/work" }
func (m *Manifest) envFile() string     { return confDir + "/" + m.Name + ".env" }
func (m *Manifest) unit() string        { return "quarry-runner-" + m.Name + ".service" }
func (m *Manifest) bridge() (string, error) {
	u, err := url.Parse(m.URL)
	if err != nil {
		return "", err
	}
	return u.Scheme + "://" + u.Host + "/r/" + url.PathEscape(m.Repo) + "/ci/bridge", nil
}

func jobUsers(name string, n int) []string {
	var out []string
	for i := 1; i <= n; i++ {
		out = append(out, "qj-"+name+"-"+strconv.Itoa(i))
	}
	return out
}

func manifestPath(name string) string { return confDir + "/" + name + ".json" }

func loadManifest(s System, name string) (*Manifest, error) {
	raw, err := s.ReadFile(manifestPath(name))
	if err != nil {
		return nil, err
	}
	var m Manifest
	if err := json.Unmarshal(raw, &m); err != nil {
		return nil, fmt.Errorf("%s: %v", manifestPath(name), err)
	}
	if m.Name != name || m.Jobs < 1 || m.URL == "" {
		return nil, fmt.Errorf("%s does not describe runner %s", manifestPath(name), name)
	}
	return &m, nil
}

// repoOf reads a repository URL: the repository's name, which is the
// last segment of /r/<owner>/<repo> and of /r/<repo>.
func repoOf(raw string) (*url.URL, string, error) {
	raw = strings.TrimSuffix(strings.TrimSpace(raw), "/")
	u, err := url.Parse(raw)
	if err != nil || u.Scheme == "" || u.Host == "" {
		return nil, "", fmt.Errorf("%q is not a repository URL like https://forge.example/r/name", raw)
	}
	if _, err := outbound.CheckOperator(raw); err != nil {
		return nil, "", fmt.Errorf("%q: %v", raw, err)
	}
	segs := strings.Split(strings.Trim(u.Path, "/"), "/")
	if len(segs) < 2 || segs[0] != "r" {
		return nil, "", fmt.Errorf("%q is not a repository URL like https://forge.example/r/name", raw)
	}
	return u, segs[len(segs)-1], nil
}

// parseArgs parses flags that may follow the positional arguments, as
// `install URL --code X` has them.
func parseArgs(fs *flag.FlagSet, args []string) ([]string, error) {
	fs.SetOutput(io.Discard)
	var pos []string
	for {
		if err := fs.Parse(args); err != nil {
			return nil, &ExitError{Code: 2, Msg: err.Error()}
		}
		if fs.NArg() == 0 {
			return pos, nil
		}
		pos = append(pos, fs.Arg(0))
		args = fs.Args()[1:]
	}
}

// txn undoes what a run created, newest first, when it fails.
type txn struct{ undo []func() }

func (t *txn) onFail(f func()) { t.undo = append(t.undo, f) }
func (t *txn) rollback() {
	for i := len(t.undo) - 1; i >= 0; i-- {
		t.undo[i]()
	}
	t.undo = nil
}

// Install is `quarry-runner install URL`.
func Install(ctx context.Context, e Env, args []string) error {
	fs := flag.NewFlagSet("install", flag.ContinueOnError)
	code := fs.String("code", "", "install code from the repository's Settings, Runners")
	jobs := fs.Int("jobs", 2, "job users, which is also how many tasks run at once")
	checkout := fs.String("checkout", "", `pass --checkout to the runner ("zit" clones, the default downloads)`)
	zitBin := fs.String("zit", "", "pass --zit to the runner")
	branch := fs.String("branch", "", "pass --branch to the runner")
	tasks := fs.String("tasks", "", "pass --tasks to the runner")
	label := fs.String("label", redeemLabel, "the worker token's label beside this host's name")
	userMode := fs.Bool("user", false, "install without root (not available yet)")
	pos, err := parseArgs(fs, args)
	if err != nil {
		return err
	}
	if *userMode {
		return errors.New("--user installs are not available yet; install as root with sudo")
	}
	if len(pos) != 1 {
		return &ExitError{Code: 2, Msg: "usage: quarry-runner install https://<forge>/r/<repo> [--code CODE] [--jobs N] [--checkout zit] [--zit PATH] [--branch NAME] [--tasks LIST]"}
	}
	if *jobs < 1 || *jobs > 64 {
		return &ExitError{Code: 2, Msg: "--jobs is from 1 to 64"}
	}
	u, repo, err := repoOf(pos[0])
	if err != nil {
		return err
	}
	repoURL := strings.TrimSuffix(strings.TrimSpace(pos[0]), "/")
	s := e.Sys
	name := Name(ci.GroupFor(repo))
	m := &Manifest{Version: e.Version, URL: repoURL, Repo: repo, Name: name, Jobs: *jobs,
		Checkout: *checkout, Zit: *zitBin, Branch: *branch, Tasks: *tasks, Program: program}

	if err := checkHost(ctx, e, u); err != nil {
		return err
	}

	// What the machine already holds decides whether a token is needed.
	prev, _ := loadManifest(s, name)
	haveToken := strings.TrimSpace(e.Getenv("QUARRY_RUNNER_TOKEN"))
	if haveToken == "" {
		if raw, err := s.ReadFile(m.envFile()); err == nil {
			haveToken = envToken(raw)
		}
	}
	redeemCode := ""
	if haveToken == "" {
		redeemCode = strings.TrimSpace(*code)
		if redeemCode == "" {
			redeemCode = strings.TrimSpace(e.Getenv("QUARRY_INSTALL_CODE"))
		}
		if redeemCode == "" && e.Prompt != nil {
			if redeemCode, err = e.Prompt("Install code: "); err != nil {
				return err
			}
			redeemCode = strings.TrimSpace(redeemCode)
		}
		if redeemCode == "" {
			return errors.New("no install code: pass --code, set QUARRY_INSTALL_CODE, or set QUARRY_RUNNER_TOKEN to a worker token")
		}
	}
	if prev != nil {
		m.TokenID = prev.TokenID
		m.Users, m.Groups, m.Paths = prev.Users, prev.Groups, prev.Paths
	}

	tx := &txn{}
	b := &builder{s: s, m: m, tx: tx}
	if err := b.create(ctx); err != nil {
		tx.rollback()
		return err
	}

	token := haveToken
	if redeemCode != "" {
		host, _ := s.Hostname()
		lbl := *label
		var red struct {
			Value string `json:"value"`
			Group string `json:"group"`
			Token struct {
				ID string `json:"id"`
			} `json:"token"`
		}
		err := postJSON(ctx, e.client(), u.Scheme+"://"+u.Host+"/api/v1/r/"+url.PathEscape(repo)+"/ci/runner-codes/redeem", "",
			map[string]string{"code": redeemCode, "host": host, "label": lbl}, &red)
		if err != nil {
			tx.rollback()
			return fmt.Errorf("redeeming the install code: %w", err)
		}
		token, m.TokenID = red.Value, red.Token.ID
		if token == "" {
			tx.rollback()
			return errors.New("the forge answered the install code with no token")
		}
		// From here the code is spent: nothing is undone.
		tx = &txn{}
		if red.Group != "" && Name(red.Group) != name {
			return fmt.Errorf("the forge's worker group %q is not the one this install was made for (%s); uninstall, then install again with a new code", red.Group, name)
		}
	}
	after := func(err error) error {
		return fmt.Errorf("%w; the token is kept in %s, read `quarry-runner doctor %s` to see what is wrong", err, m.envFile(), name)
	}
	if err := b.file(m.envFile(), []byte("QUARRY_RUNNER_TOKEN="+token+"\n"), "root", "root", 0o600); err != nil {
		return after(err)
	}
	if err := b.manifest(); err != nil {
		return after(err)
	}
	if _, err := s.Run(ctx, "systemctl", "enable", "--now", m.unit()); err != nil {
		return after(err)
	}
	fmt.Fprintf(e.Out, "installed %s; checking it\n", m.unit())
	return Doctor(ctx, e, []string{name})
}

// checkHost is the first step: the machine can take an install.
func checkHost(ctx context.Context, e Env, forge *url.URL) error {
	s := e.Sys
	if e.GOOS != "linux" {
		return fmt.Errorf("install as root is for Linux with systemd, not %s; macOS installs come with --user", e.GOOS)
	}
	if s.Geteuid() != 0 {
		return errors.New("install as root: run it with sudo")
	}
	if !s.Exists("/run/systemd/system") {
		return errors.New("this machine does not run systemd")
	}
	for _, tool := range []string{"useradd", "sudo", "visudo", "systemctl"} {
		if _, err := s.LookPath(tool); err != nil {
			return fmt.Errorf("%s is not installed", tool)
		}
	}
	req, err := http.NewRequestWithContext(ctx, "GET", forge.Scheme+"://"+forge.Host+"/api/v1", nil)
	if err != nil {
		return err
	}
	resp, err := e.client().Do(req)
	if err != nil {
		return fmt.Errorf("the forge is not reachable: %v", err)
	}
	resp.Body.Close()
	if resp.StatusCode != http.StatusOK {
		return fmt.Errorf("the forge answered %d for /api/v1", resp.StatusCode)
	}
	return nil
}

// builder makes what the table in RFC 0030 lists, each step a repair
// when it is already done, and tells the transaction how to undo it.
type builder struct {
	s  System
	m  *Manifest
	tx *txn
}

func addOnce(list *[]string, v string) {
	for _, x := range *list {
		if x == v {
			return
		}
	}
	*list = append(*list, v)
	sort.Strings(*list)
}

func (b *builder) group(name string) error {
	if b.s.GroupExists(name) {
		return nil
	}
	if err := b.s.AddGroup(name); err != nil {
		return err
	}
	b.tx.onFail(func() { b.s.DelGroup(name) })
	addOnce(&b.m.Groups, name)
	return nil
}

func (b *builder) user(u User) error {
	if err := b.group(u.Group); err != nil {
		return err
	}
	if b.s.UserExists(u.Name) {
		return nil
	}
	if err := b.s.AddUser(u); err != nil {
		return err
	}
	b.tx.onFail(func() { b.s.DelUser(u.Name) })
	addOnce(&b.m.Users, u.Name)
	return nil
}

// dir makes a directory this install owns and lists it.
func (b *builder) dir(path, owner, group string, mode fs.FileMode) error {
	return b.mkdir(path, owner, group, mode, true)
}

// sharedDir makes a directory runners share: not listed, and removed
// again only while empty.
func (b *builder) sharedDir(path string) error {
	if b.s.Exists(path) {
		return nil
	}
	return b.mkdir(path, "root", "root", 0o755, false)
}

func (b *builder) mkdir(path, owner, group string, mode fs.FileMode, list bool) error {
	info, err := b.s.Stat(path)
	if err == nil && info.IsDir && info.Owner == owner && info.Group == group && info.Mode == mode {
		if list {
			addOnce(&b.m.Paths, path)
		}
		return nil
	}
	existed := err == nil
	if err := b.s.Mkdir(path, owner, group, mode); err != nil {
		return err
	}
	if !existed {
		b.tx.onFail(func() { b.s.Remove(path) })
	}
	if list {
		addOnce(&b.m.Paths, path)
	}
	return nil
}

// file writes data unless the file already is exactly that.
func (b *builder) file(path string, data []byte, owner, group string, mode fs.FileMode) error {
	info, err := b.s.Stat(path)
	if err == nil && info.Owner == owner && info.Group == group && info.Mode == mode {
		if cur, err := b.s.ReadFile(path); err == nil && bytes.Equal(cur, data) {
			return nil
		}
	}
	if err := b.s.WriteFile(path, data, owner, group, mode); err != nil {
		return err
	}
	if err != nil { // it was not there
		b.tx.onFail(func() { b.s.Remove(path) })
	}
	return nil
}

func (b *builder) create(ctx context.Context) error {
	m, s := b.m, b.s
	svc := m.serviceUser()
	if err := b.user(User{Name: svc, Group: svc, Home: m.home(), Shell: nologin}); err != nil {
		return err
	}
	for _, j := range m.jobUsers() {
		if err := b.user(User{Name: j, Group: j, Shell: nologin}); err != nil {
			return err
		}
		if !s.InGroup(svc, j) {
			if err := s.AddToGroup(svc, j); err != nil {
				return err
			}
		}
	}
	for _, d := range []string{stateDir, confDir, programDir} {
		if err := b.sharedDir(d); err != nil {
			return err
		}
	}
	if err := b.dir(m.home(), svc, svc, 0o711); err != nil {
		return err
	}
	if err := b.dir(m.work(), svc, svc, 0o750); err != nil {
		return err
	}
	if err := b.programCopy(); err != nil {
		return err
	}
	if err := b.sudoers(ctx); err != nil {
		return err
	}
	if err := b.file(unitDir+"/"+m.unit(), []byte(unitText(m)), "root", "root", 0o644); err != nil {
		return err
	}
	if err := b.manifest(); err != nil {
		return err
	}
	return nil
}

func (b *builder) programCopy() error {
	self, err := b.s.Executable()
	if err != nil {
		return err
	}
	if self == program {
		return nil
	}
	src, err := b.s.ReadFile(self)
	if err != nil {
		return err
	}
	return b.file(program, src, "root", "root", 0o755)
}

func (b *builder) sudoers(ctx context.Context) error {
	m := b.m
	path := sudoersDir + "/quarry-runner-" + m.Name
	rule := fmt.Sprintf("%s ALL=(%s) NOPASSWD: ALL\n", m.serviceUser(), strings.Join(m.jobUsers(), ","))
	if info, err := b.s.Stat(path); err == nil && info.Owner == "root" && info.Mode == 0o440 {
		if cur, err := b.s.ReadFile(path); err == nil && string(cur) == rule {
			addOnce(&m.Paths, path)
			return nil
		}
	}
	// A name with a dot is one sudo ignores, so the candidate is never
	// live before visudo has read it.
	tmp := path + ".tmp"
	if err := b.s.WriteFile(tmp, []byte(rule), "root", "root", 0o440); err != nil {
		return err
	}
	if _, err := b.s.Run(ctx, "visudo", "-cf", tmp); err != nil {
		b.s.Remove(tmp)
		return fmt.Errorf("the sudo rule did not pass visudo: %w", err)
	}
	_, had := b.s.Stat(path)
	if err := b.s.Rename(tmp, path); err != nil {
		b.s.Remove(tmp)
		return err
	}
	if had != nil {
		b.tx.onFail(func() { b.s.Remove(path) })
	}
	addOnce(&b.m.Paths, path)
	return nil
}

func (b *builder) manifest() error {
	m := b.m
	for _, p := range []string{unitDir + "/" + m.unit(), m.envFile()} {
		addOnce(&m.Paths, p)
	}
	raw, err := json.MarshalIndent(m, "", "  ")
	if err != nil {
		return err
	}
	return b.file(manifestPath(m.Name), append(raw, '\n'), "root", "root", 0o644)
}

func unitText(m *Manifest) string {
	exec := fmt.Sprintf("%s --work %s --max-tasks %d --job-users %s", program, m.work(), m.Jobs, strings.Join(m.jobUsers(), ","))
	for _, p := range [][2]string{{"--checkout", m.Checkout}, {"--zit", m.Zit}, {"--branch", m.Branch}, {"--tasks", m.Tasks}} {
		if p[1] != "" {
			exec += " " + p[0] + " " + p[1]
		}
	}
	exec += " " + m.URL
	svc, home := m.serviceUser(), m.home()
	return "[Unit]\n" +
		"Description=quarry runner for " + m.URL + "\n" +
		"After=network-online.target\n" +
		"Wants=network-online.target\n" +
		"\n[Service]\n" +
		"User=" + svc + "\n" +
		"Group=" + svc + "\n" +
		"WorkingDirectory=" + home + "\n" +
		"EnvironmentFile=" + m.envFile() + "\n" +
		"Environment=HOME=" + home + "\n" +
		"ExecStart=" + exec + "\n" +
		"Restart=always\n" +
		"RestartSec=5\n" +
		"KillMode=mixed\n" +
		"TimeoutStopSec=90\n" +
		"\n[Install]\n" +
		"WantedBy=multi-user.target\n"
}

// envToken reads the token out of an env file.
func envToken(raw []byte) string {
	for _, l := range strings.Split(string(raw), "\n") {
		if v, ok := strings.CutPrefix(strings.TrimSpace(l), "QUARRY_RUNNER_TOKEN="); ok {
			return strings.TrimSpace(v)
		}
	}
	return ""
}

// postJSON posts body, with bearer when set, and decodes a 200 answer
// into out. Anything else is an error carrying the forge's sentence.
func postJSON(ctx context.Context, c *http.Client, to, bearer string, body, out any) error {
	raw, err := json.Marshal(body)
	if err != nil {
		return err
	}
	req, err := http.NewRequestWithContext(ctx, "POST", to, bytes.NewReader(raw))
	if err != nil {
		return err
	}
	req.Header.Set("Content-Type", "application/json")
	if bearer != "" {
		req.Header.Set("Authorization", "Bearer "+bearer)
	}
	resp, err := c.Do(req)
	if err != nil {
		return err
	}
	defer resp.Body.Close()
	data, _ := io.ReadAll(io.LimitReader(resp.Body, 1<<20))
	if resp.StatusCode != http.StatusOK {
		return &statusError{Code: resp.StatusCode, Msg: strings.TrimSpace(string(data))}
	}
	if out != nil {
		if err := json.Unmarshal(data, out); err != nil {
			return fmt.Errorf("the forge's answer is not JSON: %v", err)
		}
	}
	return nil
}

type statusError struct {
	Code int
	Msg  string
}

func (e *statusError) Error() string { return fmt.Sprintf("the forge answered %d: %s", e.Code, e.Msg) }