util

package
v1.6.3 Latest Latest
Warning

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

Go to latest
Published: Apr 25, 2019 License: MIT Imports: 16 Imported by: 0

Documentation

Overview

Provides a multitude of support functions to help make development easier. Use of these functions should be prefered, as it allows for easier maintainence.

Index

Constants

View Source
const ReservedIps uint32 = 3

ReservedIps indicates the number of ip addresses reserved in a cluster's subnet

Variables

View Source
var NodesPerCluster uint32

Functions

func CombineConfig

func CombineConfig(entries []string) string

CombineConfig combines an Array with \n as the delimiter. Useful for generating configuration files.

func ConvertToStringMap

func ConvertToStringMap(in interface{}) map[string]string

func CopyMap

func CopyMap(m map[string]interface{}) (map[string]interface{}, error)

Map performs a deep copy of the given map m.

func Distances

func Distances(pnts []Point) [][]float64

Create a distance matrix, of all the distances between the given points

func Distribute

func Distribute(nodes []string, dist []int) ([][]string, error)

Distribute generates a roughly uniform random distribution for connections among nodes.

func ExtractJwt

func ExtractJwt(r *http.Request) (string, error)

func FormatError

func FormatError(res string, err error) error

func GenerateUniformRandMeshNetwork

func GenerateUniformRandMeshNetwork(nodes int, conns int) ([][]int, error)

Generate a random mesh network that ensures that there is always a path between all the nodes

func GenerateworstCaseNetwork

func GenerateworstCaseNetwork(nodes int) [][]int

Generates a random path through all nodes

func GetGateway

func GetGateway(server int, node int) string

GetGateway calculates the gateway IP address for a node, base on the current IP scheme

func GetGateways

func GetGateways(server int, nodes int) []string

GetGateways calculates the gateway IP addresses for all of the nodes on a server.

func GetInfoFromIP

func GetInfoFromIP(ipStr string) (int, int)

GetInfoFromIP returns the server number and the node number calculated from the given IPv4 address based on the current IP scheme.

func GetJSONBool

func GetJSONBool(data map[string]interface{}, field string, out *bool) error

GetJSONInt64 checks and extracts a bool from data[field]. Will return an error if data[field] does not exist or is of the wrong type.

func GetJSONInt64

func GetJSONInt64(data map[string]interface{}, field string, out *int64) error

GetJSONInt64 checks and extracts a int64 from data[field]. Will return an error if data[field] does not exist or is of the wrong type.

func GetJSONNumber

func GetJSONNumber(data map[string]interface{}, field string) (json.Number, error)

GetJSONNumber checks and extracts a json.Number from data[field]. Will return an error if data[field] does not exist or is of the wrong type.

func GetJSONString

func GetJSONString(data map[string]interface{}, field string, out *string) error

GetJSONInt64 checks and extracts a string from data[field]. Will return an error if data[field] does not exist or is of the wrong type.

func GetJSONStringArr

func GetJSONStringArr(data map[string]interface{}, field string, out *[]string) error

GetJSONInt64 checks and extracts a []string from data[field]. Will return an error if data[field] does not exist or is of the wrong type.

func GetKidFromJwt

func GetKidFromJwt(jwt string) (string, error)

func GetNetworkAddress

func GetNetworkAddress(server int, node int) string

GetNetworkAddress gets the network address of the cluster the given node belongs to.

func GetNodeIP

func GetNodeIP(server int, node int) string

GetNodeIP calculates the IP address of a node, based on the current IP scheme

func GetPath

func GetPath(path string) string

GetPath extracts the base path of the given path

func GetServiceIps

func GetServiceIps(services []Service) (map[string]string, error)

GetServiceIps creates a map of the service names to their ip addresses. Useful for determining the ip address of a service.

func GetServiceNetwork

func GetServiceNetwork() (string, string, error)

GetServiceNetwork gets the network address in CIDR of the service network

func GetSubnet

func GetSubnet() int

GetSubnet calculates the subnet based on the IP scheme

func GetUUIDString

func GetUUIDString() (string, error)

func GetWholeNetworkIp

func GetWholeNetworkIp(server int) string

GetWholeNetworkIp gets the network ip of the whole network for a server.

func HttpRequest

func HttpRequest(method string, url string, bodyData string) ([]byte, error)

Sends an http request and returns the body. Gives an error if the http request failed or returned a non success code.

func InetNtoa

func InetNtoa(ip uint32) string

InetNtoa converts the IP address, given in network byte order, to a string in IPv4 dotted-decimal notation.

func JwtHttpRequest

func JwtHttpRequest(method string, url string, jwt string, bodyData string) (string, error)

func Lsr

func Lsr(_dir string) ([]string, error)

Lsr lists the contents of a directory recursively

func MergeStringMaps

func MergeStringMaps(m1 map[string]interface{}, m2 map[string]interface{}) map[string]interface{}

func Mkdir

