agentservice

package
v0.3.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Nov 27, 2026 License: MIT Imports: 10 Imported by: 0 modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Overview

Package agentservice installs the agent of this host as a service that starts at boot: a systemd unit on Linux, a launch daemon on macOS, with a config that has the join string.

Index

Constants

This section is empty.

Variables

View Source
var ErrExists = errors.New("the config file exists: --force replaces it")

ErrExists is a config file that exists, which install does not replace without --force.

View Source
var ErrNotRoot = errors.New("run this as root: the service and its config are root's")

ErrNotRoot is install or uninstall run by a user that is not root.

View Source
var ErrPlatform = errors.New("osapi installs a service on Linux (systemd) and macOS (launchd) only")

ErrPlatform is a platform with no service manager that osapi knows.

View Source
var Group = command.Register(command.Define(
	"service", "Run the agent of this host as a service that starts at boot",
	command.Under(command.ParentAgent),
	command.Uses[Deps](),
	command.Method[InstallInput, InstallOutput]("install").
		Short("Install the agent as a service, with a join string, and start it").
		Long("Run this as root on the host. It writes the config file with the join\n"+
			"string and agent.pki.key_dir, a systemd unit (Linux) or a launch daemon\n"+
			"(macOS) that runs this binary as osapi agent start, and starts it. Then\n"+
			"it waits up to a minute for the join. After the join, the host keeps its\n"+
			"keys and its NATS user in agent.pki.key_dir, and each start at boot\n"+
			"reads them: the join string is used one time.").
		Example("--join 'tls://nats.example.com:4222?creds=...&fp=SHA256%3A...&ns=osapi&token=osapi_jt_...'").
		Prints(sampleInstall).
		Fails("no join string", InstallInput{}).
		Fails("a join string of more than 8192 characters", InstallInput{
			Join: "tls://" + strings.Repeat("a", 8193),
		}).
		Handle(Install),
	command.Method[UninstallInput, UninstallOutput]("uninstall").
		Short("Stop the agent's service and remove it").
		Long("Run this as root on the host. It stops the service and removes its unit\n"+
			"or launch daemon. With --purge it also removes the config file and the\n"+
			"keys in agent.pki.key_dir: then the host is new to osapi, and joins again\n"+
			"only with a new join string.").
		Example("--purge").
		Prints(sampleUninstall).
		Handle(Uninstall),
))

Group is osapi agent service.

Functions

This section is empty.

Types

type Deps

type Deps struct {
	FS         avfs.VFS
	GOOS       string
	EUID       func() int
	Binary     func() (string, error)
	Run        func(ctx context.Context, name string, args ...string) error
	ConfigFile string
	KeyDir     string
	Wait       func(ctx context.Context, file string) bool
}

Deps is what install and uninstall use: the file system, the platform, the user that runs the command, this binary, a command runner, the config file and agent.pki.key_dir, and how to wait for a file.

type InstallInput

type InstallInput struct {
	Join   string            `json:"join"   validate:"required,max=8192" doc:"The join string that osapi controller join token create printed."`
	Labels map[string]string `json:"labels" validate:"omitempty"         doc:"Labels of the host, as key=value, beside the labels of the join token."`
	Force  bool              `json:"force"  validate:"omitempty"         doc:"Replace a config file that exists."`
}

InstallInput is the join string, and the labels that the host adds.

type InstallOutput

type InstallOutput struct {
	Service    string `                json:"service"`
	ConfigFile string `                json:"config_file"    label:"Config"`
	Joined     bool   `                json:"joined"`
	Next       string `                json:"next,omitempty"`
	// contains filtered or unexported fields
}

InstallOutput is what install wrote and whether the host joined.

func Install

func Install(
	ctx context.Context,
	d Deps,
	in InstallInput,
) (InstallOutput, error)

Install writes the config with the join string, the unit of the service, starts the service, and waits for the join.

type UninstallInput

type UninstallInput struct {
	Purge bool `json:"purge" validate:"omitempty" doc:"Also remove the config file and the keys of agent.pki.key_dir."`
}

UninstallInput is whether to remove the config and the keys too.

type UninstallOutput

type UninstallOutput struct {
	Removed []string `                json:"removed"`
	// contains filtered or unexported fields
}

UninstallOutput is each file that uninstall removed.

func Uninstall

func Uninstall(
	ctx context.Context,
	d Deps,
	in UninstallInput,
) (UninstallOutput, error)

Uninstall stops the service and removes its unit, and with Purge the config file and agent.pki.key_dir.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL