summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--Gemfile2
-rw-r--r--README.markdown25
-rw-r--r--lib/facter/puppet_settings.rb8
-rw-r--r--lib/puppet/parser/functions/ensure_packages.rb2
-rw-r--r--lib/puppet/parser/functions/fqdn_uuid.rb92
-rw-r--r--lib/puppet/parser/functions/pry.rb30
-rw-r--r--metadata.json3
-rw-r--r--spec/aliases/hash_spec.rb32
-rw-r--r--spec/fixtures/test/manifests/ensure_resources.pp4
-rw-r--r--spec/fixtures/test/manifests/hash.pp8
-rwxr-xr-xspec/functions/ensure_packages_spec.rb8
-rw-r--r--spec/functions/fqdn_uuid_spec.rb14
-rw-r--r--spec/unit/ensure_resources_spec.rb22
-rw-r--r--types/compat/hash.pp2
14 files changed, 249 insertions, 3 deletions
diff --git a/Gemfile b/Gemfile
index 8a568f6..3d46720 100644
--- a/Gemfile
+++ b/Gemfile
@@ -47,6 +47,8 @@ group :development do
gem 'rubocop-rspec', '~> 1.6', :require => false if Gem::Version.new(RUBY_VERSION.dup) >= Gem::Version.new('2.3.0')
gem 'pry', :require => false
gem 'json_pure', '<= 2.0.1', :require => false if Gem::Version.new(RUBY_VERSION.dup) < Gem::Version.new('2.0.0')
+ gem 'fast_gettext', '1.1.0', :require => false if Gem::Version.new(RUBY_VERSION.dup) < Gem::Version.new('2.1.0')
+ gem 'fast_gettext', :require => false if Gem::Version.new(RUBY_VERSION.dup) >= Gem::Version.new('2.1.0')
end
group :system_tests do
diff --git a/README.markdown b/README.markdown
index a6b7bda..a4c30b5 100644
--- a/README.markdown
+++ b/README.markdown
@@ -578,6 +578,15 @@ fqdn_rotate([1, 2, 3], 'custom seed')
*Type*: rvalue.
+#### `fqdn_uuid`
+
+Returns a rfc4122 valid version 5 UUID based on an FQDN string under the DNS namespace
+
+ * fqdn_uuid('puppetlabs.com') returns '9c70320f-6815-5fc5-ab0f-debe68bf764c'
+ * fqdn_uuid('google.com') returns '64ee70a4-8cc1-5d25-abf2-dea6c79a09c8'
+
+*Type*: rvalue.
+
#### `get_module_path`
Returns the absolute path of the specified module for the current environment.
@@ -935,6 +944,20 @@ For example:
*Type*: rvalue.
+#### `pry`
+
+This function invokes a pry debugging session in the current scope object. This is useful for debugging manifest code at specific points during a compilation. Should only be used when running `puppet apply` or running a puppet master in the foreground. This requires the `pry` gem to be installed in puppet's rubygems.
+
+*Examples:*
+```puppet
+pry()
+```
+Once in a pry session, some interesting commands:
+
+* Run `catalog` to see the contents currently compiling catalog
+* Run `cd catalog` and `ls` to see catalog methods and instance variables
+* Run `@resource_table` to see the current catalog resource table
+
#### `assert_private`
Sets the current class or definition as private. Calling the class or definition from outside the current module will fail.
@@ -1056,7 +1079,7 @@ Returns a new string where runs of the same character that occur in this set are
#### `str2bool`
-Converts certain strings to a boolean. This attempts to convert strings that contain the values '1', 't', 'y', or 'yes' to true. Strings that contain values '0', 'f', 'n', or 'no', or that are an empty string or undefined are converted to false. Any other value causes an error. *Type*: rvalue.
+Converts certain strings to a boolean. This attempts to convert strings that contain the values '1', 'true', 't', 'y', or 'yes' to true. Strings that contain values '0', 'false', 'f', 'n', or 'no', or that are an empty string or undefined are converted to false. Any other value causes an error. These checks are case insensitive. *Type*: rvalue.
#### `str2saltedsha512`
diff --git a/lib/facter/puppet_settings.rb b/lib/facter/puppet_settings.rb
index e6a066a..eac9e97 100644
--- a/lib/facter/puppet_settings.rb
+++ b/lib/facter/puppet_settings.rb
@@ -33,3 +33,11 @@ Facter.add(:puppet_environmentpath) do
end
end
end
+
+Facter.add(:puppet_server) do
+ setcode do
+ Facter::Util::PuppetSettings.with_puppet do
+ Puppet[:server]
+ end
+ end
+end
diff --git a/lib/puppet/parser/functions/ensure_packages.rb b/lib/puppet/parser/functions/ensure_packages.rb
index 532b702..439af1e 100644
--- a/lib/puppet/parser/functions/ensure_packages.rb
+++ b/lib/puppet/parser/functions/ensure_packages.rb
@@ -25,7 +25,7 @@ third argument to the ensure_resource() function.
end
Puppet::Parser::Functions.function(:ensure_resources)
- function_ensure_resources(['package', Hash(arguments[0]), defaults ])
+ function_ensure_resources(['package', arguments[0].dup, defaults ])
else
packages = Array(arguments[0])
diff --git a/lib/puppet/parser/functions/fqdn_uuid.rb b/lib/puppet/parser/functions/fqdn_uuid.rb
new file mode 100644
index 0000000..30205d0
--- /dev/null
+++ b/lib/puppet/parser/functions/fqdn_uuid.rb
@@ -0,0 +1,92 @@
+require 'digest/sha1'
+
+module Puppet::Parser::Functions
+ newfunction(:fqdn_uuid, :type => :rvalue, :doc => <<-END) do |args|
+
+ Creates a UUID based on a given string, assumed to be the FQDN
+
+ For example, to generate a UUID based on the FQDN of a system:
+
+ Usage:
+
+ $uuid = fqdn_uuid($::fqdn)
+
+ The generated UUID will be the same for the given hostname
+
+ The resulting UUID is returned on the form:
+
+ 1d839dea-5e10-5243-88eb-e66815bd7d5c
+
+ (u.e. without any curly braces.)
+
+ The generated UUID is a version 5 UUID with the V5 DNS namespace:
+
+ 6ba7b810-9dad-11d1-80b4-00c04fd430c8
+
+ This only supports a the V5 SHA-1 hash, using the DNS namespace.
+
+ Please consult http://www.ietf.org/rfc/rfc4122.txt for the details on
+ UUID generation and example implementation.
+
+ No verification is present at the moment as whether the domain name given
+ is in fact a correct fully-qualified domain name. Therefore any arbitrary
+ string and/or alpha-numeric value can subside for a domain name.
+ EOS
+
+ END
+
+ if args.length == 0
+ raise(ArgumentError, "fqdn_uuid: No arguments given")
+ elsif args.length == 1
+ fqdn = args[0]
+ else
+ raise(ArgumentError, "fqdn_uuid: Too many arguments given (#{args.length})")
+ end
+
+ # Code lovingly taken from
+ # https://github.com/puppetlabs/marionette-collective/blob/master/lib/mcollective/ssl.rb
+
+ # This is the UUID version 5 type DNS name space which is as follows:
+ #
+ # 6ba7b810-9dad-11d1-80b4-00c04fd430c8
+ #
+ uuid_name_space_dns = [0x6b,
+ 0xa7,
+ 0xb8,
+ 0x10,
+ 0x9d,
+ 0xad,
+ 0x11,
+ 0xd1,
+ 0x80,
+ 0xb4,
+ 0x00,
+ 0xc0,
+ 0x4f,
+ 0xd4,
+ 0x30,
+ 0xc8
+ ].map {|b| b.chr}.join
+
+ sha1 = Digest::SHA1.new
+ sha1.update(uuid_name_space_dns)
+ sha1.update(fqdn)
+
+ # first 16 bytes..
+ bytes = sha1.digest[0, 16].bytes.to_a
+
+ # version 5 adjustments
+ bytes[6] &= 0x0f
+ bytes[6] |= 0x50
+
+ # variant is DCE 1.1
+ bytes[8] &= 0x3f
+ bytes[8] |= 0x80
+
+ bytes = [4, 2, 2, 2, 6].collect do |i|
+ bytes.slice!(0, i).pack('C*').unpack('H*')
+ end
+
+ bytes.join('-')
+ end
+end
diff --git a/lib/puppet/parser/functions/pry.rb b/lib/puppet/parser/functions/pry.rb
new file mode 100644
index 0000000..c18ef7e
--- /dev/null
+++ b/lib/puppet/parser/functions/pry.rb
@@ -0,0 +1,30 @@
+#
+# pry.rb
+#
+
+module Puppet::Parser::Functions
+ newfunction(:pry, :type => :statement, :doc => <<-EOS
+This function invokes a pry debugging session in the current scope object. This is useful for debugging manifest code at specific points during a compilation.
+
+*Examples:*
+
+ pry()
+ EOS
+ ) do |arguments|
+ begin
+ require 'pry'
+ rescue LoadError
+ raise(Puppet::Error, "pry(): Requires the 'pry' rubygem to use, but it was not found")
+ end
+ #
+ ## Run `catalog` to see the contents currently compiling catalog
+ ## Run `cd catalog` and `ls` to see catalog methods and instance variables
+ ## Run `@resource_table` to see the current catalog resource table
+ #
+ if $stdout.isatty
+ binding.pry # rubocop:disable Lint/Debugger
+ else
+ Puppet.warning 'pry(): cowardly refusing to start the debugger on a daemonized master'
+ end
+ end
+end
diff --git a/metadata.json b/metadata.json
index a5a4705..b697da0 100644
--- a/metadata.json
+++ b/metadata.json
@@ -65,7 +65,8 @@
"operatingsystemrelease": [
"10.04",
"12.04",
- "14.04"
+ "14.04",
+ "16.06"
]
},
{
diff --git a/spec/aliases/hash_spec.rb b/spec/aliases/hash_spec.rb
new file mode 100644
index 0000000..e10a04b
--- /dev/null
+++ b/spec/aliases/hash_spec.rb
@@ -0,0 +1,32 @@
+require 'spec_helper'
+
+if Puppet.version.to_f >= 4.5
+ describe 'test::hash', type: :class do
+ describe 'accepts hashes' do
+ [
+ {},
+ {'one' => "two"},
+ {'wan' => 3},
+ {'001' => "helly"},
+ ].each do |value|
+ describe value.inspect do
+ let(:params) {{ value: value }}
+ it { is_expected.to compile }
+ end
+ end
+ end
+ describe 'rejects other values' do
+ [
+ '',
+ 'one',
+ '1',
+ [],
+ ].each do |value|
+ describe value.inspect do
+ let(:params) {{ value: value }}
+ it { is_expected.to compile.and_raise_error(/parameter 'value' expects a Stdlib::Compat::Hash/) }
+ end
+ end
+ end
+ end
+end
diff --git a/spec/fixtures/test/manifests/ensure_resources.pp b/spec/fixtures/test/manifests/ensure_resources.pp
new file mode 100644
index 0000000..bd26002
--- /dev/null
+++ b/spec/fixtures/test/manifests/ensure_resources.pp
@@ -0,0 +1,4 @@
+# A helper class to test the ensure_resources function
+class test::ensure_resources($resource_type, $title_hash, $attributes_hash) {
+ ensure_resources($resource_type, $title_hash, $attributes_hash)
+}
diff --git a/spec/fixtures/test/manifests/hash.pp b/spec/fixtures/test/manifests/hash.pp
new file mode 100644
index 0000000..c243570
--- /dev/null
+++ b/spec/fixtures/test/manifests/hash.pp
@@ -0,0 +1,8 @@
+# Class to test the Stdlib::Compat::Hash type alias
+class test::hash(
+ Stdlib::Compat::Hash $value,
+ ) {
+
+ notice("Success")
+
+}
diff --git a/spec/functions/ensure_packages_spec.rb b/spec/functions/ensure_packages_spec.rb
index c824732..5d97684 100755
--- a/spec/functions/ensure_packages_spec.rb
+++ b/spec/functions/ensure_packages_spec.rb
@@ -33,4 +33,12 @@ describe 'ensure_packages' do
it { expect(lambda { catalogue }).to contain_package('facter').with_ensure('present').with_provider("gem") }
end
end
+
+ context 'given hash of packages' do
+ before { subject.call([{"foo" => { "provider" => "rpm" }, "bar" => { "provider" => "gem" }}, { "ensure" => "present"}]) }
+
+ # this lambda is required due to strangeness within rspec-puppet's expectation handling
+ it { expect(lambda { catalogue }).to contain_package('foo').with({'provider' => 'rpm', 'ensure' => 'present'}) }
+ it { expect(lambda { catalogue }).to contain_package('bar').with({'provider' => 'gem', 'ensure' => 'present'}) }
+ end
end
diff --git a/spec/functions/fqdn_uuid_spec.rb b/spec/functions/fqdn_uuid_spec.rb
new file mode 100644
index 0000000..a2d1618
--- /dev/null
+++ b/spec/functions/fqdn_uuid_spec.rb
@@ -0,0 +1,14 @@
+require 'spec_helper'
+
+describe 'fqdn_uuid' do
+
+ context "Invalid parameters" do
+ it { should run.with_params().and_raise_error(ArgumentError, /No arguments given$/) }
+ end
+
+ context "given string" do
+ it { should run.with_params('puppetlabs.com').and_return('9c70320f-6815-5fc5-ab0f-debe68bf764c') }
+ it { should run.with_params('google.com').and_return('64ee70a4-8cc1-5d25-abf2-dea6c79a09c8') }
+ end
+
+end
diff --git a/spec/unit/ensure_resources_spec.rb b/spec/unit/ensure_resources_spec.rb
new file mode 100644
index 0000000..aea723e
--- /dev/null
+++ b/spec/unit/ensure_resources_spec.rb
@@ -0,0 +1,22 @@
+require 'spec_helper'
+
+describe 'test::ensure_resources', type: :class do
+ let(:params) {{ resource_type: 'user', title_hash: title_param, attributes_hash: {'ensure' => 'present'} }}
+
+ describe 'given a title hash of multiple resources' do
+
+ let(:title_param) { {'dan' => { 'gid' => 'mygroup', 'uid' => '600' }, 'alex' => { 'gid' => 'mygroup', 'uid' => '700'}} }
+
+ it { is_expected.to compile }
+ it { is_expected.to contain_user('dan').with({ 'gid' => 'mygroup', 'uid' => '600', 'ensure' => 'present'}) }
+ it { is_expected.to contain_user('alex').with({ 'gid' => 'mygroup', 'uid' => '700', 'ensure' => 'present'}) }
+ end
+
+ describe 'given a title hash of a single resource' do
+
+ let(:title_param) { {'dan' => { 'gid' => 'mygroup', 'uid' => '600' }} }
+
+ it { is_expected.to compile }
+ it { is_expected.to contain_user('dan').with({ 'gid' => 'mygroup', 'uid' => '600', 'ensure' => 'present'}) }
+ end
+end
diff --git a/types/compat/hash.pp b/types/compat/hash.pp
new file mode 100644
index 0000000..e84a10b
--- /dev/null
+++ b/types/compat/hash.pp
@@ -0,0 +1,2 @@
+# Emulate the is_hash and validate_hash functions
+type Stdlib::Compat::Hash = Hash[Any, Any]