zinit

package
v0.1.7 Latest Latest
Warning

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

Go to latest
Published: Apr 28, 2025 License: Apache-2.0 Imports: 15 Imported by: 0

README

zinit package

-zinit Package exposes function to interact with zinit service life cycle management. So we can add, remove, list ... services

Create zinit client

  • To interact with zinit services you first should create an instance on zinit client
cl := zinit.New("/var/run/zinit.sock")
// or just use default to connect to the default socket path
cl := zinit.Default()

Adding service

err := cl.AddService("name", zinit.InitService{
		Exec: cmd, // the command to be executed i.e the actual service
		Env: map[string]string{
			"Key": "value",
		},
        Oneshot: false,
		After:   []string{},
	})

Removing a service

 err := cl.RemoveService(name);

Loading a service so you can check its properties or do modifications

service, err := cl.LoadService(name)

list all services

services, err := cl.List()

Check if a specific service exists

exists, err := cl.Exists("name")

Getting a service by name

cfg, err := cl.Get("name")

Getting service status

status, err := cl.Status("name")

check if the service is exited/stopped

status, err := cl.Status("name")
exited := status.State.Exited()

Getting zinit binary version

version, err := cl.Version()

Rebooting and shutdown the node

err := cl.Reboot()
err := cl.Shutdown()

Starting/Stopping a service

err := cl.Start("name")
err := cl.stop("name")

Start/Stop a service and wait for it to start

err := cl.StartWait(time.Second*20, "name");
err := cl.StopWait(time.Second*20, "name");

Start monitoring a service

err := cl.Monitor("name");

Forget a stopped service

err := cl.Forget("name");

Kill a running service

err := cl.Kill("name", zinit.SIGTERM); // or choose whatever signal you want

Starting/Stopping multiple services

err := cl.StartMultiple(time.Second*20, "name1", "name2", ...., "namex")
err := cl.StopMultiple(time.Second*20, "name1", "name2", ...., "namex")

List services that matches some filters

filter1 := zinit.WithExec("udhcpc")
filter2 := zinit.zinit.WithName(dhcp)
matched, err := s.z.Matches(filter1, filter2)

Destroy a service (Destroy given services completely (stop, forget and remove))

err := cl.Destroy(20*time.Second, "name");

Documentation

Overview

Package zinit exposes function to interat with zinit service life cyle management

Index

Constants

View Source
const (
	// ServiceStateUnknown is return when we cannot determine the status of a service
	ServiceStateUnknown PossibleState = "unknown"
	// ServiceStateRunning is return when we a service process is running and healthy
	ServiceStateRunning = "running"
	// ServiceStateBlocked  is returned if the service can't start because of an unsatisfied dependency
	ServiceStateBlocked = "blocked"
	// ServiceStateSpawned service has started, but zinit is not sure about its status yet.
	// this is usually a short-lived status, unless a test command is provided. In that case
	// the spawned state will only go to success if the test pass
	ServiceStateSpawned = "spawned"
	// ServiceStateSuccess is return when a one shot service exited without errors
	ServiceStateSuccess = "success"
	// ServiceStateError is return when we a service exit with an error (exit code != 0)
	ServiceStateError = "error"
	//ServiceStateFailure is set of zinit can not spawn a service in the first place
	//due to a missing executable for example. Unlike `error` which is returned if the
	//service itself exits with an error.
	ServiceStateFailure = "failure"
)

Variables

