Create Upstart-compatible state files for each interface

created by l23network in runtime.
This functionality required for correct 'ifdown' command work
first time without ifup (interface created by l23network runtime).

Change-Id: I05aecf2a69158f15eac636e33cedbdfbe817adc0
Closes-bug: #1674430
This commit is contained in:
Sergey Vasilenko 2017-04-17 16:12:01 +03:00
parent 3f01bb9d23
commit 0695eedc5f
10 changed files with 127 additions and 0 deletions

View File

@ -195,6 +195,43 @@ class Puppet::Provider::InterfaceToolset < Puppet::Provider
# ---------------------------------------------------------------------------
def self.ensure_upstart_state_file(iface, base_dir='/var/run/network')
if File.directory?(base_dir)
if_name = iface.to_s
# we run under Debian/Ubuntu ifup/ifdown compatible network initialize subsystem
if ! File.file?("#{base_dir}/ifstate.#{if_name}")
# create state file for network interface for upstert
f = File.new("#{base_dir}/ifstate.#{if_name}", mode="w")
f.puts(if_name)
f.close()
end
# add interfase to alias list
if ! File.file?("#{base_dir}/ifstate")
f = File.new("#{base_dir}/ifstate", mode="w")
f.puts("#{if_name}=#{if_name}")
f.close()
else
found = false
f = File.new("#{base_dir}/ifstate", mode="r")
f.each_line do |line|
i = line.split('=')[0]
if if_name == i
found = true
break
end
end
f.close()
if ! found
f = File.new("#{base_dir}/ifstate", mode="a")
f.puts("#{if_name}=#{if_name}")
f.close()
end
end
end
end
# ---------------------------------------------------------------------------
def self.get_if_addr_mappings
if_list = {}

View File

@ -54,6 +54,7 @@ Puppet::Type.type(:l2_bond).provide(:lnx, :parent => Puppet::Provider::Lnx_base)
debug("CREATE resource: #{@resource}")
@old_property_hash = {}
@property_flush = {}.merge! @resource
self.class.ensure_upstart_state_file(@resource[:name])
self.class.set_sys_class('/sys/class/net/bonding_masters', "+#{@resource[:name]}")
end

View File

@ -27,6 +27,7 @@ Puppet::Type.type(:l2_bond).provide(:ovs, :parent => Puppet::Provider::Ovs_base)
debug("CREATE resource: #{@resource}")
@old_property_hash = {}
@property_flush = {}.merge! @resource
self.class.ensure_upstart_state_file(@resource[:bond])
@resource[:slaves].each do |slave|
self.class.addr_flush(slave)

View File

