[cookbooks] Added network cookbook with network_interface resource
This commit is contained in:
parent
0e81fc2854
commit
26ff2e16a5
|
@ -0,0 +1,26 @@
|
|||
# Network cookbook
|
||||
|
||||
## Resources
|
||||
|
||||
* network_interface - configure network interface
|
||||
|
||||
network_interface 'eth0' do
|
||||
address '192.168.1.2'
|
||||
netmask '255.255.255.0'
|
||||
end
|
||||
|
||||
or
|
||||
|
||||
# configure VLAN interface with automatic address via DHCP
|
||||
network_interface 'eth0.50'
|
||||
|
||||
Attributes:
|
||||
|
||||
* address - IPv4 network interface address
|
||||
* netmask - network mask in IPv4 format (e.g. '255.255.255.0')
|
||||
* vlan - vlan ID
|
||||
* mtu - interface's MTU value
|
||||
* metric - interface's metric value
|
||||
* onboot - start this interface on boot (defaults to true)
|
||||
|
||||
|
|
@ -0,0 +1,10 @@
|
|||
maintainer "Mirantis Inc."
|
||||
maintainer_email "product@mirantis.com"
|
||||
license "Apache 2.0"
|
||||
description "Installs/Configures nailgun"
|
||||
long_description IO.read(File.join(File.dirname(__FILE__), 'README.md'))
|
||||
version "0.0.1"
|
||||
|
||||
supports "centos"
|
||||
|
||||
|
|
@ -0,0 +1,140 @@
|
|||
require 'chef/shell_out'
|
||||
|
||||
def load_current_resource
|
||||
cmd = run_command("ifconfig | awk '/^#{full_device_name} /,/^$/'")
|
||||
|
||||
config = cmd.stdout.strip
|
||||
|
||||
return if config.empty?
|
||||
|
||||
|
||||
interface = Chef::Resource::NetworkInterface.new(new_resource.name)
|
||||
interface.vlan @new_resource.vlan
|
||||
# interface.hwaddress = $1 if /HWaddr (\S+)/ =~ config
|
||||
interface.address $1 if /inet addr:(\S+)/ =~ config
|
||||
interface.netmask $1 if /Mask:(\S+)/ =~ config
|
||||
interface.mtu $1.to_i if /MTU:(\d+)/ =~ config
|
||||
interface.metric $1.to_i if /Metric:(\d+)/ =~ config
|
||||
|
||||
interface.state = (/ UP / =~ config) ? :up : :down
|
||||
|
||||
@current_resource = interface
|
||||
end
|
||||
|
||||
action :create do
|
||||
if !current_resource && new_resource.vlan
|
||||
Chef::Log.debug("Creating VLAN subinterface #{full_device_name}")
|
||||
run_command("vconfig add #{new_resource.device} #{new_resource.vlan}")
|
||||
end
|
||||
|
||||
if current_resource == new_resource
|
||||
Chef::Log.debug("Skipping configuration of network interface as current configuration matches target one")
|
||||
next
|
||||
end
|
||||
|
||||
Chef::Log.info("Configuring network interface #{full_device_name}")
|
||||
|
||||
command = "ifconfig #{full_device_name} #{new_resource.address}"
|
||||
command << " netmask #{new_resource.netmask}" if new_resource.netmask
|
||||
command << " metric #{new_resource.metric}" if new_resource.metric
|
||||
command << " mtu #{new_resource.mtu}" if new_resource.mtu
|
||||
|
||||
Chef::Log.debug("Running command: #{command}")
|
||||
|
||||
run_command(command)
|
||||
|
||||
new_resource.state = current_resource ? current_resource.state : :down
|
||||
new_resource.updated_by_last_action(true)
|
||||
|
||||
create_config
|
||||
end
|
||||
|
||||
action :delete do
|
||||
if current_resource && current_resource.state != :down
|
||||
Chef::Log.info("Disabling network interface #{full_device_name}")
|
||||
|
||||
run_command("ifconfig #{full_device_name} down")
|
||||
|
||||
new_resource.state = :down
|
||||
new_resource.updated_by_last_action(true)
|
||||
end
|
||||
|
||||
if current_resource && current_resource.vlan
|
||||
Chef::Log.debug("Removing networking interface #{full_device_name}")
|
||||
|
||||
run_command("vconfig rem #{full_device_name}")
|
||||
end
|
||||
|
||||
delete_config
|
||||
end
|
||||
|
||||
action :up do
|
||||
if current_resource && current_resource.state == :up
|
||||
Chef::Log.debug("Network interface #{full_device_name} is already up")
|
||||
next
|
||||
end
|
||||
|
||||
Chef::Log.info("Enabling network interface #{full_device_name}")
|
||||
|
||||
run_command("ifconfig #{full_device_name} up")
|
||||
|
||||
new_resource.state = :up
|
||||
new_resource.updated_by_last_action(true)
|
||||
end
|
||||
|
||||
action :down do
|
||||
if current_resource && current_resource.state == :down
|
||||
Chef::Log.debug("Network interface #{full_device_name} is already down")
|
||||
next
|
||||
end
|
||||
|
||||
Chef::Log.info("Disabling network interface #{full_device_name}")
|
||||
|
||||
run_command("ifconfig #{full_device_name} down")
|
||||
|
||||
new_resource.state = :down
|
||||
new_resource.updated_by_last_action(true)
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def full_device_name
|
||||
name = new_resource.device
|
||||
name += ".#{new_resource.vlan}" if new_resource.vlan
|
||||
name
|
||||
end
|
||||
|
||||
def create_config
|
||||
template "/etc/sysconfig/network-scripts/ifcfg-#{full_device_name}" do
|
||||
source 'ifcfg.erb'
|
||||
owner 'root'
|
||||
group 'root'
|
||||
mode 0644
|
||||
|
||||
variables({
|
||||
:device => new_resource.device,
|
||||
:vlan => new_resource.vlan,
|
||||
:address => new_resource.address,
|
||||
:netmask => new_resource.netmask,
|
||||
:onboot => new_resource.onboot,
|
||||
})
|
||||
|
||||
action :create
|
||||
end
|
||||
end
|
||||
|
||||
def delete_config
|
||||
template "/etc/sysconfig/network-scripts/ifcfg-#{full_device_name}" do
|
||||
source 'ifcfg.erb'
|
||||
|
||||
action :delete
|
||||
end
|
||||
end
|
||||
|
||||
def run_command(command, options={})
|
||||
cmd = Chef::ShellOut.new(command, options)
|
||||
cmd.run_command
|
||||
cmd.error!
|
||||
cmd
|
||||
end
|
||||
|
|
@ -0,0 +1,8 @@
|
|||
|
||||
case node.platform
|
||||
when 'centos', 'redhat', 'fedora'
|
||||
package 'vconfig'
|
||||
when 'ubuntu', 'debian'
|
||||
package 'vlan'
|
||||
end
|
||||
|
|
@ -0,0 +1,55 @@
|
|||
require 'chef/mixin/language_include_recipe'
|
||||
|
||||
include Chef::Mixin::LanguageIncludeRecipe
|
||||
|
||||
actions :create, :delete, :up, :down
|
||||
|
||||
attribute :device, :name_attribute => true, :kind_of => String, :required => true
|
||||
attribute :vlan, :kind_of => Integer
|
||||
|
||||
attribute :address, :kind_of => String
|
||||
attribute :netmask
|
||||
|
||||
attribute :mtu, :kind_of => Integer
|
||||
attribute :metric, :kind_of => Integer
|
||||
|
||||
attribute :onboot, :default => true
|
||||
|
||||
|
||||
attr_accessor :state
|
||||
|
||||
def initialize(*args)
|
||||
super
|
||||
@action = [:create, :up]
|
||||
end
|
||||
|
||||
def up?
|
||||
state == :up
|
||||
end
|
||||
|
||||
def down?
|
||||
state == :down
|
||||
end
|
||||
|
||||
def device(arg=nil)
|
||||
return @device if arg == nil && instance_variable_defined?(:@device)
|
||||
|
||||
# parse device from name
|
||||
arg = $1 if /(.+)\.\d+/ =~ name
|
||||
|
||||
@device = validate({:device => (arg || name)}, :device => {:required => true, :kind_of => String})[:device]
|
||||
end
|
||||
|
||||
def vlan(arg=nil)
|
||||
return @vlan if arg == nil && instance_variable_defined?(:@vlan)
|
||||
|
||||
# parse vlan from name
|
||||
arg ||= $1.to_i if /.+\.(\d+)/ =~ name
|
||||
|
||||
@vlan = validate({:vlan => (arg)}, :vlan => {:kind_of => Integer})[:vlan]
|
||||
end
|
||||
|
||||
def after_created
|
||||
include_recipe 'network::vlan_support' if vlan
|
||||
end
|
||||
|
|
@ -0,0 +1,15 @@
|
|||
<% if @vlan %>
|
||||
VLAN=yes
|
||||
DEVICE=<%= @device %>.<%= @vlan %>
|
||||
<% else %>
|
||||
DEVICE=<%= @device %>
|
||||
<% end %>
|
||||
TYPE=Ethernet
|
||||
ONBOOT=<%= @onboot ? "yes" : "no" %>
|
||||
<% if @address %>
|
||||
BOOTPROTO=static
|
||||
IPADDR=<%= @address %>
|
||||
NETMASK=<%= @netmask %>
|
||||
<% else %>
|
||||
BOOTPROTO=dhcp
|
||||
<% end %>
|
Loading…
Reference in New Issue