resource

package
v0.0.0-alpha9 Latest Latest
Warning

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

Go to latest
Published: Mar 21, 2023 License: GPL-3.0 Imports: 17 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var IndividuallyManageableResourceTypeMap = map[Type]IndividuallyManageableResource{}

IndividuallyManageableResourceTypeMap maps Type to IndividuallyManageableResource.

View Source
var ManageableResourcesStateMap = map[Type]State{}

ManageableResourcesStateMap maps Type to its State.

View Source
var MergeableManageableResourcesTypeMap = map[Type]MergeableManageableResources{}

MergeableManageableResourcesTypeMap maps Type to MergeableManageableResources.

Functions

func Diff

func Diff(a, b interface{}) []diffmatchpatch.Diff

func DiffsHasChanges

func DiffsHasChanges(diffs []diffmatchpatch.Diff) bool

DiffsHasChanges return true when the diff contains no changes.

Types

type Action

type Action int

Action to be executed for a resource.

const (
	ActionNone Action = iota
	// ActionOk means state is as expected
	ActionOk
	// ActionSkip denotes no action is required as the resource was merged.
	ActionSkip
	// ActionRefresh means that any in-memory state is to be refreshed (eg: restart a service, reload configuration from files etc).
	ActionRefresh
	// ActionConfigure means that the state of the resource is not as expected and it that it must be configured.
	ActionConfigure
	// ActionDestroy means that the resource is no longer needed and is to be destroyed.
	ActionDestroy
	// ActionCount has the number of existing actions
	ActionCount
)

func (Action) Actionable

func (a Action) Actionable() bool

func (Action) Emoji

func (a Action) Emoji() string

Emoji representing the action

func (Action) String

func (a Action) String() string

type Bundle

type Bundle []Resources

Bundle holds all resources for a host.

func LoadBundle

func LoadBundle(ctx context.Context, hst host.Host, root string) (Bundle, error)

LoadBundle search for .yaml files at root, each having the Resources schema, loads and returns all of them. Bundle is sorted by alphabetical order.

func NewRollbackBundle

func NewRollbackBundle(
	newBundle Bundle,
	previousBundle *Bundle,
	typeNameStateMap TypeNameStateMap,
	intendedAction Action,
) Bundle

func (Bundle) HasTypeName

func (b Bundle) HasTypeName(typeName TypeName) bool

HasTypeName returns true if Resource is contained at Bundle.

func (Bundle) IsClean

func (b Bundle) IsClean(
	ctx context.Context,
	hst host.Host,
	typeNameStateMap TypeNameStateMap,
) (bool, error)

IsClean whether all resources at Bundle are clean.

func (Bundle) Resources

func (b Bundle) Resources() Resources

Resources returns all Resource at the bundle

func (Bundle) TypeNames

func (b Bundle) TypeNames() []TypeName

TypeNames returns all TypeName at the bundle

type HostState

type HostState struct {
	// Version of the binary used to put the host in this state.
	Version        version.Version `yaml:"version"`
	PreviousBundle Bundle          `yaml:"previous_bundle"`
}

HostState holds the state for a host

func NewHostState

func NewHostState(previousBundle Bundle) HostState

func (HostState) Refresh

func (hs HostState) Refresh(
	ctx context.Context, typeNameStateMap TypeNameStateMap,
) (HostState, error)

Refresh gets current host state and returns it as it is.

func (HostState) String

func (hs HostState) String() string

type IndividuallyManageableResource

type IndividuallyManageableResource interface {
	ManageableResource

	// GetState gets the state of the resource, or nil if not present.
	GetState(ctx context.Context, hst host.Host, name Name) (State, error)

	// Configure configures the resource to given state.
	// Must be idempotent.
	Configure(ctx context.Context, hst host.Host, name Name, state State) error

	// Destroy a configured resource at given host.
	// Must be idempotent.
	Destroy(ctx context.Context, hst host.Host, name Name) error
}

IndividuallyManageableResource is an interface for managing a single resource name. This is the most common use case, where resources can be individually managed without one resource having dependency on others and changing one resource does not affect the state of another.

type ManageableResource

type ManageableResource interface {
	// ValidateName validates the name of the resource
	ValidateName(name Name) error
}

ManageableResource defines a common interface for managing resource state.

type MergeableManageableResources

