Skip to content
quarry Artifact [3995c03c28]
Login

Artifact [3995c03c28]

Artifact 3995c03c2831b243a608e5664be017c62c9e508f4cc4be941a62738d2c6a3e97:


// Package install is `quarry-runner install`, `doctor` and `uninstall`
// (RFC 0030, A CI runner by one command): a CI runner as a systemd
// service with its own users, from one command. Every effect on the
// machine goes through System, so the logic runs on any host against a
// fake; the real System shells out to the shadow-utils and systemctl.
package install

import (
	"context"
	"io/fs"
)

// User is a system user to create: its primary group is a group of the
// same name, which must exist.
type User struct {
	Name  string
	Group string
	Home  string
	Shell string
}

// Info is what Stat says about a path.
type Info struct {
	Owner string
	Group string
	Mode  fs.FileMode
	IsDir bool
}

// System is everything install, doctor and uninstall do to the machine.
type System interface {
	Geteuid() int
	Hostname() (string, error)
	// Executable is the running program, the one install copies.
	Executable() (string, error)
	LookPath(name string) (string, error)

	// Run runs a command as the current user; RunAs runs it as user.
	Run(ctx context.Context, name string, args ...string) ([]byte, error)
	RunAs(ctx context.Context, user, name string, args ...string) ([]byte, error)

	UserExists(name string) bool
	GroupExists(name string) bool
	InGroup(user, group string) bool
	AddGroup(name string) error
	AddUser(u User) error
	AddToGroup(user, group string) error
	DelUser(name string) error
	DelGroup(name string) error

	// Mkdir makes one directory (its parent exists) and sets owner and
	// mode, also on one that is already there.
	Mkdir(path, owner, group string, mode fs.FileMode) error
	// WriteFile replaces path atomically with data, owner and mode.
	WriteFile(path string, data []byte, owner, group string, mode fs.FileMode) error
	// CopyFile does the same with another file's bytes.
	CopyFile(src, dst, owner, group string, mode fs.FileMode) error
	Rename(from, to string) error
	ReadFile(path string) ([]byte, error)
	ReadDir(path string) ([]string, error)
	Stat(path string) (Info, error)
	Exists(path string) bool
	// Remove removes a file or an empty directory; an absent path is
	// not an error, a non-empty directory is.
	Remove(path string) error
	RemoveAll(path string) error
	// DiskFree is the bytes free to an unprivileged writer at path.
	DiskFree(path string) (uint64, error)
}