[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]

[Ovirt-devel] [PATCH] Managed node configuration generator.



This generator takes as input an object graph for a Host and a map of mac
addresses to interface names on the managed node. It then generates the
configuration details for that managed node (currently, only the NIC
configuration is processed). With those details it generates the content
for a configuration file that will be retrieved by the managed node and
run through augtool to configure the managed node.

Signed-off-by: Darryl L. Pierce <dpierce redhat com>
---
 wui/src/lib/managed_node_configuration.rb          |   50 ++++++++
 wui/src/test/fixtures/hosts.yml                    |    9 ++
 wui/src/test/fixtures/nics.yml                     |    7 +-
 wui/src/test/fixtures/pools.yml                    |    4 +
 .../functional/managed_node_configuration_test.rb  |  123 ++++++++++++++++++++
 5 files changed, 192 insertions(+), 1 deletions(-)
 create mode 100644 wui/src/lib/managed_node_configuration.rb
 create mode 100644 wui/src/test/functional/managed_node_configuration_test.rb

diff --git a/wui/src/lib/managed_node_configuration.rb b/wui/src/lib/managed_node_configuration.rb
new file mode 100644
index 0000000..8933706
--- /dev/null
+++ b/wui/src/lib/managed_node_configuration.rb
@@ -0,0 +1,50 @@
+# 
+# Copyright (C) 2008 Red Hat, Inc.
+# Written by Darryl L. Pierce <dpierce redhat com>.
+#
+# This program is free software; you can redistribute it and/or modify
+# it under the terms of the GNU General Public License as published by
+# the Free Software Foundation; version 2 of the License.
+#
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with this program; if not, write to the Free Software
+# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
+# MA  02110-1301, USA.  A copy of the GNU General Public License is
+# also available at http://www.gnu.org/copyleft/gpl.html.
+
+# +ManagedNodeConfiguration+ takes in the description for a managed node and,
+# from that, generates the configuration file that is consumed the next time
+# the managed node starts up.
+#
+
+require 'stringio'
+
+$: << File.join(File.dirname(__FILE__), "../dutils")
+$: << File.join(File.dirname(__FILE__), "../")
+
+class ManagedNodeConfiguration
+  NIC_ENTRY_PREFIX='/files/etc/sysconfig/network-scripts'
+
+  def self.generate(host, macs)
+    result = StringIO.new
+    
+    host.nics.each do |nic| 
+      iface_name = macs[nic.mac]
+      
+      if iface_name
+        result.puts "rm #{NIC_ENTRY_PREFIX}/ifcfg-#{iface_name}"
+        result.puts "set #{NIC_ENTRY_PREFIX}/ifcfg-#{iface_name}/DEVICE #{iface_name}"
+        result.puts "set #{NIC_ENTRY_PREFIX}/ifcfg-#{iface_name}/IPADDR #{nic.ip_addr}"    if nic.ip_addr
+        result.puts "set #{NIC_ENTRY_PREFIX}/ifcfg-#{iface_name}/BOOTPROTO dhcp"           if nic.ip_addr == nil            
+        result.puts "set #{NIC_ENTRY_PREFIX}/ifcfg-#{iface_name}/BRIDGE #{nic.bridge}"     if nic.bridge       
+      end
+    end
+
+    result.string
+  end
+end
diff --git a/wui/src/test/fixtures/hosts.yml b/wui/src/test/fixtures/hosts.yml
index f10a756..64707da 100644
--- a/wui/src/test/fixtures/hosts.yml
+++ b/wui/src/test/fixtures/hosts.yml
@@ -1,3 +1,12 @@
+mailservers_managed_node:
+    uuid:             '182a8596-961d-11dc-9387-001558c41534'
+    hostname:         'mail.mynetwork.com'
+    arch:             'i386'
+    memory:           16384
+    is_disabled:      0
+    hypervisor_type:  'kvm'
+    hardware_pool_id: <%= Fixtures.identify(:prodops_pool) %>
+
 one:
   id: 1
   uuid: '1148fdf8-961d-11dc-9387-001558c41534'
diff --git a/wui/src/test/fixtures/nics.yml b/wui/src/test/fixtures/nics.yml
index 008cfb7..c37f3d4 100644
--- a/wui/src/test/fixtures/nics.yml
+++ b/wui/src/test/fixtures/nics.yml
@@ -1,4 +1,9 @@
-# Read about fixtures at http://ar.rubyonrails.org/classes/Fixtures.html
+mailserver_nic_one:
+    mac:        '00:11:22:33:44:55'
+    usage_type: '1'
+    bandwidth:  100
+    host_id:    <%= Fixtures.identify(:mailservers_managed_node) %>
+
 one:
   id: 1
   mac: '00:11:22:33:44:55'
diff --git a/wui/src/test/fixtures/pools.yml b/wui/src/test/fixtures/pools.yml
index 4cf7c97..91dd6e2 100644
--- a/wui/src/test/fixtures/pools.yml
+++ b/wui/src/test/fixtures/pools.yml
@@ -1,3 +1,7 @@
+prodops_pool:
+    name: 'Production Operations'
+    type: 'HardwarePool'
+
 one:
   id: 1
   name: 'master pool'