type MergeableManageableResources interface {
	ManageableResource

	// GetStates gets the state of all resources, or nil if not present.
	GetStates(ctx context.Context, hst host.Host, names []Name) (map[Name]State, error)

	// ConfigureAll configures all resource to given state.
	// Must be idempotent.
	ConfigureAll(
		ctx context.Context, hst host.Host, actionNameStateMap map[Action]map[Name]State,
	) error
}

MergeableManageableResources is an interface for managing multiple resources together. The use cases for this are resources where there's inter-dependency between resources, and they must be managed "all or nothing". The typical use case is Linux distribution package management, where one package may conflict with another, and the transaction of the final state must be computed altogether.

type Name

type Name string

Name is a name that globally uniquely identifies a resource instance of a given type. Eg: for File type a Name would be the file absolute path such as /etc/issue.

type Plan

type Plan struct {
	Steps            []*Step
	TypeNameStateMap TypeNameStateMap
}

Plan is a directed graph which contains the plan for applying resources to a host.

func NewPlan

func NewPlan(
	ctx context.Context,
	hst host.Host,
	newBundle Bundle,
	previousBundle *Bundle,
	typeNameStateMap TypeNameStateMap,
	intendedAction Action,
) (Plan, error)

func (Plan) Actionable

func (p Plan) Actionable() bool

func (Plan) Execute

func (p Plan) Execute(ctx context.Context, hst host.Host) error

Execute every Step from the Plan.

func (Plan) Graphviz

func (p Plan) Graphviz() string

Graphviz returns a DOT directed graph containing the apply plan.

func (Plan) Print

func (p Plan) Print(ctx context.Context, hst host.Host) error

Print the whole plan

type RefreshableManageableResource

type RefreshableManageableResource interface {
	ManageableResource

	// Refresh the resource. This is typically used to update the in-memory state of a resource
	// (eg: kerner: sysctl, iptables; process: systemd service) after persistent changes are made
	// (eg: change configuration file)
	Refresh(ctx context.Context, hst host.Host, name Name) error
}

RefreshableManageableResource defines an interface for resources that can be refreshed. Refresh means updating in-memory state as a function of file changes (eg: restarting a service, loading iptables rules to the kernel etc.)

type Resource

type Resource struct {
	TypeName TypeName `yaml:"resource"`
	State    State    `yaml:"state"`
	Destroy  bool     `yaml:"destroy"`
}

Resource holds a single resource.

func NewResource

func NewResource(typeName TypeName, state State, destroy bool) Resource

func (Resource) IsMergeableManageableResources

func (r Resource) IsMergeableManageableResources() bool

IsMergeableManageableResources returns true only if ManageableResource is of type MergeableManageableResources.

func (Resource) ManageableResource

func (r Resource) ManageableResource() ManageableResource

func (Resource) MustIndividuallyManageableResource

func (r Resource) MustIndividuallyManageableResource() IndividuallyManageableResource

MustIndividuallyManageableResource returns IndividuallyManageableResource from ManageableResource or panics if it isn't of the required type.

func (Resource) MustMergeableManageableResources

func (r Resource) MustMergeableManageableResources() MergeableManageableResources

MustMergeableManageableResources returns MergeableManageableResources from ManageableResource or panics if it isn't of the required type.

func (Resource) MustName

func (r Resource) MustName() Name

func (Resource) MustType

func (r Resource) MustType() Type

func (Resource) Refreshable

func (r Resource) Refreshable() bool

Refreshable returns whether the resource is refreshable or not.

func (Resource) String

func (r Resource) String() string

func (*Resource) UnmarshalYAML

func (r *Resource) UnmarshalYAML(node *yaml.Node) error

type Resources

type Resources []Resource

Resources is the schema used to declare multiple resources at a single file.

func LoadResources

func LoadResources(ctx context.Context, hst host.Host, path string) (Resources, error)

LoadBundle loads resources from given Yaml file path.

func (Resources) Len

func (rs Resources) Len() int

func (Resources) Less

func (rs Resources) Less(i, j int) bool

func (Resources) Swap

func (rs Resources) Swap(i, j int)

func (Resources) TypeNames

func (rs Resources) TypeNames() []TypeName

func (Resources) Validate

func (rs Resources) Validate() error

type State

type State interface {
	// ValidateAndUpdate validates and updates the state with any required information from the host.
	// Eg: transform username into UID.
	ValidateAndUpdate(ctx context.Context, hst host.Host) (State, error)
}

State is a Type specific interface for defining resource state as configured by users.

type Step

type Step struct {
	StepAction StepAction
	// contains filtered or unexported fields
}

Step that's used at a Plan

func (Step) ActionResourcesMap

