Darryl L. Pierce
2008-Jul-31 21:25 UTC
[Ovirt-devel] [PATCH] Ruby interface for Cobbler XML-RPC APIs.
The interface is stored in a module named Cobbler. Examples for using the interface are stored in ./examples. Still looking for some feedback from anybody with some experience with Ruby. I'm now working on the save functionality. I'm playing with having each child class declare a saving script and passing it to the cobbler_save_method code generator. I'd like some input from anybody on that path. Looking forward to some input. :) Signed-off-by: Darryl L. Pierce <dpierce at redhat.com> --- ruby/rubygem-cobbler/Rakefile | 30 ++++ ruby/rubygem-cobbler/cobbler.gemspec | 37 +++++ ruby/rubygem-cobbler/examples/create_system.rb | 52 +++++++ ruby/rubygem-cobbler/examples/has_profile.rb | 66 +++++++++ ruby/rubygem-cobbler/examples/has_system.rb | 67 +++++++++ ruby/rubygem-cobbler/examples/list_distros.rb | 48 ++++++ ruby/rubygem-cobbler/examples/list_profiles.rb | 63 ++++++++ ruby/rubygem-cobbler/examples/list_systems.rb | 63 ++++++++ ruby/rubygem-cobbler/examples/remove_system.rb | 43 ++++++ ruby/rubygem-cobbler/lib/cobbler.rb | 25 ++++ ruby/rubygem-cobbler/lib/cobbler/base.rb | 150 ++++++++++++++++++++ ruby/rubygem-cobbler/lib/cobbler/distro.rb | 52 +++++++ ruby/rubygem-cobbler/lib/cobbler/profile.rb | 60 ++++++++ ruby/rubygem-cobbler/lib/cobbler/system.rb | 62 ++++++++ .../nbproject/private/private.properties | 4 + ruby/rubygem-cobbler/nbproject/project.properties | 12 ++ ruby/rubygem-cobbler/nbproject/project.xml | 17 +++ ruby/rubygem-cobbler/test/test_base.rb | 51 +++++++ ruby/rubygem-cobbler/test/test_profile.rb | 83 +++++++++++ 19 files changed, 985 insertions(+), 0 deletions(-) create mode 100644 ruby/rubygem-cobbler/README create mode 100644 ruby/rubygem-cobbler/Rakefile create mode 100644 ruby/rubygem-cobbler/cobbler.gemspec create mode 100755 ruby/rubygem-cobbler/examples/create_system.rb create mode 100755 ruby/rubygem-cobbler/examples/has_profile.rb create mode 100755 ruby/rubygem-cobbler/examples/has_system.rb create mode 100755 ruby/rubygem-cobbler/examples/list_distros.rb create mode 100755 ruby/rubygem-cobbler/examples/list_profiles.rb create mode 100755 ruby/rubygem-cobbler/examples/list_systems.rb create mode 100755 ruby/rubygem-cobbler/examples/remove_system.rb create mode 100644 ruby/rubygem-cobbler/lib/cobbler.rb create mode 100644 ruby/rubygem-cobbler/lib/cobbler/base.rb create mode 100644 ruby/rubygem-cobbler/lib/cobbler/distro.rb create mode 100644 ruby/rubygem-cobbler/lib/cobbler/profile.rb create mode 100644 ruby/rubygem-cobbler/lib/cobbler/system.rb create mode 100644 ruby/rubygem-cobbler/nbproject/private/private.properties create mode 100644 ruby/rubygem-cobbler/nbproject/project.properties create mode 100644 ruby/rubygem-cobbler/nbproject/project.xml create mode 100755 ruby/rubygem-cobbler/test/test_base.rb create mode 100644 ruby/rubygem-cobbler/test/test_profile.rb diff --git a/ruby/rubygem-cobbler/README b/ruby/rubygem-cobbler/README new file mode 100644 index 0000000..e69de29 diff --git a/ruby/rubygem-cobbler/Rakefile b/ruby/rubygem-cobbler/Rakefile new file mode 100644 index 0000000..448a67e --- /dev/null +++ b/ruby/rubygem-cobbler/Rakefile @@ -0,0 +1,30 @@ +# 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. + +require 'rake' +require 'rake/clean' +require 'rake/testtask' +require 'rake/gempackagetask' + +GEM_VERSION="1.0" + +Rake::TestTask.new do |task| + task.libs << 'test' + task.test_files = Dir.glob( 'test/test*.rb' ) + task.verbose = true +end diff --git a/ruby/rubygem-cobbler/cobbler.gemspec b/ruby/rubygem-cobbler/cobbler.gemspec new file mode 100644 index 0000000..5057300 --- /dev/null +++ b/ruby/rubygem-cobbler/cobbler.gemspec @@ -0,0 +1,37 @@ +# 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. + +require 'rubygems' + +spec = Gem::Specification.new do |s| + s.name = 'Cobbler' + s.version = '0.0.1' + s.author = 'Darryl L. Pierce' + s.email = 'dpierce at redhat.com' + s.homepage = 'http://cobbler.et.redhat.com/' + s.platform = Gem::Platform::RUBY + s.summary = 'An interface for interacting with a Cobbler server.' + s.files = ["lib/cobbler.rb", "lib/cobbler/system.rb"] + s.require_path = "." + s.autorequire = "cobbler" +end + +if $0 == __FILE__ + Gem::manage_gems + Gem::Builder.new(spec).build +end \ No newline at end of file diff --git a/ruby/rubygem-cobbler/examples/create_system.rb b/ruby/rubygem-cobbler/examples/create_system.rb new file mode 100755 index 0000000..ead6609 --- /dev/null +++ b/ruby/rubygem-cobbler/examples/create_system.rb @@ -0,0 +1,52 @@ +#!/usr/bin/ruby -W0 +# +# create_system.rb - example of using rubygem-cobbler to create a system. +# +# 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. + +base = File.expand_path(File.join(File.dirname(__FILE__), "..")) +$LOAD_PATH << File.join(base, "lib") +$LOAD_PATH << File.join(base, "examples") + +require 'cobbler' + +if ARGV.empty? || ARGV.size < 6 + puts "Usage: #{$0} hostname system-name profile-name mac-address username password" + exit 1 +end + +hostname = ARGV[0] +system_name = ARGV[1] +profile_name = ARGV[2] +mac_address = ARGV[3] +username = ARGV[4] +password = ARGV[5] + +puts "Attempting to create a new system named #{system_name} based on profile #{profile_name}" + +system = Cobbler::System.new(hostname) +system.username = username +system.password = password + +unless system.has_profile?(profile_name) + puts "No such profile: #{profile_name}" + exit 1 +end + +system.create_system(system_name,profile_name,mac_address) diff --git a/ruby/rubygem-cobbler/examples/has_profile.rb b/ruby/rubygem-cobbler/examples/has_profile.rb new file mode 100755 index 0000000..1c5c920 --- /dev/null +++ b/ruby/rubygem-cobbler/examples/has_profile.rb @@ -0,0 +1,66 @@ +#!/usr/bin/ruby -W0 +# +# has_profile.rb - example of using rubygem-cobbler to chekc if a profile exists. +# +# 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. + +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/base' +require 'cobbler/profile' + +include Cobbler + +opts = GetoptLong.new( + ["--server", "-s", GetoptLong::REQUIRED_ARGUMENT ], + ["--profile", "-p", GetoptLong::REQUIRED_ARGUMENT ], + ["--help", "-h", GetoptLong::NO_ARGUMENT] +) + + at hostname = nil + at profile = nil + +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 --profile profile-name\n" + end +end + +SystemExit.new('No hostname specified.') unless @hostname + +if @hostname + Base.hostname = @hostname + + puts "Finding any profile that matches \"#{@profile}\"" + + result = Profile.find_by_name(@profile) + + if result + puts "#{result.name} exists, and is owned by #{result.owners}." + else + puts "No such profile: #{@profile}" + end +end \ No newline at end of file diff --git a/ruby/rubygem-cobbler/examples/has_system.rb b/ruby/rubygem-cobbler/examples/has_system.rb new file mode 100755 index 0000000..529e94b --- /dev/null +++ b/ruby/rubygem-cobbler/examples/has_system.rb @@ -0,0 +1,67 @@ +#!/usr/bin/ruby -W0 +# +# has_system.rb - example of using rubygem-cobbler to check if a system exists. +# +# 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. + +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/base' +require 'cobbler/system' + +include Cobbler + +opts = GetoptLong.new( + ["--server", "-s", GetoptLong::REQUIRED_ARGUMENT ], + ["--system", "-t", GetoptLong::REQUIRED_ARGUMENT ], + ["--help", "-h", GetoptLong::NO_ARGUMENT] +) + + at hostname = nil + at system = nil + at find_all = true + +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" + end +end + +SystemExit.new('No hostname specified.') unless @hostname + +if @hostname + Base.hostname = @hostname + + puts "Finding any system that matches \"#{@system}\"" + + result = System.find_by_name(@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 diff --git a/ruby/rubygem-cobbler/examples/list_distros.rb b/ruby/rubygem-cobbler/examples/list_distros.rb new file mode 100755 index 0000000..be99ef8 --- /dev/null +++ b/ruby/rubygem-cobbler/examples/list_distros.rb @@ -0,0 +1,48 @@ +#!/usr/bin/ruby -W0 +# list_distros.rb - example of using rubygem-cobbler to list distros. +# +# 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. + +base = File.expand_path(File.join(File.dirname(__FILE__), "..")) +$LOAD_PATH << File.join(base, "lib") +$LOAD_PATH << File.join(base, "examples") + +require 'cobbler' + +if ARGV.empty? + puts "Usage: #{$0} hostname" + exit 1 +end + +hostname = ARGV[0] + +puts "Connecting to #{hostname}" + +system = Cobbler::System.new(hostname) + +system.distros.each do |distro| + puts "[Distro: #{distro['name']}]" + + distro.keys.each do |key| + puts "#{key}: #{distro[key]}" unless key == 'name' + end + + puts "\n" +end + diff --git a/ruby/rubygem-cobbler/examples/list_profiles.rb b/ruby/rubygem-cobbler/examples/list_profiles.rb new file mode 100755 index 0000000..620a2c6 --- /dev/null +++ b/ruby/rubygem-cobbler/examples/list_profiles.rb @@ -0,0 +1,63 @@ +#!/usr/bin/ruby -W0 +# +# list_profiles.rb - example of using rubygem-cobbler to list profiles. +# +# 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. + +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/base' +require 'cobbler/profile' + +include Cobbler + +opts = GetoptLong.new( + ["--server", "-s", GetoptLong::REQUIRED_ARGUMENT ], + ["--help", "-h", GetoptLong::NO_ARGUMENT] +) + + at hostname + +opts.each do |opt, arg| + case opt + when '--server' then @hostname = arg + when '--help' then + puts "Usage: #{$0} --server hostname\n" + end +end + +if @hostname + Base.hostname = @hostname + + profiles = Profile.find + + if profiles.empty? + puts "No profiles found." + else + puts "Found #{profiles.size} profiles." + + profiles.each do |profile| + puts "#{profile.name} is based on #{profile.distro}." + end + end +end \ No newline at end of file diff --git a/ruby/rubygem-cobbler/examples/list_systems.rb b/ruby/rubygem-cobbler/examples/list_systems.rb new file mode 100755 index 0000000..3304378 --- /dev/null +++ b/ruby/rubygem-cobbler/examples/list_systems.rb @@ -0,0 +1,63 @@ +#!/usr/bin/ruby -W0 +# +# list_systems.rb - example of using rubygem-cobbler to list system. +# +# 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. + +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/base' +require 'cobbler/system' + +include Cobbler + +opts = GetoptLong.new( + ["--server", "-s", GetoptLong::REQUIRED_ARGUMENT ], + ["--help", "-h", GetoptLong::NO_ARGUMENT] +) + + at hostname + +opts.each do |opt, arg| + case opt + when '--server' then @hostname = arg + when '--help' then + puts "Usage: #{$0} --server hostname\n" + end +end + +if @hostname + Base.hostname = @hostname + + systems = System.find + + if systems.empty? + puts "No systems found." + else + puts "Found #{systems.size} profiles." + + systems.each do |system| + puts "#{system.name} is based on #{system.profile}." + end + end +end \ No newline at end of file diff --git a/ruby/rubygem-cobbler/examples/remove_system.rb b/ruby/rubygem-cobbler/examples/remove_system.rb new file mode 100755 index 0000000..21e77bf --- /dev/null +++ b/ruby/rubygem-cobbler/examples/remove_system.rb @@ -0,0 +1,43 @@ +#!/usr/bin/ruby -W0 +# +# remove_system.rb - example of using rubygem-cobbler to remove a system. +# +# 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. + +base = File.expand_path(File.join(File.dirname(__FILE__), "..")) +$LOAD_PATH << File.join(base, "lib") +$LOAD_PATH << File.join(base, "examples") + +require 'cobbler' + +if ARGV.empty? || ARGV.size < 4 + puts "Usage: #{$0} hostname system-name username password" + exit 1 +end + +hostname = ARGV[0] +system_name = ARGV[1] +username = ARGV[2] +password = ARGV[3] + +system = Cobbler::System.new(hostname) +system.username = username +system.password = password + +system.remove_system(system_name) \ No newline at end of file diff --git a/ruby/rubygem-cobbler/lib/cobbler.rb b/ruby/rubygem-cobbler/lib/cobbler.rb new file mode 100644 index 0000000..df78813 --- /dev/null +++ b/ruby/rubygem-cobbler/lib/cobbler.rb @@ -0,0 +1,25 @@ +# cobbler.rb - Cobbler module declaration. +# +# 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. + +require 'cobbler/system' + +module Cobbler + +end diff --git a/ruby/rubygem-cobbler/lib/cobbler/base.rb b/ruby/rubygem-cobbler/lib/cobbler/base.rb new file mode 100644 index 0000000..fdd696d --- /dev/null +++ b/ruby/rubygem-cobbler/lib/cobbler/base.rb @@ -0,0 +1,150 @@ +# base.rb +# +# 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. + +require 'xmlrpc/client' +require 'pp' + +module Cobbler + include XMLRPC + + # +Base+ represents a remote Cobbler server. + # + # 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 + # 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. + # + # 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: + # + # + class Base + + @@connection = nil + @@hostname = nil + + @attrs + + def attributes(name) + return @attrs ? @attrs[name] : nil + end + + # Makes a remote call to the Cobbler server. + # + def self.make_call(*args) + conn = connection(false) + + conn.call(*args) + end + + def self.connection=(connection) + @@connection = connection + end + + def self.connection(writable = false) + result = @@connection + + unless result + result = XMLRPC::Client.new2("http://#{@@hostname}/cobbler_api#{writable ? '_rw' : ''}") + end + + return result + end + + def self.hostname=(hostname) + @@hostname = hostname + end + + class << self + # Allows the class to define the means by which it is saved. + # + def cobbler_save_method(script) + module_eval <<-"end;" + def save + begin_transaction(true) + #{script} + end_transaction + end + end; + end + + # Creates a complete finder method + # + def cobbler_find_all(method_name) + module_eval <<-"end;" + def self.find + records = make_call('#{method_name}') + + result = Array.new + + if records + records.each { |record| result << create(record) } + end + + return result + end + end; + end + + # Allows for dynamically declaring fields that will come from + # Cobbler. + # + def cobbler_field(field,*args) # :nodoc: + + if args + for arg in args + for key in arg.keys + case key + when :findable then + + module_eval <<-"end;" + def self.find_by_#{field.to_s}(name,&block) + properties = make_call('#{arg[key]}',name) + + return create(properties) if properties && !properties.empty? + + return nil + end + end; + + end + end + end + + module_eval <<-"end;" + def #{field.to_s}(&block) + return attributes('#{field.to_s}') + end + end; + end + end + end + end +end \ No newline at end of file diff --git a/ruby/rubygem-cobbler/lib/cobbler/distro.rb b/ruby/rubygem-cobbler/lib/cobbler/distro.rb new file mode 100644 index 0000000..6294ce1 --- /dev/null +++ b/ruby/rubygem-cobbler/lib/cobbler/distro.rb @@ -0,0 +1,52 @@ +# distro.rb +# +# 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. + +# +Distro+ represents a single distrobution. +# +module Cobbler + + # +Distro+ represents a single distrobution within Cobbler. + # + class Distro < Base + cobbler_field :name, :findable => 'get_distro' + cobbler_field :owners + cobbler_field :kernel + cobbler_field :breed + cobbler_field :depth + cobbler_field :arch + cobbler_field :initrd + cobbler_field :source_repos + cobbler_field :kernel_options + cobbler_field :parent + cobbler_field :ks_meta + + def initialize(attrs) + @attrs = attrs + end + + private + + # Creates a new instance of +Profile+ from a result received from Cobbler. + # + def self.create(attrs) + Profile.new(attrs) + end + end +end diff --git a/ruby/rubygem-cobbler/lib/cobbler/profile.rb b/ruby/rubygem-cobbler/lib/cobbler/profile.rb new file mode 100644 index 0000000..66a304c --- /dev/null +++ b/ruby/rubygem-cobbler/lib/cobbler/profile.rb @@ -0,0 +1,60 @@ +# profile.rb +# +# 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. + +# +Profile+ represents a single profile. +# +module Cobbler + + # +Profile+ represents a single profile within Cobbler. + # + class Profile < Base + cobbler_find_all 'get_profiles' + + cobbler_field :name, :findable => 'get_profile' + cobbler_field :parent + cobbler_field :owners + cobbler_field :dhcp_tag + cobbler_field :depth + cobbler_field :virt_file_size + cobbler_field :virt_path + cobbler_field :virt_type + cobbler_field :repos + cobbler_field :distro + cobbler_field :server + cobbler_field :virt_bridge + cobbler_field :virt_ram + cobbler_field :kernel_options + cobbler_field :virt_cpus + cobbler_field :ks_meta + cobbler_field :kickstart + + def initialize(attrs) + @attrs = attrs + end + + private + + # Creates a new instance of +Profile+ from a result received from Cobbler. + # + def self.create(attrs) + Profile.new(attrs) + end + end +end diff --git a/ruby/rubygem-cobbler/lib/cobbler/system.rb b/ruby/rubygem-cobbler/lib/cobbler/system.rb new file mode 100644 index 0000000..a02a6f3 --- /dev/null +++ b/ruby/rubygem-cobbler/lib/cobbler/system.rb @@ -0,0 +1,62 @@ +# system.rb +# +# 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. + +module Cobbler + + # +System+ represents a system within Cobbler. + # + class System < Base + + cobbler_save_method <<-"SCRIPT" + puts "WOO!" + SCRIPT + + cobbler_find_all 'get_systems' + cobbler_field :name, :findable => 'get_system' + 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_field :interfaces + + def initialize(attrs) + @attrs = attrs + end + + private + + # Creates a new instance of +System+ from a result received from Cobbler. + # + def self.create(attrs) + System.new(attrs) + end + end +end diff --git a/ruby/rubygem-cobbler/nbproject/private/private.properties b/ruby/rubygem-cobbler/nbproject/private/private.properties new file mode 100644 index 0000000..45c547d --- /dev/null +++ b/ruby/rubygem-cobbler/nbproject/private/private.properties @@ -0,0 +1,4 @@ +file.reference.ruby-rubygem-cobbler=/home/mcpierce/Programming/cobbler/ruby/rubygem-cobbler +file.reference.rubygem-cobbler-lib=/home/mcpierce/Programming/cobbler/ruby/rubygem-cobbler/lib +file.reference.rubygem-cobbler-test=/home/mcpierce/Programming/cobbler/ruby/rubygem-cobbler/test +platform.active=Ruby diff --git a/ruby/rubygem-cobbler/nbproject/project.properties b/ruby/rubygem-cobbler/nbproject/project.properties new file mode 100644 index 0000000..f5ba218 --- /dev/null +++ b/ruby/rubygem-cobbler/nbproject/project.properties @@ -0,0 +1,12 @@ +file.reference.ruby-rubygem-cobbler=. +file.reference.rubygem-cobbler-lib=lib +file.reference.rubygem-cobbler-test=test +javac.classpath+main.file+platform.active=Ruby +ruby.includejava=false +source.encoding=UTF-8 +src.dir=${file.reference.rubygem-cobbler-lib} +src.docs.dir=docs +src.examples.dir=examples +test.src.dir=${file.reference.rubygem-cobbler-test} diff --git a/ruby/rubygem-cobbler/nbproject/project.xml b/ruby/rubygem-cobbler/nbproject/project.xml new file mode 100644 index 0000000..532bfe6 --- /dev/null +++ b/ruby/rubygem-cobbler/nbproject/project.xml @@ -0,0 +1,17 @@ +<?xml version="1.0" encoding="UTF-8"?> +<project xmlns="http://www.netbeans.org/ns/project/1"> + <type>org.netbeans.modules.ruby.rubyproject</type> + <configuration> + <data xmlns="http://www.netbeans.org/ns/ruby-project/1"> + <name>rubygem-cobbler</name> + <source-roots> + <root id="src.examples.dir"/> + <root id="src.docs.dir"/> + <root id="src.dir"/> + </source-roots> + <test-roots> + <root id="test.src.dir"/> + </test-roots> + </data> + </configuration> +</project> diff --git a/ruby/rubygem-cobbler/test/test_base.rb b/ruby/rubygem-cobbler/test/test_base.rb new file mode 100755 index 0000000..4370af3 --- /dev/null +++ b/ruby/rubygem-cobbler/test/test_base.rb @@ -0,0 +1,51 @@ +# test_system.rb - Unit tests. +# +# 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 'test/unit' +require 'flexmock/test_unit' +require 'cobbler/base' + +module Cobbler + class TestBase < Test::Unit::TestCase + def setup + @connection = flexmock('connection') + Base.connection = @connection + Base.hostname = "localhost" + 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.connection, 'Should have created a new connection.' + end + + # Ensures that setting a mock connection works (for unit tests). + # + def test_connection + assert_same @connection, Base.connection + end + end +end diff --git a/ruby/rubygem-cobbler/test/test_profile.rb b/ruby/rubygem-cobbler/test/test_profile.rb new file mode 100644 index 0000000..01a8184 --- /dev/null +++ b/ruby/rubygem-cobbler/test/test_profile.rb @@ -0,0 +1,83 @@ +# test_profile.rb - Tests the Profile class. +# +# 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 'test/unit' +require 'flexmock/test_unit' +require 'cobbler/base' +require 'cobbler/profile' + +module Cobbler + class TestProfile < Test::Unit::TestCase + def setup + @connection = flexmock('connection') + Base.connection = @connection + Base.hostname = "localhost" + + @profiles = Array.new + @profiles[0] = Hash.new + @profiles[0]['profile'] = 'Fedora-9-i386' + @profiles[0]['distro'] = 'Fedora-9-i386' + @profiles[0]['dhcp tag'] = 'default' + @profiles[0]['kernel options'] = {} + @profiles[0]['kickstart'] = '/etc/cobbler/sample_end.ks' + @profiles[0]['ks metadata'] = {} + @profiles[0]['owners'] = ['admin'] + @profiles[0]['repos'] = [] + @profiles[0]['server'] = '<<inherit>>' + @profiles[0]['virt bridge'] = 'xenbr0' + @profiles[0]['virt cpus'] = '1' + @profiles[0]['virt file size'] = '5' + @profiles[0]['virt path'] = '' + @profiles[0]['virt ram'] = '512' + @profiles[0]['virt type'] = 'xenpv' + + @profiles[1] = Hash.new + @profiles[1]['profile'] = 'Fedora-9-x86_64' + @profiles[1]['distro'] = 'Fedora-9-x86_64' + @profiles[1]['dhcp tag'] = 'default' + @profiles[1]['kernel options'] = {} + @profiles[1]['kickstart'] = '/etc/cobbler/sample_end.ks' + @profiles[1]['ks metadata'] = {} + @profiles[1]['owners'] = ['admin'] + @profiles[1]['repos'] = [] + @profiles[1]['server'] = '<<inherit>>' + @profiles[1]['virt bridge'] = 'xenbr0' + @profiles[1]['virt cpus'] = '1' + @profiles[1]['virt file size'] = '5' + @profiles[1]['virt path'] = '' + @profiles[1]['virt ram'] = '512' + @profiles[1]['virt type'] = 'xenpv' + end + + # Ensures that an attempt to find all of a profile works as expected. + # + def test_find + @connection.should_receive(:call).with('get_profiles').once.returns(@profiles) + + result = Profile.find + + assert result, 'Expected a result set.' + assert_equal 2, result.size, 'Did not receive the right number of results' + end + end +end -- 1.5.5.1
David Lutterkort
2008-Aug-01 02:21 UTC
[Ovirt-devel] [PATCH] Ruby interface for Cobbler XML-RPC APIs.
On Thu, 2008-07-31 at 17:25 -0400, Darryl L. Pierce wrote:> The interface is stored in a module named Cobbler. > Examples for using the interface are stored in ./examples.Neat> Still looking for some feedback from anybody with some experience with Ruby. > I'm now working on the save functionality. I'm playing with having each child > class declare a saving script and passing it to the cobbler_save_method code > generator. I'd like some input from anybody on that path.Why not just decalre a save method straight up in each class ? I don't see what the metaprogramming there buys you.> ruby/rubygem-cobbler/Rakefile | 30 ++++ > ruby/rubygem-cobbler/cobbler.gemspec | 37 +++++Generally, people put hte gemspec right in the Rakefile; for an example see ruby-libvirt's Rakefile> new file mode 100755 > index 0000000..ead6609 > --- /dev/null > +++ b/ruby/rubygem-cobbler/examples/create_system.rb > @@ -0,0 +1,52 @@ > +#!/usr/bin/ruby -W0What do you set the warning level ? That flag shouldn't be needed.> --- /dev/null > +++ b/ruby/rubygem-cobbler/examples/has_profile.rb> + at hostname = nil > + at profile = nilDon't use instance variables here .. just simple 'hostname' and 'profile' are better in a simple script. Same comment for the other examples.> diff --git a/ruby/rubygem-cobbler/lib/cobbler.rb b/ruby/rubygem-cobbler/lib/cobbler.rb > new file mode 100644 > index 0000000..df78813 > --- /dev/null > +++ b/ruby/rubygem-cobbler/lib/cobbler.rb > @@ -0,0 +1,25 @@ > +# cobbler.rb - Cobbler module declaration. > +# > +# 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. > + > +require 'cobbler/system'Commonly, the toplevel file in a ruby library requires all the files in subdirectories, so that you can say "require 'cobbler'" and get systems, profiles, distros etc.> +module Cobbler > + > +endNuke that ... it has no effect.> diff --git a/ruby/rubygem-cobbler/lib/cobbler/base.rb b/ruby/rubygem-cobbler/lib/cobbler/base.rb > new file mode 100644 > index 0000000..fdd696d > --- /dev/null > +++ b/ruby/rubygem-cobbler/lib/cobbler/base.rb > @@ -0,0 +1,150 @@ > +# base.rb > +# > +# 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. > + > +require 'xmlrpc/client' > +require 'pp' > + > +module Cobbler > + include XMLRPC > + > + # +Base+ represents a remote Cobbler server. > + # > + # 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 > + # 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. > + # > + # 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: > + # > + # > + class Base > + > + @@connection = nil > + @@hostname = nil > + > + @attrs > + > + def attributes(name) > + return @attrs ? @attrs[name] : nil > + endAttributes is kinda an overloaded name in Ruby - how about fields or similar ? Also, should @attrs really be an instance variable rather than a class variable ?> + # Makes a remote call to the Cobbler server. > + # > + def self.make_call(*args) > + conn = connection(false) > + > + conn.call(*args) > + end > + > + def self.connection=(connection) > + @@connection = connection > + end > + > + def self.connection(writable = false) > + result = @@connection > + > + unless result > + result = XMLRPC::Client.new2("http://#{@@hostname}/cobbler_api#{writable ? '_rw' : ''}") > + end > + > + return result > + endDon't you want to cache the connection here, i.e. more something like def self.connection(writable = false) unless @@connection @@connection = XMLRPC::Client.new2("http://#{@@hostname}/cobbler_api#{writable ? '_rw' : ''}") end return @@connection end It would be cleaner if you passed the connection into the constructor (possibly wrapped in some convenience class); having it as a class variable is really strange - especially since it limits you to talking to one cobbler server for the lifetime of the Ruby interpreter.> + class << selfMy head is spinning: you should stick to one idiom or the other (i.e., either 'def self.foo' or 'class << self') Nice metaprogramming though.> diff --git a/ruby/rubygem-cobbler/nbproject/private/private.properties b/ruby/rubygem-cobbler/nbproject/private/private.propertiesThat should not be in the patch.> diff --git a/ruby/rubygem-cobbler/test/test_profile.rb b/ruby/rubygem-cobbler/test/test_profile.rb > new file mode 100644 > index 0000000..01a8184 > --- /dev/null > +++ b/ruby/rubygem-cobbler/test/test_profile.rb > @@ -0,0 +1,83 @@ > +# test_profile.rb - Tests the Profile class. > +# > +# 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 'test/unit' > +require 'flexmock/test_unit' > +require 'cobbler/base' > +require 'cobbler/profile' > + > +module Cobbler > + class TestProfile < Test::Unit::TestCase > + def setup > + @connection = flexmock('connection') > + Base.connection = @connection > + Base.hostname = "localhost" > + > + @profiles = Array.new > + @profiles[0] = Hash.new > + @profiles[0]['profile'] = 'Fedora-9-i386' > + @profiles[0]['distro'] = 'Fedora-9-i386' > + @profiles[0]['dhcp tag'] = 'default' > + @profiles[0]['kernel options'] = {} > + @profiles[0]['kickstart'] = '/etc/cobbler/sample_end.ks' > + @profiles[0]['ks metadata'] = {} > + @profiles[0]['owners'] = ['admin'] > + @profiles[0]['repos'] = [] > + @profiles[0]['server'] = '<<inherit>>' > + @profiles[0]['virt bridge'] = 'xenbr0' > + @profiles[0]['virt cpus'] = '1' > + @profiles[0]['virt file size'] = '5' > + @profiles[0]['virt path'] = '' > + @profiles[0]['virt ram'] = '512' > + @profiles[0]['virt type'] = 'xenpv'This can be written more compactly as @profiles << { 'profile' => 'Fedora-9-i386', 'distro' => ... } David