Servicemanager Go-API
servicemanager
Section titled “servicemanager”import "givc/modules/pkgs/servicemanager"
Copyright 2024 TII (SSRC) and the Ghaf contributors SPDX-License-Identifier: Apache-2.0
The servicemanager package provides functionality to manage systemd services and applications.
- Constants
- type SystemdControlServer
- func NewSystemdControlServer(whitelist []string, applications []types.ApplicationManifest) (*SystemdControlServer, error)
- func (s *SystemdControlServer) Close()
- func (s *SystemdControlServer) FreezeUnit(ctx context.Context, req *givc_systemd.UnitRequest) (*givc_systemd.UnitResponse, error)
- func (s *SystemdControlServer) GetUnitStatus(ctx context.Context, req *givc_systemd.UnitRequest) (*givc_systemd.UnitResponse, error)
- func (s *SystemdControlServer) KillUnit(ctx context.Context, req *givc_systemd.UnitRequest) (*givc_systemd.UnitResponse, error)
- func (s *SystemdControlServer) MonitorUnit(req *givc_systemd.UnitResourceRequest, stream givc_systemd.UnitControlService_MonitorUnitServer) error
- func (s *SystemdControlServer) Name() string
- func (s *SystemdControlServer) RegisterGrpcService(srv *grpc.Server)
- func (s *SystemdControlServer) StartApplication(ctx context.Context, req *givc_systemd.AppUnitRequest) (*givc_systemd.UnitResponse, error)
- func (s *SystemdControlServer) StartUnit(ctx context.Context, req *givc_systemd.UnitRequest) (*givc_systemd.UnitResponse, error)
- func (s *SystemdControlServer) StopUnit(ctx context.Context, req *givc_systemd.UnitRequest) (*givc_systemd.UnitResponse, error)
- func (s *SystemdControlServer) UnfreezeUnit(ctx context.Context, req *givc_systemd.UnitRequest) (*givc_systemd.UnitResponse, error)
- func (s *SystemdControlServer) getUnitStatus(ctx context.Context, name string) (*givc_systemd.UnitStatus, error)
- type SystemdController
- func NewController(whitelist []string, applications []givc_types.ApplicationManifest) (*SystemdController, error)
- func (c *SystemdController) Close()
- func (c *SystemdController) FindUnit(name string) ([]dbus.UnitStatus, error)
- func (c *SystemdController) FreezeUnit(ctx context.Context, name string) error
- func (c *SystemdController) IsUnitWhitelisted(name string) bool
- func (c *SystemdController) KillUnit(ctx context.Context, name string) error
- func (c *SystemdController) StartApplication(ctx context.Context, serviceName string, serviceArgs []string) (*dbus.UnitStatus, error)
- func (c *SystemdController) StartUnit(ctx context.Context, name string) error
- func (c *SystemdController) StopUnit(ctx context.Context, name string) error
- func (c *SystemdController) UnfreezeUnit(ctx context.Context, name string) error
- func (c *SystemdController) getUnitCpuAndMem(ctx context.Context, pid uint32) (float64, float32, error)
- func (c *SystemdController) getUnitProperties(ctx context.Context, unitName string) (map[string]interface{}, error)
- func (c *SystemdController) getUnitPropertyString(ctx context.Context, unitName string, propertyName string) (string, error)
Constants
Section titled “Constants”const ( NO_WAIT_FOR_MERGE = 0 * time.Second WAIT_FOR_MERGE = 2 * time.Second)
const ( ResourceStreamInterval = 400 * time.Millisecond)
type SystemdControlServer
Section titled “type SystemdControlServer”type SystemdControlServer struct { Controller *SystemdController givc_systemd.UnimplementedUnitControlServiceServer}
func NewSystemdControlServer
Section titled “func NewSystemdControlServer”func NewSystemdControlServer(whitelist []string, applications []types.ApplicationManifest) (*SystemdControlServer, error)
NewSystemdControlServer creates a new instance of SystemdControlServer with the provided service whitelist and applications.
func (*SystemdControlServer) Close
Section titled “func (*SystemdControlServer) Close”func (s *SystemdControlServer) Close()
func (*SystemdControlServer) FreezeUnit
Section titled “func (*SystemdControlServer) FreezeUnit”func (s *SystemdControlServer) FreezeUnit(ctx context.Context, req *givc_systemd.UnitRequest) (*givc_systemd.UnitResponse, error)
FreezeUnit handles the gRPC request to freeze (pause) a systemd unit.
func (*SystemdControlServer) GetUnitStatus
Section titled “func (*SystemdControlServer) GetUnitStatus”func (s *SystemdControlServer) GetUnitStatus(ctx context.Context, req *givc_systemd.UnitRequest) (*givc_systemd.UnitResponse, error)
GetUnitStatus handles the gRPC request to get the status of a systemd unit.
func (*SystemdControlServer) KillUnit
Section titled “func (*SystemdControlServer) KillUnit”func (s *SystemdControlServer) KillUnit(ctx context.Context, req *givc_systemd.UnitRequest) (*givc_systemd.UnitResponse, error)
KillUnit handles the gRPC request to kill a systemd unit.
func (*SystemdControlServer) MonitorUnit
Section titled “func (*SystemdControlServer) MonitorUnit”func (s *SystemdControlServer) MonitorUnit(req *givc_systemd.UnitResourceRequest, stream givc_systemd.UnitControlService_MonitorUnitServer) error
MonitorUnit handles the gRPC request to monitor a systemd unit’s resource usage. This is legacy code and will be removed.
func (*SystemdControlServer) Name
Section titled “func (*SystemdControlServer) Name”func (s *SystemdControlServer) Name() string
func (*SystemdControlServer) RegisterGrpcService
Section titled “func (*SystemdControlServer) RegisterGrpcService”func (s *SystemdControlServer) RegisterGrpcService(srv *grpc.Server)
func (*SystemdControlServer) StartApplication
Section titled “func (*SystemdControlServer) StartApplication”func (s *SystemdControlServer) StartApplication(ctx context.Context, req *givc_systemd.AppUnitRequest) (*givc_systemd.UnitResponse, error)
StartApplication handles the gRPC request to start an application unit.
func (*SystemdControlServer) StartUnit
Section titled “func (*SystemdControlServer) StartUnit”func (s *SystemdControlServer) StartUnit(ctx context.Context, req *givc_systemd.UnitRequest) (*givc_systemd.UnitResponse, error)
StartUnit handles the gRPC request to start a systemd unit.
func (*SystemdControlServer) StopUnit
Section titled “func (*SystemdControlServer) StopUnit”func (s *SystemdControlServer) StopUnit(ctx context.Context, req *givc_systemd.UnitRequest) (*givc_systemd.UnitResponse, error)
StopUnit handles the gRPC request to stop a systemd unit.
func (*SystemdControlServer) UnfreezeUnit
Section titled “func (*SystemdControlServer) UnfreezeUnit”func (s *SystemdControlServer) UnfreezeUnit(ctx context.Context, req *givc_systemd.UnitRequest) (*givc_systemd.UnitResponse, error)
UnfreezeUnit handles the gRPC request to unfreeze (unpause) a systemd unit.
func (*SystemdControlServer) getUnitStatus
Section titled “func (*SystemdControlServer) getUnitStatus”func (s *SystemdControlServer) getUnitStatus(ctx context.Context, name string) (*givc_systemd.UnitStatus, error)
getUnitStatus fetches the status of a systemd unit by its name.
type SystemdController
Section titled “type SystemdController”type SystemdController struct { conn *dbus.Conn whitelist []string applications []givc_types.ApplicationManifest cancelCtx context.CancelFunc}
func NewController
Section titled “func NewController”func NewController(whitelist []string, applications []givc_types.ApplicationManifest) (*SystemdController, error)
NewController creates a new SystemdController instance, connecting to either the system or session bus, depending on the user (root or other). It further initializes the whitelist to restrict service/application access to only pre-defined units.
func (*SystemdController) Close
Section titled “func (*SystemdController) Close”func (c *SystemdController) Close()
func (*SystemdController) FindUnit
Section titled “func (*SystemdController) FindUnit”func (c *SystemdController) FindUnit(name string) ([]dbus.UnitStatus, error)
FindUnit returns the status of all units matching the name. It performs a whitelist check to ensure the unit is allowed to be queried.
func (*SystemdController) FreezeUnit
Section titled “func (*SystemdController) FreezeUnit”func (c *SystemdController) FreezeUnit(ctx context.Context, name string) error
FreezeUnit freezes a unit.
func (*SystemdController) IsUnitWhitelisted
Section titled “func (*SystemdController) IsUnitWhitelisted”func (c *SystemdController) IsUnitWhitelisted(name string) bool
IsUnitWhitelisted checks if a unit is whitelisted.
func (*SystemdController) KillUnit
Section titled “func (*SystemdController) KillUnit”func (c *SystemdController) KillUnit(ctx context.Context, name string) error
KillUnit forcefully terminates a unit.
func (*SystemdController) StartApplication
Section titled “func (*SystemdController) StartApplication”func (c *SystemdController) StartApplication(ctx context.Context, serviceName string, serviceArgs []string) (*dbus.UnitStatus, error)
StartApplication starts an application service with the dynamic arguments. Dynamic arguments are validated in accordance with the application manifest. The application is started as a transient systemd unit, which means it is not persisted. Since applications may be merged into a single unit, this function implements a pre-start analysis and a post-start watch mechanism to determine if the application is merged or not.
func (*SystemdController) StartUnit
Section titled “func (*SystemdController) StartUnit”func (c *SystemdController) StartUnit(ctx context.Context, name string) error
StartUnit starts any systemd unit.
func (*SystemdController) StopUnit
Section titled “func (*SystemdController) StopUnit”func (c *SystemdController) StopUnit(ctx context.Context, name string) error
StopUnit stops a unit by name.
func (*SystemdController) UnfreezeUnit
Section titled “func (*SystemdController) UnfreezeUnit”func (c *SystemdController) UnfreezeUnit(ctx context.Context, name string) error
UnfreezeUnit unfreezes (thaw) a unit.
func (*SystemdController) getUnitCpuAndMem
Section titled “func (*SystemdController) getUnitCpuAndMem”func (c *SystemdController) getUnitCpuAndMem(ctx context.Context, pid uint32) (float64, float32, error)
The function getUnitCpuAndMem returns the CPU and memory usage of a unit. No check against unit whitelist is performed and must be enforced by the caller. This functionality is deprecated.
func (*SystemdController) getUnitProperties
Section titled “func (*SystemdController) getUnitProperties”func (c *SystemdController) getUnitProperties(ctx context.Context, unitName string) (map[string]interface{}, error)
The function getUnitProperties returns all properties of a unit as a map. No check against unit whitelist is performed and must be enforced by the caller.
func (*SystemdController) getUnitPropertyString
Section titled “func (*SystemdController) getUnitPropertyString”func (c *SystemdController) getUnitPropertyString(ctx context.Context, unitName string, propertyName string) (string, error)
The function getUnitPropertyString returns the value of a specific property of a unit as a string. No check against unit whitelist is performed and must be enforced by the caller.
Generated by gomarkdoc