func (s Step) ActionResourcesMap() map[Action]Resources

func (Step) Actionable

func (s Step) Actionable() bool

func (Step) Execute

func (s Step) Execute(ctx context.Context, hst host.Host) error

Execute StepAction

func (Step) HasTypeName

func (s Step) HasTypeName(typeName TypeName) bool

func (Step) String

func (s Step) String() string

type StepAction

type StepAction interface {
	// Execute actions for all bundled resources.
	Execute(ctx context.Context, hst host.Host) error
	String() string
	// Actionable returns whether any action is different from ActionOk or ActionSkip
	Actionable() bool
	// ActionResources returns a map from Action to a slice of Resource.
	ActionResourcesMap() map[Action]Resources
}

StepAction defines an interface for an action that can be executed from a Step.

type StepActionIndividual

type StepActionIndividual struct {
	Resource Resource
	Action   Action
}

StepActionIndividual is a StepAction which can execute a single Resource.

func (StepActionIndividual) ActionResourcesMap

func (sai StepActionIndividual) ActionResourcesMap() map[Action]Resources

func (StepActionIndividual) Actionable

func (sai StepActionIndividual) Actionable() bool

func (StepActionIndividual) Execute

func (sai StepActionIndividual) Execute(ctx context.Context, hst host.Host) error

Execute Action for the Resource.

func (StepActionIndividual) String

func (sai StepActionIndividual) String() string

type StepActionMerged

type StepActionMerged map[Action]Resources

StepActionMerged is a StepAction which contains multiple merged Resource.

func (StepActionMerged) ActionResourcesMap

func (sam StepActionMerged) ActionResourcesMap() map[Action]Resources

func (StepActionMerged) Actionable

func (sam StepActionMerged) Actionable() bool

func (StepActionMerged) Execute

func (sam StepActionMerged) Execute(ctx context.Context, hst host.Host) error

Execute the required Action for each Resource.

func (StepActionMerged) MustMergeableManageableResources

func (sam StepActionMerged) MustMergeableManageableResources() MergeableManageableResources

MustMergeableManageableResources returns MergeableManageableResources common to all Resource or panics.

func (StepActionMerged) String

func (sam StepActionMerged) String() string

func (StepActionMerged) StringNoAction

func (sam StepActionMerged) StringNoAction() string

type Type

type Type string

Type is the name of the resource type.

func NewTypeFromStr

func NewTypeFromStr(tpeStr string) (Type, error)

func (Type) ManageableResource

func (t Type) ManageableResource() ManageableResource

ManageableResource returns an instance for the resource type.

func (Type) MustMergeableManageableResources

func (t Type) MustMergeableManageableResources() MergeableManageableResources

MustMergeableManageableResources returns MergeableManageableResources from ManageableResource or panics if it isn't of the required type.

type TypeName

type TypeName string

TypeName is a string that identifies a resource type and name. Eg: File[/etc/issue].

func NewTypeName

func NewTypeName(tpe Type, name Name) (TypeName, error)

func NewTypeNameFromStr

func NewTypeNameFromStr(typeNameStr string) (TypeName, error)

func (TypeName) IsIndividuallyManageableResource

func (tn TypeName) IsIndividuallyManageableResource() bool

IsIndividuallyManageableResource returns true if ManageableResource() is of type IndividuallyManageableResource.

func (TypeName) IsMergeableManageableResources

func (tn TypeName) IsMergeableManageableResources() bool

IsMergeableManageableResources returns true only if ManageableResource() is of type MergeableManageableResources.

func (TypeName) ManageableResource

func (tn TypeName) ManageableResource() ManageableResource

ManageableResource returns an instance for the resource type.

func (TypeName) MustIndividuallyManageableResource

func (tn TypeName) MustIndividuallyManageableResource() IndividuallyManageableResource

MustIndividuallyManageableResource returns IndividuallyManageableResource from ManageableResource or panics if it isn't of the required type.

func (TypeName) Name

func (tn TypeName) Name() Name

func (TypeName) Type

func (tn TypeName) Type() Type

func (*TypeName) UnmarshalYAML

func (tn *TypeName) UnmarshalYAML(node *yaml.Node) error

type TypeNameStateMap

type TypeNameStateMap map[TypeName]State

func GetTypeNameStateMap

func GetTypeNameStateMap(
	ctx context.Context, hst host.Host, typeNames []TypeName,
) (TypeNameStateMap, error)

GetTypeNameStateMap gets current state for all given TypeName.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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