simulation

package
v0.5.8 Latest Latest
Warning

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

Go to latest
Published: Jan 15, 2021 License: GPL-3.0 Imports: 41 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func IsDockerAvailable

func IsDockerAvailable(daemonAddr string) bool

IsDockerAvailable can be used to check the connectivity to the docker daemon

func IsKubernetesAvailable

func IsKubernetesAvailable(kubeConfigPath string) bool

IsKubernetesAvailable checks if a kubernetes configuration file exists

Types

type Adapter

type Adapter interface {
	// NewNode creates a new node based on the NodeConfig
	NewNode(config NodeConfig) Node
	// Snapshot returns a snapshot of the adapter
	Snapshot() AdapterSnapshot
}

Adapter can handle Node creation

type AdapterSnapshot

type AdapterSnapshot struct {
	Type   string      `json:"type"`
	Config interface{} `json:"config"`
}

AdapterSnapshot is a snapshot of the configuration of an adapter - The type can be an arbitrary strings, e.g. "exec", "docker", etc. - The config will depend on the type, as every adapter has different configuration options

type ConnectionSnapshot

type ConnectionSnapshot struct {
	From string `json:"from"`
	To   string `json:"to"`
}

ConnectionSnapshot is a snapshot of a connection between peers

type DockerAdapter

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

DockerAdapter is an adapter that can manage DockerNodes

func NewDockerAdapter

func NewDockerAdapter(config DockerAdapterConfig) (*DockerAdapter, error)

NewDockerAdapter creates a DockerAdapter by receiving a DockerAdapterConfig

func (DockerAdapter) NewNode

func (a DockerAdapter) NewNode(config NodeConfig) Node

NewNode creates a new node

func (DockerAdapter) Snapshot

func (a DockerAdapter) Snapshot() AdapterSnapshot

Snapshot returns a snapshot of the adapter

type DockerAdapterConfig

type DockerAdapterConfig struct {
	// BuildContext can be used to build a docker image
	// from a Dockerfile and a context directory
	BuildContext *DockerBuildContext `json:"build,omitempty"`
	// DockerImage points to an existing docker image
	// e.g. ethersphere/swarm:latest
	DockerImage string `json:"image,omitempty"`
	// DaemonAddr is the docker daemon address
	DaemonAddr string `json:"daemonAddr,omitempty"`
}

DockerAdapterConfig is the configuration that can be provided when initializing a DockerAdapter

func DefaultDockerAdapterConfig

func DefaultDockerAdapterConfig() DockerAdapterConfig

DefaultDockerAdapterConfig returns the default configuration that uses the local docker daemon

type DockerBuildContext

type DockerBuildContext struct {
	// Dockefile is the path to the dockerfile
	Dockerfile string `json:"dockerfile"`
	// Directory is the directory that will be used
	// in the context of a docker build
	Directory string `json:"directory"`
	// Tag is used to tag the image
	Tag string `json:"tag"`
}

DockerBuildContext defines the build context to build local docker images

func DefaultDockerBuildContext

func DefaultDockerBuildContext() DockerBuildContext

DefaultDockerBuildContext returns the default build context that uses a Dockerfile

type DockerNode

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

DockerNode is a node that was started via the DockerAdapter

func (*DockerNode) Info

func (n *DockerNode) Info() NodeInfo

Info returns the node status

func (*DockerNode) Snapshot

func (n *DockerNode) Snapshot() (NodeSnapshot, error)

Snapshot returns a snapshot of the node

func (*DockerNode) Start

func (n *DockerNode) Start() error

Start starts the node

func (*DockerNode) Stop

func (n *DockerNode) Stop() error

Stop stops the node

type ExecAdapter

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

ExecAdapter can manage local exec nodes

func NewExecAdapter

func NewExecAdapter(config ExecAdapterConfig) (*ExecAdapter, error)

NewExecAdapter creates an ExecAdapter by receiving a ExecAdapterConfig

func (ExecAdapter) NewNode

func (a ExecAdapter) NewNode(config NodeConfig) Node

NewNode creates a new node

func (ExecAdapter) Snapshot

func (a ExecAdapter) Snapshot() AdapterSnapshot

Snapshot returns a snapshot of the adapter

type ExecAdapterConfig

type ExecAdapterConfig struct {
	// Path to the executable
	ExecutablePath string `json:"executable"`
	// BaseDataDirectory stores all the nodes' data directories
	BaseDataDirectory string `json:"basedir"`
}

ExecAdapterConfig is used to configure an ExecAdapter

