action

package
v0.7.0 Latest Latest
Warning

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

Go to latest
Published: Apr 5, 2024 License: Apache-2.0 Imports: 39 Imported by: 3

Documentation

Overview

Package action provides implementations of discovering and running actions.

Index

Constants

View Source
const ConfigImagesKey = "images"

ConfigImagesKey is a field name in launchr config file.

Variables

This section is empty.

Functions

func CobraImpl added in v0.0.9

func CobraImpl(a *Action, streams cli.Streams) (*cobra.Command, error)

CobraImpl returns cobra command implementation for an action command.

func ConvertInputToTplVars added in v0.1.2

func ConvertInputToTplVars(input Input, ac *DefAction) map[string]interface{}

ConvertInputToTplVars creates a map with input variables suitable for template engine.

func ForwardAllSignals

func ForwardAllSignals(ctx context.Context, cli driver.ContainerRunner, cid string, sigc <-chan os.Signal)

ForwardAllSignals forwards signals to the container

The channel you pass in must already be setup to receive any signals you want to forward.

func WithDefaultRunEnvironment added in v0.1.2

func WithDefaultRunEnvironment(m Manager, a *Action)

WithDefaultRunEnvironment adds a default RunEnvironment for an action.

Types

type Action

type Action struct {
	ID     string // ID is an action unique id compiled from path.
	Loader Loader // Loader is a function to load action definition. Helpful to reload with replaced variables.
	// contains filtered or unexported fields
}

Action is an action definition with a contextual id (name), working directory path and a runtime context such as input arguments and options.

func NewAction added in v0.1.2

func NewAction(id, wd, fsdir, fpath string) *Action

NewAction creates a new action.

func (*Action) ActionDef added in v0.1.2

func (a *Action) ActionDef() *DefAction

ActionDef returns action definition with replaced variables.

func (*Action) Clone added in v0.1.2

func (a *Action) Clone() *Action

Clone returns a copy of an action.

func (*Action) DefinitionEncoded added in v0.1.2

func (a *Action) DefinitionEncoded() ([]byte, error)

DefinitionEncoded returns encoded action file content.

func (*Action) Dir added in v0.1.2

func (a *Action) Dir() string

Dir returns an action file directory.

func (*Action) EnsureLoaded added in v0.1.2

func (a *Action) EnsureLoaded() (err error)

EnsureLoaded loads an action file with replaced arguments and options.

func (*Action) Execute added in v0.1.2

func (a *Action) Execute(ctx context.Context) error

Execute runs action in the specified environment.

func (*Action) Filepath added in v0.1.2

func (a *Action) Filepath() string

Filepath returns action file path.

func (*Action) GetInput added in v0.1.2

func (a *Action) GetInput() Input

GetInput returns action input.

func (*Action) GetProcessors added in v0.7.0

func (a *Action) GetProcessors() map[string]ValueProcessor

GetProcessors returns processors map.

func (*Action) ImageBuildInfo added in v0.1.2

func (a *Action) ImageBuildInfo(image string) *types.BuildDefinition

ImageBuildInfo implements ImageBuildResolver.

func (*Action) JSONSchema added in v0.1.2

func (a *Action) JSONSchema() jsonschema.Schema

JSONSchema returns json schema of an action.

func (*Action) Reset added in v0.1.2

func (a *Action) Reset()

Reset unsets loaded action to force reload.

func (*Action) SetInput added in v0.1.2

func (a *Action) SetInput(input Input) (err error)

SetInput saves arguments and options for later processing in run, templates, etc.

func (*Action) SetProcessors added in v0.7.0

func (a *Action) SetProcessors(list map[string]ValueProcessor)

SetProcessors sets the value processors for an Action.

func (*Action) SetRunEnvironment added in v0.1.2

func (a *Action) SetRunEnvironment(env RunEnvironment)

SetRunEnvironment sets environment to run the action.

func (*Action) ValidateInput added in v0.1.2

func (a *Action) ValidateInput(inp Input) error

ValidateInput validates arguments and options according to a specified json schema in action definition. @todo move to jsonschema

func (*Action) WorkDir added in v0.5.5

func (a *Action) WorkDir() string

WorkDir returns action working directory.

type Argument

type Argument struct {
	Name        string            `yaml:"name"`
	Title       string            `yaml:"title"`
	Description string            `yaml:"description"`
	Type        jsonschema.Type   `yaml:"type"`
	Process     []ValueProcessDef `yaml:"process"`
	RawMap      map[string]interface{}
}

Argument stores command arguments declaration.

func (*Argument) JSONSchema added in v0.1.2

func (a *Argument) JSONSchema() map[string]interface{}

