Version information
Start using this module
Add this module to your Puppetfile:
mod 'arusso-oski', '0.0.9'
Learn more about managing modules with a PuppetfileDocumentation
Oski Function Library
This module provides some useful library functions not found in stdlib
Facts
osplatform
Contains the fact osplatform, which currently only supports RedHat distros. For RHEL6 deriviatives, fact would contain 'el6', for RHEL5 deriviatives fact would contain 'el5'.
ssh_dsa_fp
The host's SSH DSA fingerprint
ssh_rsa_fp
The host's SSH RSA fingerprint
hostname_s#
Splits a hostname by the '-' character, and presents each segment as a separate fact. For example, hostname 'node-blah-prod' would be:
- hostname_s1: node
- hostname_s2: blah
- hostname_s3: prod
Functions
any2bool
Given a string or boolean that looks like a boolean, it converts it to a bool.
Arguments:
- value : value to convert to a boolean
Example:
any2bool('f')
any2bool(true)
any2boold('1')
any2bool('')
any2bool('arusso')
Would Result In:
false
true
true
false
Parse Error
Truth Table:
Input | Output |
---|---|
'1', 't', 'y', 'true', 'yes' | true |
'0', 'f', 'n', 'false', 'no' | false |
'', 'undef', 'undefined' | false |
default | Error |
array_difference
Given two arrays, lhs and rhs, removes the entries in rhs from lhs.
Arguments:
- lhs : an array to have values subtracted from
- rhs : an array of values to subtract from $lhs
Example:
array_difference( [ '1', '2', '3' ], [ '1', '3', '5' ])
Would result in:
[ '2' ]
array_do
Takes an array of values, and iterates over the array while executing the function 'func' against all of the values. If passed a string/array of additional values, those will be appended to the iterated value as the parameter array to the function.
Arguments:
- values : an array of values to iterate over
- func : function to execute against the elements of $values
- params : additional parameters to pass to the function
Example:
$usernames = [ 'tom', 'jerry', 'bruno', 'myrtle' ]
array_do( $usernames, 'validate_re', '^(tom|jerry|bruno)$' )
Would result in a parse error, since myrtle would not match the validat_re call:
validate_re('myrtle','^(tom|jerry|bruno)$')
array_do_r
Takes an array of values, and iterates over the array while executing the function 'func' against all of the values. If passed a string/array of additional values, those will be appended to the iterated value as the parameter array to the function.
Arguments:
- values: an array of values to iterate over
- func : function to execute against the elemnts of $values
- params : additional parameters to pass the function
This function returns the output in a hash, with each iterated element as the key, and the output as the value.
Example:
$ips = [ '127.0.0.1', '::1', '192.168.0.0/24' ]
$out = array_do_r( $ips, 'is_ipv6' )
delete_key_with_value( $out, false )
Would result in:
{ '::1' => true }
array_intersect
Given two arrays, returns all elements that are in common to both arrays
Arguments:
- lhs : an array of values
- rhs : an array of values
Example:
$incommon = array_intersect( [ '1', '2', '3' ], [ '1', '5', '8' ])
Would result in:
[ '1' ]
bool2str
Converts a boolean to string
delete_key_with_value
When passed a hash table, and a match string/regex/boolean (or an array of), this function will delete any key-value pairs it matches against.
Arguments:
- hash : hash table of values to work with
- value : the value of the keys to delete
is_ipv4
Returns true if the string passed is an ipv4 address/network. Returns false otherwise.
Arguments:
- string value
is_ipv6
Returns true if the string passed is an ipv6 address/network. Returns false otherwise.
Arguments:
- string value
lead
Given an integer value and width, ensures that integer value is at least the provided width, inserting leading zeroes as necessary
Arguments:
- value : Integer value to add leading zeroes
- width : Minimum width of integer
Example:
lead( 4, 3 )
lead( 22, 4)
Would result in:
'004'
'0022'
num2str
Converts any numeric type to a string. If a string is formatted like a number.
Arguments:
- num : numeric value
Example:
num2str( 4 )
num2str( '-6' )
num2str( -1.23 )
num2str( '2.56' )
Would result in:
'4'
'-6'
'-1.23'
'2.56'
params_lookup
This function looks up the value of a variable in multiple locations, providing a simple way to ascertain a value from any of those locations. The function currently looks at the following locations, in order, returning the first one found.
Note: This function is deprecated, and will be removed in later versions of this module
Arguments:
- varname : The variable name we want to define
- is_global : Should we look for this variable at the top level (ie. $::varname)
- default : If no value is found, what should we set the default
-
Top-Scope Variable ($::class_[subclass_]*_varname)
Useful for ENC support
-
Params Value ( $class::params::varname )
This doesn't normally work in the top-level class, unless the class inherits the params class, which is not a recommended practice.
-
Top-Scope Module Variable ($::module_varname)
Again, useful for ENCs
-
Top-Scope Global varname ($::varname)
Useful for ENCs and site.pp definitions. Only valid if is_global is set to true
-
Default value
Returns whatever we set as our default
-
Empty string
If no default is set, we'll return an empty string.
similar_elements
Given two arrays, returns a single array with elements that are in common between both. Essentially a logical AND of the two arrays
Note: this function has been deprecated in favor of using array_intersect()
Arguments:
- array_lhs : An array of elements
- array_rhs : An array of elements
Example:
similar_elements(['1','2','9'],['2','3','9'])
Would result in:
[ '2', '9' ]
validate_ip
Given an ipv4 address, validates the address and throws an error if its not a properly formatted address.
Arguments:
- ip : An string containing an ip address in CIDR format
Example:
validate_ip('192.168.0.1/24')
validate_ip('10.0.0.2')
validate_ip('alphabet soup')
validate_ip('2600::1/64')
Would result in:
true
true
false
false
License
See LICENSE file
Copyright
Copyright © 2014 The Regents of the University of California
Contact
Aaron Russo arusso@berkeley.edu
Support
Please log tickets and issues at the Projects site
2014-03-10 Aaron Russo arusso@berkeely.edu - 0.0.9
- Added function num2str
2014-02-28 Aaron Russo arusso@berkeley.edu - 0.0.8
- Added hostname_s# fact
- Cleaned up docs a bit
2013-08-05 Aaron Russo arusso@berkeley.edu - 0.0.7
- Added license and copyright
2013-06-02 Aaron Russo arusso@berkeley.edu - 0.0.6
- Updating to acct for github account rename
2013-05-28 Aaron Russo arusso@berkeley.edu - 0.0.5
- Added bool2str function
2013-05-17 Aaron Russo arusso@berkeley.edu - 0.0.4
- Added array_do and array_do_r as a poor mans loop
2013-05-13 Aaron Russo arusso@berkeley.edu - 0.0.3
- Added is_ipv4, is_ipv6 functions.
- Added ssh_dsa_fp and ssh_rsa_fp facts
2013-05-13 Aaron Russo arusso@berkeley.edu - 0.0.2
- Added array_difference and array_intersect functions
2013-04-29 Aaron Russo arusso@berkeley.edu - 0.0.1
- Initial release
The MIT License (MIT) Copyright (c) 2013 The Regents of the University of California Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.