util

package
v1.8.1 Latest Latest
Warning

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

Go to latest
Published: May 21, 2019 License: GPL-3.0 Imports: 16 Imported by: 0

Documentation

Overview

Package util provides a multitude of support functions to help make development easier. Use of these functions should be preferred, as it allows for easier maintenance.

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

NodesPerCluster represents the maximum number of nodes allowed in a cluster

Functions

func CombineConfig

func CombineConfig(entries []string) string

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

func ConvertToStringMap

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

ConvertToStringMap converts a map of string to interface to a map of string to json

func CopyMap

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

CopyMap performs a deep copy of the given map m.

func Distances

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

Distances creates 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)

ExtractJwt will attempt to extract and return the jwt from the auth header

func ExtractStringMap added in v1.6.5

func ExtractStringMap(in map[string]interface{}, key string) (map[string]interface{}, bool)

ExtractStringMap extracts a map[string]interface from a map[string]interface

func FormatError

func FormatError(res string, err error) error

FormatError produced a standard error for execution.

func GenerateDependentMeshNetwork added in v1.7.2

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

GenerateDependentMeshNetwork generates a random mesh network that ensures the if built in order, each node will be given a list of peers which is already up and running. Note: This means that the first node will have an empty list

func GenerateNoDuplicateMeshNetwork added in v1.7.2

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

GenerateNoDuplicateMeshNetwork generates a random mesh network that ensures that peering there is always a path between all the nodes, without any duplication. That is, if 1 contains 3, 3 won't contain 1 by elimination

func GenerateUniformRandMeshNetwork

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

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

func GenerateworstCaseNetwork

func GenerateworstCaseNetwork(nodes int) [][]int

GenerateworstCaseNetwork generates a random path through all nodes

func GetGateway

func GetGateway(server int, network int) string

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

func GetGateways

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

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

func GetInfoFromIP

func GetInfoFromIP(ipStr string) (int, int, int)

GetInfoFromIP returns the server number and the node number calculated from the given IPv4 address based on the current IP scheme. (server,network,index)

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 GetJSONString

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

GetJSONString 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)

GetKidFromJwt will attempt to extract the kid from the given jwt

func GetNetworkAddress

func GetNetworkAddress(server int, network int) string

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

func GetNodeIP

func GetNodeIP(server int, network int, index int) (string, error)

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)

GetUUIDString generates a new UUID

func GetUniqueStrings added in v1.6.5

func GetUniqueStrings(in []string) []string

GetUniqueStrings returns the given slice of strings without the duplicates

func GetWholeNetworkIP added in v1.6.5

func GetWholeNetworkIP(server int) string

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

func HTTPRequest added in v1.6.5

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

HTTPRequest Sends a 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 added in v1.6.5

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

JwtHTTPRequest is similar to HttpRequest, but it have the content-type set as application/json and it will put the given jwt in the auth header

func LogError added in v1.7.1

func LogError(err error) error

LogError acts like log.Println() but takes in an error and returns that error. Used to help reduce code clutter from all the log.Println(err) in the code. Has no effect is err == nil

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{}

MergeStringMaps merges two maps of string to interface together and returns it If there are conflicting keys, the value in m2 will be chosen.

func Rm

func Rm(directories ...string) error

Rm removes all of the given directories or files. Convenience function for os.RemoveAll

func ValidNormalCharacter

func ValidNormalCharacter(chr rune) bool

ValidNormalCharacter checks to make sure a character is within a safe range to naively prevent most bash injects (Not for security, only for debugging)

func ValidateASCII added in v1.6.5

func ValidateASCII(str string) error

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

func ValidateCommandLine

func ValidateCommandLine(str string) error

ValidateCommandLine naively checks for ppntential accidental bash injections. Like ValidNormalCharacter, is not too be considered useful for security, only for picking up on potential bugs.

func ValidateFilePath

func ValidateFilePath(path string) error

ValidateFilePath check to make sure there is nothing malicous in the file path

func ValidateNormalASCII added in v1.6.5

func ValidateNormalASCII(str string) error

ValidateNormalASCII is 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
}

Command represents a previously executed command

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"`
	Verbosity          string  `json:"verbosity"`
	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"`
	APIEndpoint        string  `json:"api-endpoint"`
	NibblerEndPoint    string  `json:"nibbler-end-point"`
}

Config groups all of the global configuration parameters into a single struct

func GetConfig

func GetConfig() *Config

GetConfig gets a pointer to the global config object. Do not modify c object

func LoadConfig

func LoadConfig() *Config

LoadConfig loads the config from the configuration file

func (*Config) AutoFillMissing

func (c *Config) AutoFillMissing()

AutoFillMissing fills in the missing essential values with the defaults.

func (*Config) LoadFromEnv

func (c *Config) LoadFromEnv()

LoadFromEnv loads the configuration from the Environment

type EndPoint

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

EndPoint represents an endpoint with basic auth

type KeyPair

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

KeyPair represents a cryptographic key pair

type Point

type Point struct {
	//X represents the position on the x axis
	X int `json:"x"`
	//Y represents the position on the y axis
	Y int `json:"y"`
}

Point represents a basic 2D coordinate point

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 omitted, then it
	// is assumed to be bytes. This is not case sensitive.
	Memory string `json:"memory"`
	// Volumes to be used by each node.
	Volumes []string `json:"volumes"`
	// Ports to be opened for each node, each item associated with one node.
	Ports []string `json:"ports"`
}

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

func (Resources) GetMemory

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

GetMemory gets the memory value as an integer.

func (Resources) NoCPULimits added in v1.6.5

func (res Resources) NoCPULimits() bool

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

func (Resources) NoLimits

func (res Resources) NoLimits() bool

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

func (Resources) NoMemoryLimits

func (res Resources) NoMemoryLimits() bool

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

func (Resources) Validate

func (res Resources) Validate() error

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

func (Resources) ValidateAndSetDefaults

func (res 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