From 83f5fa0c4e669b7c8e8f0b85fcdf42f5b8a1f05d Mon Sep 17 00:00:00 2001 From: Andrey Ivanov Date: Sat, 17 Aug 2024 16:17:25 +0300 Subject: [PATCH] Add interfaces order --- lib/puppet/provider/network_config/interfaces.rb | 14 +++++++++++--- lib/puppet/type/network_config.rb | 4 ++++ manifests/init.pp | 5 +++++ 3 files changed, 20 insertions(+), 3 deletions(-) diff --git a/lib/puppet/provider/network_config/interfaces.rb b/lib/puppet/provider/network_config/interfaces.rb index fc2f6a14..bf2c811a 100644 --- a/lib/puppet/provider/network_config/interfaces.rb +++ b/lib/puppet/provider/network_config/interfaces.rb @@ -39,6 +39,10 @@ def self.raise_malformed raise MalformedInterfacesError end + def order_rules + @resource[:order_rules] + end + class Instance attr_reader :name @@ -240,6 +244,10 @@ def self.parse_file(_filename, contents) Instance.all_instances.map { |_name, instance| instance.to_hash } end + def self.interface_order(name, rules) + (rules['rules'].map { |entry| entry.last if name.match(entry.first) } + [rules['default']]).compact.first + end + # Generate an array of sections def self.format_file(_filename, providers) contents = [] @@ -249,7 +257,7 @@ def self.format_file(_filename, providers) auto_interfaces = providers.select { |provider| provider.onboot == true } unless auto_interfaces.empty? stanza = [] - stanza << ('auto ' + auto_interfaces.map(&:name).sort.join(' ')) + stanza << ('auto ' + auto_interfaces.sort_by { |provider| interface_order(provider.name, provider.order_rules) }.map(&:name).join(' ')) contents << stanza.join("\n") end @@ -257,12 +265,12 @@ def self.format_file(_filename, providers) hotplug_interfaces = providers.select { |provider| provider.hotplug == true } unless hotplug_interfaces.empty? stanza = [] - stanza << ('allow-hotplug ' + hotplug_interfaces.map(&:name).sort.join(' ')) + stanza << ('allow-hotplug ' + hotplug_interfaces.sort_by { |provider| interface_order(provider.name, provider.order_rules) }.map(&:name).join(' ')) contents << stanza.join("\n") end # Build iface stanzas - providers.sort_by(&:name).each do |provider| + providers.sort_by { |provider| interface_order(provider.name, provider.order_rules) }.each do |provider| # TODO: add validation method raise Puppet::Error, "#{provider.name} does not have a method." if provider.method.nil? raise Puppet::Error, "#{provider.name} does not have a family." if provider.family.nil? diff --git a/lib/puppet/type/network_config.rb b/lib/puppet/type/network_config.rb index 92b05e66..6e91c8cd 100644 --- a/lib/puppet/type/network_config.rb +++ b/lib/puppet/type/network_config.rb @@ -99,6 +99,10 @@ desc 'Reconfigure the interface after the configuration has been updated' end + newparam(:order_rules) do + desc 'Specify order rules of interfaces' + end + newproperty(:mtu) do desc 'The Maximum Transmission Unit size to use for the interface' validate do |value| diff --git a/manifests/init.pp b/manifests/init.pp index 03f736fd..6e4fcdd4 100644 --- a/manifests/init.pp +++ b/manifests/init.pp @@ -62,6 +62,7 @@ $ipaddress_provider = 'puppet_gem', $manage_ipaddress = true, $ensure_ipaddress = absent, + $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]] }, ) { if $facts['os']['family'] == 'Debian' and $manage_ifupdown_extra { package { $ifupdown_extra: @@ -78,4 +79,8 @@ } Package[$ipaddress] -> Network_config <| |> } + + Network_config <| |> { + order_rules => $order_rules, + } }