Skip to main content

Service

c.Service calls the API's Service group. The systemd services of the host.

Methods​

MethodReturnsPermissionDescription
List(ctx, hostname)*Response[Collection[ServiceEntry]]service:readList the services of the host.
Create(ctx, hostname, input)*Response[Collection[ServiceResult]]service:writeDeploy a service.
Get(ctx, hostname, name)*Response[Collection[ServiceEntry]]service:readGet one service.
Update(ctx, hostname, input)*Response[Collection[ServiceResult]]service:writeDeploy a new unit file for a service.
Delete(ctx, hostname, name)*Response[Collection[ServiceResult]]service:writeRemove a service that osapi deployed.
Disable(ctx, hostname, name)*Response[Collection[ServiceResult]]service:executeMake a service that osapi deployed not start at boot.
Enable(ctx, hostname, name)*Response[Collection[ServiceResult]]service:executeMake a service that osapi deployed start at boot.
Restart(ctx, hostname, name)*Response[Collection[ServiceResult]]service:executeRestart a service that osapi deployed.
Start(ctx, hostname, name)*Response[Collection[ServiceResult]]service:executeStart a service that osapi deployed.
Stop(ctx, hostname, name)*Response[Collection[ServiceResult]]service:executeStop a service that osapi deployed.

List​

func (s *ServiceService) List(
ctx context.Context,
hostname string,
) (*Response[Collection[ServiceEntry]], error)

List the services of the host.

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. A Mac has no systemd, and reports skipped.

It sends GET /api/node/{hostname}/service, and needs the permission service:read.

The examples of the method's definition:

A service that osapi deployed reads by its name, go run service.go list:

resp, err := c.Service.List(ctx, target)

Create​

func (s *ServiceService) Create(
ctx context.Context,
hostname string,
input ServiceCreateInput,
) (*Response[Collection[ServiceResult]], error)

Deploy a service.

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.

It sends POST /api/node/{hostname}/service, and needs the permission service:write.

The examples of the method's definition:

Deploy a unit file, go run service.go create:

resp, err := c.Service.Create(ctx, target, client.ServiceCreateInput{
Name: "app",
Object: "app.service",
})

Get​

func (s *ServiceService) Get(
ctx context.Context,
hostname string,
name string,
) (*Response[Collection[ServiceEntry]], error)

Get one service.

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.

It sends GET /api/node/{hostname}/service/{name}, and needs the permission service:read.

The examples of the method's definition:

A running service, go run service.go get:

resp, err := c.Service.Get(ctx, target, "ssh")

A name with .service, go run service.go get-2:

resp, err := c.Service.Get(ctx, target, "ssh.service")

A service that osapi deployed, by the name it was deployed with, go run service.go get-3:

resp, err := c.Service.Get(ctx, target, "app")

A service that osapi deployed, by its unit's name, go run service.go get-4:

resp, err := c.Service.Get(ctx, target, "osapi-app")

Update​

func (s *ServiceService) Update(
ctx context.Context,
hostname string,
input ServiceUpdateInput,
) (*Response[Collection[ServiceResult]], error)

Deploy a new unit file for a service.

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.

It sends PUT /api/node/{hostname}/service/{name}, and needs the permission service:write.

The examples of the method's definition:

A new unit file, go run service.go update:

resp, err := c.Service.Update(ctx, target, client.ServiceUpdateInput{
Name: "app",
Object: "app-v2.service",
})

Delete​

func (s *ServiceService) Delete(
ctx context.Context,
hostname string,
name string,
) (*Response[Collection[ServiceResult]], error)

Remove a service that osapi deployed.

Stops and disables the service, removes its unit file and reloads systemd. A service with no unit file does not change.

It sends DELETE /api/node/{hostname}/service/{name}, and needs the permission service:write.

The examples of the method's definition:

Remove a service, go run service.go delete:

resp, err := c.Service.Delete(ctx, target, "app")

A unit file that osapi did not deploy, go run service.go delete-2:

resp, err := c.Service.Delete(ctx, target, "agent")

Disable​

func (s *ServiceService) Disable(
ctx context.Context,
hostname string,
name string,
) (*Response[Collection[ServiceResult]], error)

