ganeti-3.1: Cluster-based virtualization management software
Safe HaskellSafe-Inferred
LanguageHaskell2010

Ganeti.Utils

Description

Utility functions.

Synopsis

Documentation

debug :: Show a => a -> a #

To be used only for debugging, breaks referential integrity.

debugFn :: Show b => (a -> b) -> a -> a #

Displays a modified form of the second parameter before returning it.

debugXy :: Show a => a -> b -> b #

Show the first parameter before returning the second one.

sepSplit :: Eq a => a -> [a] -> [[a]] #

Split a list on a separator and return a list of lists.

findFirst :: (Ord a, Enum a) => a -> Set a -> a #

Finds the first unused element in a set starting from a given base.

stdDev :: [Double] -> Double #

Standard deviation function.

if' #

Arguments

:: Bool

condition

-> a

"then" result

-> a

"else" result

-> a

"then" or "else" result depending on the condition

"if" as a function, rather than as syntactic sugar.

select #

Arguments

:: a

default result

-> [(Bool, a)]

list of "condition, result"

-> a

first result which has a True condition, or default

Return the first result with a True condition, or the default otherwise.

applyIf :: Bool -> (a -> a) -> a -> a #

Apply the function if condition holds, otherwise use default value.

commaJoin :: [String] -> String #

Comma-join a string list.

ensureQuoted :: String -> String #

Ensure a value is quoted if needed.

tryRead :: (MonadFail m, Read a) => String -> String -> m a #

Safe read function returning data encapsulated in a Result.

readMaybe :: Read a => String -> Maybe a #

Parse a string using the Read instance. Succeeds if there is exactly one valid result.

Backport from Text.Read introduced in base-4.6.0.0

formatTable :: [[String]] -> [Bool] -> [[String]] #

Format a table of strings to maintain consistent length.

printTable :: String -> [String] -> [[String]] -> [Bool] -> String #

Constructs a printable table from given header and rows

parseUnit :: (MonadFail m, Integral a, Read a) => String -> m a #

Tries to extract number and scale from the given string.

Input must be in the format NUMBER+ SPACE* [UNIT]. If no unit is specified, it defaults to MiB. Return value is always an integral value in MiB.

parseUnitAssumeBinary :: (MonadFail m, Integral a, Read a) => String -> m a #

Tries to extract a number and scale from a given string, taking all kilos to be binary.

plural :: Int -> String -> String -> String #

Simple pluralize helper

niceSort :: [String] -> [String] #

Sort a list of strings based on digit and non-digit groupings.

Given a list of names [a1, a10, a11, a2] this function will sort the list in the logical order [a1, a2, a10, a11].

The sort algorithm breaks each name in groups of either only-digits or no-digits, and sorts based on each group.

Internally, this is not implemented via regexes (like the Python version), but via actual splitting of the string in sequences of either digits or everything else, and converting the digit sequences in Left Integer and the non-digit ones in Right String, at which point sorting becomes trivial due to the built-in Either ordering; we only need one extra step of dropping the key at the end.

niceSortKey :: (a -> String) -> [a] -> [a] #

Key-version of niceSort. We use sortBy and compare on fst since we don't want to add an ordering constraint on the a type, hence the need to only compare the first element of the (key, a) tuple.

exitIfBad :: String -> Result a -> IO a #

Unwraps a Result, exiting the program if it is a Bad value, otherwise returning the actual contained value.

exitErr :: String -> IO a #

Exits immediately with an error message.

exitWhen :: Bool -> String -> IO () #

Exits with an error message if the given boolean condition if true.

exitUnless :: Bool -> String -> IO () #

Exits with an error message unless the given boolean condition if true, the opposite of exitWhen.

logWarningIfBad :: String -> a -> Result a -> IO a #

Unwraps a Result, logging a warning message and then returning a default value if it is a Bad value, otherwise returning the actual contained value.

rStripSpace :: String -> String #

Strip space characthers (including newline). As this is expensive, should only be run on small strings.

newUUID :: IO String #

Returns a random UUID. This is a Linux-specific method as it uses the /proc filesystem.

isUUID :: String -> Bool #

Checks if the string is a valid UUID as in "Ganeti.Constants.uuidRegex".

chompPrefix :: String -> String -> Maybe String #

Strip a prefix from a string, allowing the last character of the prefix (which is assumed to be a separator) to be absent from the string if the string terminates there.

>>> chompPrefix "foo:bar:" "a:b:c" Nothing

>>> chompPrefix "foo:bar:" "foo:bar:baz" Just "baz"

>>> chompPrefix "foo:bar:" "foo:bar:" Just ""

>>> chompPrefix "foo:bar:" "foo:bar" Just ""