JSONSchema returns argument json schema definition.

func (*Argument) UnmarshalYAML added in v0.1.2

func (a *Argument) UnmarshalYAML(node *yaml.Node) (err error)

UnmarshalYAML implements yaml.Unmarshaler to parse Argument.

type ArgumentsList

type ArgumentsList []*Argument

ArgumentsList is used for custom yaml parsing of arguments list.

func (*ArgumentsList) JSONSchema added in v0.1.2

func (l *ArgumentsList) JSONSchema() (map[string]interface{}, []string)

JSONSchema collects all arguments json schema definition and also returns fields that are required.

func (*ArgumentsList) UnmarshalYAML

func (l *ArgumentsList) UnmarshalYAML(nodeList *yaml.Node) (err error)

UnmarshalYAML implements yaml.Unmarshaler to parse for ArgumentsList.

type ChainImageBuildResolver added in v0.1.2

type ChainImageBuildResolver []ImageBuildResolver

ChainImageBuildResolver is a image build resolver that takes first available image in the chain.

func (ChainImageBuildResolver) ImageBuildInfo added in v0.1.2

func (r ChainImageBuildResolver) ImageBuildInfo(image string) *types.BuildDefinition

ImageBuildInfo implements ImageBuildResolver.

type ConfigImages added in v0.1.0

type ConfigImages map[string]*types.BuildDefinition

ConfigImages is a container to parse launchr config in yaml format.

type ContainerNameProvider added in v0.3.0

type ContainerNameProvider struct {
	Prefix       string
	RandomSuffix bool
}

ContainerNameProvider provides an ability to generate a random container name

func (ContainerNameProvider) Get added in v0.3.0

func (p ContainerNameProvider) Get(name string) string

Get generates a new container name

type ContainerRunEnvironment added in v0.1.2

type ContainerRunEnvironment interface {
	RunEnvironment
	// SetContainerNameProvider sets container name provider.
	SetContainerNameProvider(ContainerNameProvider)
	// AddImageBuildResolver adds an image build resolver to a chain.
	AddImageBuildResolver(ImageBuildResolver)
	// SetImageBuildCacheResolver sets an image build cache resolver
	// to check when image must be rebuilt.
	SetImageBuildCacheResolver(*ImageBuildCacheResolver)
}

ContainerRunEnvironment is an interface for container run environments.

type DecorateWithFn added in v0.1.2

type DecorateWithFn = func(m Manager, a *Action)

DecorateWithFn is a type alias for functions accepted in a Manager.Decorate interface method.

func WithContainerRunEnvironmentConfig added in v0.1.2

func WithContainerRunEnvironmentConfig(cfg launchr.Config, prefix string) DecorateWithFn

WithContainerRunEnvironmentConfig configures a ContainerRunEnvironment.

func WithValueProcessors added in v0.7.0

func WithValueProcessors() DecorateWithFn

WithValueProcessors sets processors for action from manager.

type DefAction added in v0.1.2

type DefAction struct {
	Title       string                 `yaml:"title"`
	Description string                 `yaml:"description"`
	Arguments   ArgumentsList          `yaml:"arguments"`
	Options     OptionsList            `yaml:"options"`
	Command     StrSliceOrStr          `yaml:"command"`
	Image       string                 `yaml:"image"`
	Build       *types.BuildDefinition `yaml:"build"`
	ExtraHosts  StrSlice               `yaml:"extra_hosts"`
	Env         EnvSlice               `yaml:"env"`
	User        string                 `yaml:"user"`
}

DefAction holds action configuration

func (*DefAction) JSONSchema added in v0.1.2

func (a *DefAction) JSONSchema() jsonschema.Schema

JSONSchema returns jsonschema for the arguments and options of the action.

func (*DefAction) UnmarshalYAML added in v0.1.2

func (a *DefAction) UnmarshalYAML(n *yaml.Node) (err error)

UnmarshalYAML implements yaml.Unmarshaler to parse action definition.

type Definition added in v0.1.2

type Definition struct {
	Version string     `yaml:"version"`
	WD      string     `yaml:"working_directory"`
	Action  *DefAction `yaml:"action"`
}

Definition is a representation of an action file

func CreateFromYaml

func CreateFromYaml(r io.Reader) (*Definition, error)

CreateFromYaml creates an action file definition from yaml configuration. It returns pointer to Definition or nil on error.

func CreateFromYamlTpl

func CreateFromYamlTpl(b []byte) (*Definition, error)

CreateFromYamlTpl creates an action file definition from yaml configuration as CreateFromYaml but considers that it has unescaped template values.

func (*Definition) Content added in v0.1.2