View Source
var (

	// ErrUnknownService is an error that is returned when a service is unknown to zinit
	ErrUnknownService = errors.New("unknown service")

	ErrAlreadyMonitored = errors.New("already monitored")

	ErrNotSupported = errors.New("operation not supported")
)
View Source
var (
	SIGABRT   = Signal("SIGABRT")
	SIGALRM   = Signal("SIGALRM")
	SIGBUS    = Signal("SIGBUS")
	SIGCHLD   = Signal("SIGCHLD")
	SIGCLD    = Signal("SIGCLD")
	SIGCONT   = Signal("SIGCONT")
	SIGFPE    = Signal("SIGFPE")
	SIGHUP    = Signal("SIGHUP")
	SIGILL    = Signal("SIGILL")
	SIGINT    = Signal("SIGINT")
	SIGIO     = Signal("SIGIO")
	SIGIOT    = Signal("SIGIOT")
	SIGKILL   = Signal("SIGKILL")
	SIGPIPE   = Signal("SIGPIPE")
	SIGPOLL   = Signal("SIGPOLL")
	SIGPROF   = Signal("SIGPROF")
	SIGPWR    = Signal("SIGPWR")
	SIGQUIT   = Signal("SIGQUIT")
	SIGSEGV   = Signal("SIGSEGV")
	SIGSTKFLT = Signal("SIGSTKFLT")
	SIGSTOP   = Signal("SIGSTOP")
	SIGSYS    = Signal("SIGSYS")
	SIGTERM   = Signal("SIGTERM")
	SIGTRAP   = Signal("SIGTRAP")
	SIGTSTP   = Signal("SIGTSTP")
	SIGTTIN   = Signal("SIGTTIN")
	SIGTTOU   = Signal("SIGTTOU")
	SIGUNUSED = Signal("SIGUNUSED")
	SIGURG    = Signal("SIGURG")
	SIGUSR1   = Signal("SIGUSR1")
	SIGUSR2   = Signal("SIGUSR2")
	SIGVTALRM = Signal("SIGVTALRM")
	SIGWINCH  = Signal("SIGWINCH")
	SIGXCPU   = Signal("SIGXCPU")
	SIGXFSZ   = Signal("SIGXFSZ")
)

List of supported signal

Functions

func AddService

func AddService(name string, service InitService) error

AddService write the service into a file in the expected location for Zinit you usually want to call Monitor(name) after adding a service

func RemoveService

func RemoveService(name string) error

RemoveService delete the service file from the filesystem make sure the service has been stopped and forgot before deleting it

Types

type Client

type Client struct {
	// contains filtered or unexported fields
}

Client is a client for zinit action it talks to zinit directly over its unis socket

func Default

func Default() *Client

func New

func New(socket string) *Client

New create a new Zinit client

func (*Client) Destroy

func (c *Client) Destroy(timeout time.Duration, services ...string) error

Destroy given services completely (stop, forget and remove)

func (*Client) Exists

func (c *Client) Exists(service string) (bool, error)

Exists checks whether a service is monitored or not

func (*Client) Forget

func (c *Client) Forget(service string) error

Forget forgets a service. you can only forget a stopped service

func (*Client) Get

func (c *Client) Get(service string) (InitService, error)

Get gets the service info

func (*Client) Kill

func (c *Client) Kill(service string, sig Signal) error

Kill sends a signal to a running service. sig must be a valid signal (SIGINT, SIGKILL,...)

func (*Client) List

func (c *Client) List() (out map[string]ServiceState, err error)

List returns all the service monitored and their status

func (*Client) Matches

func (c *Client) Matches(filters ...Filter) ([]string, error)

Search for services. A services will be matched if all (not some) filters match this service

func (*Client) Monitor

func (c *Client) Monitor(service string) error

Monitor starts monitoring a service

func (*Client) Reboot

func (c *Client) Reboot() error

func (*Client) Shutdown

func (c *Client) Shutdown() error

func (*Client) Start

func (c *Client) Start(service string) error

Start start service. has no effect if the service is already running

func (*Client) StartMultiple

func (c *Client) StartMultiple(timeout time.Duration, service ...string) error

Start multiple services

func (*Client) StartWait

func (c *Client) StartWait(timeout time.Duration, service string) error

StartWait starts a service and wait until its running, or until the timeout (seconds) pass. If timedout, the method returns an error if the service is not running timout of 0 means no wait. (similar to Stop) timout is a min of 1 second

