Skip to content

Commit

Permalink
Add interfaces order
Browse files Browse the repository at this point in the history
  • Loading branch information
hfrog committed Aug 17, 2024
1 parent 6f76c55 commit 4603809
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 3 deletions.
14 changes: 11 additions & 3 deletions lib/puppet/provider/network_config/interfaces.rb
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,10 @@ def self.raise_malformed
raise MalformedInterfacesError
end

def order_rules
@resource[:order_rules]
end

class Instance
attr_reader :name

Expand Down Expand Up @@ -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| if name.match(entry.first) then entry.last end } + [rules['default']]).compact.first

Check failure on line 248 in lib/puppet/provider/network_config/interfaces.rb

View workflow job for this annotation

GitHub Actions / Puppet / Static validations

Layout/SpaceBeforeBlockBraces: Space missing to the left of {.

Check failure on line 248 in lib/puppet/provider/network_config/interfaces.rb

View workflow job for this annotation

GitHub Actions / Puppet / Static validations

Style/IfUnlessModifier: Favor modifier `if` usage when having a single-line body. Another good alternative is the usage of control flow `&&`/`||`. (https://rubystyle.guide#if-as-a-modifier)
end

# Generate an array of sections
def self.format_file(_filename, providers)
contents = []
Expand All @@ -249,20 +257,20 @@ 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(' '))

Check failure on line 260 in lib/puppet/provider/network_config/interfaces.rb

View workflow job for this annotation

GitHub Actions / Puppet / Static validations

Layout/SpaceBeforeBlockBraces: Space missing to the left of {.
contents << stanza.join("\n")
end

# Add hotpluggable interfaces
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(' '))

Check failure on line 268 in lib/puppet/provider/network_config/interfaces.rb

View workflow job for this annotation

GitHub Actions / Puppet / Static validations

Layout/SpaceBeforeBlockBraces: Space missing to the left of {.
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|

Check failure on line 273 in lib/puppet/provider/network_config/interfaces.rb

View workflow job for this annotation

GitHub Actions / Puppet / Static validations

Layout/SpaceBeforeBlockBraces: Space missing to the left of {.
# 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?
Expand Down
4 changes: 4 additions & 0 deletions lib/puppet/type/network_config.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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|
Expand Down
5 changes: 5 additions & 0 deletions manifests/init.pp
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -78,4 +79,8 @@
}
Package[$ipaddress] -> Network_config <| |>
}

Network_config <| |> {
order_rules => $order_rules,
}
}

0 comments on commit 4603809

Please sign in to comment.