machine

package
v0.26.1 Latest Latest
Warning

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

Go to latest
Published: Aug 3, 2022 License: Apache-2.0 Imports: 24 Imported by: 2

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func ValidateDir

func ValidateDir(dir string) (validationErrors []string, err error)

ValidateDir validates a machine.yaml against the v1 schema

Types

type ActivationChecker added in v0.24.0

type ActivationChecker func(*logrus.Entry) bool

ActivationChecker allows embedded machines to determine if they should activate or not

type InfoSource

type InfoSource interface {
	// Identity retrieves the identity of the node hosting this machine, "unknown" when not set
	Identity() string
	// Version returns the version of the machine
	Version() string
	// Name is the name of the machine
	Name() string
	// State returns the current state of the machine
	State() string
	// InstanceID return the unique ID of the machine instance
	InstanceID() string
}

InfoSource provides information about a running machine

type Machine

type Machine struct {
	// MachineName is the unique name for this machine
	MachineName string `json:"name" yaml:"name"`

	// MachineVersion is the semver compliant version for the running machine
	MachineVersion string `json:"version" yaml:"version"`

	// InitialState is the state this machine starts in when it first starts
	InitialState string `json:"initial_state" yaml:"initial_state"`

	// Transitions contain a list of valid events of transitions this machine can move through
	Transitions []*Transition `json:"transitions" yaml:"transitions"`

	// WatcherDefs contains all the watchers that can interact with the system
	WatcherDefs []*watchers.WatcherDef `json:"watchers" yaml:"watchers"`

	// SplayStart causes a random sleep of maximum this many seconds before the machine starts
	SplayStart int `json:"splay_start" yaml:"splay_start"`

	// ActivationCheck when set this can be called to avoid activating a plugin
	// typically this would be used when compiling machines into the binary
	ActivationCheck ActivationChecker `json:"-" yaml:"-"`

	sync.Mutex
	// contains filtered or unexported fields
}

Machine is a autonomous agent implemented as a Finite State Machine and hosted within Choria Server

func FromDir

func FromDir(dir string, manager WatcherManager) (m *Machine, err error)

func FromPlugin added in v0.24.0

func FromPlugin(p model.MachineConstructor, manager WatcherManager, log *logrus.Entry) (*Machine, error)

func FromYAML

func FromYAML(file string, manager WatcherManager) (m *Machine, err error)

FromYAML loads a machine from a YAML definition

func (*Machine) AvailableTransitions

func (m *Machine) AvailableTransitions() []string

AvailableTransitions reports the transitions thats possible in the current state

func (*Machine) Can

func (m *Machine) Can(t string) bool

Can determines if a transition could be performed

func (*Machine) ChoriaStatusFile added in v0.19.0

func (m *Machine) ChoriaStatusFile() (string, int)

ChoriaStatusFile is the path to and write frequency of the choria status file, empty when not set

func (*Machine) Data added in v0.23.0

func (m *Machine) Data() map[string]interface{}

Data retrieves a copy of the current data stored by the machine, changes will not be reflected in the machine

func (*Machine) DataDelete added in v0.23.0

func (m *Machine) DataDelete(key string) error

DataDelete deletes a value from the store

func (*Machine) DataGet added in v0.23.0

func (m *Machine) DataGet(key string) (interface{}, bool)

DataGet gets the value for a key, empty string and false when no value is stored

func (*Machine) DataPut added in v0.23.0

func (m *Machine) DataPut(key string, val interface{}) error

DataPut stores a value in a key

func (*Machine) Debugf

func (m *Machine) Debugf(watcher string, format string, args ...interface{})

Debugf implements NotificationService

func (*Machine) Delete added in v0.15.0

func (m *Machine) Delete()

Delete deletes a running machine by canceling its context and giving its manager a change to do clean up before final termination

func (*Machine) Directory

func (m *Machine) Directory() string

Directory returns the directory where the machine definition is, "" when unknown

func (*Machine) Errorf

func (m *Machine) Errorf(watcher string, format string, args ...interface{})

