summaryrefslogtreecommitdiffstats
path: root/contrib/ruby/examples
diff options
context:
space:
mode:
authorDarryl L. Pierce <dpierce@redhat.com>2008-08-13 13:09:27 -0400
committerDarryl L. Pierce <dpierce@redhat.com>2008-08-14 08:27:27 -0400
commitd7d731ffa00bbd50a49750940006ab53f6cad2fa (patch)
tree332a1493de538d1b2c6ae902fc8fcfcabc80f547 /contrib/ruby/examples
parent5ec6348575e18f942902fc039cca24480d14984b (diff)
downloadcobbler-d7d731ffa00bbd50a49750940006ab53f6cad2fa.tar.gz
cobbler-d7d731ffa00bbd50a49750940006ab53f6cad2fa.tar.xz
cobbler-d7d731ffa00bbd50a49750940006ab53f6cad2fa.zip
The auth_token is now cached as Cobbler::Base@@auth_token.
Also fixed up some documentation and how properties are handled that are either arrays of values or hashes.
Diffstat (limited to 'contrib/ruby/examples')
-rwxr-xr-xcontrib/ruby/examples/create_system.rb29
-rwxr-xr-xcontrib/ruby/examples/has_distro.rb34
-rwxr-xr-xcontrib/ruby/examples/has_image.rb34
-rwxr-xr-xcontrib/ruby/examples/has_profile.rb35
-rwxr-xr-xcontrib/ruby/examples/has_system.rb40
-rwxr-xr-xcontrib/ruby/examples/list_distros.rb22
-rwxr-xr-xcontrib/ruby/examples/list_images.rb14
-rwxr-xr-xcontrib/ruby/examples/list_profiles.rb22
-rwxr-xr-xcontrib/ruby/examples/list_systems.rb24
-rwxr-xr-xcontrib/ruby/examples/remove_distro.rb37
-rwxr-xr-xcontrib/ruby/examples/remove_image.rb72
-rwxr-xr-xcontrib/ruby/examples/remove_system.rb34
12 files changed, 249 insertions, 148 deletions
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