L23network::l3::ifconfig -- refactor dns_*

This commit is contained in:
Sergey Vasilenko 2013-03-26 15:12:18 +04:00
parent f40b98aece
commit 8a07ac087e
5 changed files with 106 additions and 12 deletions

View File

@ -0,0 +1,36 @@
#
# array_or_string_to_array.rb
#
module Puppet::Parser::Functions
newfunction(:array_or_string_to_array, :type => :rvalue, :doc => <<-EOS
This function get array or string with seperator (comma, colon or space).
and return array without empty or false elements.
*Examples:*
array_or_string_to_array(['a','b','c','d'])
array_or_string_to_array('a,b:c d'])
Would result in: ['a','b','c','d']
EOS
) do |arguments|
# Technically we support two arguments but only first is mandatory ...
raise(Puppet::ParseError, "array_or_string_to_array(): Wrong number of arguments " +
"given (#{arguments.size} for 1)") if arguments.size < 1
in_data = arguments[0]
if in_data.is_a?(String)
rv = in_data.split(/[\:\,\s]+/).delete_if{|a| a=='' or !a}
elsif in_data.is_a?(Array)
rv = in_data.delete_if{|a| a==''}
else
raise(Puppet::ParseError, 'array_or_string_to_array(): Requires array or string to work with')
end
return rv
end
end
# vim: set ts=2 sw=2 et :

View File

@ -0,0 +1,35 @@
#
# merge_arrays.rb
#
module Puppet::Parser::Functions
newfunction(:merge_arrays, :type => :rvalue, :doc => <<-EOS
This function get arrays, merge it and return.
*Examples:*
merge_arrays(['a','b'], ['c','d'])
Would result in: ['a','b','c','d']
EOS
) do |arguments|
# Technically we support two arguments but only first is mandatory ...
raise(Puppet::ParseError, "merge_arrays(): Wrong number of arguments " +
"given (#{arguments.size} for 1)") if arguments.size < 1
rv = []
for arg in arguments
if arg.is_a?(Array)
rv += arg
else
raise(Puppet::ParseError, 'merge_arrays(): Requires only array as argument')
end
end
return rv
end
end
# vim: set ts=2 sw=2 et :

View File

@ -42,7 +42,13 @@
# [*dns_nameservers*]
# Specify pair of nameservers if need. Must be array, for example:
# nameservers => ['8.8.8.8', '8.8.4.4']
# TODO: realize dns_domain derecive
#
# [*dns_domain*]
# Specify DOMAIN option for interface. Implemened only in ubuntu.
#
# [*dns_search*]
# Specify SEARCH option for interface. Must be array, for example:
# dns_search => ['aaaa.com', 'bbbb.org']
#
# [*dhcp_hostname*]
# Specify hostname for DHCP if need.
@ -112,23 +118,39 @@ define l23network::l3::ifconfig (
/(?i)debian/: {
$if_files_dir = '/etc/network/interfaces.d'
$interfaces = '/etc/network/interfaces'
if $dns_nameservers {
$dns_nameservers_join = join($dns_nameservers, ' ')
}
}
/(?i)redhat/: {
$if_files_dir = '/etc/sysconfig/network-scripts'
$interfaces = false
if $dns_nameservers {
$dns_nameservers_1 = $dns_nameservers[0]
$dns_nameservers_2 = $dns_nameservers[1]
}
}
default: {
fail("Unsupported OS: ${::osfamily}/${::operatingsystem}")
}
}
# DNS nameservers, search and domain options
if $dns_nameservers {
$dns_nameservers_list = merge_arrays( array_or_string_to_array($dns_nameservers), [false, false])
$dns_nameservers_1 = $dns_nameservers_list[0]
$dns_nameservers_2 = $dns_nameservers_list[1]
}
if $dns_search {
$dns_search_list = array_or_string_to_array($dns_search)
if $dns_search_list {
$dns_search_string = join($dns_search_list, ' ')
} else {
fail("dns_search option must be array or string")
}
}
if $dns_domain {
$dns_domain_list = array_or_string_to_array($dns_domain)
if $dns_domain_list {
$dns_domain_string = $dns_domain_list[0]
} else {
fail("dns_domain option must be array or string")
}
}
# Detect VLAN and bond mode configuration
case $interface {
/^vlan(\d+)/: {

View File

@ -4,12 +4,12 @@ iface <%= interface %> inet static
address <%= ipaddr %>
netmask <%= netmask %>
<% if @def_gateway %>gateway <%= @def_gateway %><% end %>
<% if @dns_nameservers_join %>dns-nameservers <%= @dns_nameservers_join %><% end %>
<% if @dns_search %>dns-search <%= @dns_search %><% end %>
<% if @dns_domain %>dns-domain <%= @dns_domain %><% end %>
<% if @dns_nameservers_1 or @dns_nameservers_2 %>dns-nameservers <% if @dns_nameservers_1 %><%= @dns_nameservers_1 %><% end %> <% if @dns_nameservers_2 %><%= @dns_nameservers_2 %><% end %><% end %>
<% if @dns_search_string %>dns-search <%= @dns_search_string %><% end %>
<% if @dns_domain_string %>dns-domain <%= @dns_domain_string %><% end %>
<% if @mtu %>mtu <%= @mtu %><% end %>
<% if @bond_mode %>slaves none
bond-mode <%= @bond_mode %><% if @bond_miimon %>
bond-miimon <%= @bond_miimon %><% end %><% if @bond_lacp_rate %>
bond-lacp-rate <%= @bond_lacp_rate %><% end %>
<% end %>
<% end %>

View File

@ -10,5 +10,6 @@ PHYSDEV=<%= @vlan_dev %><% end %>
<% if @def_gateway %>GATEWAY=<%= @def_gateway %><% end %>
<% if @dns_nameservers_1 %>DNS1=<%= @dns_nameservers_1 %><% end %>
<% if @dns_nameservers_2 %>DNS2=<%= @dns_nameservers_2 %><% end %>
<% if @dns_search_string %>SEARCH=<%= @dns_search_string %><% end %>
<% if @mtu %>MTU=<%= @mtu %><% end %>
<% if @bond_mode %>BONDING_OPTS="mode=<%= @bond_mode %><% if @bond_miimon %> miimon=<%= @bond_miimon %><% end %><% if @bond_lacp_rate %> bond-lacp-rate=<%= @bond_lacp_rate %><% end %>"<% end %>