func (*Client) Status

func (c *Client) Status(service string) (result ServiceStatus, err error)

Status returns the status of a service

func (*Client) Stop

func (c *Client) Stop(service string) error

Stop stops a service

func (*Client) StopMultiple

func (c *Client) StopMultiple(timeout time.Duration, service ...string) error

Stop multiple services

func (*Client) StopWait

func (c *Client) StopWait(timeout time.Duration, service string) error

StopWait stops a service and wait until it exits, or until the timeout (seconds) pass. If timedout, the service is killed with -9. timout of 0 means no wait. (similar to Stop) timout is a min of 1 second

func (*Client) Version

func (c *Client) Version() (semver.Version, error)

type Filter

type Filter interface {
	// contains filtered or unexported methods
}

func WithExec

func WithExec(basename string) Filter

matches the exec basename

func WithExecRegex

func WithExecRegex(regex string) Filter

matche the exec if it matches the given regular expression note that it has to be a valid regular expression, otherwise it won't be matched

func WithName

func WithName(name string) Filter

matches the service name

type InitService

type InitService struct {
	Exec    string            `yaml:"exec,omitempty"`
	Oneshot bool              `yaml:"oneshot,omitempty"`
	Test    string            `yaml:"test,omitempty"`
	After   []string          `yaml:"after,omitempty"`
	Env     map[string]string `yaml:"env,omitempty"`
	Log     LogType           `yaml:"log,omitempty"`
}

InitService represent a Zinit service file

func LoadService

func LoadService(name string) (service InitService, err error)

type LogType

type LogType string

LogType is an enum type

const (
	StdoutLogType LogType = "stdout"
	RingLogType   LogType = "ring"
	NoneLogType   LogType = "none"
)

All the type of logging supported by zinit

func (*LogType) UnmarshalYAML

func (s *LogType) UnmarshalYAML(unmarshal func(interface{}) error) error

UnmarshalYAML implements the yaml.Unmarshaler interface

type PossibleState

type PossibleState string

PossibleState represents the state of a service managed by zinit

type ServiceState

type ServiceState struct {
	// contains filtered or unexported fields
}

ServiceState describes the service state

func (*ServiceState) Any

func (s *ServiceState) Any(states ...PossibleState) bool

Is checks if service state is equal to a possible state

func (*ServiceState) Exited

func (s *ServiceState) Exited() bool

Exited is true if the service state in a (stopped) state

func (*ServiceState) Is

func (s *ServiceState) Is(state PossibleState) bool

Is checks if service state is equal to a possible state

func (*ServiceState) MarshalYAML

func (s *ServiceState) MarshalYAML() (interface{}, error)

MarshalYAML implements the yaml.Unmarshaler interface

func (*ServiceState) String

func (s *ServiceState) String() string

func (*ServiceState) UnmarshalText

func (s *ServiceState) UnmarshalText(text []byte) error

UnmarshalYAML implements the yaml.Unmarshaler interface

type ServiceStatus

type ServiceStatus struct {
	Name   string            `json:"name"`
	Pid    int               `json:"pid"`
	State  ServiceState      `json:"state"`
	Target ServiceTarget     `json:"target"`
	After  map[string]string `json:"after"`
}

ServiceStatus represent the status of a service

type ServiceTarget

type ServiceTarget string

ServiceTarget represents the desired state of a service

const (
	// ServiceTargetUp means the service has been asked to start
	ServiceTargetUp ServiceTarget = "Up"
	// ServiceTargetDown means the service has been asked to stop
	ServiceTargetDown = "Down"
)

func (*ServiceTarget) UnmarshalYAML

func (s *ServiceTarget) UnmarshalYAML(unmarshal func(interface{}) error) error

UnmarshalYAML implements the yaml.Unmarshaler interface

type Signal

type Signal string

Signal is a type that maps linux signal to a string it is used by the Kill method

Jump to

Keyboard shortcuts

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