service

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: 15 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 service is the service behavior: the services of the host, systemd on Linux and launchd on macOS.

Index

Constants

This section is empty.

Variables

View Source
var Behavior = behavior.Register(behavior.Define(
	"service", "The services of the host: systemd on Linux, launchd on macOS",
	behavior.Node,
	behavior.API,
	behavior.Area(area.Services),
	behavior.CLI,
	behavior.Requires(object.Behavior),
	behavior.Requires(managedfile.Behavior),
	behavior.Platform(behavior.PlatformDebian, newDebian),
	behavior.Platform(behavior.PlatformLinux, newReader),
	behavior.Platform(behavior.PlatformDarwin, newDarwin),
	behavior.Writes("/etc/systemd/system/osapi-*.service"),
	behavior.Writes("/Library/LaunchDaemons/io.osapi.*.plist"),
	behavior.RunsOnPlatform(behavior.PlatformLinux, "systemctl"),
	behavior.RunsOnPlatform(behavior.PlatformDarwin, "launchctl"),
	behavior.SudoOnPlatform(behavior.PlatformDebian, "systemctl"),
	behavior.SudoOnPlatform(behavior.PlatformDarwin, "launchctl"),
	behavior.Read("list", List).
		Describe("List the services of the host").
		Explain("Lists each service that systemd has loaded, with its state, whether it "+
			"starts at boot and its description, through gohai. A service that osapi "+
			"deployed shows by the name it was deployed with, not its unit's osapi- "+
			"name, and is managed. On a Mac, it lists each daemon that launchd has loaded "+
			"in the system domain, by its label. A daemon that osapi deployed reads by the "+
			"name it was deployed with, app for io.osapi.app.").
		Example("A service that osapi deployed reads by its name", ListInput{},
			[]Entry{ssh, appEntry},

			behavior.OnPlatform(behavior.PlatformDebian),
			stored, deployed,
			behavior.Prints(units, sshUnit+appDead),
			behavior.Prints(files, sshFile+appOff),
		).
		Example("A host with two services", ListInput{},
			[]Entry{ssh, {Name: "osapi-app", State: "dead", Description: "Example app"}},
			linuxOnly,
			behavior.Prints(units, sshUnit+appDead),
			behavior.Prints(files, sshFile+appOff),
		).
		Example("A Mac, with a daemon that osapi deployed", ListInput{},
			[]Entry{cron, appMac},
			macOnly, storedPlist, deployedPlist,
			behavior.Prints(lcSystem, lcOut(lcCron+lcAppUp, "")),
		),
	behavior.Read("get", Get).Key("name").
		Describe("Get one service").
		Explain("Reads one service from the same list, by its name with .service or "+
			"without it. A service that osapi deployed reads by the name it was deployed "+
			"with, such as app, as its writes take it, or by its unit's name, osapi-app. "+
			"A service that systemd has not loaded is not found. On a Mac, a daemon that osapi "+
			"deployed and launchd has not loaded is dead.").
		Example("A running service", GetInput{Name: "ssh"}, ssh,
			linuxOnly,
			behavior.Live("name", behavior.FactSSHService),
			behavior.Prints(units, sshUnit),
			behavior.Prints(files, sshFile),
		).
		Example("A name with .service", GetInput{Name: "ssh.service"}, ssh,
			linuxOnly,
			behavior.Live("name", behavior.FactSSHUnit),
			behavior.Prints(units, sshUnit),
			behavior.Prints(files, sshFile),
		).
		Example("A service that osapi deployed, by the name it was deployed with",
			GetInput{Name: "app"}, appEntry,

			behavior.OnPlatform(behavior.PlatformDebian),
			stored, deployed,
			behavior.Prints(units, appDead),
			behavior.Prints(files, appOff),
		).
		Example("A service that osapi deployed, by its unit's name",
			GetInput{Name: "osapi-app"}, appEntry,

			behavior.OnPlatform(behavior.PlatformDebian),
			stored, deployed,
			behavior.Prints(units, appDead),
			behavior.Prints(files, appOff),
		).
		Example("A daemon on a Mac, by its label", GetInput{Name: "com.apple.cron"}, cron,
			macOnly,
			behavior.Prints(lcSystem, lcOut(lcCron, "")),
		).
		Example("A daemon that osapi deployed and launchd has not loaded",
			GetInput{Name: "app"}, Entry{Name: "app", State: "dead", Enabled: true, Managed: true},
			macOnly, storedPlist, deployedPlist,
			behavior.Prints(lcSystem, lcOut(lcCron, "")),
		).
		Fails("A service that systemd has not loaded", GetInput{Name: "nosuch"}, "not found").
		Fails("A name with a space", GetInput{Name: "a b"}, "Name"),
	behavior.Write("create", Create).
		On(behavior.PlatformDebian, behavior.PlatformDarwin).
		Describe("Deploy a service").
		Explain("Gets the object from the object store and writes it as the unit file "+
			"of the service, such as /etc/systemd/system/osapi-app.service for app, then reloads systemd. "+
			"A unit file at that path that osapi did not write, such as the agent's own "+
			"osapi-agent.service, is refused. A unit file "+
			"that already has the content does not change. The service does not start: "+
			"start and enable are their own actions. On a Mac, the object is the plist "+
			"of the daemon, /Library/LaunchDaemons/io.osapi.app.plist for app, with the label "+
			"io.osapi.app, and launchd does not load it until start.").
		Example("Deploy a unit file", CreateInput{Name: "app", Object: "app.service"},
			Result{Name: "app", Changed: true},
			debianOnly,
			behavior.Stored("app.service", unitText, types.ContentRaw),
			behavior.Wrote(appPath, unitText),
			behavior.Ran("systemctl", "daemon-reload"),
			behavior.Reads(GetInput{Name: "app"}, Entry{Name: "app", Managed: true}),
		).
		Example("A unit file that has the content", CreateInput{Name: "app", Object: "app.service"},
			Result{Name: "app"},
			debianOnly,
			behavior.Stored("app.service", unitText, types.ContentRaw),
			behavior.After(CreateInput{Name: "app", Object: "app.service"}),
			behavior.Reads(GetInput{Name: "app"}, Entry{Name: "app", Managed: true}),
		).
		Example("Deploy a plist on a Mac", CreateInput{Name: "app", Object: "app.plist"},
			Result{Name: "app", Changed: true},
			macOnly, storedPlist,
			behavior.Wrote(plistApp, plistText),
			behavior.Reads(GetInput{Name: "app"}, Entry{Name: "app", Managed: true}),
		).
		Example("A plist that has the content", CreateInput{Name: "app", Object: "app.plist"},
			Result{Name: "app"},
			macOnly, storedPlist, deployedPlist,
			behavior.Reads(GetInput{Name: "app"}, Entry{Name: "app", Managed: true}),
		).
		Fails("An object the store does not have", CreateInput{Name: "app", Object: "nosuch"}, "not found").
		Fails("A unit file or a plist that osapi did not deploy",
			CreateInput{Name: "agent", Object: "app.service"},
			"osapi did not deploy it", stored, agentUnit, agentPlist).
		Fails("No object", CreateInput{Name: "app"}, "Object").
		Fails("An object name over 255 characters",
			CreateInput{Name: "app", Object: strings.Repeat("a", 256)}, "Object").
		Fails("A name with a space", CreateInput{Name: "a b", Object: "app.service"}, "Name").
		Fails("A name that is an option", CreateInput{Name: "--now", Object: "app.service"},
			"not a valid service name"),
	behavior.Write("update", Update).Key("name").
		On(behavior.PlatformDebian, behavior.PlatformDarwin).
		Describe("Deploy a new unit file for a service").
		Explain("Writes the object as the unit file of a service that osapi deployed, "+
			"then reloads systemd when the file changed. A service that osapi did not "+
			"deploy is not found. On a Mac, it writes the plist, and a daemon that runs keeps "+
			"its old plist until restart.").
		Example("A new unit file", UpdateInput{Name: "app", Object: "app-v2.service"},
			Result{Name: "app", Changed: true},
			debianOnly,
			stored,
			deployed,
			behavior.Stored("app-v2.service", unitText+"Restart=always\n", types.ContentRaw),
			behavior.Wrote(appPath, unitText+"Restart=always\n"),
			behavior.Ran("systemctl", "daemon-reload"),
			behavior.Reads(GetInput{Name: "app"}, Entry{Name: "app", Managed: true}),
		).
		Fails("A service that osapi did not deploy", UpdateInput{Name: "ssh", Object: "app.service"}, "not found").
		Example("A new plist on a Mac", UpdateInput{Name: "app", Object: "app-v2.plist"},
			Result{Name: "app", Changed: true},
			macOnly, storedPlist, deployedPlist,
			behavior.Stored("app-v2.plist", plistV2, types.ContentRaw),
			behavior.Wrote(plistApp, plistV2),
			behavior.Reads(GetInput{Name: "app"}, Entry{Name: "app", Managed: true}),
		).
		Fails("A unit file or a plist that osapi did not deploy",
			UpdateInput{Name: "agent", Object: "app.service"},
			"not found", stored, agentUnit, agentPlist).
		Fails("No object", UpdateInput{Name: "app"}, "Object").
		Fails("An object name over 255 characters",
			UpdateInput{Name: "app", Object: strings.Repeat("a", 256)}, "Object").
		Fails("A name with a space", UpdateInput{Name: "a b", Object: "app.service"}, "Name").
		Fails("A name that is an option", UpdateInput{Name: "--now", Object: "app.service"},
			"not a valid service name"),
	behavior.Write("delete", Delete).Key("name").
		On(behavior.PlatformDebian, behavior.PlatformDarwin).
		Describe("Remove a service that osapi deployed").
		Explain("Stops and disables the service, removes its unit file and reloads "+
			"systemd. A service with no unit file does not change. On a Mac, it boots the "+
			"daemon out of launchd when it is loaded, and removes its plist.").
		Example("Remove a service", DeleteInput{Name: "app"},
			Result{Name: "app", Changed: true},
			debianOnly,
			behavior.Stored("app.service", unitText, types.ContentRaw),
			behavior.After(CreateInput{Name: "app", Object: "app.service"}),
			behavior.Removed(appPath),
			behavior.Ran("systemctl", "disable", "--now", "osapi-app.service"),
			behavior.Ran("systemctl", "daemon-reload"),
			behavior.ReadsNone(GetInput{Name: "app"}, Entry{Name: "app", Managed: true}, "name", "managed"),
		).
		Example("A service that osapi did not deploy", DeleteInput{Name: "app"},
			Result{Name: "app"},
			behavior.ReadsNone(GetInput{Name: "app"}, Entry{Name: "app", Managed: true}, "name", "managed"),
		).
		Example("A unit file that osapi did not deploy", DeleteInput{Name: "agent"},
			Result{Name: "agent"},
			debianOnly, agentUnit,
			behavior.ReadsNone(GetInput{Name: "agent"}, Entry{Name: "agent", Managed: true}, "name", "managed"),
		).
		Example("Remove a daemon that runs, on a Mac", DeleteInput{Name: "app"},
			Result{Name: "app", Changed: true},
			macOnly, storedPlist, deployedPlist,
			behavior.After(StartInput{Name: "app"}),
			behavior.Prints(lcSystem, lcOut(lcCron+lcAppUp, "")),
			behavior.Prints(lcPrint, lcLoaded),
			behavior.Ran("launchctl", "bootout", "system/io.osapi.app"),
			behavior.Removed(plistApp),
			behavior.ReadsNone(GetInput{Name: "app"}, Entry{Name: "app", Managed: true}, "name", "managed"),
		).
		Example("A plist that osapi did not deploy", DeleteInput{Name: "agent"},
			Result{Name: "agent"},
			macOnly, agentPlist,
			behavior.ReadsNone(GetInput{Name: "agent"}, Entry{Name: "agent", Managed: true}, "name", "managed"),
		).
		Fails("A name with a space", DeleteInput{Name: "a b"}, "Name").
		Fails("A name that is an option", DeleteInput{Name: "--now"}, "not a valid service name"),
	action(
		"start",
		Start,
		"Start a service that osapi deployed",
		"Starts the service. A service that runs does not change.",
		StartInput{
			Name: "app",
		},
		appDead,
		appOff,
		appUp,
		appOff,
		StartInput{Name: "ssh"},
		StartInput{Name: "agent"},
		StartInput{Name: "a b"},
		StartInput{Name: "--now"},
	),
	action(
		"stop",
		Stop,
		"Stop a service that osapi deployed",
		"Stops the service. A service that does not run does not change.",
		StopInput{
			Name: "app",
		},
		appUp,
		appOff,
		appDead,
		appOff,
		StopInput{Name: "ssh"},
		StopInput{Name: "agent"},
		StopInput{Name: "a b"},
		StopInput{Name: "--now"},
	),
	action(
		"restart",
		Restart,
		"Restart a service that osapi deployed",
		"Restarts the service. It always changes the host.",
		RestartInput{
			Name: "app",
		},
		appUp,
		appOn,
		appUp,
		appOn,
		RestartInput{Name: "ssh"},
		RestartInput{Name: "agent"},
		RestartInput{Name: "a b"},
		RestartInput{Name: "--now"},
	),
	action(
		"enable",
		Enable,
		"Make a service that osapi deployed start at boot",
		"Enables the service. A service that is enabled does not change, and nor does a unit "+
			"with no [Install] section, which systemd calls static: it cannot start at boot.",
		EnableInput{
			Name: "app",
		},
		appDead,
		appOff,
		appDead,
		appOn,
		EnableInput{Name: "ssh"},
		EnableInput{Name: "agent"},
		EnableInput{Name: "a b"},
		EnableInput{Name: "--now"},
	).
		Example("A unit with no [Install] section", EnableInput{Name: "static"},
			Result{Name: "static"},
			debianOnly,
			behavior.Stored("static.service", staticText, types.ContentRaw),
			behavior.After(CreateInput{Name: "static", Object: "static.service"}),
			behavior.Prints(units, "osapi-static.service loaded inactive dead Example static\n"),
			behavior.Prints(files, "osapi-static.service static -\n"),
			behavior.Ran("systemctl", "enable", "osapi-static.service"),
			behavior.Reads(GetInput{Name: "static"}, Entry{Name: "static", Managed: true}, "name", "enabled", "managed"),
		),
	action(
		"disable",
		Disable,
		"Make a service that osapi deployed not start at boot",
		"Disables the service. A service that is not enabled does not change.",
		DisableInput{
			Name: "app",
		},
		appDead,
		appOn,
		appDead,
		appOff,
		DisableInput{Name: "ssh"},
		DisableInput{Name: "agent"},
		DisableInput{Name: "a b"},
		DisableInput{Name: "--now"},
	),
))