type ExecNode

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

ExecNode is a node that is executed locally

func (*ExecNode) Info

func (n *ExecNode) Info() NodeInfo

Info returns the node info

func (*ExecNode) Snapshot

func (n *ExecNode) Snapshot() (NodeSnapshot, error)

Snapshot returns a snapshot of the node

func (*ExecNode) Start

func (n *ExecNode) Start() error

Start starts the node

func (*ExecNode) Stop

func (n *ExecNode) Stop() error

Stop stops the node

type KubernetesAdapter

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

KubernetesAdapter can manage nodes on a kubernetes cluster

func NewKubernetesAdapter

func NewKubernetesAdapter(config KubernetesAdapterConfig) (*KubernetesAdapter, error)

NewKubernetesAdapter creates a KubernetesAdpater by receiving a KubernetesAdapterConfig

func (KubernetesAdapter) NewNode

func (a KubernetesAdapter) NewNode(config NodeConfig) Node

NewNode creates a new node

func (KubernetesAdapter) Snapshot

func (a KubernetesAdapter) Snapshot() AdapterSnapshot

Snapshot returns a snapshot of the Adapter

type KubernetesAdapterConfig

type KubernetesAdapterConfig struct {
	// KubeConfigPath is the path to your kubernetes configuration path
	KubeConfigPath string `json:"kubeConfigPath"`
	// Namespace is the kubernetes namespaces where the pods should be running
	Namespace string `json:"namespace"`
	// BuildContext can be used to build a docker image
	// from a Dockerfile and a context directory
	BuildContext *KubernetesBuildContext `json:"build,omitempty"`
	// DockerImage points to an existing docker image
	// e.g. ethersphere/swarm:latest
	DockerImage string `json:"image,omitempty"`
}

KubernetesAdapterConfig is the configuration provided to a KubernetesAdapter

func DefaultKubernetesAdapterConfig

func DefaultKubernetesAdapterConfig() KubernetesAdapterConfig

DefaultKubernetesAdapterConfig uses the default ~/.kube/config to discover the kubernetes clusters. It also uses the "default" namespace.

type KubernetesBuildContext

type KubernetesBuildContext struct {
	// Dockefile is the path to the dockerfile
	Dockerfile string `json:"dockerfile"`
	// Directory is the directory that will be used
	// in the context of a docker build
	Directory string `json:"dir"`
	// Tag is used to tag the image
	Tag string `json:"tag"`
	// Registry is the image registry where the image will be pushed to
	Registry string `json:"registry"`
	// Username is the user used to push the image to the registry
	Username string `json:"username"`
	// Password is the password of the user that is used to push the image
	// to the registry
	Password string `json:"-"`
}

KubernetesBuildContext defines the build context to build local docker images

func (*KubernetesBuildContext) ImageTag

func (bc *KubernetesBuildContext) ImageTag() string

ImageTag is the full image tag, including the registry

type KubernetesNode

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

KubernetesNode is a node that was started via the KubernetesAdapter

func (*KubernetesNode) Info

func (n *KubernetesNode) Info() NodeInfo

Info returns the node info

func (*KubernetesNode) Snapshot

func (n *KubernetesNode) Snapshot() (NodeSnapshot, error)

Snapshot returns a snapshot of the node

func (*KubernetesNode) Start

func (n *KubernetesNode) Start() error

Start starts the node

func (*KubernetesNode) Stop

func (n *KubernetesNode) Stop() error

Stop stops the node

type Node

type Node interface {
	Info() NodeInfo
	// Start starts the node
	Start() error
	// Stop stops the node
	Stop() error
	// Snapshot returns a snapshot of the node
	Snapshot() (NodeSnapshot, error)
}

Node is a node within a simulation

type NodeConfig

type NodeConfig struct {
	// Arbitrary string used to identify a node
	ID NodeID `json:"id"`
	// Command line arguments
	Args []string `json:"args"`
	// Environment variables
	Env []string `json:"env,omitempty"`
	// Stdout and Stderr specify the nodes' standard output and error
	Stdout io.Writer `json:"-"`
	Stderr io.Writer `json:"-"`
}

NodeConfig is the configuration of a specific node

type NodeID

type NodeID string

NodeID is the node identifier within a simulation. This can be an arbitrary string.

type NodeInfo

type NodeInfo struct {
	ID      NodeID
	Enode   string
	BzzAddr string

	RPCListen   string // RPC listener address. Should be a valid ipc or websocket path
	HTTPListen  string // HTTP listener address: e.g. http://localhost:8500
	PprofListen string // PProf listener address: e.g http://localhost:6060
}

