Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
25 commits
Select commit Hold shift + click to select a range
cc5936f
[CHEF-3029] Redhat ifconfig provider detached
zuazo Feb 8, 2013
8fdc8c1
[CHEF-3029] some requires and class names fixed
zuazo Feb 8, 2013
4073b75
[CHEF-3029] ifconfig debian provider added
zuazo Feb 8, 2013
ba9d0c4
[CHEF-3029] first working version of Ifconfig::Debian provider
zuazo Feb 9, 2013
27c2231
[CHEF-3029] removed trailing whitespaces generated by Ifconfig::Redha…
zuazo Feb 9, 2013
6772a00
[CHEF-3029] Ifconfig:Debian, end-of-line comments are not supported, …
zuazo Feb 9, 2013
fc34adc
[CHEF-3029] added hwaddr, metric and mtu configuration support to Ifc…
zuazo Feb 9, 2013
97ff35e
[CHEF-3029] Provider::Ifconfig tests fixed
zuazo Feb 9, 2013
ee0cb15
[CHEF-3029] Provider::Ifconfig some regex improved on tests
zuazo Feb 9, 2013
9bc7be2
[CHEF-3029] Provider::Ifconfig::Debian basic unit tests created
zuazo Feb 10, 2013
367d9f8
[CHEF-3029] Provider::Ifconfig::Debian unit tests improved
zuazo Feb 10, 2013
497eda4
[CHEF-3029] Provider::Ifconfig::Redhat tests little clean up
zuazo Feb 10, 2013
c00daf8
[CHEF-3029] Chef::Version tests fixed to test versions such as "1" co…
zuazo Feb 10, 2013
5e778f6
[CHEF-3029] Tests added to Provider::Ifconfig for testing redhat, ubu…
zuazo Feb 10, 2013
e4313b9
[CHEF-3029] Provider::Ifconfig::Debian why_run support fixed
zuazo Feb 10, 2013
0e6aa85
[CHEF-3029] a typo in Ifconfig::Debian unit tests
zuazo Feb 10, 2013
aa16cc7
[CHEF-3029] Avoid code duplication in Ifconfig::Debian and Ifconfig::…
zuazo Feb 10, 2013
36c7642
[CHEF-3029] some empty lines and whitespaces cleaned up
zuazo Feb 10, 2013
1ad492b
[CHEF-3029] Ifconfig::Debian interfaces.d directory creation fixed
zuazo Feb 10, 2013
c811410
[CHEF-3029] require fileutils moved one line below
zuazo Feb 10, 2013
e036dc6
[CHEF-3029] Removed accidental code duplication inside Provider::Ifco…
zuazo Feb 10, 2013
f401c94
[CHEF-3029] source directive using full path (required to work properly)
zuazo Feb 11, 2013
b5a0d73
[CHEF-3029] removed not needed "require"
zuazo Feb 11, 2013
35e2417
[CHEF-3029] onboot improved/fixed in Provider::Ifconfig::Redhat
zuazo Feb 11, 2013
2bae329
[CHEF-3029] fixed old tests from Version, already exists in the Versi…
zuazo Apr 1, 2013
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 13 additions & 3 deletions lib/chef/platform.rb
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
require 'chef/provider/mount'
require 'chef/provider/service'
require 'chef/provider/package'
require 'chef/provider/ifconfig'


