agent

package module
v0.13.0 Latest Latest
Warning

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

Go to latest
Published: Jan 22, 2025 License: AGPL-3.0 Imports: 35 Imported by: 0

README

Viam Agent

A self-updating service manager that maintains the lifecycle for viam-server (as built from RDK) and other Viam provided system services.

For more information, see the Viam Agent documentation.

Development

Makefile Targets
  • make will build a viam-agent for your current CPU architecture. Note that as only linux is supported, this will be a linux binary.
  • make arm64 arm64 specific build.
  • make amd64 amd64 specific.
  • make all will build for all (both) supported architectures.
  • make lint to lint.
Version Tagging

The makefile will attempt to get a tagged version from Git. If you want to manually force a version, set TAG_VERSION=0.1.2 in the make command.
Note that there is no "v" in the actual version, though it is expected in git. E.g. a git tag of v0.1.2 becomes TAG_VERSION=0.1.2
Ex: make all TAG_VERSION=0.1.2

DevMode

Setting the environment variable VIAM_AGENT_DEVMODE=1 will skip the self-location check for the binary, so you can run it directly during development, without installing to /opt/viam.

Documentation

Overview

Package agent contains the public interfaces, functions, consts, and vars for the viam-server agent.

Index

Constants

View Source
const (
	ShortFailTime   = time.Second * 30
	StartTimeout    = time.Minute
	StopTermTimeout = time.Second * 30
	StopKillTimeout = time.Second * 10
)
View Source
const (
	SubsystemName = "viam-agent"
)

Variables

View Source
var (
	// versions embedded at build time.
	Version     = ""
	GitRevision = ""

	ViamDirs = map[string]string{"viam": "/opt/viam"}
)
View Source
var ErrSubsystemDisabled = errors.New("subsystem disabled")

Functions

func CheckIfSame

func CheckIfSame(path1, path2 string) (bool, error)

func ConvertAttributes added in v0.9.0

func ConvertAttributes[T any](attributes *structpb.Struct) (*T, error)

func DecompressFile

func DecompressFile(inPath string) (outPath string, errRet error)

DecompressFile extracts a compressed file and returns the path to the extracted file.

func DownloadFile

func DownloadFile(ctx context.Context, rawURL string) (outPath string, errRet error)

DownloadFile downloads a file into the cache directory and returns a path to the file.

func ForceSymlink(orig, symlink string) error

func GetFileSum

func GetFileSum(filepath string) (outSum []byte, errRet error)

func GetRevision added in v0.9.0

func GetRevision() string

GetRevision returns the git revision embedded at build time.

func GetVersion added in v0.9.0

func GetVersion() string

GetVersion returns the version embedded at build time.

func InitPaths added in v0.1.4

func InitPaths() error

func SyncFS added in v0.3.0

func SyncFS(syncPath string) (errRet error)

func WriteFileIfNew added in v0.9.0

func WriteFileIfNew(outPath string, data []byte) (bool, error)

Types

type AgentSubsystem

type AgentSubsystem struct {
	CacheData *CacheData
	// contains filtered or unexported fields
}

AgentSubsystem is a wrapper for the real subsystems, mostly allowing sharing of download/update code.

func NewAgentSubsystem

func NewAgentSubsystem(
	ctx context.Context,
	name string,
	logger logging.Logger,
	subsys BasicSubsystem,
) (*AgentSubsystem, error)

NewAgentSubsystem returns a new wrapped subsystem.

func (*AgentSubsystem) HealthCheck

func (s *AgentSubsystem) HealthCheck(ctx context.Context) error

HealthCheck calls the inner subsystem's HealthCheck() to verify, and logs failures/successes.

func (*AgentSubsystem) LoadCache

func (s *AgentSubsystem) LoadCache() error

LoadCache loads the cached data for the subsystem from disk.

func (*AgentSubsystem) SaveCache

func (s *AgentSubsystem) SaveCache() error

SaveCache saves the cached data to disk.

func (*AgentSubsystem) Start

func (s *AgentSubsystem) Start(ctx context.Context) error

Start starts the subsystem.

func (*AgentSubsystem) Stop

func (s *AgentSubsystem) Stop(ctx context.Context) error

Stop stops the subsystem.

func (*AgentSubsystem) Update

Update is the main function of the AgentSubsystem wrapper, as it's shared between subsystems. Returns true if a restart is needed.

func (*AgentSubsystem) Version

func (s *AgentSubsystem) Version() string

Version returns the running version.

type BasicSubsystem

type BasicSubsystem interface {
	// Start runs the subsystem
	Start(ctx context.Context) error

	// Stop signals the subsystem to shutdown
	Stop(ctx context.Context) error

	// HealthCheck reports if a subsystem is running correctly (it is restarted if not)
	HealthCheck(ctx context.Context) error
}

BasicSubsystem is the minimal interface.

type CacheData

type CacheData struct {
	CurrentVersion  string                  `json:"current_version"`
	PreviousVersion string                  `json:"previous_version"`
	Versions        map[string]*VersionInfo `json:"versions"`
}

CacheData stores VersionInfo and the current/previous versions for (TODO) rollback.

type InternalSubsystem added in v0.2.0

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

InternalSubsystem is shared start/stop/update code between "internal" (not viam-server) subsystems.

