Skip to content
quarry Artifact [11deeb5959]
Login

Artifact [11deeb5959]

Artifact 11deeb595907756da68ace77842bb4981cb2f98cc061991c3a78d4cc8c7856b0:


package install

import (
	"bytes"
	"context"
	"crypto/sha256"
	"encoding/hex"
	"encoding/json"
	"errors"
	"flag"
	"fmt"
	"io"
	"io/fs"
	"net/http"
	"net/url"
	"regexp"
	"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)
	// Wait and Poll bound how long a runner request waits for its
	// owner and how often it asks; zero is fifteen minutes and the
	// rate the forge allows.
	Wait, Poll time.Duration
}

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"`
	// Platform is "darwin" for a per-user install on macOS and empty for
	// the root install on Linux; Home is the user's home it was made in.
	Platform string `json:"platform,omitempty"`
	Home     string `json:"home,omitempty"`
}

// keyFile is where a runner request keeps its pickup key until the
// token is collected.
func (m *Manifest) keyFile() string { return confDir + "/" + m.Name + ".pickup" }

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" }

// nameShape is what S, a user and a group may be made of.
var nameShape = regexp.MustCompile(`^[A-Za-z0-9_-]{1,64}$`)

func loadManifest(s System, name string) (*Manifest, error) {
	if !nameShape.MatchString(name) {
		return nil, fmt.Errorf("%q is not a runner name", name)
	}
	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)
	}
	if err := m.check(); err != nil {
		return nil, fmt.Errorf("%s: %v", manifestPath(name), err)
	}
	return &m, nil
}

// check is whether the manifest lists only what an install of this name
// makes. It is read as root and drives removals: a path, user or group
// it names that this install could not have made is refused.
func (m *Manifest) check() error {
	if !nameShape.MatchString(m.Name) || m.Jobs > 64 {
		return fmt.Errorf("%q is not a runner name", m.Name)
	}
	if m.Platform != "" {
		return fmt.Errorf("this is a %s install, not a root install on Linux", m.Platform)
	}
	if m.Program != program {
		return fmt.Errorf("program %q is not %s", m.Program, program)
	}
	own := map[string]bool{m.serviceUser(): true}
	for _, j := range m.jobUsers() {
		own[j] = true
	}
	for _, n := range append(append([]string{}, m.Users...), m.Groups...) {
		if !own[n] {
			return fmt.Errorf("lists %q, which this install does not make", n)
		}
	}
	paths := map[string]bool{m.home(): true, m.work(): true, m.envFile(): true, m.keyFile(): true, m.sudoersPath(): true, unitDir + "/" + m.unit(): true}
	for _, p := range m.Paths {
		if !paths[p] {
			return fmt.Errorf("lists %q, which this install does not make", p)
		}
	}
	return nil
}

func (m *Manifest) sudoersPath() string { return sudoersDir + "/quarry-runner-" + m.Name }

// 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
}

// installOpts are the flags of install.
type installOpts struct {
	code, label, checkout, zit, branch, tasks string
	jobs                                      int
}

