Skip to content

Commit

Permalink
Split brick_str_to_hash into separate function.
Browse files Browse the repository at this point in the history
This should work around a regression introduced in a recent version of
puppet. (Perhaps 3.7.1?) Allowing custom helper functions in ruby was
apparently never "supposed" to work, and was removed silently... :(

binford2k: it was an accidental loophole got closed during code cleanup

The bug about this issue is:

https://tickets.puppetlabs.com/browse/PUP-3404

Thanks to binford2k for tracking down this bug. His URL finding skills
are impeccable!

Thanks to my puppet-gluster users for finding the issue!
  • Loading branch information
purpleidea committed Oct 21, 2014
1 parent e99afc5 commit 06af205
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 32 deletions.
20 changes: 2 additions & 18 deletions lib/puppet/parser/functions/brick_layout_chained.rb
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ module Puppet::Parser::Functions
ENDHEREDOC

Puppet::Parser::Functions.function('warning') # load function
Puppet::Parser::Functions.function('brick_str_to_hash') # load function
# signature: replica, bricks -> bricks
unless args.length == 2
raise Puppet::ParseError, "brick_layout_chained(): wrong number of arguments (#{args.length}; must be 2)"
Expand All @@ -44,23 +45,6 @@ module Puppet::Parser::Functions
replica = args[0].to_i # convert from string if needed
bricks = args[1]

# TODO: these functions could be in separate puppet files
# eg: Puppet::Parser::Functions.function('myfunc')
# function_myfunc(...)
def brick_str_to_hash(bricks)
# this loop converts brick strings to brick dict's...
result = []
bricks.each do |x|
a = x.split(':')
#assert a.length == 2 # TODO
p = a[1]
p = ((p[-1, 1] == '/') ? p : (p+'/')) # endswith

result.push({'host'=> a[0], 'path'=> p})
end
return result
end

def get_hostlist(bricks)
hosts = []
bricks.each do |x|
Expand Down Expand Up @@ -99,7 +83,7 @@ def get_brickstacks(bricks, sort=false)

final = []
pointer = 0
parsed = brick_str_to_hash(bricks)
parsed = function_brick_str_to_hash([bricks])
# TODO: there should probably be a proper 'sorted' function for
# hostnames, in case they aren't numbered sanely _WITH_ padding.
hosts = get_hostlist(parsed).sort
Expand Down
17 changes: 3 additions & 14 deletions lib/puppet/parser/functions/brick_layout_simple.rb
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@ module Puppet::Parser::Functions
ENDHEREDOC

Puppet::Parser::Functions.function('brick_str_to_hash') # load function

# signature: replica, bricks -> bricks
unless args.length == 2
raise Puppet::ParseError, "brick_layout_simple(): wrong number of arguments (#{args.length}; must be 2)"
Expand All @@ -52,22 +54,9 @@ module Puppet::Parser::Functions
# TODO: these functions could be in separate puppet files
# eg: Puppet::Parser::Functions.function('myfunc')
# function_myfunc(...)
def brick_str_to_hash(bricks)
# this loop converts brick strings to brick dict's...
result = []
bricks.each do |x|
a = x.split(':')
#assert a.length == 2 # TODO
p = a[1]
p = ((p[-1, 1] == '/') ? p : (p+'/')) # endswith

result.push({'host'=> a[0], 'path'=> p})
end
return result
end

collect = {}
parsed = brick_str_to_hash(bricks)
parsed = function_brick_str_to_hash([bricks])
parsed.each do |x|
key = x['host']
val = x['path']
Expand Down
48 changes: 48 additions & 0 deletions lib/puppet/parser/functions/brick_str_to_hash.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
# GlusterFS module by James
# Copyright (C) 2010-2013+ James Shubin
# Written by James Shubin <james@shubin.ca>
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.

module Puppet::Parser::Functions
newfunction(:brick_str_to_hash, :type => :rvalue, :doc => <<-'ENDHEREDOC') do |args|
Helper function for the brick layout algorithms.
ENDHEREDOC

# signature: replica, bricks -> bricks
unless args.length == 1
raise Puppet::ParseError, "brick_str_to_hash(): wrong number of arguments (#{args.length}; must be 1)"
end
unless args[0].is_a?(Array)
raise Puppet::ParseError, "brick_str_to_hash(): expects the first argument to be an array, got #{args[0].inspect} which is of type #{args[0].class}"
end

bricks = args[0]
# this loop converts brick strings to brick dict's...
result = []
bricks.each do |x|
a = x.split(':')
#assert a.length == 2 # TODO
p = a[1]
p = ((p[-1, 1] == '/') ? p : (p+'/')) # endswith

result.push({'host'=> a[0], 'path'=> p})
end

result # return
end
end

# vim: ts=8

0 comments on commit 06af205

Please sign in to comment.