utils

package
v0.48.5 Latest Latest
Warning

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

Go to latest
Published: Nov 29, 2023 License: BSD-3-Clause Imports: 35 Imported by: 0

Documentation

Index

Constants

View Source
const (
	SIOCETHTOOL     = 0x8946     // linux/sockios.h
	ETHTOOL_GTXCSUM = 0x00000016 // linux/ethtool.h
	ETHTOOL_STXCSUM = 0x00000017 // linux/ethtool.h
	IFNAMSIZ        = 16         // linux/if.h
)
View Source
const (
	UndefinedFileName = "undefined"
)

Variables

View Source
var (
	// map of commands per platform which start a CLI app.
	NetworkOSCLICmd = map[string]string{
		"arista_eos":    "Cli",
		"nokia_srlinux": "sr_cli",
	}

	// map of the cli exec command and its argument per runtime
	// which is used to spawn CLI session.
	CLIExecCommand = map[string]map[string]string{
		"docker": {
			"exec": "docker",
			"open": "exec -it",
		},
		"podman": {
			"exec": "podman",
			"open": "exec -it",
		},
	}
)
View Source
var IfWaitScript string = `` /* 440-byte string literal not displayed */

IfWaitScript is used in ENTRYPOINT/CMD of the nodes that need to ensure that all of the clab links/interfaces are available in the container before calling the main process.

Functions

func AdjustFileACLs added in v0.48.0

func AdjustFileACLs(fsPath string) error

AdjustFileACLs takes the given fs path, tries to load the access file acl of that path and adds ACL rules rwx for the SUDO_UID and r-x for the SUDO_GID group.

func BoolPointer added in v0.44.0

func BoolPointer(b bool) *bool

BoolPointer returns a pointer to a bool.

func BridgeByName

func BridgeByName(name string) (*netlink.Bridge, error)

BridgeByName returns a *netlink.Bridge referenced by its name.

func ContainerNSToPID added in v0.26.0

func ContainerNSToPID(cID string) (int, error)

ContainerNSToPID resolves the name of a container via the "/run/netns/<CONTAINERNAME>" to its PID.

func ConvertEnvs

func ConvertEnvs(m map[string]string) []string

ConvertEnvs converts env variables passed as a map to a list of them.

func CopyFile

func CopyFile(src, dst string, mode os.FileMode) (err error)

CopyFile copies a file from src to dst. If src and dst files exist, and are the same, then return success. Otherwise, copy the file contents from src to dst. mode is the desired target file permissions, e.g. "0644".

func CopyFileContents

func CopyFileContents(src, dst string, mode os.FileMode) (err error)

CopyFileContents copies the contents of the file named src to the file named by dst. The file will be created if it does not already exist. If the destination file exists, all it's contents will be replaced by the contents of the source file. src can be an http(s) URL as well.

func CreateDirectory

func CreateDirectory(path string, perm os.FileMode)

CreateDirectory creates a directory by a path with a mode/permission specified by perm. If directory exists, the function does not do anything.

func CreateFile

func CreateFile(file, content string) (err error)

CreateFile writes content to a file by path `file`.

func DeleteNetnsSymlink(n string) error

DeleteNetnsSymlink deletes a network namespace and removes the symlink created by LinkContainerNS func.

func DirExists added in v0.48.4

func DirExists(filename string) bool

DirExists returns true if a dir referenced by path exists & accessible.

func EthtoolTXOff

func EthtoolTXOff(name string) error

EthtoolTXOff disables TX checksum offload on specified interface.

func ExpandEnvVarsInStrSlice added in v0.25.0

func ExpandEnvVarsInStrSlice(s []string)

ExpandEnvVarsInStrSlice makes an in-place expansion of env vars in a slice of strings.

func ExpandHome added in v0.25.0

func ExpandHome(p string) string

ExpandHome expands `~` char in the path to home path of a current user in provided path p. When sudo is used, it expands to home dir of a sudo user.

func ExtractDNSServersFromResolvConf added in v0.47.0

func ExtractDNSServersFromResolvConf(filesys fs.FS, filenames []string) ([]string, error)

ExtractDNSServersFromResolvConf extracts IP addresses of the DNS servers from the resolv.conf-formatted files passed in filenames list. Returns a list of IP addresses of the DNS servers.

func FileExists

func FileExists(filename string) bool

FileExists returns true if a file referenced by filename exists & accessible.

func FileLines added in v0.44.0

func FileLines(path, commentStr string) ([]string, error)

FileLines opens a file by the `path` and returns a slice of strings for each line excluding lines that start with `commentStr` or are empty.

func FileOrDirExists added in v0.48.0

func FileOrDirExists(filename string) bool

FileOrDirExists returns true if a file or dir referenced by path exists & accessible.

func FilenameForURL added in v0.38.0

func FilenameForURL(rawUrl string) string

FilenameForURL extracts a filename from a given url returns "undefined" when unsuccessful.

func FirstLinkIPs added in v0.23.0

func FirstLinkIPs(ln string) (v4, v6 string, err error)

FirstLinkIPs returns string representation of the first IPv4/v6 address found for a link referenced by name.

func GenMac added in v0.15.0

func GenMac(oui string) (net.HardwareAddr, error)

GenMac generates a random MAC address for a given OUI.

func GetCNIBinaryPath added in v0.15.0

func GetCNIBinaryPath() string

func GetCanonicalImageName added in v0.15.0

func GetCanonicalImageName(imageName string) string

GetCanonicalImageName produces a canonical image name. if the input name did not specify a tag, the implicit "latest" tag is returned.

func GetLinksByNamePrefix added in v0.46.0

func GetLinksByNamePrefix(prefix string) ([]netlink.Link, error)

GetLinksByNamePrefix returns a list of links whose name matches a prefix.

