diff options
author | James Cammarata <jimi@sngx.net> | 2009-03-07 21:44:00 -0600 |
---|---|---|
committer | James Cammarata <jimi@sngx.net> | 2009-03-07 21:44:00 -0600 |
commit | d3fdc6d4965b02ce18346f067c14080115943a38 (patch) | |
tree | d2fa630d3f0e941acb7f5115c31ea91aabb4ae11 /contrib/ruby | |
parent | 54e462f3dca472d8bb8dc8a2b0a69c60ed9fe2ec (diff) | |
parent | 61db7baa541acc32b305ea6977a14ee8f5b3f470 (diff) | |
download | cobbler-d3fdc6d4965b02ce18346f067c14080115943a38.tar.gz cobbler-d3fdc6d4965b02ce18346f067c14080115943a38.tar.xz cobbler-d3fdc6d4965b02ce18346f067c14080115943a38.zip |
Merge branch 'devel' of git://git.fedorahosted.org/cobbler into ris-devel
Conflicts:
cobbler/utils.py
Diffstat (limited to 'contrib/ruby')
-rw-r--r-- | contrib/ruby/Rakefile | 2 | ||||
-rwxr-xr-x | contrib/ruby/examples/check_version.rb | 62 | ||||
-rwxr-xr-x | contrib/ruby/examples/create_system.rb | 50 | ||||
-rwxr-xr-x | contrib/ruby/examples/list_systems.rb | 36 | ||||
-rw-r--r-- | contrib/ruby/lib/cobbler.rb | 16 | ||||
-rw-r--r-- | contrib/ruby/lib/cobbler/base.rb | 39 | ||||
-rw-r--r-- | contrib/ruby/lib/cobbler/system.rb | 58 | ||||
-rw-r--r-- | contrib/ruby/test/test_base.rb | 50 | ||||
-rw-r--r-- | contrib/ruby/test/test_system.rb | 4 |
9 files changed, 202 insertions, 115 deletions
diff --git a/contrib/ruby/Rakefile b/contrib/ruby/Rakefile index 7828c0ad..722749cb 100644 --- a/contrib/ruby/Rakefile +++ b/contrib/ruby/Rakefile @@ -22,7 +22,7 @@ require 'rake/testtask' require 'rake/gempackagetask' PKG_NAME='rubygem-cobbler' -PKG_VERSION='0.1.3' +PKG_VERSION='0.1.9' PKG_FILES=FileList[ 'Rakefile', 'README', 'ChangeLog', 'COPYING', 'NEWS', 'TODO', 'lib/**/*.rb', diff --git a/contrib/ruby/examples/check_version.rb b/contrib/ruby/examples/check_version.rb new file mode 100755 index 00000000..1d5653e5 --- /dev/null +++ b/contrib/ruby/examples/check_version.rb @@ -0,0 +1,62 @@ +#!/usr/bin/ruby -w +# +# create_system.rb - example of using rubygem-cobbler to create a system. +# +# Copyright (C) 2008 Red Hat, Inc. +# Written by Darryl L. Pierce <dpierce@redhat.com> +# +# This file is part of rubygem-cobbler. +# +# rubygem-cobbleris free software: you can redistribute it and/or modify +# it under the terms of the GNU Lesser General Public License as published +# by the Free Software Foundation, either version 2.1 of the License, or +# (at your option) any later version. +# +# rubygem-cobbler 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 rubygem-cobbler. If not, see <http://www.gnu.org/licenses/>. +# + +base = File.expand_path(File.join(File.dirname(__FILE__), "..")) +$LOAD_PATH << File.join(base, "lib") +$LOAD_PATH << File.join(base, "examples") + +require 'getoptlong' + +require 'cobbler' + +include Cobbler + +opts = GetoptLong.new( + ['--hostname', '-s', GetoptLong::REQUIRED_ARGUMENT ], + ['--help', '-h', GetoptLong::NO_ARGUMENT]) + +hostname = nil + +def usage + puts "Usage: #{$0} --hostname hostname\n" + exit +end + +opts.each do |opt, arg| + case opt + when '--hostname' then hostname = arg + when '--help' then usage + end +end + +if hostname + Base.hostname = hostname + version = Base.remote_version + if version + puts "Remote version: #{version}" + else + puts "Unable to determine version." + end +else + usage +end diff --git a/contrib/ruby/examples/create_system.rb b/contrib/ruby/examples/create_system.rb index dd938fe1..b2d2138a 100755 --- a/contrib/ruby/examples/create_system.rb +++ b/contrib/ruby/examples/create_system.rb @@ -1,14 +1,14 @@ -#!/usr/bin/ruby -w +#!/usr/bin/ruby -w # # create_system.rb - example of using rubygem-cobbler to create a system. -# +# # Copyright (C) 2008 Red Hat, Inc. # Written by Darryl L. Pierce <dpierce@redhat.com> # # This file is part of rubygem-cobbler. # # rubygem-cobbleris free software: you can redistribute it and/or modify -# it under the terms of the GNU Lesser General Public License as published +# it under the terms of the GNU Lesser General Public License as published # by the Free Software Foundation, either version 2.1 of the License, or # (at your option) any later version. # @@ -20,7 +20,7 @@ # You should have received a copy of the GNU General Public License # along with rubygem-cobbler. If not, see <http://www.gnu.org/licenses/>. # - + base = File.expand_path(File.join(File.dirname(__FILE__), "..")) $LOAD_PATH << File.join(base, "lib") $LOAD_PATH << File.join(base, "examples") @@ -31,41 +31,49 @@ require 'cobbler' include Cobbler -opts = GetoptLong.new( - ['--hostname', '-s', GetoptLong::REQUIRED_ARGUMENT ], - ['--name', '-n', GetoptLong::REQUIRED_ARGUMENT ], - ['--profile', '-f', GetoptLong::REQUIRED_ARGUMENT ], - ['--username', '-u', GetoptLong::REQUIRED_ARGUMENT ], - ['--password', '-p', GetoptLong::REQUIRED_ARGUMENT ], - ['--help', '-h', GetoptLong::NO_ARGUMENT] -) +opts = GetoptLong.new(['--hostname', '-s', GetoptLong::REQUIRED_ARGUMENT ], + ['--name', '-n', GetoptLong::REQUIRED_ARGUMENT ], + ['--profile', '-f', GetoptLong::REQUIRED_ARGUMENT ], + ['--image', '-i', GetoptLong::REQUIRED_ARGUMENT ], + ['--username', '-u', GetoptLong::REQUIRED_ARGUMENT ], + ['--password', '-p', GetoptLong::REQUIRED_ARGUMENT ], + ['--help', '-h', GetoptLong::NO_ARGUMENT], + ['--debug', '-d', GetoptLong::NO_ARGUMENT]) -name = profile = hostname = username = password = nil +name = profile = image = hostname = username = password = debug = nil def usage - puts "Usage: #{$0} --name system-name --profile profile-name [--hostname hostname] [--username username] [--password password]\n" + puts "Usage: #{$0} --name system-name [--profile profile-name | --image image-name ] [--hostname hostname] [--username username] [--password password]\n" exit end - + opts.each do |opt, arg| case opt when '--hostname' then hostname = arg when '--name' then name = arg when '--profile' then profile = arg + when '--image' then image = arg when '--username' then username = arg when '--password' then password = arg when '--help' then usage + when '--debug' then debug = true end end -if name && profile - +if (profile && image) + puts "\nYou must specify either a profile or an image, but not both.\n\n" + usage +end + +if name && (profile || image) System.hostname = hostname if hostname System.username = username if username System.password = password if password - - system = System.new('name' => name,'profile' => profile) - + System.debug = true if debug + + system = System.new('name' => name,'profile' => profile) if profile + system = System.new('name' => name,'image' => image) if image + system.interfaces=[NetworkInterface.new({'mac_address' => '00:11:22:33:44:55:66:77'})] puts "Saving a new system with name #{system.name} based on the profile #{system.profile}." @@ -78,4 +86,4 @@ if name && profile end else usage -end
\ No newline at end of file +end diff --git a/contrib/ruby/examples/list_systems.rb b/contrib/ruby/examples/list_systems.rb index 61afe800..4474905a 100755 --- a/contrib/ruby/examples/list_systems.rb +++ b/contrib/ruby/examples/list_systems.rb @@ -1,14 +1,14 @@ -#!/usr/bin/ruby +#!/usr/bin/ruby # # list_systems.rb - example of using rubygem-cobbler to list system. -# -# Copyright (C) 2008 Red Hat, Inc. +# +# Copyright (C) 2008, 2009, Red Hat, Inc. # Written by Darryl L. Pierce <dpierce@redhat.com> # # This file is part of rubygem-cobbler. # # rubygem-cobbleris free software: you can redistribute it and/or modify -# it under the terms of the GNU Lesser General Public License as published +# it under the terms of the GNU Lesser General Public License as published # by the Free Software Foundation, either version 2.1 of the License, or # (at your option) any later version. # @@ -20,7 +20,7 @@ # You should have received a copy of the GNU General Public License # along with rubygem-cobbler. If not, see <http://www.gnu.org/licenses/>. # - + base = File.expand_path(File.join(File.dirname(__FILE__), "..")) $LOAD_PATH << File.join(base, "lib") $LOAD_PATH << File.join(base, "examples") @@ -31,14 +31,14 @@ require 'cobbler' include Cobbler -opts = GetoptLong.new( - ['--hostname', '-s', GetoptLong::REQUIRED_ARGUMENT ], - ['--details', '-d', GetoptLong::NO_ARGUMENT ], - ['--help', '-h', GetoptLong::NO_ARGUMENT ] -) +opts = GetoptLong.new(['--hostname', '-s', GetoptLong::REQUIRED_ARGUMENT ], + ['--details', '-v', GetoptLong::NO_ARGUMENT ], + ['--help', '-h', GetoptLong::NO_ARGUMENT ], + ['--debug', '-d', GetoptLong::NO_ARGUMENT ]) hostname = nil details = false +debug = false def usage puts "Usage: #{$0} [--hostname hostname] [--details]\n" @@ -50,18 +50,20 @@ opts.each do |opt, arg| when '--hostname' then hostname = arg when '--details' then details = true when '--help' then usage + when '--debug' then debug = true end end Base.hostname = hostname if hostname - +Base.debug = debug if debug + puts "Results:" -System.find do |system| - puts "\"#{system.name}\" is based on \"#{system.profile}\"." - +System.find do |system| + puts "\"#{system.name}\" is based on the \"#{system.profile}\" profile." unless system.profile.empty? + puts "\"#{system.name}\" is based on the \"#{system.image}\" image." unless system.image.empty? + if details puts "\tOwner: #{system.owners}" - system.interfaces.each_pair { |id,nic| puts "\tNIC[#{id}]: #{nic.mac_address}"} - end + system.interfaces.each_pair { |id,nic| puts "\tNIC[#{id}]: #{nic.mac_address}"} + end end - diff --git a/contrib/ruby/lib/cobbler.rb b/contrib/ruby/lib/cobbler.rb index e4fe0c57..28556e54 100644 --- a/contrib/ruby/lib/cobbler.rb +++ b/contrib/ruby/lib/cobbler.rb @@ -1,7 +1,7 @@ # # cobbler.rb - Cobbler module declaration. -# -# Copyright (C) 2008 Red Hat, Inc. +# +# Copyright (C) 2008,2009 Red Hat, Inc. # Written by Darryl L. Pierce <dpierce@redhat.com> # # This file is part of rubygem-cobbler. @@ -19,23 +19,23 @@ # You should have received a copy of the GNU General Public License # along with rubygem-cobbler. If not, see <http://www.gnu.org/licenses/>. # - + require 'cobbler/base' require 'cobbler/distro' require 'cobbler/image' require 'cobbler/network_interface' require 'cobbler/profile' require 'cobbler/system' - -module Cobbler + +module Cobbler config = (ENV['COBBLER_YML'] || File.expand_path("config/cobbler.yml")) - + yml = YAML::load(File.open(config)) if File.exist?(config) - + if yml Base.hostname = yml['hostname'] Base.username = yml['username'] Base.password = yml['password'] end - + end diff --git a/contrib/ruby/lib/cobbler/base.rb b/contrib/ruby/lib/cobbler/base.rb index ff5f565a..df84ac11 100644 --- a/contrib/ruby/lib/cobbler/base.rb +++ b/contrib/ruby/lib/cobbler/base.rb @@ -1,7 +1,6 @@ - # base.rb # -# Copyright (C) 2008 Red Hat, Inc. +# Copyright (C) 2008,2009 Red Hat, Inc. # Written by Darryl L. Pierce <dpierce@redhat.com> # # This file is part of rubygem-cobbler. @@ -67,6 +66,7 @@ module Cobbler @@hostname = nil @@connection = nil @@auth_token = nil + @@debug = false attr_accessor :definitions @@ -95,16 +95,43 @@ module Cobbler @@password = password end + # Enables debugging of communications. + # + def self.debug=(debug) + @@debug = debug + end + # Sets the connection. This method is only needed during unit testing. # def self.connection=(connection) @@connection = connection end + # Returns the version for the remote cobbler instance. + # + def self.remote_version + connect(false) unless @@connection + @@version ||= make_call("version") + end + # Returns a connection to the Cobbler server. # def self.connect(writable) - @@connection || XMLRPC::Client.new2("http://#{@@hostname}/cobbler_api#{writable ? '_rw' : ''}") + puts "Connection: writable=#{writable}" if @@debug + @@connection ||= XMLRPC::Client.new2("http://#{@@hostname}/cobbler_api") + + # in pre-1.5 versions, a separate path was used for writable calls + # TODO: remove this code in 1.6 (dlp) + version = remote_version + puts "Remote version: #{version}" if @@debug + if writable && + version < 1.5 && + !(@@connection.instance_variable_get('@path') =~ /rw$/) + puts "Older version detected: connecting to R/W endpoint" if @@debug + @@connection = XMLRPC::Client.new2("http://#{@@hostname}/cobbler_api_rw") + end + + return @@connection end # Establishes a connection with the Cobbler system. @@ -124,7 +151,10 @@ module Cobbler def self.make_call(*args) raise Exception.new('No connection established.') unless @@connection - @@connection.call(*args) + puts "Remote call: #{args.first}" if @@debug + result = @@connection.call(*args) + puts "Result: #{result}\n" if @@debug + return result end # Ends a transaction and disconnects. @@ -132,6 +162,7 @@ module Cobbler def self.end_transaction @@connection = nil @@auth_token = nil + @@version = nil end class << self diff --git a/contrib/ruby/lib/cobbler/system.rb b/contrib/ruby/lib/cobbler/system.rb index 398f9934..5751ede1 100644 --- a/contrib/ruby/lib/cobbler/system.rb +++ b/contrib/ruby/lib/cobbler/system.rb @@ -1,13 +1,13 @@ # -# system.rb -# -# Copyright (C) 2008 Red Hat, Inc. +# system.rb +# +# Copyright (C) 2008, 2009, Red Hat, Inc. # Written by Darryl L. Pierce <dpierce@redhat.com> # # This file is part of rubygem-cobbler. # # rubygem-cobbleris free software: you can redistribute it and/or modify -# it under the terms of the GNU Lesser General Public License as published +# it under the terms of the GNU Lesser General Public License as published # by the Free Software Foundation, either version 2.1 of the License, or # (at your option) any later version. # @@ -19,17 +19,17 @@ # You should have received a copy of the GNU General Public License # along with rubygem-cobbler. If not, see <http://www.gnu.org/licenses/>. # - + module Cobbler - + # +System+ represents a system within Cobbler. # class System < Base - - cobbler_lifecycle :find_all => 'get_systems', - :find_one => 'get_system', + + cobbler_lifecycle :find_all => 'get_systems', + :find_one => 'get_system', :remove => 'remove_system' - + cobbler_field :name cobbler_field :parent cobbler_field :profile @@ -47,48 +47,48 @@ module Cobbler cobbler_field :virt_path cobbler_field :virt_ram cobbler_field :virt_type - cobbler_field :virt_bridge + cobbler_field :virt_bridge def initialize(definitions = nil) super(definitions) end - + # Saves this instance. # def save Base.begin_transaction(true) - - token = Base.login - + + token = Base.login + raise Exception.new('Update failed prior to saving') unless Base.make_call('update') - + sysid = Base.make_call('new_system',token) - + Base.make_call('modify_system',sysid,'name', name, token) Base.make_call('modify_system',sysid,'profile',profile,token) if profile Base.make_call('modify_system',sysid,'image', image, token) if image - + if @interfaces count = 0 @interfaces.each do |interface| - - values = interface.bundle_for_saving(count) - - unless values.empty? - Base.make_call('modify_system',sysid,'modify-interface',values,token) + + values = interface.bundle_for_saving(count) + + unless values.empty? + Base.make_call('modify_system',sysid,'modify-interface',values,token) count = count + 1 end - + end end - + Base.make_call('save_system',sysid,token) - - Base.end_transaction + + Base.end_transaction end - + private - + # Creates a new instance of +System+ from a result received from Cobbler. # def self.create(attrs) diff --git a/contrib/ruby/test/test_base.rb b/contrib/ruby/test/test_base.rb index 36d0eacf..83b767d3 100644 --- a/contrib/ruby/test/test_base.rb +++ b/contrib/ruby/test/test_base.rb @@ -1,13 +1,13 @@ # # test_system.rb - Unit tests. -# -# Copyright (C) 2008 Red Hat, Inc. +# +# Copyright (C) 2008, 2009, Red Hat, Inc. # Written by Darryl L. Pierce <dpierce@redhat.com> # # This file is part of rubygem-cobbler. # # rubygem-cobbleris free software: you can redistribute it and/or modify -# it under the terms of the GNU Lesser General Public License as published +# it under the terms of the GNU Lesser General Public License as published # by the Free Software Foundation, either version 2.1 of the License, or # (at your option) any later version. # @@ -19,7 +19,7 @@ # You should have received a copy of the GNU General Public License # along with rubygem-cobbler. If not, see <http://www.gnu.org/licenses/>. # - + $:.unshift File.join(File.dirname(__FILE__),'..','lib') require 'test/unit' @@ -31,72 +31,52 @@ module Cobbler def setup @connection = flexmock('connection') Base.connection = @connection - Base.hostname = "localhost" + Base.hostname = "localhost" @username = 'dpierce' @password = 'farkle' Base.username = @username Base.password = @password end - - # Ensures that the default behavior for the base is to create a connection - # if one wasn't set. - # - def test_connection_without_mock - Base.connection = nil - - assert Base.connect(true), 'Should have created a new connection.' - Base.connection = nil - - assert Base.connect(false), 'Should have created a new connection.' - end - - # Ensures that setting a mock connection works (for unit tests). - # - def test_connect - assert_same @connection, Base.connect(true), 'Got the wrong connection object.' - assert_same @connection, Base.connect(false), 'Got the wrong connection object.' - end - # Ensures that beginning a transaction results in creating a connection. # def test_begin_transaction assert_same @connection, Base.begin_transaction, 'Did not create a connection.' end - + # Ensures that a login submits the username and password to the Cobbler server. # def test_login @connection.should_receive(:call).with('login',@username, @password).once.returns(true) - + Base.login end - + # Ensures that, if no connection exists, making a call throws an exception. # def test_make_call_without_connection Base.connection = nil - + assert_raises(Exception) {Base.make_call('test')} end - + # Ensures that making a call actually sends the data. # def test_make_call @connection.should_receive(:call).with('test').once.returns('farkle') - + result = Base.make_call('test') - + assert_equal result, 'farkle', 'Did not get the expected result.' end - + # Ensures that ending a transaction closes the connection. # def test_end_transaction Base.end_transaction - + # we can't just call Base.connect since that will create a new object, - # so we'll try to send data and, if that raises an exception, we'll + # so we'll try to send data and, if that raises an exception, we'll # know indirectly that the connection object is gone. assert_raises(Exception) {Base.make_call} end diff --git a/contrib/ruby/test/test_system.rb b/contrib/ruby/test/test_system.rb index a7856bf5..727ea7ec 100644 --- a/contrib/ruby/test/test_system.rb +++ b/contrib/ruby/test/test_system.rb @@ -117,6 +117,7 @@ module Cobbler # def test_save_and_update_fails @connection.should_receive(:call).with('login',@username,@password).once.returns(@auth_token) + @connection.should_receive(:call).with('version').once.returns("1.5") @connection.should_receive(:call).with('update').once.returns{ false } system = System.new('name' => @system_name, 'profile' => @profile_name) @@ -128,6 +129,7 @@ module Cobbler # def test_save_with_profile @connection.should_receive(:call).with('login',@username,@password).once.returns(@auth_token) + @connection.should_receive(:call).with('version').once.returns("1.5") @connection.should_receive(:call).with('update').once.returns { true } @connection.should_receive(:call).with('new_system',@auth_token).once.returns(@system_id) @connection.should_receive(:call).with('modify_system',@system_id,'name',@system_name,@auth_token).once.returns(true) @@ -159,6 +161,7 @@ module Cobbler # def test_save_with_new_nics @connection.should_receive(:call).with('login',@username,@password).once.returns(@auth_token) + @connection.should_receive(:call).with('version').once.returns("1.5") @connection.should_receive(:call).with('update').once.returns { true } @connection.should_receive(:call).with('new_system',@auth_token).once.returns(@system_id) @connection.should_receive(:call).with('modify_system',@system_id,'name',@system_name,@auth_token).once.returns(true) @@ -177,6 +180,7 @@ module Cobbler # def test_remove_system @connection.should_receive(:call).with('login',@username,@password).once.returns(@auth_token) + @connection.should_receive(:call).with('version').once.returns("1.5") @connection.should_receive(:call).with('remove_system',@system_name,@auth_token).once.returns(true) system = System.new('name' => @system_name, 'profile' => @profile_name) |