puppet-debugger 0.10.3 → 0.11.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/.gitlab-ci.yml +16 -74
- data/.rubocop_todo.yml +59 -59
- data/.ruby-version +1 -1
- data/CHANGELOG.md +7 -0
- data/Gemfile +3 -2
- data/lib/plugins/puppet-debugger/input_responders/functions.rb +100 -1
- data/lib/puppet-debugger/cli.rb +1 -2
- data/lib/puppet-debugger/input_responder_plugin.rb +8 -1
- data/lib/puppet-debugger/support.rb +14 -2
- data/lib/puppet-debugger/version.rb +1 -1
- data/puppet-debugger.gemspec +2 -0
- data/spec/fixtures/modules/extlib/.editorconfig +14 -0
- data/spec/fixtures/modules/extlib/.fixtures.yml +3 -0
- data/spec/fixtures/modules/extlib/.github/CONTRIBUTING.md +184 -0
- data/spec/fixtures/modules/extlib/.github/ISSUE_TEMPLATE.md +26 -0
- data/spec/fixtures/modules/extlib/.github/PULL_REQUEST_TEMPLATE.md +20 -0
- data/spec/fixtures/modules/extlib/.gitignore +21 -0
- data/spec/fixtures/modules/extlib/.msync.yml +1 -0
- data/spec/fixtures/modules/extlib/.overcommit.yml +64 -0
- data/spec/fixtures/modules/extlib/.pmtignore +21 -0
- data/spec/fixtures/modules/extlib/.rspec +2 -0
- data/spec/fixtures/modules/extlib/.rspec_parallel +1 -0
- data/spec/fixtures/modules/extlib/.rubocop.yml +545 -0
- data/spec/fixtures/modules/extlib/.sync.yml +12 -0
- data/spec/fixtures/modules/extlib/.travis.yml +47 -0
- data/spec/fixtures/modules/extlib/.yardopts +2 -0
- data/spec/fixtures/modules/extlib/Gemfile +82 -0
- data/spec/fixtures/modules/extlib/Rakefile +82 -0
- data/spec/fixtures/modules/extlib/functions/dir_split.pp +25 -0
- data/spec/fixtures/modules/extlib/functions/file_separator.pp +7 -0
- data/spec/fixtures/modules/extlib/functions/mkdir_p.pp +20 -0
- data/spec/fixtures/modules/extlib/functions/path_join.pp +29 -0
- data/spec/fixtures/modules/extlib/lib/puppet/functions/cache_data.rb +11 -0
- data/spec/fixtures/modules/extlib/lib/puppet/functions/default_content.rb +11 -0
- data/spec/fixtures/modules/extlib/lib/puppet/functions/dump_args.rb +11 -0
- data/spec/fixtures/modules/extlib/lib/puppet/functions/echo.rb +11 -0
- data/spec/fixtures/modules/extlib/lib/puppet/functions/extlib/cache_data.rb +48 -0
- data/spec/fixtures/modules/extlib/lib/puppet/functions/extlib/default_content.rb +37 -0
- data/spec/fixtures/modules/extlib/lib/puppet/functions/extlib/dump_args.rb +20 -0
- data/spec/fixtures/modules/extlib/lib/puppet/functions/extlib/echo.rb +45 -0
- data/spec/fixtures/modules/extlib/lib/puppet/functions/extlib/has_module.rb +33 -0
- data/spec/fixtures/modules/extlib/lib/puppet/functions/extlib/ip_to_cron.rb +39 -0
- data/spec/fixtures/modules/extlib/lib/puppet/functions/extlib/random_password.rb +63 -0
- data/spec/fixtures/modules/extlib/lib/puppet/functions/extlib/resources_deep_merge.rb +87 -0
- data/spec/fixtures/modules/extlib/lib/puppet/functions/extlib/sort_by_version.rb +15 -0
- data/spec/fixtures/modules/extlib/lib/puppet/functions/ip_to_cron.rb +11 -0
- data/spec/fixtures/modules/extlib/lib/puppet/functions/random_password.rb +11 -0
- data/spec/fixtures/modules/extlib/lib/puppet/functions/resources_deep_merge.rb +11 -0
- data/spec/fixtures/modules/extlib/metadata.json +66 -0
- data/spec/input_responder_plugin_spec.rb +1 -1
- data/spec/input_responders/functions_spec.rb +46 -1
- data/spec/spec_helper.rb +5 -5
- data/spec/support_spec.rb +0 -8
- metadata +69 -4
- data/lib/puppet-debugger/support/functions.rb +0 -72
@@ -0,0 +1,82 @@
|
|
1
|
+
require 'puppetlabs_spec_helper/rake_tasks'
|
2
|
+
|
3
|
+
# load optional tasks for releases
|
4
|
+
# only available if gem group releases is installed
|
5
|
+
begin
|
6
|
+
require 'voxpupuli/release/rake_tasks'
|
7
|
+
rescue LoadError
|
8
|
+
end
|
9
|
+
|
10
|
+
PuppetLint.configuration.log_format = '%{path}:%{line}:%{check}:%{KIND}:%{message}'
|
11
|
+
PuppetLint.configuration.absolute_classname_reverse = true
|
12
|
+
|
13
|
+
exclude_paths = %w(
|
14
|
+
pkg/**/*
|
15
|
+
vendor/**/*
|
16
|
+
.vendor/**/*
|
17
|
+
spec/**/*
|
18
|
+
)
|
19
|
+
PuppetLint.configuration.ignore_paths = exclude_paths
|
20
|
+
PuppetSyntax.exclude_paths = exclude_paths
|
21
|
+
|
22
|
+
desc 'Auto-correct puppet-lint offenses'
|
23
|
+
task 'lint:auto_correct' do
|
24
|
+
Rake::Task[:lint_fix].invoke
|
25
|
+
end
|
26
|
+
|
27
|
+
desc 'Run acceptance tests'
|
28
|
+
RSpec::Core::RakeTask.new(:acceptance) do |t|
|
29
|
+
t.pattern = 'spec/acceptance'
|
30
|
+
end
|
31
|
+
|
32
|
+
desc 'Run tests'
|
33
|
+
task test: [:release_checks]
|
34
|
+
|
35
|
+
namespace :check do
|
36
|
+
desc 'Check for trailing whitespace'
|
37
|
+
task :trailing_whitespace do
|
38
|
+
Dir.glob('**/*.md', File::FNM_DOTMATCH).sort.each do |filename|
|
39
|
+
next if filename =~ %r{^((modules|acceptance|\.?vendor|spec/fixtures|pkg)/|REFERENCE.md)}
|
40
|
+
File.foreach(filename).each_with_index do |line, index|
|
41
|
+
if line =~ %r{\s\n$}
|
42
|
+
puts "#{filename} has trailing whitespace on line #{index + 1}"
|
43
|
+
exit 1
|
44
|
+
end
|
45
|
+
end
|
46
|
+
end
|
47
|
+
end
|
48
|
+
end
|
49
|
+
Rake::Task[:release_checks].enhance ['check:trailing_whitespace']
|
50
|
+
|
51
|
+
desc "Run main 'test' task and report merged results to coveralls"
|
52
|
+
task test_with_coveralls: [:test] do
|
53
|
+
if Dir.exist?(File.expand_path('../lib', __FILE__))
|
54
|
+
require 'coveralls/rake/task'
|
55
|
+
Coveralls::RakeTask.new
|
56
|
+
Rake::Task['coveralls:push'].invoke
|
57
|
+
else
|
58
|
+
puts 'Skipping reporting to coveralls. Module has no lib dir'
|
59
|
+
end
|
60
|
+
end
|
61
|
+
|
62
|
+
desc 'Generate REFERENCE.md'
|
63
|
+
task :reference, [:debug, :backtrace] do |t, args|
|
64
|
+
patterns = 'lib/puppet/functions/extlib/*.rb functions/*.pp'
|
65
|
+
Rake::Task['strings:generate:reference'].invoke(patterns, args[:debug], args[:backtrace])
|
66
|
+
end
|
67
|
+
|
68
|
+
begin
|
69
|
+
require 'github_changelog_generator/task'
|
70
|
+
GitHubChangelogGenerator::RakeTask.new :changelog do |config|
|
71
|
+
version = (Blacksmith::Modulefile.new).version
|
72
|
+
config.future_release = "v#{version}" if version =~ /^\d+\.\d+.\d+$/
|
73
|
+
config.header = "# Changelog\n\nAll notable changes to this project will be documented in this file.\nEach new release typically also includes the latest modulesync defaults.\nThese should not affect the functionality of the module."
|
74
|
+
config.exclude_labels = %w{duplicate question invalid wontfix wont-fix modulesync skip-changelog}
|
75
|
+
config.user = 'voxpupuli'
|
76
|
+
metadata_json = File.join(File.dirname(__FILE__), 'metadata.json')
|
77
|
+
metadata = JSON.load(File.read(metadata_json))
|
78
|
+
config.project = metadata['name']
|
79
|
+
end
|
80
|
+
rescue LoadError
|
81
|
+
end
|
82
|
+
# vim: syntax=ruby
|
@@ -0,0 +1,25 @@
|
|
1
|
+
# @summary Splits the given directory or directories into individual paths.
|
2
|
+
#
|
3
|
+
# Use this function when you need to split a absolute path into multiple absolute paths
|
4
|
+
# that all descend from the given path.
|
5
|
+
#
|
6
|
+
# @param dirs [Variant[Stdlib::Absolutepath, Array[Stdlib::Absolutepath]]] - either an absolute path or a array of absolute paths.
|
7
|
+
# @return [Array[String]] - an array of absolute paths after being cut into individual paths.
|
8
|
+
# @example calling the function
|
9
|
+
# extlib::dir_split('/opt/puppetlabs') => ['/opt', '/opt/puppetlabs']
|
10
|
+
function extlib::dir_split(Variant[Stdlib::Absolutepath, Array[Stdlib::Absolutepath]] $dirs) >> Array[String] {
|
11
|
+
$sep = extlib::file_separator()
|
12
|
+
|
13
|
+
$dirs_array = [$dirs].flatten.unique.map | Stdlib::Absolutepath $dir | {
|
14
|
+
$dir.split(shell_escape($sep)).reduce([]) |Array $acc, $value | {
|
15
|
+
$counter = $acc.length - 1
|
16
|
+
$acc_value = ($acc[$counter] =~ Undef) ? { true => '', false => $acc[$counter] }
|
17
|
+
unless empty($value) {
|
18
|
+
$acc + extlib::path_join([$acc_value, $value])
|
19
|
+
} else {
|
20
|
+
$acc
|
21
|
+
}
|
22
|
+
}
|
23
|
+
}
|
24
|
+
$dirs_array.flatten.unique
|
25
|
+
}
|
@@ -0,0 +1,7 @@
|
|
1
|
+
# @summary Returns the os specific file path separator.
|
2
|
+
# @return [String] - The os specific path separator.
|
3
|
+
# @example Example of how to use
|
4
|
+
# extlib::file_separator() => '/'
|
5
|
+
function extlib::file_separator() >> String {
|
6
|
+
($::facts['kernel'] == 'windows' ) ? { true => "\\", false => '/' }
|
7
|
+
}
|
@@ -0,0 +1,20 @@
|
|
1
|
+
# @summary Like the unix command mkdir_p except with puppet code.
|
2
|
+
# This creates file resources for all directories and utilizes the dir_split() function
|
3
|
+
# to get a list of all the descendant directories. You will have no control over any other parameters
|
4
|
+
# for the file resource. If you wish to control the file resources you can use the dir_split() function
|
5
|
+
# and get an array of directories for use in your own code. Please note this does not use an exec resource.
|
6
|
+
#
|
7
|
+
# @param dirs [Variant[Stdlib::Absolutepath, Array[Stdlib::Absolutepath]]] - the path(s) to create
|
8
|
+
# @return [Array[Stdlib::Absolutepath]]
|
9
|
+
# @example How to use
|
10
|
+
# extlib::mkdir_p('/opt/puppetlabs/bin') => ['/opt', '/opt/puppetlabs', '/opt/puppetlabs/bin']
|
11
|
+
# @note splits the given directories into paths that are then created using file resources
|
12
|
+
# @note if you wish to create the directories manually you can use the extlib::dir_split() function in the same manner
|
13
|
+
function extlib::mkdir_p(Variant[Stdlib::Absolutepath, Array[Stdlib::Absolutepath]] $dirs) >> Array[Stdlib::Absolutepath] {
|
14
|
+
$dirs_array = extlib::dir_split($dirs)
|
15
|
+
@file{$dirs_array:
|
16
|
+
ensure => directory,
|
17
|
+
}
|
18
|
+
realize(File[$dirs_array])
|
19
|
+
$dirs_array
|
20
|
+
}
|
@@ -0,0 +1,29 @@
|
|
1
|
+
# @summary Take one or more paths and join them together using the os specific separator.
|
2
|
+
# Because in how windows uses a different separator this function
|
3
|
+
# will format a windows path into a equilivent unix like path. This type of unix like
|
4
|
+
# path will work on windows.
|
5
|
+
#
|
6
|
+
# @param dirs Joins two or more directories by file separator.
|
7
|
+
# @return [Stdlib::Absolutepath] The joined path
|
8
|
+
# @example Joining Unix paths to return `/tmp/test/libs`
|
9
|
+
# extlib::path_join('/tmp', 'test', 'libs')
|
10
|
+
# @example Joining Windows paths to return `/c/test/libs`
|
11
|
+
# extlib::path_join('c:', 'test', 'libs')
|
12
|
+
function extlib::path_join(Array[String] $dirs) >> Stdlib::Absolutepath {
|
13
|
+
$unix_sep = '/'
|
14
|
+
$sep_regex = /\/|\\/
|
15
|
+
$first_value = $dirs[0]
|
16
|
+
# when first value is absolute path, append all other elements
|
17
|
+
# by breaking the path into pieces first, then joining
|
18
|
+
if $first_value =~ Stdlib::Absolutepath {
|
19
|
+
$fixed_dirs = $first_value.split($sep_regex) + $dirs.delete($first_value)
|
20
|
+
} else {
|
21
|
+
$fixed_dirs = $dirs
|
22
|
+
}
|
23
|
+
$no_empty_dirs = $fixed_dirs.filter |$dir| { !empty($dir) }
|
24
|
+
$dirs_without_sep = $no_empty_dirs.map |String $dir | {
|
25
|
+
# remove : and file separator
|
26
|
+
$dir.regsubst($sep_regex, '').regsubst(':', '')
|
27
|
+
}
|
28
|
+
join([$unix_sep,$dirs_without_sep.join($unix_sep)])
|
29
|
+
}
|
@@ -0,0 +1,11 @@
|
|
1
|
+
# @summary DEPRECATED. Use the namespaced function [`extlib::cache_data`](#extlibcache_data) instead.
|
2
|
+
# DEPRECATED. Use the namespaced function [`extlib::cache_data`](#extlibcache_data) instead.
|
3
|
+
Puppet::Functions.create_function(:cache_data) do
|
4
|
+
dispatch :deprecation_gen do
|
5
|
+
repeated_param 'Any', :args
|
6
|
+
end
|
7
|
+
def deprecation_gen(*args)
|
8
|
+
call_function('deprecation', 'cache_data', 'This method is deprecated, please use extlib::cache_data instead.')
|
9
|
+
call_function('extlib::cache_data', *args)
|
10
|
+
end
|
11
|
+
end
|
@@ -0,0 +1,11 @@
|
|
1
|
+
# @summary DEPRECATED. Use the namespaced function [`extlib::default_content`](#extlibdefault_content) instead.
|
2
|
+
# DEPRECATED. Use the namespaced function [`extlib::default_content`](#extlibdefault_content) instead.
|
3
|
+
Puppet::Functions.create_function(:default_content) do
|
4
|
+
dispatch :deprecation_gen do
|
5
|
+
repeated_param 'Any', :args
|
6
|
+
end
|
7
|
+
def deprecation_gen(*args)
|
8
|
+
call_function('deprecation', 'default_content', 'This method is deprecated, please use extlib::default_content instead.')
|
9
|
+
call_function('extlib::default_content', *args)
|
10
|
+
end
|
11
|
+
end
|
@@ -0,0 +1,11 @@
|
|
1
|
+
# @summary DEPRECATED. Use the namespaced function [`extlib::dump_args`](#extlibdump_args) instead.
|
2
|
+
# DEPRECATED. Use the namespaced function [`extlib::dump_args`](#extlibdump_args) instead.
|
3
|
+
Puppet::Functions.create_function(:dump_args) do
|
4
|
+
dispatch :deprecation_gen do
|
5
|
+
repeated_param 'Any', :args
|
6
|
+
end
|
7
|
+
def deprecation_gen(*args)
|
8
|
+
call_function('deprecation', 'dump_args', 'This method is deprecated, please use extlib::dump_args instead.')
|
9
|
+
call_function('extlib::dump_args', *args)
|
10
|
+
end
|
11
|
+
end
|
@@ -0,0 +1,11 @@
|
|
1
|
+
# @summary DEPRECATED. Use the namespaced function [`extlib::echo`](#extlibecho) instead.
|
2
|
+
# DEPRECATED. Use the namespaced function [`extlib::echo`](#extlibecho) instead.
|
3
|
+
Puppet::Functions.create_function(:echo) do
|
4
|
+
dispatch :deprecation_gen do
|
5
|
+
repeated_param 'Any', :args
|
6
|
+
end
|
7
|
+
def deprecation_gen(*args)
|
8
|
+
call_function('deprecation', 'echo', 'This method is deprecated, please use extlib::echo instead.')
|
9
|
+
call_function('extlib::echo', *args)
|
10
|
+
end
|
11
|
+
end
|
@@ -0,0 +1,48 @@
|
|
1
|
+
require 'fileutils'
|
2
|
+
require 'yaml'
|
3
|
+
require 'etc'
|
4
|
+
|
5
|
+
# @summary Retrieves data from a cache file, or creates it with supplied data if the file doesn't exist
|
6
|
+
#
|
7
|
+
# Retrieves data from a cache file, or creates it with supplied data if the
|
8
|
+
# file doesn't exist
|
9
|
+
#
|
10
|
+
# Useful for having data that's randomly generated once on the master side
|
11
|
+
# (e.g. a password), but then stays the same on subsequent runs. Because it's
|
12
|
+
# stored on the master on disk, it doesn't work when you use mulitple Puppet
|
13
|
+
# masters that don't share their vardir.
|
14
|
+
#
|
15
|
+
# @example Calling the function
|
16
|
+
# $password = cache_data('mysql', 'mysql_password', 'this_is_my_password')
|
17
|
+
#
|
18
|
+
# @example With a random password
|
19
|
+
# $password = cache_data('mysql', 'mysql_password', random_password())
|
20
|
+
Puppet::Functions.create_function(:'extlib::cache_data') do
|
21
|
+
# @param namespace Namespace for the cache
|
22
|
+
# @param name Cache key within the namespace
|
23
|
+
# @param initial_data The data for when there is no cache yet
|
24
|
+
# @return The cached value when it exists. The initial data when no cache exists
|
25
|
+
dispatch :cache_data do
|
26
|
+
param 'String[1]', :namespace
|
27
|
+
param 'String[1]', :name
|
28
|
+
param 'Any', :initial_data
|
29
|
+
return_type 'Any'
|
30
|
+
end
|
31
|
+
|
32
|
+
def cache_data(namespace, name, initial_data)
|
33
|
+
cache_dir = File.join(Puppet[:vardir], namespace)
|
34
|
+
cache = File.join(cache_dir, name)
|
35
|
+
|
36
|
+
if File.exist? cache
|
37
|
+
YAML.load(File.read(cache))
|
38
|
+
else
|
39
|
+
FileUtils.mkdir_p(cache_dir)
|
40
|
+
File.open(cache, 'w', 0o600) do |c|
|
41
|
+
c.write(YAML.dump(initial_data))
|
42
|
+
end
|
43
|
+
File.chown(File.stat(Puppet[:vardir]).uid, nil, cache)
|
44
|
+
File.chown(File.stat(Puppet[:vardir]).uid, nil, cache_dir)
|
45
|
+
initial_data
|
46
|
+
end
|
47
|
+
end
|
48
|
+
end
|
@@ -0,0 +1,37 @@
|
|
1
|
+
# Takes an optional content and an optional template name and returns the contents of a file.
|
2
|
+
Puppet::Functions.create_function(:'extlib::default_content') do
|
3
|
+
# @param content
|
4
|
+
# @param template_name
|
5
|
+
# The path to an .erb or .epp template file or `undef`.
|
6
|
+
# @return
|
7
|
+
# Returns the value of the content parameter if it's a non empty string.
|
8
|
+
# Otherwise returns the rendered output from `template_name`.
|
9
|
+
# Returns `undef` if both `content` and `template_name` are `undef`.
|
10
|
+
#
|
11
|
+
# @example Using the function with a file resource.
|
12
|
+
# $config_file_content = default_content($file_content, $template_location)
|
13
|
+
# file { '/tmp/x':
|
14
|
+
# ensure => 'file',
|
15
|
+
# content => $config_file_content,
|
16
|
+
# }
|
17
|
+
dispatch :default_content do
|
18
|
+
param 'Optional[String]', :content
|
19
|
+
param 'Optional[String]', :template_name
|
20
|
+
return_type 'Optional[String]'
|
21
|
+
end
|
22
|
+
|
23
|
+
def emptyish(x)
|
24
|
+
x.nil? || x.empty? || x == :undef
|
25
|
+
end
|
26
|
+
|
27
|
+
def default_content(content = :undef, template_name = :undef)
|
28
|
+
return content unless emptyish(content)
|
29
|
+
|
30
|
+
unless emptyish(template_name)
|
31
|
+
return call_function('template', template_name) unless template_name.end_with?('.epp')
|
32
|
+
return call_function('epp', template_name)
|
33
|
+
end
|
34
|
+
|
35
|
+
:undef
|
36
|
+
end
|
37
|
+
end
|
@@ -0,0 +1,20 @@
|
|
1
|
+
require 'json'
|
2
|
+
|
3
|
+
# @summary Prints the args to STDOUT in Pretty JSON format.
|
4
|
+
#
|
5
|
+
# Prints the args to STDOUT in Pretty JSON format.
|
6
|
+
#
|
7
|
+
# Useful for debugging purposes only. Ideally you would use this in
|
8
|
+
# conjunction with a rspec-puppet unit test. Otherwise the output will
|
9
|
+
# be shown during a puppet run when verbose/debug options are enabled.
|
10
|
+
Puppet::Functions.create_function(:'extlib::dump_args') do
|
11
|
+
# @param args The data you want to dump as pretty JSON.
|
12
|
+
# @return [Undef] Returns nothing.
|
13
|
+
dispatch :dump_args do
|
14
|
+
param 'Any', :args
|
15
|
+
end
|
16
|
+
|
17
|
+
def dump_args(args)
|
18
|
+
puts JSON.pretty_generate(args)
|
19
|
+
end
|
20
|
+
end
|
@@ -0,0 +1,45 @@
|
|
1
|
+
# This function outputs the variable content and its type to the
|
2
|
+
# debug log. It's similiar to the `notice` function but provides
|
3
|
+
# a better output format useful to trace variable types and values
|
4
|
+
# in the manifests.
|
5
|
+
#
|
6
|
+
# ```
|
7
|
+
# $v1 = 'test'
|
8
|
+
# $v2 = ["1", "2", "3"]
|
9
|
+
# $v3 = {"a"=>"1", "b"=>"2"}
|
10
|
+
# $v4 = true
|
11
|
+
# # $v5 is not defined
|
12
|
+
# $v6 = { "b" => { "b" => [1,2,3], "c" => true, "d" => { 'x' => 'y' }}, 'x' => 'y', 'z' => [1,2,3,4,5,6]}
|
13
|
+
# $v7 = 12345
|
14
|
+
#
|
15
|
+
# echo($v1, 'My string')
|
16
|
+
# echo($v2, 'My array')
|
17
|
+
# echo($v3, 'My hash')
|
18
|
+
# echo($v4, 'My boolean')
|
19
|
+
# echo($v5, 'My undef')
|
20
|
+
# echo($v6, 'My structure')
|
21
|
+
# echo($v7) # no comment here
|
22
|
+
# debug log output
|
23
|
+
# My string (String) "test"
|
24
|
+
# My array (Array) ["1", "2", "3"]
|
25
|
+
# My hash (Hash) {"a"=>"1", "b"=>"2"}
|
26
|
+
# My boolean (TrueClass) true
|
27
|
+
# My undef (String) ""
|
28
|
+
# My structure (Hash) {"b"=>{"b"=>["1", "2", "3"], "c"=>true, "d"=>{"x"=>"y"}}, "x"=>"y", "z"=>["1", "2", "3", "4", "5", "6"]}
|
29
|
+
# (String) "12345"
|
30
|
+
# ```
|
31
|
+
Puppet::Functions.create_function(:'extlib::echo') do
|
32
|
+
# @param value The value you want to inspect.
|
33
|
+
# @param comment An optional comment to prepend to the debug output.
|
34
|
+
# @return [Undef] Returns nothing.
|
35
|
+
dispatch :echo do
|
36
|
+
param 'Any', :value
|
37
|
+
optional_param 'String', :comment
|
38
|
+
end
|
39
|
+
|
40
|
+
def echo(value, comment = nil)
|
41
|
+
message = "(#{value.class}) #{value.inspect}"
|
42
|
+
message = "#{comment} #{message}" if comment
|
43
|
+
Puppet.debug message
|
44
|
+
end
|
45
|
+
end
|
@@ -0,0 +1,33 @@
|
|
1
|
+
require 'json'
|
2
|
+
|
3
|
+
# A function that lets you know whether a specific module is on your modulepath.
|
4
|
+
Puppet::Functions.create_function(:'extlib::has_module') do
|
5
|
+
# @param module_name The full name of the module you want to know exists or not.
|
6
|
+
# Namespace and modulename can be separated with either `-` or `/`.
|
7
|
+
# @return Returns `true` or `false`.
|
8
|
+
# @example Calling the function
|
9
|
+
# extlib::has_module('camptocamp/systemd')
|
10
|
+
dispatch :has_module do
|
11
|
+
param 'Pattern[/\A\w+[-\/]\w+\z/]', :module_name
|
12
|
+
return_type 'Boolean'
|
13
|
+
end
|
14
|
+
|
15
|
+
def has_module(module_name) # rubocop:disable Style/PredicateName
|
16
|
+
full_module_name = module_name.gsub(%r{/}, '-')
|
17
|
+
module_name = full_module_name[%r{(?<=-).+}]
|
18
|
+
begin
|
19
|
+
module_path = call_function('get_module_path', module_name)
|
20
|
+
rescue Puppet::ParseError
|
21
|
+
# stdlib function get_module_path raises Puppet::ParseError if module isn't in your environment
|
22
|
+
return false
|
23
|
+
end
|
24
|
+
|
25
|
+
metadata_json = File.join(module_path, 'metadata.json')
|
26
|
+
|
27
|
+
return false unless File.exist?(metadata_json)
|
28
|
+
|
29
|
+
metadata = JSON.parse(File.read(metadata_json))
|
30
|
+
return true if metadata['name'] == full_module_name
|
31
|
+
false
|
32
|
+
end
|
33
|
+
end
|
@@ -0,0 +1,39 @@
|
|
1
|
+
# Provides a "random" value to cron based on the last bit of the machine IP address.
|
2
|
+
# used to avoid starting a certain cron job at the same time on all servers.
|
3
|
+
# Takes the runinterval in seconds as parameter and returns an array of [hour, minute]
|
4
|
+
#
|
5
|
+
# example usage
|
6
|
+
# ```
|
7
|
+
# ip_to_cron(3600) - returns [ '*', one value between 0..59 ]
|
8
|
+
# ip_to_cron(1800) - returns [ '*', an array of two values between 0..59 ]
|
9
|
+
# ip_to_cron(7200) - returns [ an array of twelve values between 0..23, one value between 0..59 ]
|
10
|
+
# ```
|
11
|
+
Puppet::Functions.create_function(:'extlib::ip_to_cron') do
|
12
|
+
# @param runinterval The number of seconds to use as the run interval
|
13
|
+
# return [Array] Returns an array of the form `[hour, minute]`
|
14
|
+
dispatch :ip_to_cron do
|
15
|
+
optional_param 'Integer[1]', :runinterval
|
16
|
+
return_type 'Array'
|
17
|
+
end
|
18
|
+
|
19
|
+
def ip_to_cron(runinterval = 1800)
|
20
|
+
facts = closure_scope['facts']
|
21
|
+
ip = facts['networking']['ip']
|
22
|
+
ip_last_octet = ip.to_s.split('.')[3].to_i
|
23
|
+
|
24
|
+
if runinterval <= 3600
|
25
|
+
occurances = 3600 / runinterval
|
26
|
+
scope = 60
|
27
|
+
base = ip_last_octet % scope
|
28
|
+
hour = '*'
|
29
|
+
minute = (1..occurances).map { |i| (base - (scope / occurances * i)) % scope }.sort
|
30
|
+
else
|
31
|
+
occurances = 86_400 / runinterval
|
32
|
+
scope = 24
|
33
|
+
base = ip_last_octet % scope
|
34
|
+
hour = (1..occurances).map { |i| (base - (scope / occurances * i)) % scope }.sort
|
35
|
+
minute = ip_last_octet % 60
|
36
|
+
end
|
37
|
+
[hour, minute]
|
38
|
+
end
|
39
|
+
end
|
@@ -0,0 +1,63 @@
|
|
1
|
+
# vim: set ts=2 sw=2 et :
|
2
|
+
# encoding: utf-8
|
3
|
+
|
4
|
+
# random_password.rb
|
5
|
+
#
|
6
|
+
# Copyright 2012 Krzysztof Wilczynski
|
7
|
+
#
|
8
|
+
# Licensed under the Apache License, Version 2.0 (the "License");
|
9
|
+
# you may not use this file except in compliance with the License.
|
10
|
+
# You may obtain a copy of the License at
|
11
|
+
#
|
12
|
+
# http://www.apache.org/licenses/LICENSE-2.0
|
13
|
+
#
|
14
|
+
# Unless required by applicable law or agreed to in writing, software
|
15
|
+
# distributed under the License is distributed on an "AS IS" BASIS,
|
16
|
+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
17
|
+
# See the License for the specific language governing permissions and
|
18
|
+
# limitations under the License.
|
19
|
+
|
20
|
+
# For example:
|
21
|
+
# ```
|
22
|
+
# Given the following statements:
|
23
|
+
#
|
24
|
+
# $a = 4
|
25
|
+
# $b = 8
|
26
|
+
# $c = 16
|
27
|
+
#
|
28
|
+
# notice random_password($a)
|
29
|
+
# notice random_password($b)
|
30
|
+
# notice random_password($c)
|
31
|
+
#
|
32
|
+
# The result will be as follows:
|
33
|
+
#
|
34
|
+
# notice: Scope(Class[main]): fNDC
|
35
|
+
# notice: Scope(Class[main]): KcKDLrjR
|
36
|
+
# notice: Scope(Class[main]): FtvfvkS9j9wXLsd6
|
37
|
+
# ```
|
38
|
+
|
39
|
+
# A function to return a string of arbitrary length that contains randomly selected characters.
|
40
|
+
Puppet::Functions.create_function(:'extlib::random_password') do
|
41
|
+
# @param length The length of random password you want generated.
|
42
|
+
# @return [String] The random string returned consists of alphanumeric characters excluding 'look-alike' characters.
|
43
|
+
# @example Calling the function
|
44
|
+
# random_password(42)
|
45
|
+
dispatch :random_password do
|
46
|
+
param 'Integer[1]', :length
|
47
|
+
return_type 'String'
|
48
|
+
end
|
49
|
+
|
50
|
+
def random_password(length)
|
51
|
+
# These are quite often confusing ...
|
52
|
+
ambiguous_characters = %w[0 1 O I l]
|
53
|
+
|
54
|
+
# Get allowed characters set ...
|
55
|
+
set = ('a'..'z').to_a + ('A'..'Z').to_a + ('0'..'9').to_a
|
56
|
+
set -= ambiguous_characters
|
57
|
+
|
58
|
+
# Shuffle characters in the set at random and return desired number of them ...
|
59
|
+
Array.new(length) do |_i|
|
60
|
+
set[rand(set.length)]
|
61
|
+
end.join
|
62
|
+
end
|
63
|
+
end
|
@@ -0,0 +1,87 @@
|
|
1
|
+
# @summary Deeply merge a "defaults" hash into a "resources" hash like the ones expected by `create_resources()`.
|
2
|
+
#
|
3
|
+
# Deeply merge a "defaults" hash into a "resources" hash like the ones expected by `create_resources()`.
|
4
|
+
#
|
5
|
+
# Internally calls the puppetlabs-stdlib function `deep_merge()`. In case of
|
6
|
+
# duplicate keys the `resources` hash keys win over the `defaults` hash keys.
|
7
|
+
#
|
8
|
+
# Example
|
9
|
+
# ```puppet
|
10
|
+
# $defaults_hash = {
|
11
|
+
# 'one' => '1',
|
12
|
+
# 'two' => '2',
|
13
|
+
# 'three' => '3',
|
14
|
+
# 'four' => {
|
15
|
+
# 'five' => '5',
|
16
|
+
# 'six' => '6',
|
17
|
+
# 'seven' => '7',
|
18
|
+
# }
|
19
|
+
# }
|
20
|
+
#
|
21
|
+
# $numbers_hash = {
|
22
|
+
# 'german' => {
|
23
|
+
# 'one' => 'eins',
|
24
|
+
# 'three' => 'drei',
|
25
|
+
# 'four' => {
|
26
|
+
# 'six' => 'sechs',
|
27
|
+
# },
|
28
|
+
# },
|
29
|
+
# 'french' => {
|
30
|
+
# 'one' => 'un',
|
31
|
+
# 'two' => 'deux',
|
32
|
+
# 'four' => {
|
33
|
+
# 'five' => 'cinq',
|
34
|
+
# 'seven' => 'sept',
|
35
|
+
# },
|
36
|
+
# }
|
37
|
+
# }
|
38
|
+
#
|
39
|
+
# $result_hash = resources_deep_merge($numbers_hash, $defaults_hash)
|
40
|
+
# ```
|
41
|
+
#
|
42
|
+
# The $result_hash then looks like this:
|
43
|
+
#
|
44
|
+
# ```puppet
|
45
|
+
# $result_hash = {
|
46
|
+
# 'german' => {
|
47
|
+
# 'one' => 'eins',
|
48
|
+
# 'two' => '2',
|
49
|
+
# 'three' => 'drei',
|
50
|
+
# 'four' => {
|
51
|
+
# 'five' => '5',
|
52
|
+
# 'six' => 'sechs',
|
53
|
+
# 'seven' => '7',
|
54
|
+
# }
|
55
|
+
# },
|
56
|
+
# 'french' => {
|
57
|
+
# 'one' => 'un',
|
58
|
+
# 'two' => 'deux',
|
59
|
+
# 'three' => '3',
|
60
|
+
# 'four' => {
|
61
|
+
# 'five' => 'cinq',
|
62
|
+
# 'six' => '6',
|
63
|
+
# 'seven' => 'sept',
|
64
|
+
# }
|
65
|
+
# }
|
66
|
+
# }
|
67
|
+
# ```
|
68
|
+
Puppet::Functions.create_function(:'extlib::resources_deep_merge') do
|
69
|
+
# Deep-merges defaults into a resources hash
|
70
|
+
# @param resources The hash of resources.
|
71
|
+
# @param defaults The hash of defaults to merge.
|
72
|
+
# @return Returns the merged hash.
|
73
|
+
dispatch :resources_deep_merge do
|
74
|
+
param 'Hash', :resources
|
75
|
+
param 'Hash', :defaults
|
76
|
+
return_type 'Hash'
|
77
|
+
end
|
78
|
+
|
79
|
+
def resources_deep_merge(resources, defaults)
|
80
|
+
deep_merged_resources = {}
|
81
|
+
resources.each do |title, params|
|
82
|
+
deep_merged_resources[title] = call_function('deep_merge', defaults, params)
|
83
|
+
end
|
84
|
+
|
85
|
+
deep_merged_resources
|
86
|
+
end
|
87
|
+
end
|
@@ -0,0 +1,15 @@
|
|
1
|
+
# A function that sorts an array of version numbers.
|
2
|
+
Puppet::Functions.create_function(:'extlib::sort_by_version') do
|
3
|
+
# @param versions An array of version strings you want sorted.
|
4
|
+
# @return Returns the sorted array.
|
5
|
+
# @example Calling the function
|
6
|
+
# extlib::sort_by_version(['10.0.0b12', '10.0.0b3', '10.0.0a2', '9.0.10', '9.0.3'])
|
7
|
+
dispatch :sort_by_version do
|
8
|
+
param 'Array[String]', :versions
|
9
|
+
return_type 'Array[String]'
|
10
|
+
end
|
11
|
+
|
12
|
+
def sort_by_version(versions)
|
13
|
+
versions.sort_by { |v| Gem::Version.new(v) }
|
14
|
+
end
|
15
|
+
end
|
@@ -0,0 +1,11 @@
|
|
1
|
+
# @summary DEPRECATED. Use the namespaced function [`extlib::ip_to_cron`](#extlibip_to_cron) instead.
|
2
|
+
# DEPRECATED. Use the namespaced function [`extlib::ip_to_cron`](#extlibip_to_cron) instead.
|
3
|
+
Puppet::Functions.create_function(:ip_to_cron) do
|
4
|
+
dispatch :deprecation_gen do
|
5
|
+
repeated_param 'Any', :args
|
6
|
+
end
|
7
|
+
def deprecation_gen(*args)
|
8
|
+
call_function('deprecation', 'ip_to_cron', 'This method is deprecated, please use extlib::ip_to_cron instead.')
|
9
|
+
call_function('extlib::ip_to_cron', *args)
|
10
|
+
end
|
11
|
+
end
|
@@ -0,0 +1,11 @@
|
|
1
|
+
# @summary DEPRECATED. Use the namespaced function [`extlib::random_password`](#extlibrandom_password) instead.
|
2
|
+
# DEPRECATED. Use the namespaced function [`extlib::random_password`](#extlibrandom_password) instead.
|
3
|
+
Puppet::Functions.create_function(:random_password) do
|
4
|
+
dispatch :deprecation_gen do
|
5
|
+
repeated_param 'Any', :args
|
6
|
+
end
|
7
|
+
def deprecation_gen(*args)
|
8
|
+
call_function('deprecation', 'random_password', 'This method is deprecated, please use extlib::random_password instead.')
|
9
|
+
call_function('extlib::random_password', *args)
|
10
|
+
end
|
11
|
+
end
|
@@ -0,0 +1,11 @@
|
|
1
|
+
# @summary DEPRECATED. Use the namespaced function [`extlib::resources_deep_merge`](#extlibresources_deep_merge) instead.
|
2
|
+
# DEPRECATED. Use the namespaced function [`extlib::resources_deep_merge`](#extlibresources_deep_merge) instead.
|
3
|
+
Puppet::Functions.create_function(:resources_deep_merge) do
|
4
|
+
dispatch :deprecation_gen do
|
5
|
+
repeated_param 'Any', :args
|
6
|
+
end
|
7
|
+
def deprecation_gen(*args)
|
8
|
+
call_function('deprecation', 'resources_deep_merge', 'This method is deprecated, please use extlib::resources_deep_merge instead.')
|
9
|
+
call_function('extlib::resources_deep_merge', *args)
|
10
|
+
end
|
11
|
+
end
|