>>> chompPrefix "foo:bar:" "foo:barbaz" Nothing

warn :: String -> IO () #

Print a warning, but do not exit.

wrap #

Arguments

:: Int

maxWidth

-> String

string that needs wrapping

-> [String]

string "broken" in lines

Breaks a string in lines with length <= maxWidth.

NOTE: The split is OK if:

  • It doesn't break a word, i.e. the next line begins with space (isSpace . head $ rest) or the current line ends with space (null revExtra);
  • It breaks a very big word that doesn't fit anyway (null revLine).

trim :: String -> String #

Removes surrounding whitespace. Should only be used in small strings.

defaultHead :: a -> [a] -> a #

A safer head version, with a default value.

exitIfEmpty :: String -> [a] -> IO a #

A head version in the I/O monad, for validating parameters without which we cannot continue.

splitEithers :: [Either a b] -> ([a], [b], [Bool]) #

Split an Either list into two separate lists (containing the Left and Right elements, plus a "trail" list that allows recombination later.

This is splitter; for recombination, look at recombineEithers. The sum of "left" and "right" lists should be equal to the original list length, and the trail list should be the same length as well. The entries in the resulting lists are reversed in comparison with the original list.

recombineEithers :: (Show a, Show b) => [a] -> [b] -> [Bool] -> Result [Either a b] #

Recombines two "left" and "right" lists using a "trail" list into a single Either list.

This is the counterpart to splitEithers. It does the opposite transformation, and the output list will be the reverse of the input lists. Since splitEithers also reverses the lists, calling these together will result in the original list.

Mismatches in the structure of the lists (e.g. inconsistent lengths) are represented via Bad; normally this function should not fail, if lists are passed as generated by splitEithers.

resolveAddr :: Int -> String -> IO (Result (Family, SockAddr)) #

Resolves a numeric address.

monadicThe :: (Eq a, MonadFail m) => String -> [a] -> m a #

Obtain the unique element of a list in an arbitrary monad.

setOwnerAndGroupFromNames :: FilePath -> GanetiDaemon -> GanetiGroup -> IO () #

Set the owner and the group of a file (given as names, not numeric id).

setOwnerWGroupR :: FilePath -> IO () #

Resets permissions so that the owner can read/write and the group only read. All other permissions are cleared.

formatOrdinal :: (Integral a, Show a) => a -> String #

Formats an integral number, appending a suffix.

tryAndLogIOError :: IO a -> String -> (a -> Result b) -> IO (Result b) #

Try an IO interaction, log errors and unfold as a Result.

withDefaultOnIOError :: a -> IO a -> IO a #

Try an IO interaction and return a default value if the interaction throws an IOError.

lockFile :: FilePath -> IO (Result Fd) #

Attempt, in a non-blocking way, to obtain a lock on a given file; report back success. Returns the file descriptor so that the lock can be released by closing

type FStat = (EpochTime, FileID, FileOffset) #

File stat identifier.

nullFStat :: FStat #

Null FStat value.

getFStat :: FilePath -> IO FStat #

Wrapper over buildFileStatus. This reads the data from the filesystem and then builds our cache structure.

getFStatSafe :: FilePath -> IO FStat #

Safe version of getFStat, that ignores IOErrors.

needsReload :: FStat -> FilePath -> IO (Maybe FStat) #

Check if the file needs reloading

watchFile :: Eq a => FilePath -> Int -> a -> IO a -> IO a #

Within the given timeout (in seconds), wait for for the output of the given method to change and return the new value; make use of the promise that the method will only change its value, if the given file changes on disk. If the file does not exist on disk, return immediately.

watchFileBy :: FilePath -> Int -> (a -> Bool) -> IO a -> IO a #

Within the given timeout (in seconds), wait for for the output of the given method to satisfy a given predicate and return the new value; make use of the promise that the method will only change its value, if the given file changes on disk. If the file does not exist on disk, return immediately.

safeRenameFile :: FilePermissions -> FilePath -> FilePath -> IO (Result ()) #

Safely rename a file, creating the target directory, if needed.

data FilePermissions #

Type describing ownership and permissions of newly generated directories and files. All parameters are optional, with nothing meaning that the default value should be left untouched.

Constructors

FilePermissions 

Fields

ensurePermissions :: FilePath -> FilePermissions -> IO (Result ()) #

Ensure that a given file or directory has the permissions, and possibly ownerships, as required.

ordNub :: Ord a => [a] -> [a] #

Removes duplicates, preserving order.

isSubsequenceOf :: Eq a => [a] -> [a] -> Bool #

frequency :: Ord t => [t] -> [(Int, t)] #

Returns a list of tuples of elements and the number of times they occur in a list