utils

package
v0.0.0-...-a7271ac Latest Latest
Warning

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

Go to latest
Published: May 4, 2014 License: AGPL-3.0 Imports: 31 Imported by: 0

Documentation

Index

Examples

Constants

This section is empty.

Variables

View Source
var AptCommandOutput = (*exec.Cmd).CombinedOutput

AptCommandOutput calls cmd.Output, this is used as an overloading point so we can test what *would* be run without actually executing another program

View Source
var (

	// AptConfFile is the full file path for the proxy settings that are
	// written by cloud-init and the machine environ worker.
	AptConfFile = "/etc/apt/apt.conf.d/42-juju-proxy-settings"
)
View Source
var CompatSalt = string([]byte{0x75, 0x82, 0x81, 0xca})

CompatSalt is because Juju 1.16 and older used a hard-coded salt to compute the password hash for all users and agents

View Source
var FastInsecureHash = false

FastInsecureHash specifies whether a fast, insecure version of the hash algorithm will be used. Changing this will cause PasswordHash to produce incompatible passwords. It should only be changed for testing purposes - to make tests run faster.

View Source
var MinAgentPasswordLength = base64.StdEncoding.EncodedLen(randomPasswordBytes)

MinAgentPasswordLength describes how long agent passwords should be. We require this length because we assume enough entropy in the Agent password that it is safe to not do extra rounds of iterated hashing.

Functions

func AgentPasswordHash

func AgentPasswordHash(password string) string

AgentPasswordHash returns base64-encoded one-way hash of password. This is not suitable for User passwords because those will have limited entropy (see UserPasswordHash). However, since we generate long random passwords for agents, we can trust that there is sufficient entropy to prevent brute force search. And using a faster hash allows us to restart the state machines and have 1000s of agents log in in a reasonable amount of time.

func AptConfigProxy

func AptConfigProxy() (string, error)

AptConfigProxy will consult apt-config about the configured proxy settings. If there are no proxy settings configured, an empty string is returned.

func AptGetInstall

func AptGetInstall(packages ...string) error

AptGetInstall runs 'apt-get install packages' for the packages listed here

func AptProxyContent

func AptProxyContent(proxy osenv.ProxySettings) string

AptProxyContent produces the format expected by the apt config files from the ProxySettings struct.

func AtomicWriteFile

func AtomicWriteFile(filename string, contents []byte, perms os.FileMode) (err error)

AtomicWriteFile atomically writes the filename with the given contents and permissions, replacing any existing file at the same path.

func AtomicWriteFileAndChange

func AtomicWriteFileAndChange(filename string, contents []byte, change func(*os.File) error) (err error)

AtomicWriteFileAndChange atomically writes the filename with the given contents and calls the given function after the contents were written, but before the file is renamed.

func CheckPendingShutdown

func CheckPendingShutdown() (result bool)

func CommandString

func CommandString(args ...string) string

CommandString flattens a sequence of command arguments into a string suitable for executing in a shell, escaping slashes, variables and quotes as necessary; each argument is double-quoted if and only if necessary.

func CopyFile

func CopyFile(dest, source string) error

CopyFile writes the contents of the given source file to dest.

func DetectAptProxies

func DetectAptProxies() (result osenv.ProxySettings, err error)

DetectAptProxies will parse the results of AptConfigProxy to return a ProxySettings instance.

func ErrorContextf

func ErrorContextf(err *error, format string, args ...interface{})

ErrorContextf prefixes any error stored in err with text formatted according to the format specifier. If err does not contain an error, ErrorContextf does nothing.

func GetAddressForInterface

func GetAddressForInterface(interfaceName string) (string, error)

GetAddressForInterface looks for the network interface and returns the IPv4 address from the possible addresses.

func GetIPv4Address

func GetIPv4Address(addresses []net.Addr) (string, error)

GetIPv4Address iterates through the addresses expecting the format from func (ifi *net.Interface) Addrs() ([]net.Addr, error)

func GetNonValidatingHTTPClient

func GetNonValidatingHTTPClient() *http.Client

func GetPort

func GetPort() (string, error)

func GetSocket

func GetSocket() (string, error)

func Gunzip

func Gunzip(data []byte) ([]byte, error)

Gunzip uncompresses the given data.

func Gzip

func Gzip(data []byte) []byte

Gzip compresses the given data.

func IsPackageInstalled

func IsPackageInstalled(packageName string) bool

IsPackageInstalled uses dpkg-query to determine if the `packageName` package is installed.

func IsUbuntu

func IsUbuntu() bool

IsUbuntu executes lxb_release to see if the host OS is Ubuntu.

func IsValidUUIDString

func IsValidUUIDString(s string) bool