Behavior is the systemd services of the host. gohai reads every service. osapi writes and acts on the services it deploys only: each has a unit file /etc/systemd/system/osapi-<name>.service that managedfile has a record of, and whose content is an object that a person uploads once with osapi client file upload. A unit file with the prefix that osapi did not write, such as the agent's own osapi-agent.service, is not osapi's to change.

Functions

This section is empty.

Types

type CreateInput

type CreateInput Unit

CreateInput is a unit file for osapi to deploy.

type DeleteInput

type DeleteInput Target

DeleteInput names the service whose unit file osapi drops.

type DisableInput

type DisableInput Target

DisableInput names the service that disable acts on.

type EnableInput

type EnableInput Target

EnableInput names the service that enable acts on.

type Entry

type Entry struct {
	Name        string `json:"name"        doc:"The service, without .service"`
	State       string `json:"state"       doc:"What systemd says it does now, such as running, exited or dead"`
	Enabled     bool   `json:"enabled"     doc:"Whether it starts at boot: the state of its unit file"`
	Description string `json:"description" doc:"The Description= of its unit"`
	Managed     bool   `json:"managed"     doc:"Whether osapi deployed it, so its writes and actions act on it"`
}

Entry is a service of the host.

func Get

func Get(ctx context.Context, p Provider, in GetInput) (Entry, error)

