vm

package
v0.9.7 Latest Latest
Warning

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

Go to latest
Published: Apr 15, 2022 License: MIT Imports: 21 Imported by: 0

Documentation

Index

Constants

View Source
const (
	// DefaultTimeoutValue default timeout value for virtual machines
	DefaultTimeoutValue = 30 * time.Second
	// DefaultMaxVirtualMachines max number of virtual machines that can be loaded at one time
	DefaultMaxVirtualMachines uint8 = 10

	// TypeLoad text to display in script_event table when a VM is loaded
	TypeLoad = "load"
	// TypeCreate text to display in script_event table when a VM is created
	TypeCreate = "create"
	// TypeExecute text to display in script_event table when a script is executed
	TypeExecute = "execute"
	// TypeStop text to display in script_event table when a running script is stopped
	TypeStop = "stop"
	// TypeRead text to display in script_event table when a script contents is read
	TypeRead = "read"

	// StatusSuccess text to display in script_event table on successful execution
	StatusSuccess = "success"
	// StatusFailure text to display in script_event table when script execution fails
	StatusFailure = "failure"
)
View Source
const (
	// ErrNoVMFound error returned when no virtual machine is found
	ErrNoVMFound = "VM %v not found"
)
View Source
const (

	// ErrScriptFailedValidation message to display when a script fails its validation
	ErrScriptFailedValidation = "validation failed"
)
View Source
const (

	// Name is an exported subsystem name
	Name = "gctscript"
)

Variables

View Source
var (
	// ErrScriptingDisabled error message displayed when gctscript is disabled
	ErrScriptingDisabled = errors.New("scripting is disabled")
	// ErrNoVMLoaded error message displayed if a virtual machine has not been initialised
	ErrNoVMLoaded = errors.New("no virtual machine loaded")
)
View Source
var (

	// AllVMSync stores all current Virtual Machine instances
	AllVMSync = &sync.Map{}
	// VMSCount running total count of Virtual Machines
	VMSCount vmscount
)
View Source
var ErrNilSubsystem = errors.New("gct script has not been set up")
View Source
var (
	// ScriptPath path to load/save scripts
	ScriptPath string
)

Functions

func SetDefaultScriptOutput

func SetDefaultScriptOutput()

SetDefaultScriptOutput sets default output file for scripts

Types

type Config

type Config struct {
	Enabled            bool          `json:"enabled"`
	ScriptTimeout      time.Duration `json:"timeout"`
	MaxVirtualMachines uint8         `json:"max_virtual_machines"`
	AllowImports       bool          `json:"allow_imports"`
	AutoLoad           []string      `json:"auto_load"`
	Verbose            bool          `json:"verbose"`
}

Config user configurable options for gctscript

type Error

type Error struct {
	Script string
	Action string
	Cause  error
}

Error interface to meet error requirements

func (Error) Error

func (e Error) Error() string

func (Error) Unwrap

func (e Error) Unwrap() error

Unwrap returns e.Cause meeting errors interface requirements.

type GctScriptManager

type GctScriptManager struct {

	// Optional values to override stored config ('nil' if not overridden)
	MaxVirtualMachines *uint8
	// contains filtered or unexported fields
}

GctScriptManager loads and runs GCT Tengo scripts

func NewManager

func NewManager(config *Config) (*GctScriptManager, error)

NewManager creates a new instance of script manager

func (*GctScriptManager) Autoload

func (g *GctScriptManager) Autoload(name string, remove bool) error

Autoload remove entry from autoload slice

func (*GctScriptManager) GetMaxVirtualMachines

func (g *GctScriptManager) GetMaxVirtualMachines() uint8

GetMaxVirtualMachines returns the max number of VMs to create

func (*GctScriptManager) IsRunning

func (g *GctScriptManager) IsRunning() bool

IsRunning returns if gctscript manager subsystem is started

func (*GctScriptManager) New

func (g *GctScriptManager) New() *VM

New returns a new instance of VM

func (*GctScriptManager) NewVM

func (g *GctScriptManager) NewVM() (vm *VM)

NewVM attempts to create a new Virtual Machine firstly from pool

func (*GctScriptManager) RemoveVM

func (g *GctScriptManager) RemoveVM(id uuid.UUID) error

RemoveVM remove VM from list

func (*GctScriptManager) ShutdownAll

func (g *GctScriptManager) ShutdownAll() (err error)

ShutdownAll shutdown all

func (*GctScriptManager) Start

func (g *GctScriptManager) Start(wg *sync.WaitGroup) (err error)

Start starts gctscript subsystem and creates shutdown channel

func (*GctScriptManager) Stop

func (g *GctScriptManager) Stop() error

Stop stops gctscript subsystem along with all running Virtual Machines

func (*GctScriptManager) Validate

func (g *GctScriptManager) Validate(file string) (err error)

Validate will attempt to execute a script in a test/non-live environment to confirm it passes requirements for execution

type VM

type VM struct {
	ID       uuid.UUID
	Hash     string
	File     string
	Path     string
	Script   *tengo.Script
	Compiled *tengo.Compiled
	T        time.Duration
	NextRun  time.Time
	S        chan struct{}
	// contains filtered or unexported fields
}

VM contains a pointer to "script" (precompiled source) and "compiled" (compiled byte code) instances

func (*VM) Compile

func (vm *VM) Compile() (err error)

Compile compiles to byte code loaded copy of vm script

func (*VM) CompileAndRun

func (vm *VM) CompileAndRun()

CompileAndRun Compile and Run script with support for task running

func (*VM) Load

func (vm *VM) Load(file string) error

Load parses and creates a new instance of tengo script vm

func (*VM) Read

func (vm *VM) Read() ([]byte, error)

Read contents of script back and create script event

func (*VM) RunCtx

func (vm *VM) RunCtx() (err error)

RunCtx runs compiled byte code with context.Context support.

func (*VM) ShortName

func (vm *VM) ShortName() string

ShortName returns short (just filename.extension) of running script

func (*VM) Shutdown

func (vm *VM) Shutdown() error

Shutdown shuts down current VM

Jump to

Keyboard shortcuts

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