func (d *Definition) Content() ([]byte, error)

Content implements Loader interface.

func (*Definition) Load added in v0.1.2

func (d *Definition) Load(_ LoadContext) (*Definition, error)

Load implements Loader interface.

func (*Definition) LoadRaw added in v0.1.2

func (d *Definition) LoadRaw() (*Definition, error)

LoadRaw implements Loader interface.

func (*Definition) UnmarshalYAML added in v0.1.2

func (d *Definition) UnmarshalYAML(node *yaml.Node) (err error)

UnmarshalYAML implements yaml.Unmarshaler to parse action definition.

type Discovery

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

Discovery defines a common functionality for discovering action files.

func NewDiscovery added in v0.5.4

func NewDiscovery(fs DiscoveryFS, ds DiscoveryStrategy) *Discovery

NewDiscovery creates an instance of action discovery.

func NewYamlDiscovery

func NewYamlDiscovery(fs DiscoveryFS) *Discovery

NewYamlDiscovery is an implementation of discovery for searching yaml files.

func (*Discovery) Discover

func (ad *Discovery) Discover() ([]*Action, error)

Discover traverses the file structure for a given discovery path. Returns array of Action. If an action is invalid, it's ignored.

type DiscoveryFS added in v0.5.5

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

DiscoveryFS is a file system to discover actions.

func NewDiscoveryFS added in v0.5.5

func NewDiscoveryFS(fs fs.FS, wd string) DiscoveryFS

NewDiscoveryFS creates a DiscoveryFS given fs - a filesystem to discover and wd - working directory for an action, leave empty for current path.

func (DiscoveryFS) FS added in v0.5.5

func (f DiscoveryFS) FS() fs.FS

FS implements launchr.ManagedFS.

func (DiscoveryFS) Open added in v0.5.5

func (f DiscoveryFS) Open(name string) (fs.File, error)

Open implements fs.FS and decorates the managed fs.

type DiscoveryPlugin added in v0.5.4

type DiscoveryPlugin interface {
	launchr.Plugin
	DiscoverActions(fs launchr.ManagedFS) ([]*Action, error)
}

DiscoveryPlugin is a launchr plugin to discover actions.

type DiscoveryStrategy added in v0.5.4

type DiscoveryStrategy interface {
	IsValid(name string) bool
	Loader(l FileLoadFn, p ...LoadProcessor) Loader
}

DiscoveryStrategy is a way files will be discovered and loaded.

type EnvSlice

type EnvSlice []string

EnvSlice is an array of env vars or key-value.

func (*EnvSlice) UnmarshalYAML

func (l *EnvSlice) UnmarshalYAML(n *yaml.Node) (err error)

UnmarshalYAML implements yaml.Unmarshaler to parse env []string or map[string]string.

type FileLoadFn added in v0.5.4

type FileLoadFn func() (fs.File, error)

FileLoadFn is a type for loading a file.

type FuncProcessor added in v0.7.0

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

FuncProcessor represents a processor that applies a callback function to values based on certain applicable formats.

func NewFuncProcessor added in v0.7.0

func NewFuncProcessor(formats []jsonschema.Type, callback ValueProcessorFn) FuncProcessor

NewFuncProcessor creates a new instance of FuncProcessor with the specified formats and callback.

func (FuncProcessor) Execute added in v0.7.0

func (p FuncProcessor) Execute(value interface{}, options map[string]interface{}) (interface{}, error)

Execute applies the callback function of the FuncProcessor to the given value with options.

func (FuncProcessor) IsApplicable added in v0.7.0

func (p FuncProcessor) IsApplicable(valueType jsonschema.Type) bool

IsApplicable checks if the given valueType is present in the applicableFormats slice of the FuncProcessor.

type ImageBuildCacheResolver added in v0.6.0

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

ImageBuildCacheResolver is responsible for checking image build hash sums to rebuild images.

func NewImageBuildCacheResolver added in v0.6.0

func NewImageBuildCacheResolver(cfg launchr.Config) *ImageBuildCacheResolver

NewImageBuildCacheResolver creates ImageBuildCacheResolver from global configuration.

func (*ImageBuildCacheResolver) Destroy added in v0.6.0

func (r *ImageBuildCacheResolver) Destroy() error

Destroy removes the sum file from the persistent storage.

func (*ImageBuildCacheResolver) DirHash added in v0.6.0

func (r *ImageBuildCacheResolver) DirHash(path string) (string, error)

DirHash calculates the hash of a directory specified by the path parameter.

func (*ImageBuildCacheResolver) EnsureLoaded added in v0.6.0

func (r *ImageBuildCacheResolver) EnsureLoaded() (err error)

