Skip to content

Commit 37a060f

Browse files
author
Julian Todt
committed
Use puppet4 functions-api
1 parent 667c2c7 commit 37a060f

18 files changed

+290
-16
lines changed

examples/mysql_user.pp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,18 +6,18 @@
66

77
mysql_user{ 'redmine@localhost':
88
ensure => present,
9-
password_hash => mysql_password('redmine'),
9+
password_hash => mysql::password('redmine'),
1010
require => Class['mysql::server'],
1111
}
1212

1313
mysql_user{ 'dan@localhost':
1414
ensure => present,
15-
password_hash => mysql_password('blah')
15+
password_hash => mysql::password('blah')
1616
}
1717

1818
mysql_user{ 'dan@%':
1919
ensure => present,
20-
password_hash => mysql_password('blah'),
20+
password_hash => mysql::password('blah'),
2121
}
2222

2323
mysql_user{ 'socketplugin@%':
@@ -27,6 +27,6 @@
2727

2828
mysql_user{ 'socketplugin@%':
2929
ensure => present,
30-
password_hash => mysql_password('blah'),
30+
password_hash => mysql::password('blah'),
3131
plugin => 'mysql_native_password',
3232
}
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
# Recursively merges two or more hashes together and returns the resulting hash.
2+
# For example:
3+
#
4+
# $hash1 = {'one' => 1, 'two' => 2, 'three' => { 'four' => 4 } }
5+
# $hash2 = {'two' => 'dos', 'three' => { 'five' => 5 } }
6+
# $merged_hash = mysql::deepmerge($hash1, $hash2)
7+
# # The resulting hash is equivalent to:
8+
# # $merged_hash = { 'one' => 1, 'two' => 'dos', 'three' => { 'four' => 4, 'five' => 5 } }
9+
#
10+
# When there is a duplicate key that is a hash, they are recursively merged.
11+
# When there is a duplicate key that is not a hash, the key in the rightmost hash will "win."
12+
# When there are conficting uses of dashes and underscores in two keys (which mysql would otherwise equate),
13+
# the rightmost style will win.
14+
Puppet::Functions.create_function(:'mysql::deepmerge') do
15+
def deepmerge(*args)
16+
if args.length < 2
17+
raise Puppet::ParseError, _('mysql_deepmerge(): wrong number of arguments (%{args_length}; must be at least 2)') % { args_length: args.length }
18+
end
19+
20+
result = {}
21+
args.each do |arg|
22+
next if arg.is_a?(String) && arg.empty? # empty string is synonym for puppet's undef
23+
# If the argument was not a hash, skip it.
24+
unless arg.is_a?(Hash)
25+
raise Puppet::ParseError, _('mysql_deepmerge: unexpected argument type %{arg_class}, only expects hash arguments.') % { args_class: args.class }
26+
end
27+
28+
# We need to make a copy of the hash since it is frozen by puppet
29+
current = deep_copy(arg)
30+
31+
# Now we have to traverse our hash assigning our non-hash values
32+
# to the matching keys in our result while following our hash values
33+
# and repeating the process.
34+
overlay(result, current)
35+
end
36+
result
37+
end
38+
39+
def normalized?(hash, key)
40+
return true if hash.key?(key)
41+
return false unless key =~ %r{-|_}
42+
other_key = key.include?('-') ? key.tr('-', '_') : key.tr('_', '-')
43+
return false unless hash.key?(other_key)
44+
hash[key] = hash.delete(other_key)
45+
true
46+
end
47+
48+
def overlay(hash1, hash2)
49+
hash2.each do |key, value|
50+
if normalized?(hash1, key) && value.is_a?(Hash) && hash1[key].is_a?(Hash)
51+
overlay(hash1[key], value)
52+
else
53+
hash1[key] = value
54+
end
55+
end
56+
end
57+
58+
def deep_copy(inputhash)
59+
return inputhash unless inputhash.is_a? Hash
60+
hash = {}
61+
inputhash.each do |k, v|
62+
hash.store(k, deep_copy(v))
63+
end
64+
hash
65+
end
66+
end

lib/puppet/functions/mysql/dirname.rb

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
# Returns the dirname of a path.
2+
Puppet::Functions.create_function(:'mysql::dirname') do
3+
dispatch :dirname do
4+
required_param 'Variant[String, Undef]', :path
5+
return_type 'String'
6+
end
7+
8+
def dirname(path)
9+
return '' if path.nil?
10+
File.dirname(path)
11+
end
12+
end
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
require 'digest/sha1'
2+
# Returns the mysql password hash from the clear text password.
3+
# Hash a string as mysql's "PASSWORD()" function would do it
4+
Puppet::Functions.create_function(:'mysql::password') do
5+
dispatch :password do
6+
required_param 'String', :password
7+
return_type 'String'
8+
end
9+
10+
def password(password)
11+
return '' if password.empty?
12+
return password if password =~ %r{\*[A-F0-9]{40}$}
13+
'*' + Digest::SHA1.hexdigest(Digest::SHA1.digest(password)).upcase
14+
end
15+
end
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
# When given a hash this function strips out all blank entries.
2+
Puppet::Functions.create_function(:'mysql::strip_hash') do
3+
dispatch :strip_hash do
4+
required_param 'Hash', :hash
5+
return_type 'Hash'
6+
end
7+
8+
def strip_hash(hash)
9+
# Filter out all the top level blanks.
10+
hash.reject { |_k, v| v == '' }.each do |_k, v|
11+
v.reject! { |_ki, vi| vi == '' } if v.is_a?(Hash)
12+
end
13+
end
14+
end

manifests/backup/mysqlbackup.pp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525

2626
mysql_user { "${backupuser}@localhost":
2727
ensure => $ensure,
28-
password_hash => mysql_password($backuppassword),
28+
password_hash => mysql::password($backuppassword),
2929
require => Class['mysql::server::root_password'],
3030
}
3131

@@ -88,7 +88,7 @@
8888
'password' => $backuppassword,
8989
}
9090
}
91-
$options = mysql_deepmerge($default_options, $mysql::server::override_options)
91+
$options = mysql::deepmerge($default_options, $mysql::server::override_options)
9292

9393
file { 'mysqlbackup-config-file':
9494
path => '/etc/mysql/conf.d/meb.cnf',

manifests/backup/mysqldump.pp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@
3030

3131
mysql_user { "${backupuser}@localhost":
3232
ensure => $ensure,
33-
password_hash => mysql_password($backuppassword),
33+
password_hash => mysql::password($backuppassword),
3434
require => Class['mysql::server::root_password'],
3535
}
3636

manifests/backup/xtrabackup.pp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@
3333
if $backupuser and $backuppassword {
3434
mysql_user { "${backupuser}@localhost":
3535
ensure => $ensure,
36-
password_hash => mysql_password($backuppassword),
36+
password_hash => mysql::password($backuppassword),
3737
require => Class['mysql::server::root_password'],
3838
}
3939

manifests/db.pp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@
3131

3232
$user_resource = {
3333
ensure => $ensure,
34-
password_hash => mysql_password($password),
34+
password_hash => mysql::password($password),
3535
}
3636
ensure_resource('mysql_user', "${user}@${host}", $user_resource)
3737

manifests/server.pp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@
4949
}
5050

5151
# Create a merged together set of options. Rightmost hashes win over left.
52-
$options = mysql_deepmerge($mysql::params::default_options, $override_options)
52+
$options = mysql::deepmerge($mysql::params::default_options, $override_options)
5353

5454
Class['mysql::server::root_password'] -> Mysql::Db <| |>
5555

0 commit comments

Comments
 (0)