Make a service that osapi deployed not start at boot.

Disables the service. A service that is not enabled does not change. A service that osapi did not deploy is not found.

It sends POST /api/node/{hostname}/service/{name}/disable, and needs the permission service:execute.

The examples of the method's definition:

Disable the service, go run service.go disable:

resp, err := c.Service.Disable(ctx, target, "app")

Enable​

func (s *ServiceService) Enable(
ctx context.Context,
hostname string,
name string,
) (*Response[Collection[ServiceResult]], error)

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. A service that osapi did not deploy is not found.

It sends POST /api/node/{hostname}/service/{name}/enable, and needs the permission service:execute.

The examples of the method's definition:

Enable the service, go run service.go enable:

resp, err := c.Service.Enable(ctx, target, "app")

A unit with no [Install] section, go run service.go enable-2:

resp, err := c.Service.Enable(ctx, target, "static")

Restart​

func (s *ServiceService) Restart(
ctx context.Context,
hostname string,
name string,
) (*Response[Collection[ServiceResult]], error)

Restart a service that osapi deployed.

Restarts the service. It always changes the host. A service that osapi did not deploy is not found.

It sends POST /api/node/{hostname}/service/{name}/restart, and needs the permission service:execute.

The examples of the method's definition:

Restart the service, go run service.go restart:

resp, err := c.Service.Restart(ctx, target, "app")

Start​

func (s *ServiceService) Start(
ctx context.Context,
hostname string,
name string,
) (*Response[Collection[ServiceResult]], error)

Start a service that osapi deployed.

Starts the service. A service that runs does not change. A service that osapi did not deploy is not found.

It sends POST /api/node/{hostname}/service/{name}/start, and needs the permission service:execute.

The examples of the method's definition:

Start the service, go run service.go start:

resp, err := c.Service.Start(ctx, target, "app")

Stop​

func (s *ServiceService) Stop(
ctx context.Context,
hostname string,
name string,
) (*Response[Collection[ServiceResult]], error)

Stop a service that osapi deployed.

Stops the service. A service that does not run does not change. A service that osapi did not deploy is not found.

It sends POST /api/node/{hostname}/service/{name}/stop, and needs the permission service:execute.

The examples of the method's definition:

Stop the service, go run service.go stop:

resp, err := c.Service.Stop(ctx, target, "app")

Types​

The fields of each type are the properties of its schema in the API's spec.

RowStatus​

What happened on the host: ok, the operation ran and succeeded; failed, it ran and did not succeed; skipped, the host's OS family does not support it; timeout, the host did not answer, so nobody knows whether it ran.

A string, one of ok, failed, skipped, timeout.

ServiceCreateInput​

FieldTypeDescription
NamestringThe service, such as app for osapi-app.service.
ObjectstringThe object that holds the unit file, from osapi client file upload.

ServiceEntry​

FieldTypeDescription
DescriptionstringThe Description= of its unit.
EnabledboolWhether it starts at boot: the state of its unit file.
ErrorstringError message if the agent failed.
HostnamestringHostname of the agent that produced this result.
ManagedboolWhether osapi deployed it, so its writes and actions act on it.
NamestringThe service, without .service.
StatestringWhat systemd says it does now, such as running, exited or dead.
StatusRowStatusWhat happened on the host: ok, the operation ran and succeeded; failed, it ran and did not succeed; skipped, the host's OS family does not support it; timeout, the host did not answer, so nobody knows whether it ran.

ServiceResult​

FieldTypeDescription
ChangedboolWhether the host changed.
ErrorstringError message if the agent failed.
HostnamestringHostname of the agent that produced this result.
NamestringThe service.
StatusRowStatusWhat happened on the host: ok, the operation ran and succeeded; failed, it ran and did not succeed; skipped, the host's OS family does not support it; timeout, the host did not answer, so nobody knows whether it ran.

ServiceUpdateInput​

FieldTypeDescription
NamestringThe name of the path. The service, such as app for osapi-app.service.
ObjectstringThe object that holds the unit file, from osapi client file upload.

Example​

examples/sdk/client/service.go runs each example of this page. With no argument, it lists them.