machine

package
v0.13.1 Latest Latest
Warning

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

Go to latest
Published: Jan 12, 2020 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 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"`

	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 FromYAML

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

FromYAML loads a macine 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) Debugf

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

Debugf implements NotificationService

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) 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) 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) 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) RegisterNotifier

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

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

func (*Machine) SetIdentity

func (m *Machine) SetIdentity(id string)

SetIdentity sets the identity of the node hosting this machine

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) 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) 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"`
}

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
}

WatcherManager manages watchers

type WatcherStateNotification

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

WatcherStateNotification is a notification about the state of a watcher

func ParseWatcherState

func ParseWatcherState(state []byte) (n WatcherStateNotification, err error)

ParseWatcherState parses the watcher state JSON

Jump to

Keyboard shortcuts

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