Errorf implements NotificationService

func (*Machine) Facts added in v0.23.0

func (m *Machine) Facts() json.RawMessage

Facts is the active facts for the node

func (*Machine) Graph

func (m *Machine) Graph() string

Graph produce a dot graph of the fsm

func (*Machine) Hash

func (m *Machine) Hash() (string, error)

Hash computes a md5 hash of the manifest

func (*Machine) Identity

func (m *Machine) Identity() string

Identity implements InfoSource

func (*Machine) Infof

func (m *Machine) Infof(watcher string, format string, args ...interface{})

Infof implements NotificationService

func (*Machine) InstanceID

func (m *Machine) InstanceID() string

InstanceID is a unique id for the instance of a machine

func (*Machine) IsEmbedded added in v0.24.0

func (m *Machine) IsEmbedded() bool

func (*Machine) IsStarted added in v0.24.0

func (m *Machine) IsStarted() bool

IsStarted determines if the machine is currently running

func (*Machine) JetStreamConnection added in v0.23.0

func (m *Machine) JetStreamConnection() (*jsm.Manager, error)

func (*Machine) KnownStates

func (m *Machine) KnownStates() []string

KnownStates is a list of all the known states in the Machine gathered by looking at initial state and all the states mentioned in transitions

func (*Machine) KnownTransitions

func (m *Machine) KnownTransitions() []string

KnownTransitions is a list of known transition names

func (*Machine) MainCollective added in v0.23.0

func (m *Machine) MainCollective() string

MainCollective is the main collective this choria belongs to

func (*Machine) Name

func (m *Machine) Name() string

Name implements InfoSource

func (*Machine) NotifyWatcherState

func (m *Machine) NotifyWatcherState(watcher string, state interface{})

NotifyWatcherState implements NotificationService

func (*Machine) OverrideData added in v0.15.0

func (m *Machine) OverrideData() ([]byte, error)

func (*Machine) PublishLifecycleEvent added in v0.23.0

func (m *Machine) PublishLifecycleEvent(t lifecycle.Type, opts ...lifecycle.Option)

func (*Machine) RegisterNotifier

func (m *Machine) RegisterNotifier(services ...NotificationService)

RegisterNotifier adds a new NotificationService to the list of ones to receive notifications

func (*Machine) SetChoriaStatusFile added in v0.19.0

func (m *Machine) SetChoriaStatusFile(f string, freq int)

SetChoriaStatusFile sets the path and write frequency of the choria status file

func (*Machine) SetConnection added in v0.23.0

func (m *Machine) SetConnection(conn inter.Connector) error

func (*Machine) SetDirectory added in v0.24.0

func (m *Machine) SetDirectory(dir string, manifest string) error

func (*Machine) SetFactSource added in v0.23.0

func (m *Machine) SetFactSource(facts func() json.RawMessage)

SetFactSource sets a function that return current machine facts

func (*Machine) SetIdentity

func (m *Machine) SetIdentity(id string)

SetIdentity sets the identity of the node hosting this machine

func (*Machine) SetMainCollective added in v0.23.0

func (m *Machine) SetMainCollective(collective string)

SetMainCollective sets the collective name this machine lives in

func (*Machine) SetOverridesFile added in v0.15.0

func (m *Machine) SetOverridesFile(f string)

func (*Machine) SetTextFileDirectory added in v0.15.0

func (m *Machine) SetTextFileDirectory(d string)

func (*Machine) Setup

func (m *Machine) Setup() error

Setup validates and prepares the machine for execution

func (*Machine) Start

func (m *Machine) Start(ctx context.Context, wg *sync.WaitGroup) (started chan struct{})

Start runs the machine in the background

func (*Machine) StartTime

func (m *Machine) StartTime() time.Time

StartTime is the time the machine started in UTC

func (*Machine) State

func (m *Machine) State() string

State implements InfoSource

func (*Machine) Stop

func (m *Machine) Stop()

Stop stops a running machine by canceling its context

