Skip to content

Commit 83f5fa0

Browse files
committed
Add interfaces order
1 parent 6f76c55 commit 83f5fa0

File tree

3 files changed

+20
-3
lines changed

3 files changed

+20
-3
lines changed

lib/puppet/provider/network_config/interfaces.rb

+11-3
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,10 @@ def self.raise_malformed
3939
raise MalformedInterfacesError
4040
end
4141

42+
def order_rules
43+
@resource[:order_rules]
44+
end
45+
4246
class Instance
4347
attr_reader :name
4448

@@ -240,6 +244,10 @@ def self.parse_file(_filename, contents)
240244
Instance.all_instances.map { |_name, instance| instance.to_hash }
241245
end
242246

247+
def self.interface_order(name, rules)
248+
(rules['rules'].map { |entry| entry.last if name.match(entry.first) } + [rules['default']]).compact.first
249+
end
250+
243251
# Generate an array of sections
244252
def self.format_file(_filename, providers)
245253
contents = []
@@ -249,20 +257,20 @@ def self.format_file(_filename, providers)
249257
auto_interfaces = providers.select { |provider| provider.onboot == true }
250258
unless auto_interfaces.empty?
251259
stanza = []
252-
stanza << ('auto ' + auto_interfaces.map(&:name).sort.join(' '))
260+
stanza << ('auto ' + auto_interfaces.sort_by { |provider| interface_order(provider.name, provider.order_rules) }.map(&:name).join(' '))
253261
contents << stanza.join("\n")
254262
end
255263

256264
# Add hotpluggable interfaces
257265
hotplug_interfaces = providers.select { |provider| provider.hotplug == true }
258266
unless hotplug_interfaces.empty?
259267
stanza = []
260-
stanza << ('allow-hotplug ' + hotplug_interfaces.map(&:name).sort.join(' '))
268+
stanza << ('allow-hotplug ' + hotplug_interfaces.sort_by { |provider| interface_order(provider.name, provider.order_rules) }.map(&:name).join(' '))
261269
contents << stanza.join("\n")
262270
end
263271

264272
# Build iface stanzas
265-
providers.sort_by(&:name).each do |provider|
273+
providers.sort_by { |provider| interface_order(provider.name, provider.order_rules) }.each do |provider|
266274
# TODO: add validation method
267275
raise Puppet::Error, "#{provider.name} does not have a method." if provider.method.nil?
268276
raise Puppet::Error, "#{provider.name} does not have a family." if provider.family.nil?

lib/puppet/type/network_config.rb

+4
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,10 @@
9999
desc 'Reconfigure the interface after the configuration has been updated'
100100
end
101101

102+
newparam(:order_rules) do
103+
desc 'Specify order rules of interfaces'
104+
end
105+
102106
newproperty(:mtu) do
103107
desc 'The Maximum Transmission Unit size to use for the interface'
104108
validate do |value|

manifests/init.pp

+5
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@
6262
$ipaddress_provider = 'puppet_gem',
6363
$manage_ipaddress = true,
6464
$ensure_ipaddress = absent,
65+
$order_rules = { 'default' => 10, 'rules' => [[/^lo$/, 2], [/^lo:\d+$/, 6], [/^bond\d+$/, 11], [/^bond\d+:\d+$/, 12], [/\.\d+$/, 13], [/^(lxc)?br\d+$/, 14], [/^gre\d+$/, 15]] },
6566
) {
6667
if $facts['os']['family'] == 'Debian' and $manage_ifupdown_extra {
6768
package { $ifupdown_extra:
@@ -78,4 +79,8 @@
7879
}
7980
Package[$ipaddress] -> Network_config <| |>
8081
}
82+
83+
Network_config <| |> {
84+
order_rules => $order_rules,
85+
}
8186
}

0 commit comments

Comments
 (0)