diff options
Diffstat (limited to 'lib')
-rw-r--r-- | lib/puppet/parser/functions/interface_for_ip.rb | 33 | ||||
-rw-r--r-- | lib/puppet/parser/functions/local_fence_devices.rb | 34 |
2 files changed, 67 insertions, 0 deletions
diff --git a/lib/puppet/parser/functions/interface_for_ip.rb b/lib/puppet/parser/functions/interface_for_ip.rb new file mode 100644 index 0000000..1c67120 --- /dev/null +++ b/lib/puppet/parser/functions/interface_for_ip.rb @@ -0,0 +1,33 @@ +require 'ipaddr' + +# Custom function to lookup the interface which matches the subnet +# of the provided IP address. +# The function iterates over all the interfaces and chooses the +# first locally assigned interface which matches the IP. +module Puppet::Parser::Functions + newfunction(:interface_for_ip, :type => :rvalue, :doc => "Find the bind IP address for the provided subnet.") do |arg| + if arg[0].class == String + begin + ip_to_find = arg[0] + Dir.foreach('/sys/class/net/') do |interface| + next if interface == '.' or interface == '..' + iface_no_dash = interface.gsub('-', '_') + interface_ip = lookupvar("ipaddress_#{iface_no_dash}") + netmask = lookupvar("netmask_#{iface_no_dash}") + if not interface_ip.nil? then + ip1=IPAddr.new(interface_ip) + ip2=IPAddr.new(ip_to_find) + if ip1.mask(netmask) == ip2.mask(netmask) then + return interface + end + end + end + rescue JSON::ParserError + raise Puppet::ParseError, "Syntax error: #{arg[0]} is invalid" + end + else + raise Puppet::ParseError, "Syntax error: #{arg[0]} is not a String" + end + return '' + end +end diff --git a/lib/puppet/parser/functions/local_fence_devices.rb b/lib/puppet/parser/functions/local_fence_devices.rb new file mode 100644 index 0000000..1ebce67 --- /dev/null +++ b/lib/puppet/parser/functions/local_fence_devices.rb @@ -0,0 +1,34 @@ +module Puppet::Parser::Functions + newfunction(:local_fence_devices, :arity =>2, :type => :rvalue, + :doc => ("Given an array of fence device configs, limit them" + + "to fence devices whose MAC address is present on" + + "some of the local NICs, and prepare a hash which can be" + + "passed to create_resources function")) do |args| + agent = args[0] + devices = args[1] + unless agent.is_a?(String) && agent.length > 0 + raise Puppet::ParseError, "local_fence_devices: Argument 'agent' must be a non-empty string. The value given was: #{agent_type}" + end + unless devices.is_a?(Array) + raise Puppet::ParseError, "local_fence_devices: Argument 'devices' must be an array. The value given was: #{devices}" + end + + # filter by agent type + agent_type_devices = devices.select { |device| device['agent'] == agent } + + # filter by local mac address + local_devices = agent_type_devices.select do |device| + function_has_interface_with(['macaddress', device['host_mac']]) + end + + # construct a hash for create_resources + return local_devices.each_with_object({}) do |device, hash| + # disallow collisions + if hash[device['host_mac']] + raise Puppet::ParseError, "local_fence_devices: Only single fence device per agent per host is allowed. Collision on #{device['host_mac']} for #{agent}" + end + + hash[device['host_mac']] = device['params'] || {} + end + end +end |