Skip to content

Commit

Permalink
Merge pull request #56 from jhoblitt/interface_mode_property
Browse files Browse the repository at this point in the history
Interface mode property
  • Loading branch information
adrienthebo committed Feb 4, 2015
2 parents cfdb1ef + 3cbccd6 commit cd1f6c5
Show file tree
Hide file tree
Showing 8 changed files with 277 additions and 25 deletions.
27 changes: 20 additions & 7 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,13 +1,26 @@
---
sudo: false
language: ruby
before_script: 'bundle exec rake fixture:prepare'
script: 'SPEC_OPTS="--format documentation" bundle exec rake spec'
notifications:
email: false
rvm:
- 1.9.3
- 1.8.7
- 1.9.3
- 2.0.0
- 2.1
env:
- PUPPET_VERSION=2.7.21
- PUPPET_VERSION=3.0.2
- PUPPET_VERSION=3.1.0
- PUPPET_GEM_VERSION="~> 2.7"
- PUPPET_GEM_VERSION="~> 3.3"
- PUPPET_GEM_VERSION="~> 3.7"
matrix:
allow_failures:
- rvm: ruby-head
exclude:
- rvm: 1.9.3
env: PUPPET_GEM_VERSION="~> 2.7"
- rvm: 2.0.0
env: PUPPET_GEM_VERSION="~> 2.7"
- rvm: 2.1
env: PUPPET_GEM_VERSION="~> 2.7"
fast_finish: true
notifications:
email: false
24 changes: 19 additions & 5 deletions lib/puppet/provider/network_config/interfaces.rb
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ class Instance

# These fields are going to get rearranged to resolve issue 16
# https://github.com/adrienthebo/puppet-network/issues/16
attr_accessor :ipaddress, :netmask, :family, :method, :mtu
attr_accessor :ipaddress, :netmask, :family, :method, :mtu, :mode

# Options hash
attr_reader :options
Expand All @@ -70,6 +70,7 @@ def to_hash
:family => @family,
:method => @method,
:mtu => @mtu,
:mode => @mode,
:options => squeeze_options
}

Expand Down Expand Up @@ -220,10 +221,11 @@ def self.parse_file(filename, contents)
name = current_interface