func (*Machine) TextFileDirectory added in v0.15.0

func (m *Machine) TextFileDirectory() string

func (*Machine) TimeStamp

func (m *Machine) TimeStamp() time.Time

TimeStamp returns a UTC time

func (*Machine) TimeStampSeconds

func (m *Machine) TimeStampSeconds() int64

TimeStampSeconds returns the current time in unix seconds

func (*Machine) Transition

func (m *Machine) Transition(t string, args ...interface{}) error

Transition performs the machine transition as defined by event t

func (*Machine) UniqueID

func (m *Machine) UniqueID() (id string)

UniqueID creates a new unique ID, usually a v4 uuid, if that fails a random string based ID is made

func (*Machine) Validate

func (m *Machine) Validate() error

Validate performs basic validation on the machine settings

func (*Machine) Version

func (m *Machine) Version() string

Version implements InfoSource

func (*Machine) Warnf

func (m *Machine) Warnf(watcher string, format string, args ...interface{})

Warnf implements NotificationService

func (*Machine) WatcherState added in v0.17.0

func (m *Machine) WatcherState(watcher string) (interface{}, bool)

WatcherState is the status of a given watcher, boolean result is false for unknown watchers

func (*Machine) Watchers

func (m *Machine) Watchers() []*watchers.WatcherDef

Watchers retrieves the watcher definitions

type NotificationService

type NotificationService interface {
	// NotifyPostTransition receives an event after a transition completed
	NotifyPostTransition(t *TransitionNotification) error

	// NotifyWatcherState receives the current state of a watcher either after running or periodically
	NotifyWatcherState(watcher string, state WatcherStateNotification) error

	// Debugf logs a message at debug level
	Debugf(machine InfoSource, watcher string, format string, args ...interface{})

	// Infof logs a message at info level
	Infof(machine InfoSource, watcher string, format string, args ...interface{})

	// Warnf logs a message at warning level
	Warnf(machine InfoSource, watcher string, format string, args ...interface{})

	// Errorf logs a message at error level
	Errorf(machine InfoSource, watcher string, format string, args ...interface{})
}

NotificationService receives events notifications about the state machine

type Transition

type Transition struct {
	// Name is the name for the transition shown in logs and graphs
	Name string `json:"name" yaml:"name"`

	// From is a list of valid state names from where this transition event is valid
	From []string `json:"from" yaml:"from"`

	// Destination is the name of the target state this event will move the machine into
	Destination string `json:"destination" yaml:"destination"`

	// Description is a human friendly description of the purpose of this transition
	Description string `json:"description" yaml:"description"`
}

Transition describes a transition event within the Finite State Machine

type TransitionNotification

type TransitionNotification struct {
	Protocol   string `json:"protocol"`
	Identity   string `json:"identity"`
	ID         string `json:"id"`
	Version    string `json:"version"`
	Timestamp  int64  `json:"timestamp"`
	Machine    string `json:"machine"`
	Transition string `json:"transition"`
	FromState  string `json:"from_state"`
	ToState    string `json:"to_state"`

	Info InfoSource `json:"-"`
}

TransitionNotification is a notification when a transition completes

func (*TransitionNotification) CloudEvent added in v0.13.0

func (t *TransitionNotification) CloudEvent() cloudevents.Event

CloudEvent creates a cloud event from the transition

func (*TransitionNotification) String

func (t *TransitionNotification) String() string

String returns a string representation of the event

type WatcherManager

type WatcherManager interface {
	Run(context.Context, *sync.WaitGroup) error
	NotifyStateChance()
	SetMachine(interface{}) error
	WatcherState(watcher string) (interface{}, bool)
	Delete()
}

WatcherManager manages watchers

type WatcherStateNotification

type WatcherStateNotification interface {
	JSON() ([]byte, error)
	CloudEvent() cloudevents.Event
	String() string
	WatcherType() string
	SenderID() string
}

WatcherStateNotification is a notification about the state of a watcher

Jump to

Keyboard shortcuts

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