aboutsummaryrefslogtreecommitdiffstats
path: root/lib
diff options
context:
space:
mode:
authorFeng Pan <fpan@redhat.com>2016-11-07 19:22:21 -0500
committerFeng Pan <fpan@redhat.com>2016-11-21 21:27:09 -0500
commit9326e4237f4d161f297dc4493ab4928ea6e2bf0f (patch)
tree25372fa964fe84b160c5bfc58304285902edbefc /lib
parentc70bea9fb9cd5dfb29d7ab8f7aaae7324c31f1e3 (diff)
Initial Commit.
Change-Id: I212ec4be42357edddd931e9e479e33131ccd4bac Signed-off-by: Feng Pan <fpan@redhat.com>
Diffstat (limited to 'lib')
-rw-r--r--lib/puppet/provider/vpp_service/vpp.rb115
-rw-r--r--lib/puppet/type/vpp_service.rb46
2 files changed, 161 insertions, 0 deletions
diff --git a/lib/puppet/provider/vpp_service/vpp.rb b/lib/puppet/provider/vpp_service/vpp.rb
new file mode 100644
index 0000000..e1c702c
--- /dev/null
+++ b/lib/puppet/provider/vpp_service/vpp.rb
@@ -0,0 +1,115 @@
+Puppet::Type.type(:vpp_service).provide :vpp do
+
+ commands :vppctlcmd => "vppctl"
+ commands :systemctlcmd => "systemctl"
+
+ def get_int_prefix(name)
+ if %r{([[:alpha:]]*#{name})\s+} =~ `vppctl show int`
+ return $1
+ else
+ raise Puppet::Error.new("Cannot find vpp interface matching: #{name}")
+ end
+ end
+
+ def convert_pci_addr(pci_dev)
+ if pci_dev =~ /\p{XDigit}+:(\p{XDigit}+):(\p{XDigit}+)\.(\p{XDigit}+)/
+ return "%x/%x/%x" % ["0x#{$1}".hex, "0x#{$2}".hex, "0x#{$3}".hex]
+ else
+ raise Puppet::Error.new("Incorrect pci dev format: #{pci_dev}")
+ end
+ end
+
+ def vpp_pre_config
+ @resource[:pci_devs].each do |pci_dev|
+ Facter.value(:interfaces).split(',').each do |kernel_nic|
+ if pci_dev == `ethtool -i #{kernel_nic} | grep bus-info | awk '{print $2}'`.strip
+ unless system("ip link set dev #{kernel_nic} down")
+ raise Puppet::Error.new("Failed to shut down kernel nic #{kernel_nic}")
+ end
+
+ #Disable NIC on boot
+ file_data = ""
+ onboot_exists = false
+ if File.exist?("/etc/sysconfig/network-scripts/ifcfg-#{kernel_nic}")
+ IO.foreach("/etc/sysconfig/network-scripts/ifcfg-#{kernel_nic}") do |line|
+ if /ONBOOT/.match(line)
+ onboot_exists = true
+ file_data += "ONBOOT=no\n"
+ else
+ file_data += line
+ end
+ end
+ unless onboot_exists
+ file_data += "ONBOOT=no"
+ end
+ File.open("/etc/sysconfig/network-scripts/ifcfg-#{kernel_nic}", "w") {|file| file.puts file_data}
+ end
+
+ if Facter.value("ipaddress_#{kernel_nic}")
+ @int_ip_mapping[pci_dev] = Facter.value("ipaddress_#{kernel_nic}") + "/" + Facter.value("netmask_#{kernel_nic}")
+ end
+ end
+ end
+ end
+ end
+
+ def configure_vpp_interfaces
+ @resource[:pci_devs].each do |pci_dev|
+ vpp_int_name= get_int_prefix(convert_pci_addr(pci_dev))
+ vppctlcmd "set int state", vpp_int_name, @resource[:state]
+ if @resource[:copy_kernel_nic_ip] && @int_ip_mapping.has_key?(pci_dev)
+ vppctlcmd "set int ip address", vpp_int_name, @int_ip_mapping[pci_dev]
+ end
+ end
+ end
+
+ def create
+ @int_ip_mapping = {}
+ vpp_pre_config
+
+ #Bring up VPP service
+ systemctlcmd "restart", "vpp"
+ systemctlcmd "enable", "vpp"
+ sleep 10
+ systemctlcmd "is-active", "vpp"
+ systemctlcmd "is-enabled", "vpp"
+
+ #Configure VPP interfaces
+ configure_vpp_interfaces
+ end
+
+ def destroy
+ systemctlcmd "stop", "vpp"
+ systemctlcmd "disable", "vpp"
+ end
+
+ def exists?
+ if system("systemctl is-active vpp --quiet")
+ @resource[:pci_devs].each do |pci_dev|
+ int_name_str = convert_pci_addr(pci_dev)
+ if %r{([[:alpha:]]*#{int_name_str})\s+} !~ `vppctl show int`
+ return false
+ end
+ end
+ else
+ return false
+ end
+ return true
+ end
+
+ def state
+ @resource[:pci_devs].each do |pci_dev|
+ vpp_int_output = `vppctl show int #{get_int_prefix(convert_pci_addr(pci_dev))}`
+ if ! /\s+up\s+/.match(vpp_int_output)
+ return "down"
+ end
+ end
+ return "up"
+ end
+
+ def state=(value)
+ @resource[:pci_devs].each do |pci_dev|
+ vppctlcmd "set int state", get_int_prefix(convert_pci_addr(pci_dev)), value
+ end
+ end
+end \ No newline at end of file
diff --git a/lib/puppet/type/vpp_service.rb b/lib/puppet/type/vpp_service.rb
new file mode 100644
index 0000000..c1c818f
--- /dev/null
+++ b/lib/puppet/type/vpp_service.rb
@@ -0,0 +1,46 @@
+Puppet::Type.newtype(:vpp_service) do
+
+ ensurable
+
+ newparam(:name) do
+ end
+
+ newparam(:pci_devs, :array_matching => :all) do
+ desc "PCI dev addresses to be bound to VPP"
+ def insync?(is)
+ is.sort == should.sort
+ end
+
+ validate do |values|
+ values = [values] unless values.is_a?(Array)
+ values.map! do |value|
+ if value =~ /\p{XDigit}+:(\p{XDigit}+):(\p{XDigit}+)\.(\p{XDigit}+)/
+ value
+ else
+ raise(Puppet::Error, "Incorrect PCI dev address #{value}")
+ end
+ end
+ end
+
+ munge do |values|
+ if values.is_a?(Array)
+ values
+ else
+ [values]
+ end
+ end
+ end
+
+ newproperty(:state) do
+ desc "VPP interface state"
+ defaultto :up
+ newvalues(:up, :down)
+ end
+
+ newparam(:copy_kernel_nic_ip) do
+ desc "Whether to configure VPP interface with kernel NIC's IP settings"
+ defaultto :true
+ newvalues(:true, :false)
+ end
+
+end