func Mkdir(directory string) error

Mkdir creates a directory

func Rm

func Rm(directories ...string) error

Rm removes all of the given directories or files

func ValidNormalCharacter

func ValidNormalCharacter(chr rune) bool

func ValidateAscii

func ValidateAscii(str string) error

Check if the given string only contains standard ASCII characters, which can fit in a signed char

func ValidateCommandLine

func ValidateCommandLine(image string) error

func ValidateFilePath

func ValidateFilePath(path string) error

Check to make sure there is nothing malicous in the file path

func ValidateNormalAscii

func ValidateNormalAscii(str string) error

Similar to ValidateAscii, except that it excludes control characters from the set of acceptable characters. Any character 127 > c > 31 is considered valid

Types

type Command

type Command struct {
	Cmdline  string
	Node     int
	ServerId int
}

type Config

type Config struct {
	SshUser            string  `json:"ssh-user"`
	SshKey             string  `json:"ssh-key"`
	ServerBits         uint32  `json:"server-bits"`
	ClusterBits        uint32  `json:"cluster-bits"`
	NodeBits           uint32  `json:"node-bits"`
	IPPrefix           uint32  `json:"ip-prefix"`
	Listen             string  `json:"listen"`
	Verbose            bool    `json:"verbose"`
	ThreadLimit        int64   `json:"thread-limit"`
	DockerOutputFile   string  `json:"docker-output-file"`
	Influx             string  `json:"influx"`
	InfluxUser         string  `json:"influx-user"`
	InfluxPassword     string  `json:"influx-password"`
	ServiceNetwork     string  `json:"service-network"`
	ServiceNetworkName string  `json:"service-network-name"`
	NodePrefix         string  `json:"node-prefix"`
	NodeNetworkPrefix  string  `json:"node-network-prefix"`
	ServicePrefix      string  `json:"service-prefix"`
	NodesPublicKey     string  `json:"nodes-public-key"`
	NodesPrivateKey    string  `json:"nodes-private-key"`
	HandleNodeSshKeys  bool    `json:"handle-node-ssh-keys"`
	MaxNodes           int     `json:"max-nodes"`
	MaxNodeMemory      string  `json:"max-node-memory"`
	MaxNodeCpu         float64 `json:"max-node-cpu"`
	BridgePrefix       string  `json:"bridge-prefix"`
}

func GetConfig

func GetConfig() *Config

Get a pointer to the global config object. Do not modify this object

func LoadConfig

func LoadConfig() *Config

The config from a file

func (*Config) AutoFillMissing

func (c *Config) AutoFillMissing()

Fill in the missing essential values with the defaults.

func (*Config) LoadFromEnv

func (this *Config) LoadFromEnv()

Load the configuration from the Environment

type EndPoint

type EndPoint struct {
	Url  string `json:"url"`
	User string `json:"user"`
	Pass string `json:"pass"`
}

type KeyPair

type KeyPair struct {
	PrivateKey string `json:"privateKey"`
	PublicKey  string `json:"publicKey"`
}

***Standard Data Structures***

KeyPair represents a cryptographic key pair

type Point

type Point struct {
	X int `json:"x"`
	Y int `json:"y"`
}

type Resources

type Resources struct {
	/*
	   Cpus should be a floating point value represented as a string, and
	   is  equivalent to the percentage of a single cores time which can be used
	   by a node. Can be more than 1.0, meaning the node can use multiple cores at
	   a time.
	*/
	Cpus string `json:"cpus"`
	/*
	   Memory supports values up to Terrabytes (tb). If the unit is ommited, then it
	   is assumed to be bytes. This is not case sensitive.
	*/
	Memory string `json:"memory"`
}

Resources represents the maximum amount of resources that a node can use.

func (Resources) GetMemory

func (this Resources) GetMemory() (int64, error)

GetMemory gets the memory value as an integer.

func (Resources) NoCpuLimits

func (this Resources) NoCpuLimits() bool

NoCpuLimits checks if the resources object doesn't specify any cpu limits

func (Resources) NoLimits

func (this Resources) NoLimits() bool

NoLimits checks if the resources object doesn't specify any limits

func (Resources) NoMemoryLimits

func (this Resources) NoMemoryLimits() bool

NoMemoryLimits checks if the resources object doesn't specify any memory limits

func (Resources) Validate

func (this Resources) Validate() error

Validate ensures that the given resource object is valid, and allowable.

func (Resources) ValidateAndSetDefaults

func (this Resources) ValidateAndSetDefaults() error

ValidateAndSetDefaults calls Validate, and if it is valid, fills any missing information. Helps to ensure that the Maximum limits are enforced.

type Service

type Service struct {
	Name    string            `json:"name"`
	Image   string            `json:"image"`
	Env     map[string]string `json:"env"`
	Network string            `json:"network"`
}

Service represents a service for a blockchain. All env variables will be passed to the container.

Jump to

Keyboard shortcuts

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