Get is one service of the host.

func List

func List(ctx context.Context, p Provider, _ ListInput) ([]Entry, error)

List is every service of the host.

type GetInput

type GetInput struct {
	Name string `json:"name" validate:"required,service_name" doc:"The service, such as nginx, with .service or without it"`
}

GetInput names the service to read.

type ListInput

type ListInput struct{}

ListInput takes nothing: list reads every service.

type Provider

type Provider interface {
	// Services is every service of the host.
	Services(ctx context.Context) ([]Entry, error)
	// Service is one service of the host, with its main process.
	Service(ctx context.Context, name string) (Entry, error)
	// Deploy writes the unit file of u. With create, it changes nothing when
	// osapi already has the unit; without it, it fails when osapi has none.
	Deploy(ctx context.Context, u Unit, create bool) (bool, error)
	// Remove stops and disables the service and drops its unit file.
	Remove(ctx context.Context, name string) (bool, error)
	// Act runs the systemctl action, such as start, on the service that
	// osapi manages.
	Act(ctx context.Context, action string, name string) (bool, error)
}

Provider reads the services of the host, and manages the unit files and the state of the services that osapi deploys.

type RestartInput

type RestartInput Target

RestartInput names the service that restart acts on.

type Result

type Result struct {
	Name    string `json:"name"    doc:"The service"`
	Changed bool   `json:"changed" doc:"Whether the host changed"`
}