EnsureLoaded makes sure the sum file is loaded.

func (*ImageBuildCacheResolver) GetSum added in v0.6.0

func (r *ImageBuildCacheResolver) GetSum(tag string) string

GetSum returns a sum for an image tag.

func (*ImageBuildCacheResolver) Save added in v0.6.0

func (r *ImageBuildCacheResolver) Save() error

Save saves the sum file to the persistent storage.

func (*ImageBuildCacheResolver) SetSum added in v0.6.0

func (r *ImageBuildCacheResolver) SetSum(tag string, sum string)

SetSum adds sum for a tag. Provide empty sum to remove it.

type ImageBuildResolver added in v0.1.2

type ImageBuildResolver interface {
	// ImageBuildInfo takes image as name and provides build definition for that.
	ImageBuildInfo(image string) *types.BuildDefinition
}

ImageBuildResolver is an interface to resolve image build info from its source.

type Input added in v0.1.2

type Input struct {
	Args TypeArgs
	Opts TypeOpts
	IO   cli.Streams // @todo should it be in Input?
}

Input is a container for action input arguments and options.

type LaunchrConfigImageBuildResolver added in v0.1.2

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

LaunchrConfigImageBuildResolver is a resolver of image build in launchr config file.

func (LaunchrConfigImageBuildResolver) ImageBuildInfo added in v0.1.2

ImageBuildInfo implements ImageBuildResolver.

type LoadContext added in v0.2.1

type LoadContext struct {
	Action *Action
}

LoadContext stores relevant and isolated data needed for processors.

type LoadProcessor

type LoadProcessor interface {
	// Process gets an input action file data and returns a processed result.
	Process(LoadContext, []byte) ([]byte, error)
}

LoadProcessor is an interface for processing input on load.

func NewPipeProcessor

func NewPipeProcessor(p ...LoadProcessor) LoadProcessor

NewPipeProcessor creates a new processor containing several processors that handles input consequentially.

type Loader

type Loader interface {
	// Content returns the raw file content.
	Content() ([]byte, error)
	// Load parses Content to a Definition with substituted values.
	Load(LoadContext) (*Definition, error)
	// LoadRaw parses Content to a Definition raw values. Template strings are escaped.
	LoadRaw() (*Definition, error)
}

Loader is an interface for loading an action file.

type Manager added in v0.0.9

type Manager interface {
	launchr.Service
	// All returns all actions copied and decorated.
	All() map[string]*Action
	// AllRef returns all original action values from the storage.
	// Use it only if you need to read-only actions without allocations. It may be unsafe to read/write the map.
	// If you need to run actions, use Get or All, it will provide configured for run Action.
	AllRef() map[string]*Action
	// Get returns a copy of an action from the manager with default decorators.
	Get(id string) (*Action, bool)
	// GetRef returns an original action value from the storage.
	GetRef(id string) (*Action, bool)
	// AddValueProcessor adds processor to list of available processors
	AddValueProcessor(name string, vp ValueProcessor)
	// GetValueProcessors returns list of available processors
	GetValueProcessors() map[string]ValueProcessor
	// Decorate decorates an action with given behaviors and returns its copy.
	// If functions withFn are not provided, default functions are applied.
	Decorate(a *Action, withFn ...DecorateWithFn) *Action
	// Add saves an action in the manager.
	Add(*Action)
	// DefaultRunEnvironment provides the default action run environment.
	DefaultRunEnvironment() RunEnvironment

	// Run executes an action in foreground.
	Run(ctx context.Context, a *Action) (RunInfo, error)
	// RunBackground executes an action in background.
	RunBackground(ctx context.Context, a *Action) (RunInfo, chan error)
	// RunInfoByAction returns all running actions by action id.
	RunInfoByAction(aid string) []RunInfo
	// RunInfoByID returns an action matching run id.
	RunInfoByID(id string) (RunInfo, bool)
}

Manager handles actions and its execution.

func NewManager added in v0.0.9

func NewManager(withFns ...DecorateWithFn) Manager

NewManager constructs a new action manager.

type Option

type Option struct {
	Name        string            `yaml:"name"`
	Shorthand   string            `yaml:"shorthand"` // @todo test definition, validate, catch panic if overlay, add to readme.
	Title       string            `yaml:"title"`
	Description string            `yaml:"description"`
	Type        jsonschema.Type   `yaml:"type"`
	Default     interface{}       `yaml:"default"`
	Required    bool              `yaml:"required"` // @todo that conflicts with json schema object definition
	Process     []ValueProcessDef `yaml:"process"`
	RawMap      map[string]interface{}
}

Option stores command options declaration.

