common

package
v0.1.1 Latest Latest
Warning

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

Go to latest
Published: Feb 9, 2026 License: AGPL-3.0 Imports: 13 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func CreateLogFile

func CreateLogFile(logsPath LogsPath, filename string, perm os.FileMode) (*os.File, error)

Create a file and all the missing directories

func ReadFileAt

func ReadFileAt(filepath string, offset int64) ([]byte, int64, error)

func RegisterLoadDriver

func RegisterLoadDriver(d LoadDriver) error

func RegisterNodeDriver

func RegisterNodeDriver(d NodeDriver) error

func SetDeploymentError

func SetDeploymentError(repository DeploymentsRepository, deployment Deployment, msg string, args ...any) error

func TailFile

func TailFile(filepath string, w io.Writer) error

func UnregisterLoadDriver

func UnregisterLoadDriver(id LoadDriverID) error

func UnregisterNodeDriver

func UnregisterNodeDriver(id NodeDriverID) error

func UpdateMetadata

func UpdateMetadata[T any](repo DeploymentsRepository, deploymentID DeploymentID, updateFn func(metadata *T) error) error

UpdateMetadata is a generic helper that provides type-safe metadata updates. It handles the JSON marshal/unmarshal internally so callers get a typed pointer.

Types

type Deployment

type Deployment struct {
	ID         DeploymentID
	LoadName   string
	LoadDriver LoadDriver
	Status     DeploymentStatus
	Metadata   any
}

A deployment is the object created when a load has been loaded in the cluster Notice it doesn't reference Load but contains an immutable copy because a deployment cannot be modified directly, it must be updated from the DeploymentsRepository

type DeploymentID

type DeploymentID = uuid.UUID

type DeploymentStatus

type DeploymentStatus struct {
	StatusCode DeploymentStatusCode `json:"status"`
	Reason     string               `json:"reason"`
}

type DeploymentStatusCode

type DeploymentStatusCode string
const (
	DeploymentStatusNotDeployed DeploymentStatusCode = "not deployed"
	DeploymentStatusPlanned     DeploymentStatusCode = "planned"
	DeploymentStatusRunning     DeploymentStatusCode = "running"
	DeploymentStatusStopped     DeploymentStatusCode = "stopped"
	DeploymentStatusError       DeploymentStatusCode = "error"
)

type DeploymentsRepository

type DeploymentsRepository interface {
	Create(loadName string, driver LoadDriver, status DeploymentStatus, metadata any) (DeploymentID, error)
	UpdateStatus(deploymentID DeploymentID, status DeploymentStatus) error
	UpdateMetadata(deploymentID DeploymentID, updateFn func(metadata any) error) error

	GetAll() ([]Deployment, error)
	GetByLoad(loadName string) ([]Deployment, error)
	GetByLoadAndStatus(loadName string, statusCode DeploymentStatusCode) ([]Deployment, error)
	GetDeployment(deploymentID DeploymentID) (*Deployment, error)

	DeleteByLoad(loadName string) error
	DeleteDeployment(deploymentID uuid.UUID) error
}

Store interface for deployments

Notice all data is returned by an immutable copy because this repository might be stored in a distributed database so no memory references must be used

type Hash

type Hash [32]byte

type HashableLoadConfig

type HashableLoadConfig struct {
	Name         string       `json:"name"`
	Node         string       `json:"node"`
	DependsOn    []string     `json:"depends_on"`
	Driver       LoadDriverID `json:"driver"`
	DriverConfig any          `json:"driver_config"`
}

type Load

type Load struct {
	Name       string
	Node       *NodeConfig
	DependsOn  []*Load
	Driver     LoadDriver
	StartChain LoadChain
	StopChain  LoadChain
}

func (*Load) GetDependencies

func (l *Load) GetDependencies() []string

func (*Load) Hash

func (l *Load) Hash() [32]byte

func (*Load) MarshalJSON

func (l *Load) MarshalJSON() ([]byte, error)

func (*Load) UnmarshalJSON

func (l *Load) UnmarshalJSON(data []byte) error

func (*Load) UpdateLoadChains

func (l *Load) UpdateLoadChains(configGraph graph.Graph[string, string], loadsMap map[string]*Load)

type LoadChain

type LoadChain []*Load

func (LoadChain) Hash

func (chain LoadChain) Hash() Hash

Hash loads in order Order is important that's why it receives an array of loads

type LoadConfig

type LoadConfig struct {
	Name           string       `json:"name"`
	Node           string       `json:"node"`
	DependsOn      []string     `json:"depends_on"`
	Driver         LoadDriverID `json:"driver"`
	DriverConfig   any          `json:"driver_config"`
	StartChainHash string       `json:"start_chain_hash"`
	StopChainHash  string       `json:"stop_chain_hash"`
}

type LoadDriver

