internal

package
v0.32.2 Latest Latest
Warning

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

Go to latest
Published: May 31, 2023 License: GPL-3.0 Imports: 35 Imported by: 0

Documentation

Overview

Package internal houses code for wr's general utility functions.

It also implements the the config system used by the cmd package (see config.go).

import "github.com/VertebrateResequencing/wr/internal"
import "github.com/inconshreveable/log15"
deployment := internal.DefaultDeployment()
logger := log15.New()
logger.SetHandler(log15.LvlFilterHandler(log15.LvlWarn, log15.StderrHandler))
config := internal.ConfigLoad(deployment, false, logger)
port := config.ManagerPort

Index

Constants

View Source
const (

	// bits for rsa keys.
	DefaultBitsForRootRSAKey   int = 2048
	DefualtBitsForServerRSAKey int = 2048

	// certFileFlags are the certificate file flags.
	DefaultCertFileFlags int = os.O_RDWR | os.O_CREATE | os.O_TRUNC
)
View Source
const (

	// S3Prefix is the prefix used by S3 paths.
	S3Prefix = "s3://"

	// Production is the name of the main deployment.
	Production = "production"

	// Development is the name of the development deployment, used during
	// testing.
	Development = "development"

	// ConfigSourceEnvVar is a config value source.
	ConfigSourceEnvVar = "env var"

	// ConfigSourceDefault is a config value source.
	ConfigSourceDefault = "default"
)
View Source
const ZeroCoreMultiplier = 2

ZeroCoreMultiplier is the multipler of actual cores we use for the maximum of zero core jobs

Variables

View Source
var CachedUsername string

Functions

func CertExpiry added in v0.23.1

func CertExpiry(certFile string) (time.Time, error)

CertExpiry returns the time that the certificate given by the path to a pem file will expire.

func CheckCerts added in v0.12.0

func CheckCerts(serverPemFile string, serverKeyFile string) error

CheckCerts checks if the given cert and key file are readable. If one or both of them are not, returns an error.

func CurrentIP added in v0.13.0

func CurrentIP(cidr string) (string, error)

CurrentIP returns the IP address of the machine we're running on right now. The cidr argument can be an empty string, but if set to the CIDR of the machine's primary network, it helps us be sure of getting the correct IP address (for when there are multiple network interfaces on the machine).

func DedupSortStrings added in v0.18.0

func DedupSortStrings(s []string) []string

DedupSortStrings removes duplicates and then sorts the given strings, returning a new slice.

func DefaultDeployment

func DefaultDeployment(ctx context.Context) string

DefaultDeployment works out the default deployment.

func DefaultServer

func DefaultServer(ctx context.Context) string

DefaultServer works out the default server (we need this to be able to report this default before we know what deployment the user has actually chosen, ie. before we have a final config).

func FileMD5 added in v0.12.0

func FileMD5(ctx context.Context, path string) (string, error)

FileMD5 calculates the MD5 hash checksum of a file, returned as HEX encoded.

func GenerateCerts added in v0.12.0

func GenerateCerts(caFile, serverPemFile, serverKeyFile, domain string,
	bitsForRootRSAKey int, bitsForServerRSAKey int, randReader io.Reader, fileFlags int) error

GenerateCerts creates a CA certificate which is used to sign a created server certificate which will have a corresponding key, all saved as PEM files. An error is generated if any of the files already exist.

randReader := crand.Reader to be declared in calling function.

func InS3 added in v0.10.0

func InS3(path string) bool

InS3 tells you if a path is to a file in S3.

func InfobloxSetDomainIP added in v0.12.0

func InfobloxSetDomainIP(domain, ip string) error

InfobloxSetDomainIP uses infoblox to set the IP of a domain. Returns an error if INFOBLOX_HOST, INFOBLOX_USER or INFOBLOX_PASS env vars are not set.

func IsRemote added in v0.10.0

func IsRemote(path string) bool

IsRemote tells you if a path is to a remote file system or object store, based on its URI.

func LogClose added in v0.11.0

func LogClose(ctx context.Context, obj io.Closer, msg string, extra ...interface{})

LogClose is for use to Close() an object during a defer when you don't care if the Close() returns an error, but do want non-EOF errors logged. Extra args are passed as additional context for the logger.

func LogPanic added in v0.11.0

func LogPanic(ctx context.Context, desc string, die bool)

LogPanic is for use in a go routines, deferred at the start of them, to figure out what is causing runtime panics. If the die bool is true, the program exits, otherwise it continues, after logging the error message and stack trace. Desc string should be used to describe briefly what the goroutine you call this in does.

func PathToContent added in v0.18.0

func PathToContent(path string) (string, error)

PathToContent takes the path to a file and returns its contents as a string. If path begins with a tilda, TildaToHome() is used to first convert the path to an absolute path, in order to find the file.

func ProcMeminfoMBs added in v0.11.0

func ProcMeminfoMBs() (int, error)

ProcMeminfoMBs uses gopsutil (amd64 freebsd, linux, windows, darwin, openbds only!) to find the total number of MBs of memory physically installed on the current system.

func RandomString added in v0.13.0

func RandomString() string

RandomString generates a random string of length 8 characters.

func SortMapKeysByIntValue

func SortMapKeysByIntValue(imap map[string]int, reverse bool) []string

SortMapKeysByIntValue sorts the keys of a map[string]int by its values, reversed if you supply true as the second arg.

func SortMapKeysByMapIntValue

func SortMapKeysByMapIntValue(imap map[string]map[string]int, criterion string, reverse bool) []string

SortMapKeysByMapIntValue sorts the keys of a map[string]map[string]int by a the values found at a given sub value, reversed if you supply true as the second arg.

func TildaToHome added in v0.8.0

func TildaToHome(path string) string