class Chef
Expand Down Expand Up @@ -79,6 +80,9 @@ def platforms
:service => Chef::Provider::Service::Debian,
:cron => Chef::Provider::Cron,
:mdadm => Chef::Provider::Mdadm
},
'>= 11.10' => {
:ifconfig => Chef::Provider::Ifconfig::Debian
}
},
:linaro => {
Expand Down Expand Up @@ -114,6 +118,9 @@ def platforms
},
">= 6.0" => {
:service => Chef::Provider::Service::Insserv
},
">= 7.0" => {
:ifconfig => Chef::Provider::Ifconfig::Debian
}
},
:xenserver => {
Expand All @@ -129,7 +136,8 @@ def platforms
:service => Chef::Provider::Service::Redhat,
:cron => Chef::Provider::Cron,
:package => Chef::Provider::Package::Yum,
:mdadm => Chef::Provider::Mdadm
:mdadm => Chef::Provider::Mdadm,
:ifconfig => Chef::Provider::Ifconfig::Redhat
}
},
:amazon => {
Expand All @@ -153,7 +161,8 @@ def platforms
:service => Chef::Provider::Service::Redhat,
:cron => Chef::Provider::Cron,
:package => Chef::Provider::Package::Yum,
:mdadm => Chef::Provider::Mdadm
:mdadm => Chef::Provider::Mdadm,
:ifconfig => Chef::Provider::Ifconfig::Redhat
}
},
:suse => {
Expand All @@ -177,7 +186,8 @@ def platforms
:service => Chef::Provider::Service::Redhat,
:cron => Chef::Provider::Cron,
:package => Chef::Provider::Package::Yum,
:mdadm => Chef::Provider::Mdadm
:mdadm => Chef::Provider::Mdadm,
:ifconfig => Chef::Provider::Ifconfig::Redhat
}
},
:gentoo => {
Expand Down
60 changes: 25 additions & 35 deletions lib/chef/provider/ifconfig.rb
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,15 @@ class Provider
class Ifconfig < Chef::Provider
include Chef::Mixin::Command

attr_accessor :config_template
attr_accessor :config_path

def initialize(new_resource, run_context)
super(new_resource, run_context)
@config_template = nil
@config_path = nil
end

def whyrun_supported?
true
end
Expand Down Expand Up @@ -163,50 +172,31 @@ def action_disable
end
end

def can_generate_config?
! @config_template.nil? and ! @config_path.nil?
end

def generate_config
return unless can_generate_config?
b = binding
case node[:platform]
when "centos","redhat","fedora"
content = %{
<% if @new_resource.device %>DEVICE=<%= @new_resource.device %><% end %>
<% if @new_resource.onboot %>ONBOOT=<%= @new_resource.onboot %><% end %>
<% if @new_resource.bootproto %>BOOTPROTO=<%= @new_resource.bootproto %><% end %>
<% if @new_resource.target %>IPADDR=<%= @new_resource.target %><% end %>
<% if @new_resource.mask %>NETMASK=<%= @new_resource.mask %><% end %>
<% if @new_resource.network %>NETWORK=<%= @new_resource.network %><% end %>
<% if @new_resource.bcast %>BROADCAST=<%= @new_resource.bcast %><% end %>
<% if @new_resource.onparent %>ONPARENT=<%= @new_resource.onparent %><% end %>
}
template = ::ERB.new(content)
network_file_name = "/etc/sysconfig/network-scripts/ifcfg-#{@new_resource.device}"
converge_by ("generate configuration file : #{network_file_name}") do
network_file = ::File.new(network_file_name, "w")
network_file.puts(template.result(b))
network_file.close
end
Chef::Log.info("#{@new_resource} created configuration file")
when "debian","ubuntu"
# template
when "slackware"
# template
template = ::ERB.new(@config_template)
converge_by ("generate configuration file : #{@config_path}") do
network_file = ::File.new(@config_path, "w")
network_file.puts(template.result(b))
network_file.close
end
Chef::Log.info("#{@new_resource} created configuration file")
end

def delete_config
return unless can_generate_config?
require 'fileutils'
case node[:platform]
when "centos","redhat","fedora"
ifcfg_file = "/etc/sysconfig/network-scripts/ifcfg-#{@new_resource.device}"
if ::File.exist?(ifcfg_file)
converge_by ("delete the #{ifcfg_file}") do
FileUtils.rm_f(ifcfg_file, :verbose => false)
end
if ::File.exist?(@config_path)
converge_by ("delete the #{@config_path}") do
FileUtils.rm_f(@config_path, :verbose => false)
end
when "debian","ubuntu"
# delete configs
when "slackware"
# delete configs
end
Chef::Log.info("#{@new_resource} deleted configuration file")
end

end
Expand Down
71 changes: 71 additions & 0 deletions lib/chef/provider/ifconfig/debian.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
#
# Author:: Xabier de Zuazo (xabier@onddo.com)
# Copyright:: Copyright (c) 2013 Onddo Labs, SL.
# License:: Apache License, Version 2.0
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#

require 'chef/provider/ifconfig'
require 'chef/util/file_edit'

class Chef
class Provider
class Ifconfig
class Debian < Chef::Provider::Ifconfig

def initialize(new_resource, run_context)
super(new_resource, run_context)
@config_template = %{
<% if @new_resource.device %>
<% if @new_resource.onboot == "yes" %>auto <%= @new_resource.device %><% end %>
<% case @new_resource.bootproto
when "dhcp" %>
iface <%= @new_resource.device %> inet dhcp
<% when "bootp" %>
iface <%= @new_resource.device %> inet bootp
<% else %>
iface <%= @new_resource.device %> inet static
<% if @new_resource.target %>address <%= @new_resource.target %><% end %>
<% if @new_resource.mask %>netmask <%= @new_resource.mask %><% end %>
<% if @new_resource.network %>network <%= @new_resource.network %><% end %>
<% if @new_resource.bcast %>broadcast <%= @new_resource.bcast %><% end %>
<% if @new_resource.metric %>metric <%= @new_resource.metric %><% end %>
<% if @new_resource.hwaddr %>hwaddress <%= @new_resource.hwaddr %><% end %>
<% if @new_resource.mtu %>mtu <%= @new_resource.mtu %><% end %>
<% end %>
<% end %>
}
@config_path = "/etc/network/interfaces.d/ifcfg-#{@new_resource.device}"
end

def generate_config
check_interfaces_config
super
end

protected

def check_interfaces_config
converge_by ('modify configuration file : /etc/network/interfaces') do
Dir.mkdir('/etc/network/interfaces.d') unless ::File.directory?('/etc/network/interfaces.d')
conf = Chef::Util::FileEdit.new('/etc/network/interfaces')
conf.insert_line_if_no_match('^\s*source\s+/etc/network/interfaces[.]d/[*]\s*$', 'source /etc/network/interfaces.d/*')
conf.write_file
end
end

end
end
end
end
47 changes: 47 additions & 0 deletions lib/chef/provider/ifconfig/redhat.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
#
# Author:: Xabier de Zuazo (xabier@onddo.com)
# Copyright:: Copyright (c) 2013 Onddo Labs, SL.
# License:: Apache License, Version 2.0
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#

require 'chef/provider/ifconfig'

class Chef
class Provider
class Ifconfig
class Redhat < Chef::Provider::Ifconfig

def initialize(new_resource, run_context)
super(new_resource, run_context)
@config_template = %{
<% if @new_resource.device %>DEVICE=<%= @new_resource.device %><% end %>
<% if @new_resource.onboot == "yes" %>ONBOOT=<%= @new_resource.onboot %><% end %>
<% if @new_resource.bootproto %>BOOTPROTO=<%= @new_resource.bootproto %><% end %>
<% if @new_resource.target %>IPADDR=<%= @new_resource.target %><% end %>
<% if @new_resource.mask %>NETMASK=<%= @new_resource.mask %><% end %>
<% if @new_resource.network %>NETWORK=<%= @new_resource.network %><% end %>
<% if @new_resource.bcast %>BROADCAST=<%= @new_resource.bcast %><% end %>
<% if @new_resource.onparent %>ONPARENT=<%= @new_resource.onparent %><% end %>
<% if @new_resource.hwaddr %>HWADDR=<%= @new_resource.hwaddr %><% end %>
<% if @new_resource.metric %>METRIC=<%= @new_resource.metric %><% end %>
<% if @new_resource.mtu %>MTU=<%= @new_resource.mtu %><% end %>
}
@config_path = "/etc/sysconfig/network-scripts/ifcfg-#{@new_resource.device}"
end

end
end
end
end
3 changes: 3 additions & 0 deletions lib/chef/providers.rb
Original file line number Diff line number Diff line change
Expand Up @@ -103,3 +103,6 @@

require "chef/provider/lwrp_base"
require 'chef/provider/registry_key'

require 'chef/provider/ifconfig/redhat'
require 'chef/provider/ifconfig/debian'
2 changes: 1 addition & 1 deletion lib/chef/util/file_edit.rb
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ def initialize(filepath)
@file_edited = false

raise ArgumentError, "File doesn't exist" unless File.exist? @original_pathname
raise ArgumentError, "File is blank" unless (@contents = File.new(@original_pathname).readlines).length > 0
@contents = File.new(@original_pathname).readlines
end

#search the file line by line and match each line with the given regex
Expand Down
88 changes: 88 additions & 0 deletions spec/unit/provider/ifconfig/debian_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
#
# Author:: Xabier de Zuazo (xabier@onddo.com)
# Copyright:: Copyright (c) 2013 Onddo Labs, SL.
# License:: Apache License, Version 2.0
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#

require 'spec_helper'
require 'chef/exceptions'

describe Chef::Provider::Ifconfig::Debian do
before do
@node = Chef::Node.new
@cookbook_collection = Chef::CookbookCollection.new([])
@events = Chef::EventDispatch::Dispatcher.new
@run_context = Chef::RunContext.new(@node, @cookbook_collection, @events)
#This new_resource can be called anything --> it is not the same as in ifconfig.rb
@new_resource = Chef::Resource::Ifconfig.new("10.0.0.1", @run_context)
@new_resource.mask "255.255.254.0"
@new_resource.metric "1"
@new_resource.mtu "1500"
@new_resource.device "eth0"
@provider = Chef::Provider::Ifconfig::Debian.new(@new_resource, @run_context)
@current_resource = Chef::Resource::Ifconfig.new("10.0.0.1", @run_context)

status = mock("Status", :exitstatus => 0)
@provider.instance_variable_set("@status", status)
@provider.current_resource = @current_resource
@provider.stub!(:load_current_resource)
@provider.stub!(:run_command)

@config_filename_ifaces = "/etc/network/interfaces"
@config_filename_ifcfg = "/etc/network/interfaces.d/ifcfg-#{@new_resource.device}"
end

describe "generate_config for action_add" do
before do
@config_file_ifaces = StringIO.new
@config_file_ifcfg = StringIO.new
FileUtils.should_receive(:cp)
File.should_receive(:new).with(@config_filename_ifaces).and_return(StringIO.new)
File.should_receive(:open).with(@config_filename_ifaces, "w").and_yield(@config_file_ifaces)
File.should_receive(:new).with(@config_filename_ifcfg, "w").and_return(@config_file_ifcfg)
end

it "should create network-scripts directory" do
File.should_receive(:directory?).with(File.dirname(@config_filename_ifcfg)).and_return(false)
Dir.should_receive(:mkdir).with(File.dirname(@config_filename_ifcfg))
@provider.run_action(:add)
end

it "should write configure network-scripts directory" do
File.should_receive(:directory?).with(File.dirname(@config_filename_ifcfg)).and_return(true)
@provider.run_action(:add)
@config_file_ifaces.string.should match(/^\s*source\s+\/etc\/network\/interfaces[.]d\/[*]\s*$/)
end

it "should write a network-script" do
File.should_receive(:directory?).with(File.dirname(@config_filename_ifcfg)).and_return(true)
@provider.run_action(:add)
@config_file_ifcfg.string.should match(/^iface eth0 inet static\s*$/)
@config_file_ifcfg.string.should match(/^\s+address 10\.0\.0\.1\s*$/)
@config_file_ifcfg.string.should match(/^\s+netmask 255\.255\.254\.0\s*$/)
end
end

describe "delete_config for action_delete" do

it "should delete network-script if it exists" do
@current_resource.device @new_resource.device
File.should_receive(:exist?).with(@config_filename_ifcfg).and_return(true)
FileUtils.should_receive(:rm_f).with(@config_filename_ifcfg, :verbose => false)

@provider.run_action(:delete)
end
end
end
Loading