IsValidUUIDString returns true, if the given string matches a valid UUID (version 4, variant 2).

func JoinServerPath

func JoinServerPath(elem ...string) string

JoinServerPath joins any number of path elements into a single path, adding a path separator (based on the current juju server OS) if necessary. The result is Cleaned; in particular, all empty strings are ignored.

func NewHttpTLSTransport

func NewHttpTLSTransport(tlsConfig *tls.Config) *http.Transport

NewHttpTLSTransport returns a new http.Transport constructed with the TLS config and the necessary parameters for Juju.

func NewHttpTransport

func NewHttpTransport() *http.Transport

NewHttpTransport returns a new http.Transport constructed with the necessary parameters for Juju.

func NormalizePath

func NormalizePath(dir string) (string, error)

NormalizePath expands a path containing ~ to its absolute form, and removes any .. or . path elements.

func PathToWindows

func PathToWindows(filepath string) string

gsamfira: The PathToWindows is a parlous one. Quick and dirty hack to Get around the separator issue when generation windows paths on linux

func RChmod

func RChmod(path string, mode os.FileMode) error

func RandomBytes

func RandomBytes(n int) ([]byte, error)

RandomBytes returns n random bytes.

func RandomPassword

func RandomPassword() (string, error)

RandomPassword generates a random base64-encoded password.

func RandomSalt

func RandomSalt() (string, error)

RandomSalt generates a random base64 data suitable for using as a password salt The pbkdf2 guideline is to use 8 bytes of salt, so we do 12 raw bytes into 16 base64 bytes. (The alternative is 6 raw into 8 base64).

func ReadFileSHA256

func ReadFileSHA256(filename string) (string, int64, error)

ReadFileSHA256 is like ReadSHA256 but reads the contents of the given file.

func ReadSHA256

func ReadSHA256(source io.Reader) (string, int64, error)

ReadSHA256 returns the SHA256 hash of the contents read from source (hex encoded) and the size of the source in bytes.

func ReadSocketFile

func ReadSocketFile(socketPath string) (string, error)

func ReadYaml

func ReadYaml(path string, obj interface{}) error

ReadYaml unmarshals the yaml contained in the file at path into obj. See goyaml.Unmarshal.

func ReplaceFile

func ReplaceFile(source, destination string) error

Replace atomically replaces the destination file or directory with the source. The errors that are returned are identical to those returned by os.Rename.

func RunCommand

func RunCommand(command string, args ...string) (output string, err error)

RunCommand executes the command and return the combined output.

func ShQuote

func ShQuote(s string) string

ShQuote quotes s so that when read by bash, no metacharacters within s will be interpreted as such.

func Symlink(oldname, newname string) error

func TestPort

func TestPort(port int) error

func Timeit

func Timeit(action string) func()

Start a timer, used for tracking time spent. Generally used with either defer, as in:

defer utils.Timeit("my func")()

Which will track how much time is spent in your function. Or if you want to track the time spent in a function you are calling then you would use:

toc := utils.Timeit("anotherFunc()")
anotherFunc()
toc()

This tracks nested calls by indenting the output, and will print out the full stack of timing when we reach the top of the stack.

func UniqueDirectory

func UniqueDirectory(path, name string) (string, error)

UniqueDirectory returns "path/name" if that directory doesn't exist. If it does, the method starts appending .1, .2, etc until a unique name is found.

func UserHomeDir

func UserHomeDir(userName string) (homeDir string, err error)

UserHomeDir returns the home directory for the specified user, or the home directory for the current user if the specified user is empty.

func UserPasswordHash

func UserPasswordHash(password string, salt string) string

UserPasswordHash returns base64-encoded one-way hash password that is computationally hard to crack by iterating through possible passwords.

func WriteFile

func WriteFile(filename string, contents []byte, perms os.FileMode) (err error)

func WriteSocketFile

func WriteSocketFile(socketPath string) (string, error)

TODO: get rid of this once named pipes are in This actually writes a text file on disk with the port nr assigned to the unit agent.

func WriteYaml

func WriteYaml(path string, obj interface{}) error

WriteYaml marshals obj as yaml and then writes it to a file, atomically, by first writing a sibling with the suffix ".preparing" and then moving the sibling to the real path.

Types

type Attempt

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

func (*Attempt) HasNext

func (a *Attempt) HasNext() bool

HasNext returns whether another attempt will be made if the current one fails. If it returns true, the following call to Next is guaranteed to return true.

Example
package main

import (
	"time"

	gc "launchpad.net/gocheck"

	"launchpad.net/juju-core/utils"
)

func doSomething() (int, error) { return 0, nil }

func shouldRetry(error) bool { return false }

func doSomethingWith(int) {}

