[Ovirt-devel] [PATCH] Added a configuration generation for managed nodes. It takes as input a
Darryl L. Pierce
dpierce at redhat.com
Thu Aug 21 17:12:31 UTC 2008
---
wui/src/app/controllers/application.rb | 2 +-
wui/src/lib/managed_node_configuration.rb | 53 +++++++++++
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 | 98 ++++++++++++++++++++
6 files changed, 171 insertions(+), 2 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/app/controllers/application.rb b/wui/src/app/controllers/application.rb
index d653171..b27ddbe 100644
--- a/wui/src/app/controllers/application.rb
+++ b/wui/src/app/controllers/application.rb
@@ -35,7 +35,7 @@ class ApplicationController < ActionController::Base
before_filter :is_logged_in
def is_logged_in
- redirect_to (:controller => "login", :action => "login") unless get_login_user
+ redirect_to(:controller => "login", :action => "login") unless get_login_user
end
def get_login_user
diff --git a/wui/src/lib/managed_node_configuration.rb b/wui/src/lib/managed_node_configuration.rb
new file mode 100644
index 0000000..6d6b7c9
--- /dev/null
+++ b/wui/src/lib/managed_node_configuration.rb
@@ -0,0 +1,53 @@
+#
+# Copyright (C) 2008 Red Hat, Inc.
+# Written by Darryl L. Pierce <dpierce at 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
+ result.puts ""
+ end
+ end
+
+ result.puts "save"
+
+ 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..195ec6c
--- /dev/null
+++ b/wui/src/test/functional/managed_node_configuration_test.rb
@@ -0,0 +1,98 @@
+#
+# Copyright (C) 2008 Red Hat, Inc.
+# Written by Darryl L. Pierce <dpierce at 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.
+
+$:.unshift File.join(File.dirname(__FILE__),'..','lib')
+
+require File.dirname(__FILE__) + '/../test_helper'
+require 'test/unit'
+require 'managed_node_configuration'
+require 'dutils'
+
+# 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
+
+save
+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
+
+save
+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
+
+save
+HERE
+
+ result = ManagedNodeConfiguration.generate(
+ @host,
+ {'00:11:22:33:44:55' => 'eth0'}
+ )
+
+ assert_equal expected, result
+ end
+
+end
--
1.5.5.1
More information about the ovirt-devel
mailing list