TildaToHome converts a path beginning with ~/ to the absolute path based in the current home directory. If that cannot be determined, path is returned unaltered.

func Userid

func Userid() (int, error)

Userid returns the user id of the current user. This avoids problems with static compilation as it avoids the use of os/user. It will only work on linux-like systems where 'id -u' works.

func Username

func Username() (string, error)

Username returns the username of the current user. This avoids problems with static compilation as it avoids the use of os/user. It will only work on linux-like systems where 'id -u -n' works.

func WaitForFile added in v0.12.0

func WaitForFile(file string, after time.Time, timeout time.Duration) bool

WaitForFile waits as long as timeout for the given file to exist. If the file has a timestamp from before the given after, however, waits until the file is touched to have a timestamp after after. When it exists with the right timestamp, returns true. Otherwise false.

func Which added in v0.13.0

func Which(exeName string) string

Which returns the full path to the executable with the given name that is found first in the set of $PATH directories, ignoring any path that is actually a symlink to ourselves.

Types

type CertError added in v0.23.1

type CertError struct {
	Type CertificateErr // one of our CertificateErr constants
	Path string         // path to the certificate file
	Err  error          // In the case of ErrParseCert, the parsing error
}

CertError records a certificate-related error.

func (CertError) Error added in v0.23.1

func (ce CertError) Error() string

Error returns an error with a reason related to certificate and its path.

type CertificateErr added in v0.27.0

type CertificateErr string

CertificateErr is supplied to CertError to define the certain type of certificate related errors.

const (
	ErrCertParse    CertificateErr = "could not be parsed"
	ErrCertCreate   CertificateErr = "could not be created"
	ErrCertExists   CertificateErr = "already exists"
	ErrCertEncode   CertificateErr = "could not encode"
	ErrCertNotFound CertificateErr = "cert could not be found"
	ErrExpiredCert  CertificateErr = "cert expired"
)

ErrCert* are the reasons related to certificates.

type Config

type Config struct {
	ManagerPort          string `default:""`
	ManagerWeb           string `default:""`
	ManagerHost          string `default:"localhost"`
	ManagerDir           string `default:"~/.wr"`
	ManagerPidFile       string `default:"pid"`
	ManagerLogFile       string `default:"log"`
	ManagerDBFile        string `default:"db"`
	ManagerDBBkFile      string `default:"db_bk"`
	ManagerTokenFile     string `default:"client.token"`
	ManagerUploadDir     string `default:"uploads"`
	ManagerUmask         int    `default:"007"`
	ManagerScheduler     string `default:"local"`
	ManagerCAFile        string `default:"ca.pem"`
	ManagerCertFile      string `default:"cert.pem"`
	ManagerKeyFile       string `default:"key.pem"`
	ManagerCertDomain    string `default:"localhost"`
	ManagerSetDomainIP   bool   `default:"false"`
	RunnerExecShell      string `default:"bash"`
	PrivateKeyPath       string `default:"~/.ssh/id_rsa"`
	Deployment           string `default:"production"`
	CloudFlavor          string `default:""`
	CloudFlavorManager   string `default:""`
	CloudFlavorSets      string `default:""`
	CloudKeepAlive       int    `default:"120"`
	CloudServers         int    `default:"-1"`
	CloudCIDR            string `default:"192.168.64.0/18"`
	CloudGateway         string `default:"192.168.64.1"`
	CloudDNS             string `default:"8.8.4.4,8.8.8.8"`
	CloudOS              string `default:"bionic-server"`
	ContainerImage       string `default:"ubuntu:latest"`
	CloudUser            string `default:"ubuntu"`
	CloudRAM             int    `default:"2048"`
	CloudDisk            int    `default:"1"`
	CloudScript          string `default:""`
	CloudDestroyScript   string `default:""`
	CloudConfigFiles     string `default:"~/.s3cfg,~/.aws/credentials,~/.aws/config"`
	CloudSpawns          int    `default:"10"`
	CloudAutoConfirmDead int    `default:"30"`
	DeploySuccessScript  string `default:""`
	// contains filtered or unexported fields
}

Config holds the configuration options for jobqueue server and client.

func ConfigLoadFromCurrentDir added in v0.27.0

func ConfigLoadFromCurrentDir(ctx context.Context, deployment string) *Config

ConfigLoadFromCurrentDir loads and returns the config from current directory.

func ConfigLoadFromParentDir added in v0.27.0

func ConfigLoadFromParentDir(ctx context.Context, deployment string) *Config

ConfigLoadFromParentDir loads and returns the config from a parent directory for testing purposes.

func DefaultConfig added in v0.6.0

func DefaultConfig(ctx context.Context) *Config

DefaultConfig works out the default config for when we need to be able to report the default before we know what deployment the user has actually chosen, ie. before we have a final config.

func (Config) IsProduction added in v0.10.0

func (c Config) IsProduction() bool

IsProduction tells you if we're in the production deployment.

func (Config) Source added in v0.22.0

func (c Config) Source(field string) string

Source returns where the value of a Config field was defined.

func (Config) String added in v0.22.0

func (c Config) String() string

String retruns the string value of property.

func (Config) ToEnv added in v0.27.0

func (c Config) ToEnv()

ToEnv sets all config values as environment variables.

type FileExistsError added in v0.27.0

type FileExistsError struct {
	Path string
	Err  error
}

FileExistsError records a file exist error.

func (*FileExistsError) Error added in v0.27.0

func (f *FileExistsError) Error() string

Error returns error when a file already exists.

type NumberError added in v0.27.0

type NumberError struct {
	Err error
}

NumberError records a number related error.

func (*NumberError) Error added in v0.27.0

func (n *NumberError) Error() string

Error returns a number related error.

Jump to

Keyboard shortcuts

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