diff options
22 files changed, 342 insertions, 184 deletions
diff --git a/contrib/ruby/ChangeLog b/contrib/ruby/ChangeLog new file mode 100644 index 00000000..81b0104d --- /dev/null +++ b/contrib/ruby/ChangeLog @@ -0,0 +1,6 @@ +* Wed Aug 13 2008 Darryl Pierce <dpierce@redhat.com> +- Added caching for the auth_token to prevent extraneous calls to login. +- Reworked and refined how cobbler_collection fields are processed, adding + support for both array and has properties. +- Rewrote the documentation for Cobbler::Base to make it easier to understand + how to extend it to support other Cobbler types.
\ No newline at end of file diff --git a/contrib/ruby/Rakefile b/contrib/ruby/Rakefile index 1d497245..f4d88fe4 100644 --- a/contrib/ruby/Rakefile +++ b/contrib/ruby/Rakefile @@ -24,7 +24,7 @@ require 'rake/gempackagetask' PKG_NAME='rubygem-cobbler' PKG_VERSION='0.0.1' PKG_FILES=FileList[ - 'Rakefile', 'README', 'COPYING', 'NEWS', + 'Rakefile', 'README', 'ChangeLog', 'COPYING', 'NEWS', 'TODO', 'lib/**/*.rb', 'examples/**/*.rb', 'config/*', diff --git a/contrib/ruby/TODO b/contrib/ruby/TODO index 02bea04f..e5eddbe9 100644 --- a/contrib/ruby/TODO +++ b/contrib/ruby/TODO @@ -1,3 +1,8 @@ This is a list of features to be developed in future. -* Cache auth tokens to avoid unnecessary logins. +* Attempt to detech when a R/W call fails due to expired auth token and + retry the previous login. +* Rework the "find_one" method to be "find_by_[field]" instead. +* Add hierarchical relationships, so that a Profile will load it's + Distro, a System will load it's Profile, etc. +* Cache auth tokens to avoid unnecessary logins. [DONE: 13 Aug 2008] diff --git a/contrib/ruby/examples/create_system.rb b/contrib/ruby/examples/create_system.rb index 43a8fc41..f6b9c433 100755 --- a/contrib/ruby/examples/create_system.rb +++ b/contrib/ruby/examples/create_system.rb @@ -31,22 +31,25 @@ require 'cobbler' include Cobbler opts = GetoptLong.new( - ["--server", "-s", GetoptLong::REQUIRED_ARGUMENT ], - ["--name", "-n", GetoptLong::REQUIRED_ARGUMENT ], - ["--profile", "-f", GetoptLong::REQUIRED_ARGUMENT ], - ["--system", "-y", GetoptLong::REQUIRED_ARGUMENT ], - ["--username", "-u", GetoptLong::REQUIRED_ARGUMENT ], - ["--password", "-p", GetoptLong::REQUIRED_ARGUMENT ], - ["--help", "-h", GetoptLong::NO_ARGUMENT] + ['--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] ) -hostname = name = profile = system = username = password = nil +name = profile = hostname = username = password = nil +def usage + puts "Usage: #{$0} --name system-name --profile profile-name [--hostname hostname] [--username username] [--password password]\n" + exit +end + opts.each do |opt, arg| case opt - when '--server' then hostname = arg + when '--hostname' then hostname = arg when '--name' then name = arg - when '--system' then system = arg when '--profile' then profile = arg when '--username' then username = arg when '--password' then password = arg @@ -54,10 +57,6 @@ opts.each do |opt, arg| end end -def usage - puts "Usage: #{$0} [--server hostname] --name system-name --system system-name [--username username] [--password password]\n" -end - if name && profile System.hostname = hostname if hostname @@ -66,7 +65,7 @@ if name && profile system = System.new('name' => name,'profile' => profile) - system.interfaces=[NetworkInterface.new(["intf",{'mac_address' => '00:11:22:33:44:55:66:77'}])] + 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}." diff --git a/contrib/ruby/examples/has_distro.rb b/contrib/ruby/examples/has_distro.rb index 22093d72..417422b9 100755 --- a/contrib/ruby/examples/has_distro.rb +++ b/contrib/ruby/examples/has_distro.rb @@ -31,36 +31,38 @@ require 'cobbler' include Cobbler opts = GetoptLong.new( - ["--server", "-s", GetoptLong::REQUIRED_ARGUMENT ], - ["--distro", "-t", GetoptLong::REQUIRED_ARGUMENT ], - ["--help", "-h", GetoptLong::NO_ARGUMENT] + ['--hostname', '-s', GetoptLong::REQUIRED_ARGUMENT ], + ['--name', '-n', GetoptLong::REQUIRED_ARGUMENT ], + ['--help', '-h', GetoptLong::NO_ARGUMENT] ) -hostname = nil -distro = nil -find_all = true +name = hostname = nil + +def usage + puts "Usage: #{$0} --name distro-name [--hostname hostname]" + exit +end opts.each do |opt, arg| case opt - when '--server' then hostname = arg - when '--distro' then distro = arg - when '--help' then - puts "Usage: #{$0} --server hostname --distro distro-name\n" + when '--hostname' then hostname = arg + when '--name' then name = arg + when '--help' then usage end end -SystemExit.new('No hostname specified.') unless hostname - -if hostname && distro +if name Distro.hostname = hostname - puts "Finding any distro that matches \"#{distro}\"" + puts "Finding the distro named \"#{name}\"" - result = Distro.find_one(distro) + result = Distro.find_one(name) if result puts "#{result.name} exists, and is a breed of #{result.breed}." else - puts "No such system: #{distro}" + puts "No such distro" end +else + usage end
\ No newline at end of file diff --git a/contrib/ruby/examples/has_image.rb b/contrib/ruby/examples/has_image.rb index 8442f6e6..d5efd4d7 100755 --- a/contrib/ruby/examples/has_image.rb +++ b/contrib/ruby/examples/has_image.rb @@ -31,35 +31,39 @@ require 'cobbler' include Cobbler opts = GetoptLong.new( - ["--server", "-s", GetoptLong::REQUIRED_ARGUMENT ], - ["--image", "-i", GetoptLong::REQUIRED_ARGUMENT ], - ["--help", "-h", GetoptLong::NO_ARGUMENT] + ['--hostname', '-s', GetoptLong::REQUIRED_ARGUMENT ], + ['--name', '-i', GetoptLong::REQUIRED_ARGUMENT ], + ['--help', '-h', GetoptLong::NO_ARGUMENT] ) hostname = nil -image = nil +name = nil + +def usage + puts "Usage: #{$0} --name image-name [--hostname hostname]\n" + exit +end opts.each do |opt, arg| case opt - when '--server' then hostname = arg - when '--image' then image = arg - when '--help' then - puts "Usage: #{$0} --server hostname --image image-name\n" + when '--hostname' then hostname = arg + when '--name' then name = arg + when '--help' then usage end end -SystemExit.new('No hostname specified.') unless hostname - -if hostname - Base.hostname = hostname +if name + Base.hostname = hostname if hostname - puts "Finding any system that matches \"#{image}\"" + puts "Finding the image named \"#{name}\"" - result = Image.find_one(image) + result = Image.find_one(name) if result puts "#{result.name} exists, and uses #{result.file}." else - puts "No such system: #{image}" + puts "No such system." end +else + usage end
\ No newline at end of file diff --git a/contrib/ruby/examples/has_profile.rb b/contrib/ruby/examples/has_profile.rb index 401a6c1c..ddadd51e 100755 --- a/contrib/ruby/examples/has_profile.rb +++ b/contrib/ruby/examples/has_profile.rb @@ -31,35 +31,38 @@ require 'cobbler' include Cobbler opts = GetoptLong.new( - ["--server", "-s", GetoptLong::REQUIRED_ARGUMENT ], - ["--profile", "-t", GetoptLong::REQUIRED_ARGUMENT ], - ["--help", "-h", GetoptLong::NO_ARGUMENT] + ['--hostname', '-s', GetoptLong::REQUIRED_ARGUMENT ], + ['--name', '-t', GetoptLong::REQUIRED_ARGUMENT ], + ['--help', '-h', GetoptLong::NO_ARGUMENT] ) -hostname = nil -profile = nil +hostname = name = nil + +def usage + puts "Usage: #{$0} --name profile-name [--hostname hostname]\n" + exit +end opts.each do |opt, arg| case opt - when '--server' then hostname = arg - when '--profile' then profile = arg - when '--help' then - puts "Usage: #{$0} --server hostname --system system-name\n" + when '--hostname' then hostname = arg + when '--name' then name = arg + when '--help' then usage end end -SystemExit.new('No hostname specified.') unless hostname - -if hostname - Profile.hostname = hostname +if name + Base.hostname = hostname if hostname - puts "Finding any system that matches \"#{profile}\"" + puts "Finding any system that matches \'#{name}\"" - result = Profile.find_one(profile) + result = Profile.find_one(name) if result puts "#{result.name} exists, and is owned by #{result.owners}." else - puts "No such system: #{profile}" + puts "No such profile." end +else + usage end
\ No newline at end of file diff --git a/contrib/ruby/examples/has_system.rb b/contrib/ruby/examples/has_system.rb index 69c52a9d..41a5945d 100755 --- a/contrib/ruby/examples/has_system.rb +++ b/contrib/ruby/examples/has_system.rb @@ -31,33 +31,39 @@ require 'cobbler' include Cobbler opts = GetoptLong.new( - ["--server", "-s", GetoptLong::REQUIRED_ARGUMENT ], - ["--system", "-t", GetoptLong::REQUIRED_ARGUMENT ], - ["--help", "-h", GetoptLong::NO_ARGUMENT] + ['--hostname', '-s', GetoptLong::REQUIRED_ARGUMENT ], + ['--name', '-t', GetoptLong::REQUIRED_ARGUMENT ], + ['--help', '-h', GetoptLong::NO_ARGUMENT] ) -hostname = nil -system = nil +hostname = name = nil + +def usage + puts "Usage: #{$0} --name system-name [--hostname hostname]\n" + exit +end opts.each do |opt, arg| case opt - when '--server' then hostname = arg - when '--system' then system = arg - when '--help' then - puts "Usage: #{$0} --server hostname --system system-name\n" + when '--hostname' then hostname = arg + when '--name' then name = arg + when '--help' then usage end end -SystemExit.new('No hostname specified.') unless hostname +if name -Base.hostname = hostname if hostname + Base.hostname = hostname if hostname -puts "Finding any system that matches \"#{system}\"" + puts "Finding the system named \"#{name}\"" -result = System.find_one(system) + result = System.find_one(name) -if result - puts "#{result.name} exists, and is owned by #{result.owners}." + if result + puts "#{result.name} exists, and is owned by #{result.owners}." + else + puts "No such system." + end else - puts "No such system: #{system}" -end + usage +end
\ No newline at end of file diff --git a/contrib/ruby/examples/list_distros.rb b/contrib/ruby/examples/list_distros.rb index 27dcd0a9..719883e9 100755 --- a/contrib/ruby/examples/list_distros.rb +++ b/contrib/ruby/examples/list_distros.rb @@ -30,23 +30,25 @@ require 'cobbler' include Cobbler opts = GetoptLong.new( - ["--server", "-s", GetoptLong::REQUIRED_ARGUMENT ], - ["--help", "-h", GetoptLong::NO_ARGUMENT] + ['--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 '--server' then hostname = arg - when '--help' then - puts "Usage: #{$0} --server hostname\n" + when '--hostname' then hostname = arg + when '--help' then usage end end -if hostname - Distro.hostname = hostname +Distro.hostname = hostname - puts "Results:" - Distro.find { |distro| puts "\"#{distro.name}\" is a breed of \"#{distro.breed}\"."} -end
\ No newline at end of file +puts "Results:" +Distro.find { |distro| puts "\"#{distro.name}\" is a breed of \"#{distro.breed}\"."} diff --git a/contrib/ruby/examples/list_images.rb b/contrib/ruby/examples/list_images.rb index 0df5611e..e4a25569 100755 --- a/contrib/ruby/examples/list_images.rb +++ b/contrib/ruby/examples/list_images.rb @@ -31,17 +31,21 @@ require 'cobbler' include Cobbler opts = GetoptLong.new( - ["--server", "-s", GetoptLong::REQUIRED_ARGUMENT ], - ["--help", "-h", GetoptLong::NO_ARGUMENT] + ['--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 '--server' then hostname = arg - when '--help' then - puts "Usage: #{$0} --server hostname\n" + when '--hostname' then hostname = arg + when '--help' then usage end end diff --git a/contrib/ruby/examples/list_profiles.rb b/contrib/ruby/examples/list_profiles.rb index 578922b0..73799bf8 100755 --- a/contrib/ruby/examples/list_profiles.rb +++ b/contrib/ruby/examples/list_profiles.rb @@ -31,23 +31,25 @@ require 'cobbler' include Cobbler opts = GetoptLong.new( - ["--server", "-s", GetoptLong::REQUIRED_ARGUMENT ], - ["--help", "-h", GetoptLong::NO_ARGUMENT] + ['--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 '--server' then hostname = arg - when '--help' then - puts "Usage: #{$0} --server hostname\n" + when '--hostname' then hostname = arg + when '--help' then usage end end -if hostname - Profile.hostname = hostname +Profile.hostname = hostname - puts "Results:" - Profile.find { |profile| puts "\"#{profile.name}\" is based on \"#{profile.distro}\"."} -end +puts "Results:" +Profile.find { |profile| puts "\"#{profile.name}\" is based on \"#{profile.distro}\"."} diff --git a/contrib/ruby/examples/list_systems.rb b/contrib/ruby/examples/list_systems.rb index fbee42c9..5e9deb24 100755 --- a/contrib/ruby/examples/list_systems.rb +++ b/contrib/ruby/examples/list_systems.rb @@ -31,24 +31,27 @@ require 'cobbler' include Cobbler opts = GetoptLong.new( - ["--server", "-s", GetoptLong::REQUIRED_ARGUMENT ], - ["--details", "-d", GetoptLong::NO_ARGUMENT ], - ["--help", "-h", GetoptLong::NO_ARGUMENT ] + ['--hostname', '-s', GetoptLong::REQUIRED_ARGUMENT ], + ['--details', '-d', GetoptLong::NO_ARGUMENT ], + ['--help', '-h', GetoptLong::NO_ARGUMENT ] ) hostname = nil details = false +def usage + puts "Usage: #{$0} [--hostname hostname] [--details]\n" + exit +end + opts.each do |opt, arg| case opt - when '--server' then hostname = arg - when '--details' then details = true - when '--help' then - puts "Usage: #{$0} --server hostname\n" + when '--hostname' then hostname = arg + when '--details' then details = true + when '--help' then usage end end - Base.hostname = hostname if hostname puts "Results:" @@ -57,7 +60,6 @@ System.find do |system| if details puts "\tOwner: #{system.owners}" - system.interfaces.each { |nic| puts "\tNIC: #{nic.mac_address}"} - end - + system.interfaces.each_pair { |id,nic| puts "\tNIC[#{id}]: #{nic.mac_address}"} + end end
\ No newline at end of file diff --git a/contrib/ruby/examples/remove_distro.rb b/contrib/ruby/examples/remove_distro.rb index d4e34adc..c704ad49 100755 --- a/contrib/ruby/examples/remove_distro.rb +++ b/contrib/ruby/examples/remove_distro.rb @@ -31,41 +31,42 @@ require 'cobbler' include Cobbler opts = GetoptLong.new( - ["--server", "-s", GetoptLong::REQUIRED_ARGUMENT ], - ["--distro", "-d", GetoptLong::REQUIRED_ARGUMENT ], + ["--hostname", "-s", GetoptLong::REQUIRED_ARGUMENT ], + ["--name", "-d", GetoptLong::REQUIRED_ARGUMENT ], ["--username", "-u", GetoptLong::REQUIRED_ARGUMENT ], ["--password", "-p", GetoptLong::REQUIRED_ARGUMENT ], ["--help", "-h", GetoptLong::NO_ARGUMENT] ) -hostname = nil -distro = nil -username = nil -password = nil +hostname = name = username = password = nil + +def usage + puts "Usage: #{$0} --name distro-name [--hostname hostname] [--username username] [--password password]\n" + exit +end opts.each do |opt, arg| case opt - when '--server' then hostname = arg - when '--distro' then distro = arg + when '--hostname' then hostname = arg + when '--name' then name = arg when '--username' then username = arg when '--password' then password = arg - when '--help' then - puts "Usage: #{$0} --server hostname --distro distro-name --username username --password password\n" + when '--help' then usage end end -SystemExit.new('No hostname specified.') unless hostname - -if hostname && distro && username && password - System.hostname = hostname - System.username = username - System.password = password +if name + Base.hostname = hostname if hostname + Base.username = username if username + Base.password = password if password - puts "Removing \"#{distro}\"..." + puts "Removing the distro named \"#{name}\"..." begin - puts "Deleted \"#{distro}" if System.remove(distro) + puts "Deleted." if Distro.remove(name) rescue Exception => e puts "Error: #{e.message}" end +else + usage end
\ No newline at end of file diff --git a/contrib/ruby/examples/remove_image.rb b/contrib/ruby/examples/remove_image.rb new file mode 100755 index 00000000..9324cc07 --- /dev/null +++ b/contrib/ruby/examples/remove_image.rb @@ -0,0 +1,72 @@ +#!/usr/bin/ruby +# +# remove_system.rb - example of using rubygem-cobbler to remove a system. +# +# Copyright (C) 2008 Red Hat, Inc. +# Written by Darryl L. Pierce <dpierceredhat.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. + +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 ], + ['--name', '-n', GetoptLong::REQUIRED_ARGUMENT ], + ['--username', '-u', GetoptLong::REQUIRED_ARGUMENT ], + ['--password', '-p', GetoptLong::REQUIRED_ARGUMENT ], + ['--help', '-h', GetoptLong::NO_ARGUMENT] +) + +hostname = name = username = password = nil + +def usage + puts "Usage: #{$0} --name image-name [--hostname hostname] [--username username] [--password password]" + exit +end + +opts.each do |opt, arg| + case opt + when '--hostname' then hostname = arg + when '--name' then name = arg + when '--username' then username = arg + when '--password' then password = arg + when '--help' then usage + end +end + +if name + System.hostname = hostname if hostname + System.username = username if username + System.password = password if password + + puts "Removing image named \"#{name}\"..." + + begin + puts "Deleted \"#{name}" if Image.remove(name) + rescue Exception => e + puts "Error: #{e.message}" + end +else + usage +end diff --git a/contrib/ruby/examples/remove_system.rb b/contrib/ruby/examples/remove_system.rb index d592165a..d9769fef 100755 --- a/contrib/ruby/examples/remove_system.rb +++ b/contrib/ruby/examples/remove_system.rb @@ -31,41 +31,45 @@ require 'cobbler' include Cobbler opts = GetoptLong.new( - ["--server", "-s", GetoptLong::REQUIRED_ARGUMENT ], - ["--system", "-t", GetoptLong::REQUIRED_ARGUMENT ], - ["--username", "-u", GetoptLong::REQUIRED_ARGUMENT ], - ["--password", "-p", GetoptLong::REQUIRED_ARGUMENT ], - ["--help", "-h", GetoptLong::NO_ARGUMENT] + ['--hostname', '-s', GetoptLong::REQUIRED_ARGUMENT ], + ['--name', '-n', GetoptLong::REQUIRED_ARGUMENT ], + ['--username', '-u', GetoptLong::REQUIRED_ARGUMENT ], + ['--password', '-p', GetoptLong::REQUIRED_ARGUMENT ], + ['--help', '-h', GetoptLong::NO_ARGUMENT] ) hostname = nil -system = nil +name = nil username = nil password = nil +def usage + puts "Usage: #{$0} --name system-name [--hostname hostname] [--username username] [--password password]" + exit +end + opts.each do |opt, arg| case opt - when '--server' then hostname = arg - when '--system' then system = arg + when '--hostname' then hostname = arg + when '--name' then name = arg when '--username' then username = arg when '--password' then password = arg - when '--help' then - puts "Usage: #{$0} --server hostname --system system-name\n" + when '--help' then usage end end -SystemExit.new('No hostname specified.') unless hostname - -if hostname && system && username && password +if name System.hostname = hostname System.username = username System.password = password - puts "Removing \"#{system}\"..." + puts "Removing the system named \"#{name}\"..." begin - puts "Deleted \"#{system}" if System.remove(system) + puts "Deleted \"#{name}" if System.remove(name) rescue Exception => e puts "Error: #{e.message}" end +else + usage end
\ No newline at end of file diff --git a/contrib/ruby/lib/cobbler/base.rb b/contrib/ruby/lib/cobbler/base.rb index ea75ef36..7446ad28 100644 --- a/contrib/ruby/lib/cobbler/base.rb +++ b/contrib/ruby/lib/cobbler/base.rb @@ -33,26 +33,40 @@ module Cobbler # class System < Base # cobbler_lifecycle :find_all => 'get_systems' # cobbler_field :name - # cobbler_field :owner, :array => 'String' + # cobbler_collection :owners, :type => 'String', :packing => :hash # end # - # declares a class named Farkle that contains two fields. The first, "name", - # will be one that is searchable on Cobbler; i.e., a method named "find_by_name" - # will be generated and will use the "get_farkle" remote method to retrieve - # that instance from Cobbler. + # declares a class named System that contains two fields and a class-level + # method. # - # The second field, "owner", will simply be a field named Farkle.owner that - # returns a character string. - # - # +Base+ provides some common functionality for all child classes: - # + # The first field, "name", is a simple property. It will be retrieved from + # the value "name" in the remote definition for a system, identifyed by the + # +:owner+ argument. + # + # The second field, "owners", is similarly retrieved from a property also + # named "owners" in the remote definition. However, this property is a + # collection: in this case, it is an array of definitions itself. The + # +:type+ argument identifies what the +local+ class type is that will be + # used to represent each element in the collection. + # + # A +cobbler_collection+ is packed in one of two ways: either as an array + # of values or as a hash of keys and associated values. These are defined by + # the +:packing+ argument with the values +Array+ and +Hash+, respectively. + # + # The +cobbler_lifecycle+ method allows for declaring different methods for + # retrieving remote instances of the class. These methods are: + # + # +find_one+ - the remote method to find a single instance, + # +find_all+ - the remote method to find all instances, + # +remove+ - the remote method to remote an instance # class Base @@hostname = nil @@connection = nil + @@auth_token = nil - @defintions = nil + @definitions = nil def initialize(definitions) @definitions = definitions @@ -60,8 +74,8 @@ module Cobbler # Sets the connection. This method is only needed during unit testing. # - def self.connection=(mock) - @@connection = mock + def self.connection=(connection) + @@connection = connection end # Returns or creates a new connection. @@ -91,13 +105,14 @@ module Cobbler # Logs into the Cobbler server. # def self.login - make_call('login', @@username, @@password) + (@@auth_token || make_call('login', @@username, @@password)) end # Makes a remote call. # def self.make_call(*args) raise Exception.new('No connection established.') unless @@connection + @@connection.call(*args) end @@ -105,6 +120,7 @@ module Cobbler # def self.end_transaction @@connection = nil + @@auth_token = nil end def definition(key) @@ -122,7 +138,7 @@ module Cobbler def self.hostname=(hostname) @@hostname = hostname end - + class << self # Creates a complete finder method # @@ -234,24 +250,37 @@ module Cobbler # def cobbler_collection(field, *args) # :nodoc: classname = 'String' - packing = :array + packing = 'Array' # process collection definition args.each do |arg| classname = arg[:type] if arg[:type] - packing = arg[:packing] if arg[:packing] + if arg[:packing] + case arg[:packing] + when :hash then packing = 'Hash' + when :array then packing = 'Array' + end + end end module_eval <<-"end;" def #{field.to_s}(&block) unless @#{field.to_s} - @#{field.to_s} = Array.new + @#{field.to_s} = #{packing}.new - definition('#{field.to_s}').each do |value| - if value - @#{field.to_s} << #{classname}.new(value) - end + values = definition('#{field.to_s}') + + case "#{packing}" + when "Array" then + values.each do |value| + @#{field.to_s} << #{classname}.new(value) + end + + when "Hash" then + values.keys.each do |key| + @#{field.to_s}[key] = #{classname}.new(values[key]) + end end end diff --git a/contrib/ruby/lib/cobbler/distro.rb b/contrib/ruby/lib/cobbler/distro.rb index c66d29d7..b0e36fec 100644 --- a/contrib/ruby/lib/cobbler/distro.rb +++ b/contrib/ruby/lib/cobbler/distro.rb @@ -28,7 +28,7 @@ module Cobbler :remove => 'remove_distro' cobbler_field :name - cobbler_field :owners + cobbler_collection :owners, :packing => :array cobbler_field :kernel cobbler_field :breed cobbler_field :depth diff --git a/contrib/ruby/lib/cobbler/network_interface.rb b/contrib/ruby/lib/cobbler/network_interface.rb index 3437714f..3308249a 100644 --- a/contrib/ruby/lib/cobbler/network_interface.rb +++ b/contrib/ruby/lib/cobbler/network_interface.rb @@ -31,7 +31,7 @@ module Cobbler cobbler_field :ip_address def initialize(args) - @definitions = args[1] + @definitions = args end # A hack for getting the NIC's details over the wire. diff --git a/contrib/ruby/lib/cobbler/profile.rb b/contrib/ruby/lib/cobbler/profile.rb index c8df8a33..d6338d76 100644 --- a/contrib/ruby/lib/cobbler/profile.rb +++ b/contrib/ruby/lib/cobbler/profile.rb @@ -31,7 +31,7 @@ module Cobbler cobbler_field :name, :findable => 'get_profile' cobbler_field :parent - cobbler_field :owners + cobbler_collection :owners, :packing => :array cobbler_field :dhcp_tag cobbler_field :depth cobbler_field :virt_file_size diff --git a/contrib/ruby/lib/cobbler/system.rb b/contrib/ruby/lib/cobbler/system.rb index c15d2257..255391c7 100644 --- a/contrib/ruby/lib/cobbler/system.rb +++ b/contrib/ruby/lib/cobbler/system.rb @@ -32,19 +32,19 @@ module Cobbler cobbler_field :parent cobbler_field :profile cobbler_field :depth - cobbler_field :kernel_options + cobbler_collection :kernel_options, :packing => :hash cobbler_field :kickstart - cobbler_field :ks_meta + cobbler_collection :ks_meta, :packing => :hash cobbler_field :netboot_enabled cobbler_collection :owners cobbler_field :server + cobbler_collection :interfaces, :type => 'NetworkInterface', :packing => :hash cobbler_field :virt_cpus cobbler_field :virt_file_size cobbler_field :virt_path cobbler_field :virt_ram cobbler_field :virt_type cobbler_field :virt_bridge - cobbler_collection :interfaces, :type => 'NetworkInterface', :packing => :hash def initialize(definitions) super(definitions) diff --git a/contrib/ruby/rubygem-cobbler.spec b/contrib/ruby/rubygem-cobbler.spec index 2753370c..93523204 100644 --- a/contrib/ruby/rubygem-cobbler.spec +++ b/contrib/ruby/rubygem-cobbler.spec @@ -30,10 +30,20 @@ Provides Ruby bindings to interact with a Cobbler server. %install rm -rf %{buildroot} mkdir -p %{buildroot}%{gemdir} -gem install --local --install-dir %{buildroot}%{gemdir} \ - --force %{SOURCE0} +gem install --local --install-dir %{buildroot}%{gemdir} --force %{SOURCE0} -for file in %{installroot}/examples/*.rb; do chmod +x $file; done +chmod +x %{installroot}/examples/create_system.rb +chmod +x %{installroot}/examples/has_distro.rb +chmod +x %{installroot}/examples/has_image.rb +chmod +x %{installroot}/examples/has_profile.rb +chmod +x %{installroot}/examples/has_system.rb +chmod +x %{installroot}/examples/list_distros.rb +chmod +x %{installroot}/examples/list_images.rb +chmod +x %{installroot}/examples/list_profiles.rb +chmod +x %{installroot}/examples/list_systems.rb +chmod +x %{installroot}/examples/remove_distro.rb +chmod +x %{installroot}/examples/remove_image.rb +chmod +x %{installroot}/examples/remove_profile.rb %clean rm -rf %{buildroot} @@ -52,6 +62,13 @@ rm -rf %{buildroot} %changelog +* Wed Aug 13 2008 Darryl Pierce <dpierce@redhat.com> - 0.0.1-3 +- Added caching for the auth_token to prevent extraneous calls to login. +- Reworked and refined how cobbler_collection fields are processed, adding + support for both array and has properties. +- Rewrote the documentation for Cobbler::Base to make it easier to understand + how to extend it to support other Cobbler types. + * Wed Aug 13 2008 Darryl Pierce <dpierce@redhat.com> - 0.0.1-2 - Removed markup of cobbler.yml and a config file. Fixed a few small bugs in the code for using it as a gem. diff --git a/contrib/ruby/test/test_system.rb b/contrib/ruby/test/test_system.rb index cc64e846..e28da8bf 100644 --- a/contrib/ruby/test/test_system.rb +++ b/contrib/ruby/test/test_system.rb @@ -44,7 +44,7 @@ module Cobbler @profile = 'profile1' @nics = Array.new @nic_details = {'mac_address' => '00:11:22:33:44:55:66:77'} - @nic = NetworkInterface.new(['intf0',@nic_details]) + @nic = NetworkInterface.new(@nic_details) @nics << @nic @systems = Array.new @@ -103,11 +103,11 @@ module Cobbler @connection.should_receive(:call).with('get_systems').once.returns(@systems) result = System.find - + assert result, 'Expected a result set.' assert_equal 2, result.size, 'Did not receive the right number of results.' assert_equal 2, result[0].interfaces.size, 'Did not parse the NICs correctly.' - result[0].interfaces.collect do |nic| assert_equal "00:11:22:33:44:55", nic.mac_address end + result[0].interfaces.keys.each { |intf| assert_equal "00:11:22:33:44:55", result[0].interfaces[intf].mac_address } assert_equal 3, result[0].owners.size, 'Did not parse the owners correctly.' end |