func main() {
	// This example shows how Attempt.HasNext can be used to help
	// structure an attempt loop. If the godoc example code allowed
	// us to make the example return an error, we would uncomment
	// the commented return statements.
	attempts := utils.AttemptStrategy{
		Total: 1 * time.Second,
		Delay: 250 * time.Millisecond,
	}
	for attempt := attempts.Start(); attempt.Next(); {
		x, err := doSomething()
		if shouldRetry(err) && attempt.HasNext() {
			continue
		}
		if err != nil {
			// return err
			return
		}
		doSomethingWith(x)
	}
	// return ErrTimedOut
	return
}

func (utilsSuite) TestAttemptTiming(c *gc.C) {
	testAttempt := utils.AttemptStrategy{
		Total: 0.25e9,
		Delay: 0.1e9,
	}
	want := []time.Duration{0, 0.1e9, 0.2e9, 0.2e9}
	got := make([]time.Duration, 0, len(want)) // avoid allocation when testing timing
	t0 := time.Now()
	for a := testAttempt.Start(); a.Next(); {
		got = append(got, time.Now().Sub(t0))
	}
	got = append(got, time.Now().Sub(t0))
	c.Assert(got, gc.HasLen, len(want))
	const margin = 0.01e9
	for i, got := range want {
		lo := want[i] - margin
		hi := want[i] + margin
		if got < lo || got > hi {
			c.Errorf("attempt %d want %g got %g", i, want[i].Seconds(), got.Seconds())
		}
	}
}

func (utilsSuite) TestAttemptNextHasNext(c *gc.C) {
	a := utils.AttemptStrategy{}.Start()
	c.Assert(a.Next(), gc.Equals, true)
	c.Assert(a.Next(), gc.Equals, false)

	a = utils.AttemptStrategy{}.Start()
	c.Assert(a.Next(), gc.Equals, true)
	c.Assert(a.HasNext(), gc.Equals, false)
	c.Assert(a.Next(), gc.Equals, false)

	a = utils.AttemptStrategy{Total: 2e8}.Start()
	c.Assert(a.Next(), gc.Equals, true)
	c.Assert(a.HasNext(), gc.Equals, true)
	time.Sleep(2e8)
	c.Assert(a.HasNext(), gc.Equals, true)
	c.Assert(a.Next(), gc.Equals, true)
	c.Assert(a.Next(), gc.Equals, false)

	a = utils.AttemptStrategy{Total: 1e8, Min: 2}.Start()
	time.Sleep(1e8)
	c.Assert(a.Next(), gc.Equals, true)
	c.Assert(a.HasNext(), gc.Equals, true)
	c.Assert(a.Next(), gc.Equals, true)
	c.Assert(a.HasNext(), gc.Equals, false)
	c.Assert(a.Next(), gc.Equals, false)
}
Output:

func (*Attempt) Next

func (a *Attempt) Next() bool

Next waits until it is time to perform the next attempt or returns false if it is time to stop trying. It always returns true the first time it is called - we are guaranteed to make at least one attempt.

type AttemptStrategy

type AttemptStrategy struct {
	Total time.Duration // total duration of attempt.
	Delay time.Duration // interval between each try in the burst.
	Min   int           // minimum number of retries; overrides Total
}

AttemptStrategy represents a strategy for waiting for an action to complete successfully.

func (AttemptStrategy) Start

func (s AttemptStrategy) Start() *Attempt

Start begins a new sequence of attempts for the given strategy.

type UUID

type UUID [16]byte

UUID represent a universal identifier with 16 octets.

func NewUUID

func NewUUID() (UUID, error)

NewUUID generates a new version 4 UUID relying only on random numbers.

func UUIDFromString

func UUIDFromString(s string) (UUID, error)

func (UUID) Copy

func (uuid UUID) Copy() UUID

Copy returns a copy of the UUID.

func (UUID) Raw

func (uuid UUID) Raw() [16]byte

Raw returns a copy of the UUID bytes.

func (UUID) String

func (uuid UUID) String() string

String returns a hexadecimal string representation with standardized separators.

Directories

Path Synopsis
On-disk mutex protecting a resource A lock is represented on disk by a directory of a particular name, containing an information file.
On-disk mutex protecting a resource A lock is represented on disk by a directory of a particular name, containing an information file.
The parallel package provides a way of running functions concurrently while limiting the maximum number running at once.
The parallel package provides a way of running functions concurrently while limiting the maximum number running at once.
ssh
Package ssh contains utilities for dealing with SSH connections, key management, and so on.
Package ssh contains utilities for dealing with SSH connections, key management, and so on.
Package voyeur implements a concurrency-safe value that can be watched for changes.
Package voyeur implements a concurrency-safe value that can be watched for changes.

Jump to

Keyboard shortcuts

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