func (*Option) JSONSchema added in v0.1.2

func (o *Option) JSONSchema() map[string]interface{}

JSONSchema returns json schema definition of an option.

func (*Option) UnmarshalYAML added in v0.1.2

func (o *Option) UnmarshalYAML(node *yaml.Node) (err error)

UnmarshalYAML implements yaml.Unmarshaler to parse Option.

type OptionsList

type OptionsList []*Option

OptionsList is used for custom yaml parsing of options list.

func (*OptionsList) JSONSchema added in v0.1.2

func (l *OptionsList) JSONSchema() (map[string]interface{}, []string)

JSONSchema collects all options json schema definition and also returns fields that are required.

func (*OptionsList) UnmarshalYAML

func (l *OptionsList) UnmarshalYAML(nodeList *yaml.Node) (err error)

UnmarshalYAML implements yaml.Unmarshaler to parse OptionsList.

type RunEnvironment added in v0.1.2

type RunEnvironment interface {
	// Init prepares the run environment.
	Init() error
	// Execute runs action a in the environment and operates with IO through streams.
	Execute(ctx context.Context, a *Action) error
	// Close does wrap up operations.
	Close() error
}

RunEnvironment is a common interface for all action run environments.

func NewContainerEnvironment added in v0.1.2

func NewContainerEnvironment(t driver.Type) RunEnvironment

NewContainerEnvironment creates a new action container run environment.

func NewDockerEnvironment added in v0.1.2

func NewDockerEnvironment() RunEnvironment

NewDockerEnvironment creates a new action Docker environment.

type RunEnvironmentFlags added in v0.3.0

type RunEnvironmentFlags interface {
	RunEnvironment
	// FlagsDefinition provides definitions for action environment specific flags.
	FlagsDefinition() OptionsList
	// UseFlags sets environment configuration.
	UseFlags(flags TypeOpts) error
}

RunEnvironmentFlags is an interface to define environment specific runtime configuration.

type RunInfo added in v0.1.2

type RunInfo struct {
	ID     string
	Action *Action
	Status string
}

RunInfo stores information about a running action.

type RunStatusError added in v0.5.1

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

RunStatusError is an execution error also containing command exit code.

func (RunStatusError) Error added in v0.5.1

func (e RunStatusError) Error() string

func (RunStatusError) GetCode added in v0.5.1

func (e RunStatusError) GetCode() int

GetCode returns executions exit code.

type StrSlice

type StrSlice []string

StrSlice is an array of strings for command execution.

func (*StrSlice) UnmarshalYAML

func (l *StrSlice) UnmarshalYAML(n *yaml.Node) (err error)

UnmarshalYAML implements yaml.Unmarshaler to parse a string or a list of strings.

type StrSliceOrStr added in v0.1.2

type StrSliceOrStr []string

StrSliceOrStr is an array of strings for command execution.

func (*StrSliceOrStr) UnmarshalYAML added in v0.1.2

func (l *StrSliceOrStr) UnmarshalYAML(n *yaml.Node) (err error)

UnmarshalYAML implements yaml.Unmarshaler to parse a string or a list of strings.

type TypeArgs added in v0.1.2

type TypeArgs = map[string]interface{}

TypeArgs is a type alias for action arguments.

type TypeOpts added in v0.1.2

type TypeOpts = map[string]interface{}

TypeOpts is a type alias for action options.

type ValueProcessDef added in v0.7.0

type ValueProcessDef struct {
	Processor string                 `yaml:"processor"`
	Options   map[string]interface{} `yaml:"options"`
}

ValueProcessDef stores information about processor and options that should be applied to processor.

type ValueProcessor added in v0.7.0

type ValueProcessor interface {
	IsApplicable(valueType jsonschema.Type) bool
	Execute(value interface{}, options map[string]interface{}) (interface{}, error)
}

ValueProcessor defines an interface for processing a value based on its type and some options.

type ValueProcessorFn added in v0.7.0

type ValueProcessorFn func(value interface{}, options map[string]interface{}) (interface{}, error)

ValueProcessorFn is a function signature used as a callback in processors.

type YamlDiscoveryStrategy added in v0.5.4

type YamlDiscoveryStrategy struct {
	TargetRgx *regexp.Regexp
}

YamlDiscoveryStrategy is a yaml discovery strategy.

func (YamlDiscoveryStrategy) IsValid added in v0.5.4

func (y YamlDiscoveryStrategy) IsValid(name string) bool

IsValid implements DiscoveryStrategy.

func (YamlDiscoveryStrategy) Loader added in v0.5.4

Loader implements DiscoveryStrategy.

Jump to

Keyboard shortcuts

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