summaryrefslogtreecommitdiff
path: root/lib
diff options
context:
space:
mode:
Diffstat (limited to 'lib')
-rw-r--r--lib/puppet/parser/functions/getvar.rb23
-rw-r--r--lib/puppet/parser/functions/has_key.rb27
-rw-r--r--lib/puppet/parser/functions/loadyaml.rb20
-rw-r--r--lib/puppet/parser/functions/merge.rb30
-rw-r--r--lib/puppet/parser/functions/validate_array.rb35
-rw-r--r--lib/puppet/parser/functions/validate_bool.rb39
-rw-r--r--lib/puppet/parser/functions/validate_hash.rb37
-rw-r--r--lib/puppet/parser/functions/validate_re.rb35
-rw-r--r--lib/puppet/parser/functions/validate_string.rb35
-rw-r--r--lib/puppet/provider/whole_line/ruby.rb15
-rw-r--r--lib/puppet/type/anchor.rb32
-rw-r--r--lib/puppet/type/whole_line.rb44
12 files changed, 372 insertions, 0 deletions
diff --git a/lib/puppet/parser/functions/getvar.rb b/lib/puppet/parser/functions/getvar.rb
new file mode 100644
index 0000000..ffd774d
--- /dev/null
+++ b/lib/puppet/parser/functions/getvar.rb
@@ -0,0 +1,23 @@
+module Puppet::Parser::Functions
+
+ newfunction(:getvar, :type => :rvalue, :doc => <<-'ENDHEREDOC') do |args|
+ Lookup a variable in a remote namespace.
+
+ For example:
+
+ $foo = getvar('site::data::foo')
+
+ This is useful if the namespace itself is stored in a string:
+
+ $bar = getvar("${datalocation}::bar")
+ ENDHEREDOC
+
+ unless args.length == 1
+ raise Puppet::ParseError, ("getvar(): wrong number of arguments (#{args.length}; must be 1)")
+ end
+
+ self.lookupvar("#{args[0]}")
+
+ end
+
+end
diff --git a/lib/puppet/parser/functions/has_key.rb b/lib/puppet/parser/functions/has_key.rb
new file mode 100644
index 0000000..9c1c4c3
--- /dev/null
+++ b/lib/puppet/parser/functions/has_key.rb
@@ -0,0 +1,27 @@
+module Puppet::Parser::Functions
+
+ newfunction(:has_key, :type => :rvalue, :doc => <<-'ENDHEREDOC') do |args|
+ determine if a hash has a certain key value.
+
+ Example:
+ $my_hash = {'key_one' => 'value_one'}
+ if has_key($my_hash, 'key_two') {
+ notice('we will not reach here')
+ }
+ if has_key($my_hash, 'key_one') {
+ notice('this will be printed')
+ }
+
+ ENDHEREDOC
+
+ unless args.length == 2
+ raise Puppet::ParseError, ("has_key(): wrong number of arguments (#{args.length}; must be 2)")
+ end
+ unless args[0].is_a?(Hash)
+ raise Puppet::ParseError, "has_key(): expects the first argument to be a hash, got #{args[0].inspect} which is of type #{args[0].class}"
+ end
+ args[0].has_key?(args[1])
+
+ end
+
+end
diff --git a/lib/puppet/parser/functions/loadyaml.rb b/lib/puppet/parser/functions/loadyaml.rb
new file mode 100644
index 0000000..0f16f69
--- /dev/null
+++ b/lib/puppet/parser/functions/loadyaml.rb
@@ -0,0 +1,20 @@
+module Puppet::Parser::Functions
+
+ newfunction(:loadyaml, :type => :rvalue, :doc => <<-'ENDHEREDOC') do |args|
+ Load a YAML file and return the data if it contains an Array, String, or Hash
+ as a Puppet variable.
+
+ For example:
+
+ $myhash = loadyaml('/etc/puppet/data/myhash.yaml')
+ ENDHEREDOC
+
+ unless args.length == 1
+ raise Puppet::ParseError, ("loadyaml(): wrong number of arguments (#{args.length}; must be 1)")
+ end
+
+ YAML.load_file(args[0])
+
+ end
+
+end
diff --git a/lib/puppet/parser/functions/merge.rb b/lib/puppet/parser/functions/merge.rb
new file mode 100644
index 0000000..d2dc0f9
--- /dev/null
+++ b/lib/puppet/parser/functions/merge.rb
@@ -0,0 +1,30 @@
+module Puppet::Parser::Functions
+ newfunction(:merge, :type => :rvalue, :doc => <<-'ENDHEREDOC') do |args|
+ Merges two or more hashes together and returns the resulting hash.
+
+ For example:
+
+ $hash1 = {'one' => 1, 'two', => 2}
+ $hash1 = {'two' => 2, 'three', => 2}
+ $merged_hash = merge($hash1, $hash2)
+ # merged_hash = {'one' => 1, 'two' => 2, 'three' => 2}
+
+ ENDHEREDOC
+
+ if args.length < 2
+ raise Puppet::ParseError, ("merge(): wrong number of arguments (#{args.length}; must be at least 2)")
+ end
+
+ # The hash we accumulate into
+ accumulator = Hash.new
+ # Merge into the accumulator hash
+ args.each do |arg|
+ unless arg.is_a?(Hash)
+ raise Puppet::ParseError, "merge: unexpected argument type #{arg.class}, only expects hash arguments"
+ end
+ accumulator.merge!(arg)
+ end
+ # Return the fully merged hash
+ accumulator
+ end
+end
diff --git a/lib/puppet/parser/functions/validate_array.rb b/lib/puppet/parser/functions/validate_array.rb
new file mode 100644
index 0000000..a7a7165
--- /dev/null
+++ b/lib/puppet/parser/functions/validate_array.rb
@@ -0,0 +1,35 @@
+module Puppet::Parser::Functions
+
+ newfunction(:validate_array, :doc => <<-'ENDHEREDOC') do |args|
+ Validate all passed values are a Array data structure
+ value does not pass the check.
+
+ Example:
+
+ These values validate
+
+ $my_array = [ 'one', 'two' ]
+ validate_array($my_array)
+
+ These values do NOT validate
+
+ validate_array(true)
+ validate_array('some_string')
+ $undefined = undef
+ validate_array($undefined)
+
+ ENDHEREDOC
+
+ unless args.length > 0 then
+ raise Puppet::ParseError, ("validate_array(): wrong number of arguments (#{args.length}; must be > 0)")
+ end
+
+ args.each do |arg|
+ unless arg.is_a?(Array)
+ raise Puppet::ParseError, ("#{arg.inspect} is not an Array. It looks to be a #{arg.class}")
+ end
+ end
+
+ end
+
+end
diff --git a/lib/puppet/parser/functions/validate_bool.rb b/lib/puppet/parser/functions/validate_bool.rb
new file mode 100644
index 0000000..49e6378
--- /dev/null
+++ b/lib/puppet/parser/functions/validate_bool.rb
@@ -0,0 +1,39 @@
+module Puppet::Parser::Functions
+
+ newfunction(:validate_bool, :doc => <<-'ENDHEREDOC') do |args|
+ Validate all passed values are true or false. Abort catalog compilation if the
+ value does not pass the check.
+
+ Example:
+
+ These booleans validate
+
+ $iamtrue = true
+ validate_bool(true)
+ validate_bool(true, true, false, $iamtrue)
+
+ These strings do NOT validate and will abort catalog compilation
+
+ $some_array = [ true ]
+ validate_bool("false")
+ validate_bool("true")
+ validate_bool($some_array)
+
+ * Jeff McCune <jeff@puppetlabs.com>
+ * Dan Bode <dan@puppetlabs.com>
+
+ ENDHEREDOC
+
+ unless args.length > 0 then
+ raise Puppet::ParseError, ("validate_bool(): wrong number of arguments (#{args.length}; must be > 0)")
+ end
+
+ args.each do |arg|
+ unless (arg.is_a?(TrueClass) || arg.is_a?(FalseClass))
+ raise Puppet::ParseError, ("#{arg.inspect} is not a boolean. It looks to be a #{arg.class}")
+ end
+ end
+
+ end
+
+end
diff --git a/lib/puppet/parser/functions/validate_hash.rb b/lib/puppet/parser/functions/validate_hash.rb
new file mode 100644
index 0000000..1443318
--- /dev/null
+++ b/lib/puppet/parser/functions/validate_hash.rb
@@ -0,0 +1,37 @@
+module Puppet::Parser::Functions
+
+ newfunction(:validate_hash, :doc => <<-'ENDHEREDOC') do |args|
+ Validate all passed values are a Hash data structure
+ value does not pass the check.
+
+ Example:
+
+ These values validate
+
+ $my_hash = { 'one' => 'two' }
+ validate_hash($my_hash)
+
+ These values do NOT validate
+
+ validate_hash(true)
+ validate_hash('some_string')
+ $undefined = undef
+ validate_hash($undefined)
+
+ * Jeff McCune <jeff@puppetlabs.com>
+
+ ENDHEREDOC
+
+ unless args.length > 0 then
+ raise Puppet::ParseError, ("validate_hash(): wrong number of arguments (#{args.length}; must be > 0)")
+ end
+
+ args.each do |arg|
+ unless arg.is_a?(Hash)
+ raise Puppet::ParseError, ("#{arg.inspect} is not a Hash. It looks to be a #{arg.class}")
+ end
+ end
+
+ end
+
+end
diff --git a/lib/puppet/parser/functions/validate_re.rb b/lib/puppet/parser/functions/validate_re.rb
new file mode 100644
index 0000000..583f26a
--- /dev/null
+++ b/lib/puppet/parser/functions/validate_re.rb
@@ -0,0 +1,35 @@
+module Puppet::Parser::Functions
+
+ newfunction(:validate_re, :doc => <<-'ENDHEREDOC') do |args|
+ Perform simple validation of a string against a regular expression. The second
+ argument of the function should be a string regular expression (without the //'s)
+ or an array of regular expressions. If none of the regular expressions in the array
+ match the string passed in, then an exception will be raised.
+
+ Example:
+
+ These strings validate against the regular expressions
+
+ validate_re('one', '^one$')
+ validate_re('one', [ '^one', '^two' ])
+
+ These strings do NOT validate
+
+ validate_re('one', [ '^two', '^three' ])
+
+ Jeff McCune <jeff@puppetlabs.com>
+
+ ENDHEREDOC
+ if args.length != 2 then
+ raise Puppet::ParseError, ("validate_re(): wrong number of arguments (#{args.length}; must be 2)")
+ end
+
+ msg = "validate_re(): #{args[0].inspect} does not match #{args[1].inspect}"
+
+ raise Puppet::ParseError, (msg) unless args[1].any? do |re_str|
+ args[0] =~ Regexp.compile(re_str)
+ end
+
+ end
+
+end
diff --git a/lib/puppet/parser/functions/validate_string.rb b/lib/puppet/parser/functions/validate_string.rb
new file mode 100644
index 0000000..d0e1376
--- /dev/null
+++ b/lib/puppet/parser/functions/validate_string.rb
@@ -0,0 +1,35 @@
+module Puppet::Parser::Functions
+
+ newfunction(:validate_string, :doc => <<-'ENDHEREDOC') do |args|
+ Validate all passed values are a string data structure
+ value does not pass the check.
+
+ Example:
+
+ These values validate
+
+ $my_string = "one two"
+ validate_string($my_string)
+
+ These values do NOT validate
+
+ validate_string(true)
+ validate_string([ 'some', 'array' ])
+ $undefined = undef
+ validate_string($undefined)
+
+ ENDHEREDOC
+
+ unless args.length > 0 then
+ raise Puppet::ParseError, ("validate_string(): wrong number of arguments (#{args.length}; must be > 0)")
+ end
+
+ args.each do |arg|
+ unless arg.is_a?(String)
+ raise Puppet::ParseError, ("#{arg.inspect} is not a string. It looks to be a #{arg.class}")
+ end
+ end
+
+ end
+
+end
diff --git a/lib/puppet/provider/whole_line/ruby.rb b/lib/puppet/provider/whole_line/ruby.rb
new file mode 100644
index 0000000..156f255
--- /dev/null
+++ b/lib/puppet/provider/whole_line/ruby.rb
@@ -0,0 +1,15 @@
+Puppet::Type.type(:whole_line).provide(:ruby) do
+
+ def exists?
+ File.readlines(resource[:path]).find do |line|
+ line.chomp == resource[:line].chomp
+ end
+ end
+
+ def create
+ File.open(resource[:path], 'a') do |fh|
+ fh.puts resource[:line]
+ end
+ end
+
+end
diff --git a/lib/puppet/type/anchor.rb b/lib/puppet/type/anchor.rb
new file mode 100644
index 0000000..0c28b1c
--- /dev/null
+++ b/lib/puppet/type/anchor.rb
@@ -0,0 +1,32 @@
+Puppet::Type.newtype(:anchor) do
+ desc <<-'ENDOFDESC'
+ A simple resource type intended to be used as an anchor in a composite class.
+
+ class ntp {
+ class { 'ntp::package': }
+ -> class { 'ntp::config': }
+ -> class { 'ntp::service': }
+
+ # These two resources "anchor" the composed classes
+ # such that the end user may use "require" and "before"
+ # relationships with Class['ntp']
+ anchor { 'ntp::begin': } -> class { 'ntp::package': }
+ class { 'ntp::service': } -> anchor { 'ntp::end': }
+ }
+
+ This resource allows all of the classes in the ntp module to be contained
+ within the ntp class from a dependency management point of view.
+
+ This allows the end user of the ntp module to establish require and before
+ relationships easily:
+
+ class { 'ntp': } -> class { 'mcollective': }
+ class { 'mcollective': } -> class { 'ntp': }
+
+ ENDOFDESC
+
+ newparam :name do
+ desc "The name of the anchor resource."
+ end
+
+end
diff --git a/lib/puppet/type/whole_line.rb b/lib/puppet/type/whole_line.rb
new file mode 100644
index 0000000..f231602
--- /dev/null
+++ b/lib/puppet/type/whole_line.rb
@@ -0,0 +1,44 @@
+Puppet::Type.newtype(:whole_line) do
+
+ desc <<-EOT
+ Type that can append whole a line to a file if it does not already contain it.
+
+ Example:
+
+ whole_line { 'sudo_rule':
+ path => '/etc/sudoers',
+ line => '%admin ALL=(ALL) ALL',
+ }
+
+ EOT
+
+ ensurable do
+ defaultto :present
+ newvalue(:present) do
+ provider.create
+ end
+ end
+
+ newparam(:name, :namevar => true) do
+ desc 'arbitrary name used as identity'
+ end
+
+ newparam(:line) do
+ desc 'The line to be appended to the path.'
+ end
+
+ newparam(:path) do
+ desc 'File to possibly append a line to.'
+ validate do |value|
+ unless (Puppet.features.posix? and value =~ /^\//) or (Puppet.features.microsoft_windows? and (value =~ /^.:\// or value =~ /^\/\/[^\/]+\/[^\/]+/))
+ raise(Puppet::Error, "File paths must be fully qualified, not '#{value}'")
+ end
+ end
+ end
+
+ validate do
+ unless self[:line] and self[:path]
+ raise(Puppet::Error, "Both line and path are required attributes")
+ end
+ end
+end