NodeInfo contains the nodes information and connections strings

type NodeSnapshot

type NodeSnapshot struct {
	Config  NodeConfig       `json:"config"`
	Adapter *AdapterSnapshot `json:"adapter,omitempty"`
}

NodeSnapshot is a snapshot of the node, it contains the node configuration and an adapter snapshot

type Simulation

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

Simulation is used to simulate a network of nodes

func NewSimulation

func NewSimulation(adapter Adapter) *Simulation

NewSimulation creates a new simulation given an adapter

func NewSimulationFromSnapshot

func NewSimulationFromSnapshot(snapshot *Snapshot) (*Simulation, error)

NewSimulationFromSnapshot creates a simulation from a snapshot

func (*Simulation) AddBootnode

func (s *Simulation) AddBootnode(id NodeID, args []string) (Node, error)

AddBootnode adds and starts a bootnode with the given id and arguments

func (*Simulation) AddNode

func (s *Simulation) AddNode(id NodeID, args []string) (Node, error)

AddNode adds and starts a node with the given id and arguments

func (*Simulation) AddNodes

func (s *Simulation) AddNodes(idPrefix string, count int, args []string) ([]Node, error)

AddNodes adds and starts 'count' nodes with a given ID prefix, arguments. If the idPrefix is "node" and count is 3 then the following nodes will be created: node-0, node-1, node-2

func (*Simulation) CreateClusterWithBootnode

func (s *Simulation) CreateClusterWithBootnode(idPrefix string, count int, args []string) ([]Node, error)

CreateClusterWithBootnode adds and starts a bootnode. Afterwards it will add and start 'count' nodes that connect to the bootnode. All nodes can be provided by custom arguments. If the idPrefix is "node" and count is 3 then you will have the following nodes created:

node-bootnode, node-0, node-1, node-2.

The bootnode will be the first node on the returned Node slice.

func (*Simulation) DefaultAdapter

func (s *Simulation) DefaultAdapter() Adapter

DefaultAdapter returns the default adapter that the simulation was initialized with

func (*Simulation) Get

func (s *Simulation) Get(id NodeID) (Node, error)

Get returns a node by ID

func (*Simulation) GetAll

func (s *Simulation) GetAll() []Node

GetAll returns all nodes

func (*Simulation) HTTPBaseAddr

func (s *Simulation) HTTPBaseAddr(id NodeID) (string, error)

HTTPBaseAddr returns the address for the HTTP API

func (*Simulation) Init

func (s *Simulation) Init(config NodeConfig) error

Init initializes a node with the NodeConfig with the default Adapter

func (*Simulation) InitWithAdapter

func (s *Simulation) InitWithAdapter(config NodeConfig, adapter Adapter) error

InitWithAdapter initializes a node with the NodeConfig and the given Adapter

func (*Simulation) RPCClient

func (s *Simulation) RPCClient(id NodeID) (*rpc.Client, error)

RPCClient returns an RPC Client for a given node

func (*Simulation) Snapshot

func (s *Simulation) Snapshot() (*Snapshot, error)

Snapshot returns a snapshot of the simulation

func (*Simulation) Start

func (s *Simulation) Start(id NodeID) error

Start starts a node by ID

func (*Simulation) StartAll

func (s *Simulation) StartAll() error

StartAll starts all nodes

func (*Simulation) Stop

func (s *Simulation) Stop(id NodeID) error

Stop stops a node by ID

func (*Simulation) StopAll

func (s *Simulation) StopAll() error

StopAll stops all nodes

func (*Simulation) WaitForHealthyNetwork

func (s *Simulation) WaitForHealthyNetwork() error

WaitForHealthyNetwork will block until all the nodes are considered to have a healthy kademlia table

type Snapshot

type Snapshot struct {
	DefaultAdapter *AdapterSnapshot     `json:"defaultAdapter"`
	Nodes          []NodeSnapshot       `json:"nodes"`
	Connections    []ConnectionSnapshot `json:"connections"`
}

Snapshot is a snapshot of a simulation. It contains snapshots of: - the default adapter that the simulation was initialized with - the list of nodes that were created within the simulation - the list of connections between nodes

func LoadSnapshotFromFile

func LoadSnapshotFromFile(filePath string) (*Snapshot, error)

LoadSnapshotFromFile loads a snapshot from a given JSON file

Directories

Path Synopsis
examples

Jump to

Keyboard shortcuts

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