diff options
| author | Darryl L. Pierce <dpierce@redhat.com> | 2009-03-03 10:39:43 -0500 |
|---|---|---|
| committer | Darryl L. Pierce <dpierce@redhat.com> | 2009-03-05 16:37:07 -0500 |
| commit | 88eaff25762cb5b2b10352fa8c299104eda8f784 (patch) | |
| tree | c42a1f78c78d0462af95c5028f81d977c1e1599f /contrib/ruby/lib | |
| parent | dbcfc5a2f0d191001cd8000a543e41eddefd20f7 (diff) | |
| download | cobbler-88eaff25762cb5b2b10352fa8c299104eda8f784.tar.gz cobbler-88eaff25762cb5b2b10352fa8c299104eda8f784.tar.xz cobbler-88eaff25762cb5b2b10352fa8c299104eda8f784.zip | |
Adds support for the XMLRPC changes for Cobbler 1.5.
Some additional features added as a result is the ability to debug a
connection. If Base.debug is set to true then all communication between
the client and server is sent to stdout.
Also fixed the list_systems.rb example to show the image on which a
system is based if it's not based on a profile.
And some minor cleanup to those particular source modules.
Diffstat (limited to 'contrib/ruby/lib')
| -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 |
3 files changed, 72 insertions, 41 deletions
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) |