@ -28,6 +28,7 @@ Puppet::Type.type(:l2_bridge).provide(:lnx, :parent => Puppet::Provider::Lnx_bas
debug("CREATE resource: #{@resource}")
@old_property_hash = {}
@property_flush = {}.merge! @resource
self.class.ensure_upstart_state_file(@resource[:bridge])
begin
self.class.brctl(['addbr', @resource[:bridge]])
rescue

View File

@ -23,6 +23,7 @@ Puppet::Type.type(:l2_bridge).provide(:ovs, :parent => Puppet::Provider::Ovs_bas
debug("CREATE resource: #{@resource}")
@old_property_hash = {}
@property_flush = {}.merge! @resource
self.class.ensure_upstart_state_file(@resource[:bridge])
vendor_specific = @resource[:vendor_specific] || {}
datapath_type = vendor_specific["datapath_type"]
cmd = ['add-br', @resource[:bridge]]

View File

@ -56,6 +56,8 @@ Puppet::Type.type(:l2_patch).provide(:lnx, :parent => Puppet::Provider::Lnx_base
debug("CREATE resource: #{@resource}")
@old_property_hash = {}
@property_flush = {}.merge! @resource
self.class.ensure_upstart_state_file(@resource[:jacks][0])
self.class.ensure_upstart_state_file(@resource[:jacks][1])
patch_name = L23network.get_patch_name(@resource[:jacks])
begin
self.class.iproute(['link', 'add', 'dev', @resource[:jacks][0], 'type', 'veth', 'peer', 'name', @resource[:jacks][1]])

View File

@ -43,6 +43,7 @@ Puppet::Type.type(:l2_port).provide(:lnx, :parent => Puppet::Provider::Lnx_base)
debug("CREATE resource: #{@resource}")
@old_property_hash = {}
@property_flush = {}.merge! @resource
self.class.ensure_upstart_state_file(@resource[:interface])
# todo: divide simple creating interface and vlan
begin
self.class.iproute(['link', 'add', 'link', @resource[:vlan_dev], 'name', @resource[:interface], 'type', 'vlan', 'id', @resource[:vlan_id]])

View File

@ -21,6 +21,7 @@ Puppet::Type.type(:l2_port).provide(:ovs, :parent => Puppet::Provider::Ovs_base)
debug("CREATE resource: #{@resource}")
@old_property_hash = {}
@property_flush = {}.merge! @resource
self.class.ensure_upstart_state_file(@resource[:interface])
#
cmd = ['--may-exist', 'add-port', @resource[:bridge], @resource[:interface]]
# # tag and trunks for port

View File

@ -86,6 +86,7 @@ describe Puppet::Type.type(:l2_patch).provider(:ovs) do
provider_br2.stubs(:brctl).with(['addbr', 'br2']).returns(true)
provider_patch.class.stubs(:get_bridges_order_for_patch).with(['br1','br2']).returns(['br1','br2'])
File.stubs(:directory?).with('/sys/class/net/br2/bridge').returns(true)
File.stubs(:directory?).with('/var/run/network').returns(false)
provider_patch.class.stubs(:vsctl).with(
'--may-exist', 'add-port', 'br1', 'p_39a440c1-0', '--', 'set', 'Interface', 'p_39a440c1-0', 'type=internal'
).returns(true)

View File

@ -0,0 +1,81 @@
require 'spec_helper'
type_class = Puppet::Type.type(:l2_port)
provider_class = type_class.provider(:lnx)
describe provider_class do
describe "just ethN card" do
let(:resource) do
type_class.new(
:ensure => 'present',
:bridge => 'br1',
:provider => :lnx,
:name => 'eth1',
)
end
let(:provider) { resource.provider }
before(:each) do
puppet_debug_override()
end
it 'should exists' do
expect(provider).not_to be_nil
end
it "Test for existing interface" do
# this test emulates. that eth1 already initialized by 'ifup' command
# create never call for HW interfaces, but it's not matter for this test
provider.class.stubs(:iproute).returns(true)
File.stubs(:directory?).with('/var/run/network').returns(true)
File.stubs(:file?).with('/var/run/network/ifstate').returns(true)
File.stubs(:file?).with('/var/run/network/ifstate.eth1').returns(true)
File.stubs(:new).with('/var/run/network/ifstate', 'r').returns(StringIO.new("eth0=eth0\neth1=eth1\neth2=eth2\n"))
provider.create
end
end
describe "802.1q vlan subinterface" do
let(:resource) do
type_class.new(
:ensure => 'present',
:bridge => 'br1',
:provider => :lnx,
:use_ovs => false,
:name => 'eth1.101',
:vlan_dev => 'eth1',
:vlan_id => 101,
)
end
let(:provider) { resource.provider }
before(:each) do
puppet_debug_override()
end
it 'should exists' do
expect(provider).not_to be_nil
end
it "Test for newly-creatind interface" do
# this test emulates. that eth1.101 not existed, and should be created
file_f = StringIO.new
file_a = StringIO.new
File.stubs(:directory?).with('/var/run/network').returns(true)
File.stubs(:file?).with('/var/run/network/ifstate').returns(false)
File.stubs(:file?).with('/var/run/network/ifstate.eth1.101').returns(false)
File.stubs(:new).with("/var/run/network/ifstate.eth1.101", mode="w").returns(file_f)
File.stubs(:new).with("/var/run/network/ifstate", mode="w").returns(file_a)
provider.class.stubs(:iproute).with(['link', 'add', 'link', 'eth1', 'name', 'eth1.101', 'type', 'vlan', 'id', 101]).returns(true)
provider.create
expect(file_f.string).to match(/eth1\.101/)
expect(file_a.string).to match(/eth1\.101=eth1\.101/)
end
end
end