Skip to content
quarry Artifact [f740c3f52c]
Login

Artifact [f740c3f52c]

Artifact f740c3f52c0568f74b55a36ae293650ac57956e83af0855f2b0cc59fc65d52ff:


package install

import (
	"context"
	"errors"
	"flag"
	"fmt"
	"net/url"
	"path"
	"strings"
)

// Uninstall is `quarry-runner uninstall [--keep-work] S|URL`: stop the
// unit, retire the token, then remove what the manifest lists and
// nothing else.
func Uninstall(ctx context.Context, e Env, args []string) error {
	fs := flag.NewFlagSet("uninstall", flag.ContinueOnError)
	keepWork := fs.Bool("keep-work", false, "leave the work directory")
	pos, err := parseArgs(fs, args)
	if err != nil {
		return err
	}
	s := e.Sys
	if len(pos) == 1 && !strings.Contains(pos[0], "://") && pos[0] == "" {
		return &ExitError{Code: 2, Msg: "name the runner"}
	}
	name, err := target(e, pos)
	if err != nil {
		return err
	}
	if e.GOOS == "darwin" {
		return uninstallDarwin(ctx, e, name)
	}
	if s.Geteuid() != 0 {
		return errors.New("uninstall as root: run it with sudo")
	}
	m, err := loadManifest(s, name)
	if err != nil {
		return fmt.Errorf("no manifest, so nothing is known to remove: %w", err)
	}
	// The manifest is read as root and removed with RemoveAll: what it
	// names must look like what install makes.

	// Stopping lets running jobs be handed back (SIGTERM, then the unit's
	// TimeoutStopSec).
	if _, err := s.Run(ctx, "systemctl", "disable", "--now", m.unit()); err != nil {
		fmt.Fprintf(e.Out, "could not stop %s: %v\n", m.unit(), err)
	}

	var retireErr error
	token := ""
	if raw, err := s.ReadFile(m.envFile()); err == nil {
		token = envToken(raw)
	}
	if token != "" {
		if u, err := url.Parse(m.URL); err == nil {
			host, _ := s.Hostname()
			// A retired token answers 200 again, so only a success is
			// one: a 404 is a token the forge does not know, not one
			// that is revoked.
			retireErr = postJSON(ctx, e.client(), u.Scheme+"://"+u.Host+"/api/v1/r/"+url.PathEscape(m.Repo)+"/ci/worker-tokens/retire", "",
				map[string]string{"token": token, "host": host}, nil)
		} else {
			retireErr = err
		}
	} else if m.TokenID != "" {
		retireErr = errors.New("the env file holds no token to retire it with")
	}

	for _, p := range m.Paths {
		if *keepWork && (p == m.work() || p == m.home()) {
			continue
		}
		if err := s.RemoveAll(p); err != nil {
			return err
		}
	}
	s.Run(ctx, "systemctl", "daemon-reload")
	for _, u := range m.Users {
		if s.UserExists(u) {
			if err := s.DelUser(u); err != nil {
				return err
			}
		}
	}
	for _, g := range m.Groups {
		if s.GroupExists(g) {
			if err := s.DelGroup(g); err != nil {
				return err
			}
		}
	}
	if err := s.Remove(manifestPath(name)); err != nil {
		return err
	}
	// The directories and the program are shared with other runners:
	// they go with the last one.
	if rest, _ := s.ReadDir(confDir); !hasManifest(rest) {
		s.Remove(m.Program)
		s.Remove(path.Dir(m.Program))
	}
	s.Remove(confDir)
	s.Remove(stateDir)
	if retireErr != nil {
		return fmt.Errorf("removed, but the forge did not retire the worker token (id %s): %v; revoke it in the repository's Settings, Worker tokens", m.TokenID, retireErr)
	}
	fmt.Fprintf(e.Out, "uninstalled %s\n", name)
	return nil
}

func hasManifest(names []string) bool {
	for _, n := range names {
		if strings.HasSuffix(n, ".json") {
			return true
		}
	}
	return false
}