func GetRouteForIP added in v0.46.0

func GetRouteForIP(ip net.IP) (*rtnl.Route, error)

func GetSSHVersion added in v0.48.1

func GetSSHVersion() string

GetSSHVersion returns the version of the ssh client that is installed on the host.

func IsHttpURL added in v0.48.0

func IsHttpURL(s string, allowSchemaless bool) bool

IsHttpURL checks if the url is a downloadable HTTP URL. The allowSchemaless toggle when set to true will allow URLs without a schema such as "srlinux.dev/clab-srl". This is shortened notion that is used with "deploy -t <url>" only. Other callers of IsHttpURL should set the toggle to false.

func IsKernelModuleLoaded added in v0.41.0

func IsKernelModuleLoaded(name string) (bool, error)

IsKernelModuleLoaded checks if a kernel module is loaded by parsing /proc/modules file.

func LinkByNameOrAlias added in v0.46.0

func LinkByNameOrAlias(name string) (netlink.Link, error)

func LinkContainerNS

func LinkContainerNS(nspath, containerName string) error

LinkContainerNS creates a symlink for containers network namespace so that it can be managed by iproute2 utility.

func LinkIPs added in v0.23.0

func LinkIPs(ln string) (v4addrs, v6addrs []netlink.Addr, err error)

LinkIPs returns IPv4/IPv6 addresses assigned to a link referred by its name.

func LoadEnvVarFiles added in v0.26.0

func LoadEnvVarFiles(basefolder string, files []string) (map[string]string, error)

LoadEnvVarFiles load EnvVars from the given files, resolving relative paths.

func LoadSSHPubKeysFromFiles added in v0.44.0

func LoadSSHPubKeysFromFiles(paths []string) ([]ssh.PublicKey, error)

LoadSSHPubKeysFromFiles parses openssh keys from the files referenced by the paths and returns a slice of ssh.PublicKey pointers. The files may contain multiple keys each on a separate line.

func MergeMaps added in v0.15.0

func MergeMaps(dicts ...map[string]interface{}) map[string]interface{}

MergeMaps merges all dictionaries and return a new dictionary recursively if matching keys are both dictionaries.

func MergeStringMaps added in v0.15.0

func MergeStringMaps(maps ...map[string]string) map[string]string

MergeStringMaps merges all string maps and return a new map maps that are passed for merging will not be changed merging to empty maps return an empty map merging nils return nil.

func MergeStringSlices added in v0.24.0

func MergeStringSlices(ss ...[]string) []string

MergeStringSlices merges string slices with duplicates removed.

func NewHTTPClient added in v0.48.0

func NewHTTPClient() *http.Client

NewHTTPClient creates a new HTTP client with insecure skip verify set to true and min TLS version set to 1.2.

func PauseProcessGroup added in v0.26.0

func PauseProcessGroup(pgid int) error

PauseProcessGroup sends the SIGSTOP signal to a process group, causing all the processes within the group to be Paused e.g. SRL runs multilpe processes, if the container is meant to be stopped, all the related processes must be paused. To me it seams like the ProcessGroupID is set correctly so we can count on that field. The syscall.Kill interpretes negative ints as a PGID and not as a common PID.

func ReadFileContent

func ReadFileContent(file string) ([]byte, error)

func ReadPasswordFromTerminal added in v0.39.0

func ReadPasswordFromTerminal() (string, error)

func ResolvePath added in v0.25.0

func ResolvePath(p, base string) string

ResolvePath resolves a string path by expanding `~` to home dir or resolving a relative path by joining it with the base path.

func SetUIDAndGID added in v0.48.2

func SetUIDAndGID(fsPath string) error

SetUIDAndGID changes the UID and GID of the given path recursively to the values taken from SUDO_UID and SUDO_GID. Which should reflect be the non-root user that called clab via sudo.

func SpawnCLIviaExec added in v0.17.0

func SpawnCLIviaExec(platformName, contName, runtime string) (*network.Driver, error)

SpawnCLIviaExec spawns a CLI session over container runtime exec function end ensures the CLI is available to be used for sending commands over.

func StringInSlice added in v0.15.0

func StringInSlice(slice []string, val string) (int, bool)

StringInSlice checks if a slice contains `val` string and returns slice index if true.

func ToEnvKey added in v0.26.0

func ToEnvKey(s string) string

ToEnvKey capitalizes and removes special chars from a string to is used as an environment variable key.

func UnpauseProcessGroup added in v0.26.0

func UnpauseProcessGroup(pgid int) error

UnpauseProcessGroup send the SIGCONT to the given ProcessGroup identified by its ID.

Types

type EthtoolValue

type EthtoolValue struct {
	Cmd  uint32
	Data uint32
}

EthtoolValue linux/ethtool.h 'struct ethtool_value'.

type IFReqData

type IFReqData struct {
	Name [IFNAMSIZ]byte
	Data uintptr
}

IFReqData linux/if.h 'struct ifreq'.

type KernelVersion added in v0.42.0

type KernelVersion struct {
	Major     int
	Minor     int
	Revision  int
	Remainder string // the rest of the version string, e.g. "-amd64"
}

KernelVersion holds the parsed OS kernel version.

func GetKernelVersion added in v0.42.0

func GetKernelVersion() (*KernelVersion, error)

GetKernelVersion returns the parsed OS kernel version.

func (*KernelVersion) GreaterOrEqual added in v0.42.0

func (kv *KernelVersion) GreaterOrEqual(cmpKv *KernelVersion) bool

GreaterOrEqual returns true if the Kernel version is greater or equal to the compared Kernel version.

func (*KernelVersion) String added in v0.42.0

func (kv *KernelVersion) String() string

String returns the Kernel version as string.

Jump to

Keyboard shortcuts

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