diff options
Diffstat (limited to 'contrib/ruby')
21 files changed, 355 insertions, 61 deletions
diff --git a/contrib/ruby/Rakefile b/contrib/ruby/Rakefile index 994a2d82..1d497245 100644 --- a/contrib/ruby/Rakefile +++ b/contrib/ruby/Rakefile @@ -41,7 +41,7 @@ SPEC = Gem::Specification.new do |s| s.platform = Gem::Platform::RUBY s.summary = 'An interface for interacting with a Cobbler server.' s.files = PKG_FILES - s.require_path = "." + s.require_path = "lib" s.autorequire = "cobbler" s.description = <<EOF Provides Ruby bindings to interact with a Cobbler server. diff --git a/contrib/ruby/TODO b/contrib/ruby/TODO new file mode 100644 index 00000000..02bea04f --- /dev/null +++ b/contrib/ruby/TODO @@ -0,0 +1,3 @@ +This is a list of features to be developed in future. + +* Cache auth tokens to avoid unnecessary logins. diff --git a/contrib/ruby/examples/create_system.rb b/contrib/ruby/examples/create_system.rb index 0196573c..43a8fc41 100644..100755 --- a/contrib/ruby/examples/create_system.rb +++ b/contrib/ruby/examples/create_system.rb @@ -60,9 +60,13 @@ end if name && profile + System.hostname = hostname if hostname + System.username = username if username + System.password = password if password + system = System.new('name' => name,'profile' => profile) - system.interfaces=[ NetworkInterface.new('mac_address' => '00:11:22:33:44:55:66:77') ] + system.interfaces=[NetworkInterface.new(["intf",{'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..22093d72 100644..100755 --- a/contrib/ruby/examples/has_distro.rb +++ b/contrib/ruby/examples/has_distro.rb diff --git a/contrib/ruby/examples/has_image.rb b/contrib/ruby/examples/has_image.rb new file mode 100755 index 00000000..8442f6e6 --- /dev/null +++ b/contrib/ruby/examples/has_image.rb @@ -0,0 +1,65 @@ +#!/usr/bin/ruby +# +# has_image.rb - example of using rubygem-cobbler to check if an image exists. +# +# 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( + ["--server", "-s", GetoptLong::REQUIRED_ARGUMENT ], + ["--image", "-i", GetoptLong::REQUIRED_ARGUMENT ], + ["--help", "-h", GetoptLong::NO_ARGUMENT] +) + +hostname = nil +image = nil + +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" + end +end + +SystemExit.new('No hostname specified.') unless hostname + +if hostname + Base.hostname = hostname + + puts "Finding any system that matches \"#{image}\"" + + result = Image.find_one(image) + + if result + puts "#{result.name} exists, and uses #{result.file}." + else + puts "No such system: #{image}" + end +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..401a6c1c 100644..100755 --- a/contrib/ruby/examples/has_profile.rb +++ b/contrib/ruby/examples/has_profile.rb diff --git a/contrib/ruby/examples/has_system.rb b/contrib/ruby/examples/has_system.rb index 9088666d..69c52a9d 100644..100755 --- a/contrib/ruby/examples/has_system.rb +++ b/contrib/ruby/examples/has_system.rb @@ -50,16 +50,14 @@ end SystemExit.new('No hostname specified.') unless hostname -if hostname - Base.hostname = hostname +Base.hostname = hostname if hostname - puts "Finding any system that matches \"#{system}\"" +puts "Finding any system that matches \"#{system}\"" - result = System.find_one(system) +result = System.find_one(system) - if result - puts "#{result.name} exists, and is owned by #{result.owners}." - else - puts "No such system: #{system}" - end -end
\ No newline at end of file +if result + puts "#{result.name} exists, and is owned by #{result.owners}." +else + puts "No such system: #{system}" +end diff --git a/contrib/ruby/examples/list_distros.rb b/contrib/ruby/examples/list_distros.rb index 27dcd0a9..27dcd0a9 100644..100755 --- a/contrib/ruby/examples/list_distros.rb +++ b/contrib/ruby/examples/list_distros.rb diff --git a/contrib/ruby/examples/list_images.rb b/contrib/ruby/examples/list_images.rb new file mode 100755 index 00000000..0df5611e --- /dev/null +++ b/contrib/ruby/examples/list_images.rb @@ -0,0 +1,52 @@ +#!/usr/bin/ruby +# +# list_images.rb - example of using rubygem-cobbler to list images. +# +# 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. + +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( + ["--server", "-s", GetoptLong::REQUIRED_ARGUMENT ], + ["--help", "-h", GetoptLong::NO_ARGUMENT] +) + +hostname = nil + +opts.each do |opt, arg| + case opt + when '--server' then hostname = arg + when '--help' then + puts "Usage: #{$0} --server hostname\n" + end +end + + +Base.hostname = hostname if hostname + +puts "Results:" +Image.find { |image| puts "\"#{image.name}\" uses \"#{image.file}\"."} diff --git a/contrib/ruby/examples/list_profiles.rb b/contrib/ruby/examples/list_profiles.rb index 578922b0..578922b0 100644..100755 --- a/contrib/ruby/examples/list_profiles.rb +++ b/contrib/ruby/examples/list_profiles.rb diff --git a/contrib/ruby/examples/list_systems.rb b/contrib/ruby/examples/list_systems.rb index d614460f..fbee42c9 100644..100755 --- a/contrib/ruby/examples/list_systems.rb +++ b/contrib/ruby/examples/list_systems.rb @@ -31,15 +31,18 @@ require 'cobbler' include Cobbler opts = GetoptLong.new( - ["--server", "-s", GetoptLong::REQUIRED_ARGUMENT ], - ["--help", "-h", GetoptLong::NO_ARGUMENT] + ["--server", "-s", GetoptLong::REQUIRED_ARGUMENT ], + ["--details", "-d", GetoptLong::NO_ARGUMENT ], + ["--help", "-h", GetoptLong::NO_ARGUMENT ] ) hostname = nil +details = false opts.each do |opt, arg| case opt - when '--server' then hostname = arg + when '--server' then hostname = arg + when '--details' then details = true when '--help' then puts "Usage: #{$0} --server hostname\n" end @@ -49,4 +52,12 @@ end Base.hostname = hostname if hostname puts "Results:" -System.find { |system| puts "\"#{system.name}\" is based on \"#{system.profile}\"."} +System.find do |system| + puts "\"#{system.name}\" is based on \"#{system.profile}\"." + + if details + puts "\tOwner: #{system.owners}" + system.interfaces.each { |nic| puts "\tNIC: #{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..d4e34adc 100644..100755 --- a/contrib/ruby/examples/remove_distro.rb +++ b/contrib/ruby/examples/remove_distro.rb diff --git a/contrib/ruby/examples/remove_system.rb b/contrib/ruby/examples/remove_system.rb index d592165a..d592165a 100644..100755 --- a/contrib/ruby/examples/remove_system.rb +++ b/contrib/ruby/examples/remove_system.rb diff --git a/contrib/ruby/lib/cobbler.rb b/contrib/ruby/lib/cobbler.rb index 379d746f..1247a631 100644 --- a/contrib/ruby/lib/cobbler.rb +++ b/contrib/ruby/lib/cobbler.rb @@ -20,6 +20,7 @@ require 'cobbler/base' require 'cobbler/distro' +require 'cobbler/image' require 'cobbler/network_interface' require 'cobbler/profile' require 'cobbler/system' diff --git a/contrib/ruby/lib/cobbler/base.rb b/contrib/ruby/lib/cobbler/base.rb index a2b3e838..ea75ef36 100644 --- a/contrib/ruby/lib/cobbler/base.rb +++ b/contrib/ruby/lib/cobbler/base.rb @@ -30,9 +30,10 @@ module Cobbler # Child classes can define fields that will be retrieved from Cobbler by # using the +cobbler_field+ method. For example: # - # class Farkle < Base - # cobbler_field :name, findable => 'get_farkle' - # cobbler_field :owner + # class System < Base + # cobbler_lifecycle :find_all => 'get_systems' + # cobbler_field :name + # cobbler_field :owner, :array => 'String' # end # # declares a class named Farkle that contains two fields. The first, "name", @@ -106,7 +107,7 @@ module Cobbler @@connection = nil end - def definition(key) + def definition(key) @definitions ? @definitions[key] : nil end @@ -203,8 +204,8 @@ module Cobbler when :findable then module_eval <<-"end;" - def self.find_by_#{field.to_s}(name,&block) - properties = make_call('#{arg[key]}',name) + def self.find_by_#{field.to_s}(value,&block) + properties = make_call('#{arg[key]}',value) return create(properties) if properties && !properties.empty? @@ -232,17 +233,26 @@ module Cobbler # other class must be provided. # def cobbler_collection(field, *args) # :nodoc: - classname = args[0][:type] + classname = 'String' + packing = :array + + # process collection definition + args.each do |arg| + classname = arg[:type] if arg[:type] + packing = arg[:packing] if arg[:packing] + end module_eval <<-"end;" def #{field.to_s}(&block) + unless @#{field.to_s} @#{field.to_s} = Array.new - definition('#{field.to_s}').values.each do |value| - @#{field.to_s} << #{classname}.new(value) + definition('#{field.to_s}').each do |value| + if value + @#{field.to_s} << #{classname}.new(value) + end end - end @#{field.to_s} diff --git a/contrib/ruby/lib/cobbler/image.rb b/contrib/ruby/lib/cobbler/image.rb new file mode 100644 index 00000000..ce12dacf --- /dev/null +++ b/contrib/ruby/lib/cobbler/image.rb @@ -0,0 +1,55 @@ +# image.rb +# +# 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. + +module Cobbler + + # +Image+ represents an image within Cobbler. + # + class Image < Base + + cobbler_lifecycle :find_all => 'get_images', + :find_one => 'get_image', + :remove => 'remove_image' + + cobbler_field :name + cobbler_field :owners + cobbler_field :depth + cobbler_field :virt_file_size + cobbler_field :virt_path + cobbler_field :xml_file + cobbler_field :virt_bridge + cobbler_field :virt_ram + cobbler_field :file + cobbler_field :virt_cpus + cobbler_field :parent + + def initialize(definitions) + super(definitions) + end + + private + + # Creates a new instance of +System+ from a result received from Cobbler. + # + def self.create(attrs) + Image.new(attrs) + end + end +end
\ No newline at end of file diff --git a/contrib/ruby/lib/cobbler/network_interface.rb b/contrib/ruby/lib/cobbler/network_interface.rb index 5ef4a3b0..3437714f 100644 --- a/contrib/ruby/lib/cobbler/network_interface.rb +++ b/contrib/ruby/lib/cobbler/network_interface.rb @@ -30,8 +30,8 @@ module Cobbler cobbler_field :virt_bridge cobbler_field :ip_address - def initialize(definitions) - @definitions = definitions + def initialize(args) + @definitions = args[1] end # A hack for getting the NIC's details over the wire. diff --git a/contrib/ruby/lib/cobbler/system.rb b/contrib/ruby/lib/cobbler/system.rb index acfa3f54..c15d2257 100644 --- a/contrib/ruby/lib/cobbler/system.rb +++ b/contrib/ruby/lib/cobbler/system.rb @@ -28,24 +28,23 @@ module Cobbler :find_one => 'get_system', :remove => 'remove_system' - cobbler_field :name - cobbler_field :parent - cobbler_field :profile - cobbler_field :depth - cobbler_field :kernel_options - cobbler_field :kickstart - cobbler_field :ks_meta - cobbler_field :netboot_enabled - cobbler_field :owners - cobbler_field :server - 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' + cobbler_field :name + cobbler_field :parent + cobbler_field :profile + cobbler_field :depth + cobbler_field :kernel_options + cobbler_field :kickstart + cobbler_field :ks_meta + cobbler_field :netboot_enabled + cobbler_collection :owners + cobbler_field :server + 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) @@ -62,13 +61,13 @@ module Cobbler Base.make_call('modify_system',sysid,'name',self.name,token) Base.make_call('modify_system',sysid,'profile',profile,token) - unless interfaces.empty? + if @interfaces count = 0 - interfaces.each do |interface| + @interfaces.each do |interface| - values = interface.bundle_for_saving(count) + values = interface.bundle_for_saving(count) - unless values.empty? + unless values.empty? Base.make_call('modify_system',sysid,'modify-interface',values,token) count = count + 1 end diff --git a/contrib/ruby/rubygem-cobbler.spec b/contrib/ruby/rubygem-cobbler.spec index e70e4f75..2753370c 100644 --- a/contrib/ruby/rubygem-cobbler.spec +++ b/contrib/ruby/rubygem-cobbler.spec @@ -8,9 +8,9 @@ Summary: An interface for interacting with a Cobbler server Name: rubygem-%{gemname} Version: 0.0.1 -Release: 1%{?dist} +Release: 2%{?dist} Group: Development/Languages -License: LGPLv2+ or Ruby +License: LGPLv2+ URL: http://cobbler.et.redhat.com/ Source0: http://fedorapeople.org/~mcpierce/%{gemname}-%{version}.gem BuildRoot: %{_tmppath}/%{name}-%{version}-%{release}-root-%(%{__id_u} -n) @@ -48,9 +48,13 @@ rm -rf %{buildroot} %doc %{geminstdir}/NEWS %doc %{geminstdir}/README -%config %{geminstdir}/config/cobbler.yml +%{geminstdir}/config/cobbler.yml %changelog +* 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. + * Mon Aug 04 2008 Darryl Pierce <dpierce@redhat.com> - 0.0.1-1 - Initial package diff --git a/contrib/ruby/test/test_image.rb b/contrib/ruby/test/test_image.rb new file mode 100644 index 00000000..02b27764 --- /dev/null +++ b/contrib/ruby/test/test_image.rb @@ -0,0 +1,80 @@ +# test_image.rb - Tests the Image class. +# +# 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. + + +$:.unshift File.join(File.dirname(__FILE__),'..','lib') + +require 'test/unit' +require 'flexmock/test_unit' +require 'cobbler' + +module Cobbler + class TestImage < Test::Unit::TestCase + def setup + @connection = flexmock('connection') + Image.connection = @connection + Image.hostname = "localhost" + + @username = 'dpierce' + @password = 'farkle' + Image.username = @username + Image.password = @password + + @images = Array.new + @images << { + 'name' => 'Fedora-9-LiveCD-KDE', + 'owners' => 'admin', + 'depth' => '2', + 'virt_file_size' => '<<inherit>>', + 'virt_path' => '<<inherit>>', + 'virt_bridge' => '<<inherit>>', + 'virt_ram' => '<<inherit>>', + 'virt_cpus' => '<<inherit>>', + 'file' => '/var/ftp/pub/Fedora-9-i686-Live-KDE.iso', + 'parent' => nil, + } + + @images << { + 'name' => 'Fedora-9-LiveCD-GNOME', + 'owners' => 'admin', + 'depth' => '2', + 'virt_file_size' => '<<inherit>>', + 'virt_path' => '<<inherit>>', + 'virt_bridge' => '<<inherit>>', + 'virt_ram' => '<<inherit>>', + 'virt_cpus' => '<<inherit>>', + 'file' => '/var/ftp/pub/Fedora-9-i686-Live.iso', + 'parent' => nil, + } + + end + + # Ensures that an attempt to find all profiles works as expected. + # + def test_find + @connection.should_receive(:call).with('get_images').once.returns(@images) + + result = Image.find + + assert result, 'Expected a result set.' + assert_equal 2, result.size, 'Did not receive the right number of results' + end + end +end diff --git a/contrib/ruby/test/test_system.rb b/contrib/ruby/test/test_system.rb index b56d564f..cc64e846 100644 --- a/contrib/ruby/test/test_system.rb +++ b/contrib/ruby/test/test_system.rb @@ -23,6 +23,7 @@ $:.unshift File.join(File.dirname(__FILE__),'..','lib') require 'test/unit' require 'flexmock/test_unit' +require 'flexmock/argument_matchers' require 'cobbler' module Cobbler @@ -43,20 +44,25 @@ module Cobbler @profile = 'profile1' @nics = Array.new @nic_details = {'mac_address' => '00:11:22:33:44:55:66:77'} - @nics << - NetworkInterface.new(@nic_details) + @nic = NetworkInterface.new(['intf0',@nic_details]) + @nics << @nic @systems = Array.new @systems << { 'name' => 'Web-Server', - 'owners' => 'admin', + 'owners' => ['admin','dpierce','mpdehaan'], 'profile' => 'Fedora-9-i386', 'depth' => '2', 'virt_file_size' => '<<inherit>>', 'virt_path' => '<<inherit>>', 'virt_type' => '<<inherit>>', 'server' => '<<inherit>>', - 'interfaces' => 'intf0dhcp_tagmac_address00:11:22:33:44:55subnetgatewayhostnamevirt_bridgeip_address', + 'interfaces' => { + 'intf0' => { + 'mac_address' => '00:11:22:33:44:55'}, + 'intf1' => { + 'mac_address' => '00:11:22:33:44:55'} + }, 'virt_bridge' => '<<inherit>>', 'virt_ram' => '<<inherit>>', 'ks_meta' => nil, @@ -76,7 +82,9 @@ module Cobbler 'virt_path' => '<<inherit>>', 'virt_type' => '<<inherit>>', 'server' => '<<inherit>>', - 'interfaces' => 'intf0dhcp_tagmac_address00:11:22:33:44:55subnetgatewayhostnamevirt_bridgeip_address', + 'interfaces' => { + 'intf0' => { + 'mac_address' => 'AA:BB:CC:DD:EE:FF'}}, 'virt_bridge' => '<<inherit>>', 'virt_ram' => '<<inherit>>', 'ks_meta' => nil, @@ -97,7 +105,10 @@ module Cobbler 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.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 + assert_equal 3, result[0].owners.size, 'Did not parse the owners correctly.' end # Ensures that saving a system works as expected. @@ -117,12 +128,13 @@ module Cobbler # Ensures that saving a system works as expected even when network interfaces # are involved. # - def test_save + def test_save_with_new_nics @connection.should_receive(:call).with('login',@username,@password).once.returns(@auth_token) @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) @connection.should_receive(:call).with('modify_system',@system_id,'profile',@profile_name,@auth_token).once.returns(true) - @connection.should_receive(:call).with('modify_system',@system_id,'modify-interface',any,@auth_token).once.returns(true) + @connection.should_receive(:call).with("modify_system",@system_id,'modify-interface', + @nic.bundle_for_saving(0),@auth_token).once.returns(true) @connection.should_receive(:call).with('save_system',@system_id,@auth_token).once.returns(true) system = System.new(:name => @system_name, :profile => @profile_name) |