func NewInternalSubsystem added in v0.2.0

func NewInternalSubsystem(name string, extraArgs []string, logger logging.Logger, uploadAll bool) (*InternalSubsystem, error)

func (*InternalSubsystem) HealthCheck added in v0.2.0

func (is *InternalSubsystem) HealthCheck(ctx context.Context) (errRet error)

HealthCheck sends a USR1 signal to the subsystem process, which should cause it to log "HEALTHY" to stdout.

func (*InternalSubsystem) Start added in v0.2.0

func (is *InternalSubsystem) Start(ctx context.Context) error

func (*InternalSubsystem) Stop added in v0.2.0

func (is *InternalSubsystem) Stop(ctx context.Context) error

func (*InternalSubsystem) Update added in v0.2.0

func (is *InternalSubsystem) Update(ctx context.Context, cfg *pb.DeviceSubsystemConfig, newVersion bool) (bool, error)

type Manager

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

Manager is the core of the agent process, and maintains the list of subsystems, as well as cloud connection.

func NewManager

func NewManager(ctx context.Context, logger logging.Logger) (*Manager, error)

NewManager returns a new Manager.

func (*Manager) CheckUpdates

func (m *Manager) CheckUpdates(ctx context.Context) time.Duration

CheckUpdates retrieves an updated config from the cloud, and then passes it to SubsystemUpdates().

func (*Manager) CloseAll

func (m *Manager) CloseAll()

CloseAll stops all subsystems and closes the cloud connection.

func (*Manager) CreateNetAppender added in v0.4.0

func (m *Manager) CreateNetAppender() (*logging.NetAppender, error)

CreateNetAppender creates or replaces m.netAppender. Must be called after config is loaded.

func (*Manager) GetConfig

GetConfig retrieves the configuration from the cloud, or returns a cached version if unable to communicate.

func (*Manager) LoadConfig added in v0.1.0

func (m *Manager) LoadConfig(cfgPath string) error

func (*Manager) LoadSubsystems

func (m *Manager) LoadSubsystems(ctx context.Context) error

LoadSubsystems runs at startup, before getting online.

func (*Manager) SelfUpdate

func (m *Manager) SelfUpdate(ctx context.Context) (bool, error)

SelfUpdate is called early in startup to update the viam-agent subsystem before any other work is started.

func (*Manager) StartBackgroundChecks

func (m *Manager) StartBackgroundChecks(ctx context.Context)

StartBackgroundChecks kicks off a go routine that loops on a timer to check for updates and health checks.

func (*Manager) StartSubsystem added in v0.1.0

func (m *Manager) StartSubsystem(ctx context.Context, name string) error

StartSubsystem may be called early in startup when no cloud connectivity is configured.

func (*Manager) SubsystemHealthChecks

func (m *Manager) SubsystemHealthChecks(ctx context.Context)

SubsystemHealthChecks makes sure all subsystems are responding, and restarts them if not.

func (*Manager) SubsystemUpdates

func (m *Manager) SubsystemUpdates(ctx context.Context, cfg map[string]*pb.DeviceSubsystemConfig)

SubsystemUpdates checks for updates to configured subsystems and restarts them as needed.

type MatchingLogger added in v0.1.0

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

MatchingLogger provides a logger that also allows sending regex matched lines to a channel.

func NewMatchingLogger added in v0.1.0

func NewMatchingLogger(logger logging.Logger, isError, uploadAll bool) *MatchingLogger

NewMatchingLogger returns a MatchingLogger.

func (*MatchingLogger) AddMatcher added in v0.1.0

func (l *MatchingLogger) AddMatcher(name string, regex *regexp.Regexp, mask bool) (<-chan []string, error)

AddMatcher adds a named regex to filter from results and return to a channel, optionally masking it from normal logging.

func (*MatchingLogger) DeleteMatcher added in v0.1.0

func (l *MatchingLogger) DeleteMatcher(name string)

DeleteMatcher removes a previously added matcher.

func (*MatchingLogger) Write added in v0.1.0

func (l *MatchingLogger) Write(p []byte) (int, error)

Write takes input and filters it against each defined matcher, before logging it.

type VersionInfo

type VersionInfo struct {
	Version        string
	URL            string
	DlPath         string
	DlSHA          []byte
	UnpackedPath   string
	UnpackedSHA    []byte
	SymlinkPath    string
	Installed      time.Time
	StartCount     uint
	LongFailCount  uint
	ShortFailCount uint
}

VersionInfo records details about each version of a subsystem.

Directories

Path Synopsis
cmd
Package subsystems defines the subsystem interface.
Package subsystems defines the subsystem interface.
provisioning
Package provisioning is the subsystem responsible for network/wifi management, and initial device setup via hotspot.
Package provisioning is the subsystem responsible for network/wifi management, and initial device setup via hotspot.
registry
Package registry is used to register subsystems from other packages.
Package registry is used to register subsystems from other packages.
syscfg
Package syscfg contains the system configuration agent subsystem.
Package syscfg contains the system configuration agent subsystem.
viamagent
Package viamagent is the subsystem for the viam-agent itself.
Package viamagent is the subsystem for the viam-agent itself.
viamserver
Package viamserver contains the viam-server agent subsystem.
Package viamserver contains the viam-server agent subsystem.

Jump to

Keyboard shortcuts

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