case key
when 'address'; Instance[name].ipaddress = val
when 'netmask'; Instance[name].netmask = val
when 'mtu'; Instance[name].mtu = val
else Instance[name].options[key] << val
when 'address'; Instance[name].ipaddress = val
when 'netmask'; Instance[name].netmask = val
when 'mtu'; Instance[name].mtu = val
when 'vlan-raw-device'; Instance[name].mode = :vlan
else Instance[name].options[key] << val
end
else
raise_malformed
Expand Down Expand Up @@ -268,6 +270,18 @@ def self.format_file(filename, providers)
stanza = []
stanza << %{iface #{provider.name} #{provider.family} #{provider.method}}

if provider.mode == :vlan
# if this is a :vlan mode interface than the name of the
# `vlan-raw-device` is implied by the `iface` name in the format
# fooX.<vlan>

# The valid vlan ID range is 0-4095; 4096 is out of range
vlan_range_regex = %r[\d{1,3}|40[0-9][0-5]]
raw_device = provider.name.match(%r[\A([a-z]+\d+)(?::\d+|\.#{vlan_range_regex})?\Z])[1]

stanza << %{vlan-raw-device #{raw_device}}
end

[
[:ipaddress, 'address'],
[:netmask, 'netmask'],
Expand Down
19 changes: 19 additions & 0 deletions lib/puppet/provider/network_config/redhat.rb
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,19 @@ def self.munge(pairs)
end
end

# if we encounter VLAN=yes set the interface mode to :vlan
pairs.each_pair do |key, val|
if (key == 'VLAN' and val == 'yes')
props[:mode] = :vlan
end
end
pairs.delete('VLAN')

# mode is a property so it should always have a value
unless (props.key?(:mode))
props[:mode] = :raw
end

# For all of the remaining values, blindly toss them into the options hash.
props[:options] = pairs unless pairs.empty?

Expand Down Expand Up @@ -183,6 +196,12 @@ def self.format_file(filename, providers)
end
end

# :mode does not exist in NAME_MAPPINGS so we have to fetch it manually
# note that the inverse operation is in .munge instead of parse_file
if (val = provider.send(:mode) and val == :vlan)
props['VLAN'] = 'yes'
end

pairs = self.unmunge props

content = pairs.inject('') do |str, (key, val)|
Expand Down
8 changes: 8 additions & 0 deletions lib/puppet/type/network_config.rb
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,14 @@
end
end

newproperty(:mode) do
desc "The exclusive mode the interface should operate in"
# :bond and :bridge may be added in the future
newvalues(:raw, :vlan)

defaultto :raw
end

# `:options` provides an arbitrary passthrough for provider properties, so
# that provider specific behavior doesn't clutter up the main type but still
# allows for more powerful actions to be taken.
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
# This file describes the network interfaces available on your system
# and how to activate them. For more information, see interfaces(5).

# The loopback network interface
auto lo
iface lo inet loopback

# The primary network interface
auto eth0
iface eth0 inet static
address 192.168.0.2
broadcast 192.168.0.255
netmask 255.255.255.0
gateway 192.168.0.1
mtu 1500
auto eth0.1
iface eth0.1 inet static
vlan-raw-device eth0
address 172.16.0.2
broadcast 172.16.0.255
netmask 255.255.255.0
gateway 172.16.0.1
mtu 1500
66 changes: 66 additions & 0 deletions spec/unit/provider/network_config/interfaces_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,38 @@ def fixture_data(file)
}
end

it "should parse out vlan iface lines" do
fixture = fixture_data('two_interfaces_static_vlan')
data = described_class.parse_file('', fixture)
data.find { |h| h[:name] == "eth0" }.should == {
:name => "eth0",
:family => "inet",
:method => "static",
:ipaddress => "192.168.0.2",
:netmask => "255.255.255.0",
:onboot => true,
:mtu => "1500",
:options => {
"broadcast" => "192.168.0.255",
"gateway" => "192.168.0.1",
}
}
data.find { |h| h[:name] == "eth0.1" }.should == {
:name => "eth0.1",
:family => "inet",
:method => "static",
:ipaddress => "172.16.0.2",
:netmask => "255.255.255.0",
:onboot => true,
:mtu => "1500",
:mode => :vlan,
:options => {
"broadcast" => "172.16.0.255",
"gateway" => "172.16.0.1",
}
}
end

describe "when reading an invalid interfaces" do

it "with misplaced options should fail" do
Expand Down Expand Up @@ -132,6 +164,23 @@ def fixture_data(file)
:ipaddress => "169.254.0.1",
:netmask => "255.255.0.0",
:mtu => "1500",
:mode => nil,
:options => nil
)
end

let(:eth0_1_provider) do
stub('eth0_1_provider',
:name => "eth0.1",
:ensure => :present,
:onboot => true,
:hotplug => true,
:family => "inet",
:method => "static",
:ipaddress => "169.254.0.1",
:netmask => "255.255.0.0",
:mtu => "1500",
:mode => :vlan,
:options => nil
)
end
Expand All @@ -147,6 +196,7 @@ def fixture_data(file)
:ipaddress => "169.254.0.1",
:netmask => "255.255.0.0",
:mtu => "576",
:mode => nil,
:options => {
'pre-up' => '/bin/touch /tmp/eth1-up',
'post-down' => [
Expand All @@ -167,6 +217,7 @@ def fixture_data(file)
:ipaddress => nil,
:netmask => nil,
:mtu => "65536",
:mode => nil,
:options => nil
)
end
Expand Down Expand Up @@ -227,6 +278,21 @@ def fixture_data(file)
end
end

describe "writing vlan iface blocks" do
let(:content) { described_class.format_file('', [eth0_1_provider]) }

it "should add all options following the iface block" do
block = [
"iface eth0.1 inet static",
"vlan-raw-device eth0",
"address 169.254.0.1",
"netmask 255.255.0.0",
"mtu 1500",
].join("\n")
content.split('\n').find {|line| line.match(/iface eth0/)}.should match(block)
end
end

describe "writing the options section" do
let(:content) { described_class.format_file('', [eth1_provider]) }

Expand Down
Loading

0 comments on commit cd1f6c5

Please sign in to comment.