diff --git a/wui/src/test/functional/managed_node_configuration_test.rb b/wui/src/test/functional/managed_node_configuration_test.rb
new file mode 100644
index 0000000..f5ffb19
--- /dev/null
+++ b/wui/src/test/functional/managed_node_configuration_test.rb
@@ -0,0 +1,123 @@
+# 
+# Copyright (C) 2008 Red Hat, Inc.
+# Written by Darryl L. Pierce <dpierce redhat com>.
+#
+# This program is free software; you can redistribute it and/or modify
+# it under the terms of the GNU General Public License as published by
+# the Free Software Foundation; version 2 of the License.
+#
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with this program; if not, write to the Free Software
+# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
+# MA  02110-1301, USA.  A copy of the GNU General Public License is
+# also available at http://www.gnu.org/copyleft/gpl.html.
+
+require File.dirname(__FILE__) + '/../test_helper'
+require 'test/unit'
+require 'managed_node_configuration'
+
+# Performs unit tests on the +ManagedNodeConfiguration+ class.
+#
+class ManagedNodeConfigurationTest < Test::Unit::TestCase
+  def setup
+    @host   = Host.new    
+    @nic    = Nic.new(:mac => '00:11:22:33:44:55')    
+    @host.nics << @nic
+  end
+  
+  # Ensures that network interfaces uses DHCP when no IP address is specified.
+  #
+  def test_generate_with_no_ip_address
+    expected = <<-HERE
+rm /files/etc/sysconfig/network-scripts/ifcfg-eth0
+set /files/etc/sysconfig/network-scripts/ifcfg-eth0/DEVICE eth0
+set /files/etc/sysconfig/network-scripts/ifcfg-eth0/BOOTPROTO dhcp
+    HERE
+    
+    result = ManagedNodeConfiguration.generate(
+      @host, 
+      {'00:11:22:33:44:55' => 'eth0'}
+    )
+    
+    assert_equal expected, result
+  end
+  
+  # Ensures that network interfaces use the IP address when it's provided.
+  #
+  def test_generate_with_ip_address  
+    @nic.ip_addr = '192.168.2.1'
+    
+    expected = <<-HERE
+rm /files/etc/sysconfig/network-scripts/ifcfg-eth0
+set /files/etc/sysconfig/network-scripts/ifcfg-eth0/DEVICE eth0
+set /files/etc/sysconfig/network-scripts/ifcfg-eth0/IPADDR 192.168.2.1
+    HERE
+    
+    result = ManagedNodeConfiguration.generate(
+      @host,
+      {'00:11:22:33:44:55' => 'eth0'}
+    )
+    
+    assert_equal expected, result
+  end
+  
+  # Ensures the bridge is added to the configuration if one is defined.
+  #
+  def test_generate_with_bridge
+    @nic.bridge = 'ovirtbr0'
+  
+    expected = <<-HERE
+rm /files/etc/sysconfig/network-scripts/ifcfg-eth0
+set /files/etc/sysconfig/network-scripts/ifcfg-eth0/DEVICE eth0
+set /files/etc/sysconfig/network-scripts/ifcfg-eth0/BOOTPROTO dhcp
+set /files/etc/sysconfig/network-scripts/ifcfg-eth0/BRIDGE ovirtbr0
+    HERE
+    
+    result = ManagedNodeConfiguration.generate(
+      @host,
+      {'00:11:22:33:44:55' => 'eth0'}
+    )
+    
+    assert_equal expected, result
+  end
+  
+  # Ensures that more than one NIC is successfully processed.
+  #
+  def test_generate_with_multiple_nics
+    @host.nics << Nic.new(:mac => '11:22:33:44:55:66', :ip_addr => '172.31.0.15')
+    @host.nics << Nic.new(:mac => '22:33:44:55:66:77', :ip_addr => '172.31.0.100')
+    @host.nics << Nic.new(:mac => '33:44:55:66:77:88')
+    
+
+    expected = <<-HERE
+rm /files/etc/sysconfig/network-scripts/ifcfg-eth0
+set /files/etc/sysconfig/network-scripts/ifcfg-eth0/DEVICE eth0
+set /files/etc/sysconfig/network-scripts/ifcfg-eth0/BOOTPROTO dhcp
+rm /files/etc/sysconfig/network-scripts/ifcfg-eth1
+set /files/etc/sysconfig/network-scripts/ifcfg-eth1/DEVICE eth1
+set /files/etc/sysconfig/network-scripts/ifcfg-eth1/IPADDR 172.31.0.15
+rm /files/etc/sysconfig/network-scripts/ifcfg-eth2
+set /files/etc/sysconfig/network-scripts/ifcfg-eth2/DEVICE eth2
+set /files/etc/sysconfig/network-scripts/ifcfg-eth2/IPADDR 172.31.0.100
+rm /files/etc/sysconfig/network-scripts/ifcfg-eth3
+set /files/etc/sysconfig/network-scripts/ifcfg-eth3/DEVICE eth3
+set /files/etc/sysconfig/network-scripts/ifcfg-eth3/BOOTPROTO dhcp
+    HERE
+    
+    result = ManagedNodeConfiguration.generate(
+      @host,
+      {
+        '00:11:22:33:44:55' => 'eth0',
+        '11:22:33:44:55:66' => 'eth1',
+        '22:33:44:55:66:77' => 'eth2',
+        '33:44:55:66:77:88' => 'eth3'
+      })
+    
+    assert_equal expected, result      
+  end
+end
\ No newline at end of file
-- 
1.5.5.1


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]