type LoadDriver interface {
	// GetLoadDriverID returns the unique identifier for this load driver.
	GetLoadDriverID() LoadDriverID

	// DriverInfo returns metadata describing the driver for internal factory use.
	DriverInfo() LoadDriverInfo

	// Verify checks whether the driver options are valid.
	Verify() error

	// MarshalJSON serializes the driver into JSON.
	MarshalJSON() ([]byte, error)

	// UnmarshalJSON deserializes the driver from JSON.
	UnmarshalJSON(data []byte) error

	// PlanDeployment validates prerequisites and creates a deployment in "planned" status.
	// It shall check load requirements but it won't check depending loads.
	// This is invoked within the daemon and does not affect client behavior.
	//
	// Returns the deployment ID for the planned deployment.
	PlanDeployment(repository DeploymentsRepository, loadName string) (DeploymentID, error)

	// UnplanDeployment removes a planned deployment with cleanup
	// Only operates on deployments in "planned" status.
	UnplanDeployment(repository DeploymentsRepository, deployment Deployment) error

	// RunDeployment starts the load execution for an existing planned deployment.
	// This has no effect when called from the client.
	//
	// LoadDriver is responsible of the consistency of the DeploymentsRepository
	RunDeployment(repository DeploymentsRepository, deployment Deployment) error

	// StopDeployment stops a running load execution within the daemon.
	// This has no effect when called from the client.
	//
	// LoadDriver is responsible of the consistency of the DeploymentsRepository
	StopDeployment(repository DeploymentsRepository, deployment Deployment) error

	// KillDeployment stops immediately a running load execution within the daemon.
	// This has no effect when called from the client.
	//
	// LoadDriver is responsible of the consistency of the DeploymentsRepository
	KillDeployment(repository DeploymentsRepository, deployment Deployment) error

	// UpdateDeploymentStatus update and returns current state based on internal drivers factors.
	UpdateDeploymentStatus(repository DeploymentsRepository, deployment Deployment) (DeploymentStatus, error)

	// GetDriverConfig returns the configuration for this load driver.
	GetDriverConfig() LoadDriverConfig

	// Stream load stdout to writer
	StreamStdout(repository DeploymentsRepository, deployment Deployment, w io.Writer) error

	// Stream load stderr to writer
	StreamStderr(repository DeploymentsRepository, deployment Deployment, w io.Writer) error

	// Read load stdout from offset, returns bytes read and end position
	ReadStdout(repository DeploymentsRepository, deployment Deployment, offset int64) ([]byte, int64, error)

	// Read load stderr from offset, returns bytes read and end position
	ReadStderr(repository DeploymentsRepository, deployment Deployment, offset int64) ([]byte, int64, error)
}

func BuildLoadDriver

func BuildLoadDriver(d LoadDriverConfig) (LoadDriver, error)

type LoadDriverConfig

type LoadDriverConfig struct {
	Driver       LoadDriverID `json:"driver"`
	DriverConfig any          `json:"driver_config"`
}

type LoadDriverID

type LoadDriverID string

type LoadDriverInfo

type LoadDriverInfo struct {
	ID  LoadDriverID
	New func(config any) (LoadDriver, error)
}

type LogsPath

type LogsPath string

type Node

type Node struct {
	Name   string
	Url    string
	Driver NodeDriver
}

func NewNodeFromConfig

func NewNodeFromConfig(config *NodeConfig) (*Node, error)

func (*Node) Hash

func (n *Node) Hash() [32]byte

func (*Node) MarshalJSON

func (n *Node) MarshalJSON() ([]byte, error)

func (*Node) UnmarshalJSON

func (n *Node) UnmarshalJSON(data []byte) error

type NodeConfig

type NodeConfig struct {
	Name         string       `json:"name"`
	Url          string       `json:"url"`
	Driver       NodeDriverID `json:"driver,omitempty"`
	DriverConfig *any         `json:"driver_config,omitempty"`
}

type NodeDriver

type NodeDriver interface {
	// GetNodeDriverID returns the unique identifier for this node driver.
	GetNodeDriverID() NodeDriverID

	// DriverInfo returns metadata describing the driver for internal factory use.
	DriverInfo() NodeDriverInfo

	// Verify checks whether the driver options are valid.
	Verify() error

	// MarshalJSON serializes the driver into JSON.
	MarshalJSON() ([]byte, error)

	// UnmarshalJSON deserializes the driver from JSON.
	UnmarshalJSON(data []byte) error

	// PlanAndRegister validates prerequisites and creates or replace the current database entry.
	// It shall check node requirements but it won't check depending nodes.
	// This is invoked within the daemon and does not affect client behavior.
	PlanAndRegister(nodeName string, repository NodesRepository) error

	Startup() error
	Shutdown(message string, time uint32) error
	Restart(message string, time uint32) error
	GetStatus() (NodeStatus, error)

	// GetDriverConfig returns the configuration for this node driver.
	GetDriverConfig() NodeDriverConfig
}

func BuildNodeDriver

func BuildNodeDriver(d NodeDriverConfig) (NodeDriver, error)

type NodeDriverConfig

type NodeDriverConfig struct {
	Driver       NodeDriverID `json:"driver"`
	DriverConfig *any         `json:"driver_config"`
}

type NodeDriverID

type NodeDriverID string

type NodeDriverInfo

type NodeDriverInfo struct {
	ID  NodeDriverID
	New func(config *any) (NodeDriver, error)
}

type NodeEntry

type NodeEntry struct {
	NodeName   string
	NodeDriver NodeDriver
	Metadata   any
}

type NodeStatus

type NodeStatus string
const (
	NodeNotAvailable NodeStatus = "not_available"
	NodeAvailable    NodeStatus = "available"
)

type NodesRepository

type NodesRepository interface {
	Create(nodeName string, driver NodeDriver, metadata any) error

	GetByDaemonId(daemonId string) (NodeEntry, error)
	GetSelf() (NodeEntry, error)

	Delete() error
}

Store interface for nodes

Notice all data is returned by an immutable copy because this repository might be stored in a distributed database so no memory references must be used

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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