// 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 ("-" asks for it without echo); with none, a request is filed for the owner to approve`)
	jobs := fs.Int("jobs", 2, "how many tasks run at once (on Linux, also the job users)")
	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 for this user without root (the only way on macOS)")
	pos, err := parseArgs(fs, args)
	if err != nil {
		return err
	}
	if *userMode && e.GOOS != "darwin" {
		return errors.New("--user installs are only for macOS; 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]), "/")
	o := installOpts{code: *code, label: *label, checkout: *checkout, zit: *zitBin, branch: *branch, tasks: *tasks, jobs: *jobs}
	if e.GOOS == "darwin" {
		return installDarwin(ctx, e, u, repo, repoURL, o)
	}
	s := e.Sys
	name := Name(ci.GroupFor(repo))
	m := &Manifest{Version: e.Version, URL: repoURL, Repo: repo, Name: name, Jobs: o.jobs,
		Checkout: o.checkout, Zit: o.zit, Branch: o.branch, Tasks: o.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, wantRequest := "", false
	if haveToken == "" {
		if redeemCode, err = installCode(e, o.code); err != nil {
			return err
		}
		wantRequest = redeemCode == ""
	}
	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 != "" || wantRequest {
		var group string
		if wantRequest {
			if token, m.TokenID, err = b.request(ctx, e, u, m.keyFile(), "root", "root"); err != nil {
				tx.rollback()
				return err
			}
		} else {
			if token, m.TokenID, group, err = redeem(ctx, e, u, repo, redeemCode, o.label); err != nil {
				tx.rollback()
				return fmt.Errorf("redeeming the install code: %w", err)
			}
		}
		// From here the token is minted: a failure must not drop it.
		created := tx
		tx = &txn{}
		switch {
		case !tokenShape.MatchString(token):
			return b.refuseToken(ctx, e, u, created, "", "the forge answered with something that is not a worker token")
		case group != "" && Name(group) != name:
			return b.refuseToken(ctx, e, u, created, token, fmt.Sprintf("the forge's worker group %q is not the one this install was made for (%s)", 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})
}

// installCode is the install code the person gave: the flag, the
// environment or, for "-", a prompt that does not echo. Empty when none
// was given, which means a runner request.
func installCode(e Env, flagValue string) (string, error) {
	c := strings.TrimSpace(flagValue)
	if c == "-" {
		if e.Prompt == nil {
			return "", errors.New("no terminal to ask on: pass --code CODE")
		}
		got, err := e.Prompt("Install code: ")
		if err != nil {
			return "", err
		}
		if c = strings.TrimSpace(got); c == "" {
			return "", errors.New("no install code was typed")
		}
		return c, nil
	}
	if c == "" {
		c = strings.TrimSpace(e.Getenv("QUARRY_INSTALL_CODE"))
	}
	return c, nil
}

// redeem spends an install code for one worker token.
func redeem(ctx context.Context, e Env, forge *url.URL, repo, code, label string) (token, id, group string, err error) {
	host, _ := e.Sys.Hostname()
	var red struct {
		Value string `json:"value"`
		Group string `json:"group"`
		Token struct {
			ID string `json:"id"`
		} `json:"token"`
	}
	err = postJSON(ctx, e.client(), forge.Scheme+"://"+forge.Host+"/api/v1/r/"+url.PathEscape(repo)+"/ci/runner-codes/redeem", "",
		map[string]string{"code": code, "host": host, "label": label}, &red)
	if err != nil {
		return "", "", "", err
	}
	return red.Value, red.Token.ID, red.Group, nil
}

// tokenShape is what a worker token looks like: nothing that could end
// a line in the env file or add a variable to it.
var tokenShape = regexp.MustCompile(`^[A-Za-z0-9._~+/=:-]{8,512}$`)

// refuseToken stops after a redeem whose answer cannot be used. The
// token is retired so it is not left live; when that fails it is kept
// in the env file, with the doctor hint, rather than dropped.
func (b *builder) refuseToken(ctx context.Context, e Env, forge *url.URL, created *txn, token, why string) error {
	m := b.m
	if token != "" {
		host, _ := e.Sys.Hostname()
		err := postJSON(ctx, e.client(), forge.Scheme+"://"+forge.Host+"/api/v1/r/"+url.PathEscape(m.Repo)+"/ci/worker-tokens/retire", "",
			map[string]string{"token": token, "host": host}, nil)
		if err == nil {
			created.rollback()
			return fmt.Errorf("%s; the token it minted was retired and nothing is left installed; ask for a new code", why)
		}
		if werr := b.file(m.envFile(), []byte("QUARRY_RUNNER_TOKEN="+token+"\n"), "root", "root", 0o600); werr == nil && b.manifest() == nil {
			return fmt.Errorf("%s, and the token could not be retired (%v); it is kept in %s, read `quarry-runner doctor %s`, or uninstall", why, err, m.envFile(), m.Name)
		}
	}
	created.rollback()
	return fmt.Errorf("%s; the code is spent and its token (id %s) could not be retired: revoke it in the repository's Settings, Worker tokens", why, m.TokenID)
}

// 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", 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)
		}
	}
	return forgeReachable(ctx, e, forge)
}

// forgeReachable is whether the forge answers its API.
func forgeReachable(ctx context.Context, e Env, forge *url.URL) error {
	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 {
	return b.sharedDirAs(path, "root", "root", 0o755)
}

func (b *builder) sharedDirAs(path, owner, group string, mode fs.FileMode) error {
	// Judged without following: a link left where a directory belongs is
	// refused, not used.
	if info, err := b.s.Stat(path); err == nil {
		if info.IsLink {
			return fmt.Errorf("%s is a link; refusing to use it", path)
		}
		return nil
	}
	return b.mkdir(path, owner, group, mode, 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 {
	return b.programCopyTo(program, "root", "root")
}

func (b *builder) programCopyTo(program, owner, group string) error {
	self, err := b.s.Executable()
	if err != nil {
		return err
	}
	if self == program {
		return nil
	}
	// Copied as a stream: the program is far larger than anything
	// ReadFile reads whole. Skipped when the copy already matches, and
	// removed again if this run fails, as every file of a run is.
	_, existed := b.s.Stat(program)
	if existed == nil {
		have, herr := b.s.Sum256(program)
		want, werr := b.s.Sum256(self)
		if herr == nil && werr == nil && have == want {
			return nil
		}
	}
	if err := b.s.CopyFile(self, program, owner, group, 0o755); err != nil {
		return err
	}
	if existed != nil {
		b.tx.onFail(func() { b.s.Remove(program) })
	}
	return nil
}

func (b *builder) sudoers(ctx context.Context) error {
	m := b.m
	path := m.sudoersPath()
	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) }