Result is a service after a write or an action.

func Create

func Create(ctx context.Context, p Provider, in CreateInput) (Result, error)

Create deploys a unit file that osapi does not have yet.

func Delete

func Delete(ctx context.Context, p Provider, in DeleteInput) (Result, error)

Delete stops and disables a service that osapi deployed, and drops its unit file.

func Disable

func Disable(ctx context.Context, p Provider, in DisableInput) (Result, error)

Disable makes a service that osapi manages not start at boot.

func Enable

func Enable(ctx context.Context, p Provider, in EnableInput) (Result, error)

Enable makes a service that osapi manages start at boot.

func Restart

func Restart(ctx context.Context, p Provider, in RestartInput) (Result, error)

Restart restarts a service that osapi manages.

func Start

func Start(ctx context.Context, p Provider, in StartInput) (Result, error)

Start starts a service that osapi manages.

func Stop

func Stop(ctx context.Context, p Provider, in StopInput) (Result, error)

Stop stops a service that osapi manages.

func Update

func Update(ctx context.Context, p Provider, in UpdateInput) (Result, error)

Update deploys a unit file over the one osapi has.

type StartInput

type StartInput Target

StartInput names the service that start acts on.

type StopInput

type StopInput Target

StopInput names the service that stop acts on.

type Target

type Target struct {
	Name string `json:"name" validate:"required,service_name" doc:"The service, such as app for osapi-app.service" telemetry:"attr"`
}

Target names a service that osapi manages, for a delete or an action.

type Unit

type Unit struct {
	Name   string `` /* 137-byte string literal not displayed */
	Object string `json:"object" validate:"required,min=1,max=255" doc:"The object that holds the unit file, from osapi client file upload"`
}

Unit is the unit file of a service that osapi manages: the name that /etc/systemd/system/osapi-<name>.service has, and the object of the object store that holds its content.

type UpdateInput

type UpdateInput Unit

UpdateInput is the unit file to deploy over the one osapi has.

Directories

Path Synopsis
Package mocks holds the generated double of the service provider, for the methods' tests.
Package mocks holds the generated double of the service provider, for the methods' tests.

Jump to

Keyboard shortcuts

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