[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]

[Ovirt-devel] [PATCH 5/5] Sample code that shows how to use the API



---
 wui/client/bin/ovirt-cli |  104 ++++++++++++++++++++++++++++++++++++++++++++++
 wui/client/lib/ovirt.rb  |   63 ++++++++++++++++++++++++++++
 2 files changed, 167 insertions(+), 0 deletions(-)
 create mode 100755 wui/client/bin/ovirt-cli
 create mode 100644 wui/client/lib/ovirt.rb

diff --git a/wui/client/bin/ovirt-cli b/wui/client/bin/ovirt-cli
new file mode 100755
index 0000000..cfb4697
--- /dev/null
+++ b/wui/client/bin/ovirt-cli
@@ -0,0 +1,104 @@
+#! /usr/bin/ruby
+
+# This is not really a CLI, it's just an example how
+# the API can be used for a variety of tasks
+# Eventually, this needs to be replaced by the real CLI
+
+# Run this on any machine that has activeresource installed. Since there is
+# no support for authentication yet, you need to connect to Mongrel
+# directly (and therefore expose port 3000 on the OVirt Server to the wider
+# network)
+
+require 'pp'
+require 'rubygems'
+require 'activeresource'
+require 'optparse'
+
+require 'ovirt'
+
+def move_random_host(hosts, pool)
+    host = hosts[rand(hosts.size)]
+    puts "Move #{host.hostname} to #{pool.name}"
+    pool.hosts << host
+    pool.save
+end
+
+def element_path(obj)
+    "[#{obj.class.element_path(obj.id)}]"
+end
+
+def print_pool(pool)
+    puts "\n\nPool #{pool.name}: #{pool.hosts.size} hosts, #{pool.storage_pools.size} storage pools #{element_path(pool)} "
+    puts "=" * 75
+    pool.hosts.each do |h|
+        printf "%-36s %s\n", h.hostname, element_path(h)
+    end
+    pool.storage_pools.each do |sp|
+        type = sp.nfs? ? "NFS" : "iSCSI"
+        printf "%-5s %-30s %s\n", type, sp.label, element_path(sp)
+    end
+    puts "-" * 75
+end
+
+# Plumbing so we can find the OVirt server
+# "http://ovirt.watzmann.net:3000/ovirt/rest";
+OVirt::Base.site = ENV["OVIRT_SERVER"]
+opts = OptionParser.new("ovirt-cli GLOBAL_OPTS")
+opts.separator("  Run some things against an OVirt server")
+opts.separator("")
+opts.separator "Global options:"
+opts.on("-s", "--server=URL", "The OVirt server. Since there is no auth\n" +
+        "#{" "*37}yet, must be the mongrel server port.\n" +
+        "#{" "*37}Overrides env var OVIRT_SERVER") do |val|
+    OVirt::Base.site = val
+end
+
+opts.order(ARGV)
+
+unless OVirt::Base.site
+    $stderr.puts <<EOF
+You must specify the OVirt server to connect to, either with the
+--server option or through the OVIRT_SERVER environment variable
+EOF
+    exit 1
+end
+
+# Get a single host by name
+host = OVirt::Host.find_by_hostname("node3.priv.ovirt.org")
+puts "#{host.uuid} has id #{host.id}"
+
+# What's in the default pool
+defpool = OVirt::HardwarePool.default_pool
+print_pool(defpool)
+
+# Create a new hardware pool
+mypool = OVirt::HardwarePool.find_by_path("/default/mypool")
+unless mypool
+    puts "Create mypool"
+    mypool = OVirt::HardwarePool.create( { :parent_id => defpool.id,
+                                             :name => "mypool" } )
+end
+
+# Move some hosts around
+puts
+if defpool.hosts.size > 1
+    move_random_host(defpool.hosts, mypool)
+elsif mypool.hosts.size > 0
+    move_random_host(mypool.hosts, defpool)
+end
+
+# Delete all storage pools for mypool and add a new one
+mypool.storage_pools.each do |sp|
+    puts "Delete storage pool #{sp.id}"
+    sp.destroy
+end
+
+storage_pool = OVirt::StoragePool.create( { :storage_type => "NFS",
+                                            :hardware_pool_id => mypool.id,
+                                            :ip_addr => "192.168.122.50",
+                                            :export_path => "/exports/pool1" } )
+puts "Created storage pool #{storage_pool.id}"
+
+# For some reason, mypool.reload doesn't work here
+mypool = OVirt::HardwarePool.find_by_path("/default/mypool")
+print_pool(mypool)
diff --git a/wui/client/lib/ovirt.rb b/wui/client/lib/ovirt.rb
new file mode 100644
index 0000000..48739f4
--- /dev/null
+++ b/wui/client/lib/ovirt.rb
@@ -0,0 +1,63 @@
+require 'pp'
+require 'rubygems'
+require 'activeresource'
+
+module OVirt
+    class Base < ActiveResource::Base ; end
+
+    class HardwarePool < Base
+        def self.find_by_path(path)
+            find(:first, :params => { :path => path })
+        end
+
+        def self.default_pool
+            find(:first, :params => { :path => "/default" })
+        end
+    end
+
+    class StoragePool < Base
+        def iscsi?
+            attributes["type"] == "IscsiStoragePool"
+        end
+
+        def nfs?
+            attributes["type"] == "NfsStoragePool"
+        end
+
+        def label
+            if iscsi?
+                "#{ip_addr}:#{port}:#{target}"
+            elsif nfs?
+                "#{ip_addr}:#{export_path}"
+            else
+                raise "Unknown type #{attributes["type"]}"
+            end
+        end
+    end
+
+    class IscsiStoragePool < StoragePool
+        def initialize(attributes = {})
+            super(attributes.update( "type" => "IscsiStoragePool" ))
+        end
+    end
+
+    class NfsStoragePool < StoragePool
+        def initialize(attributes = {})
+            super(attributes.update( "type" => "NfsStoragePool" ))
+        end
+    end
+
+    class Host < Base
+        def self.find_by_uuid(uuid)
+            find(:first, :params => { :uuid => uuid })
+        end
+
+        def self.find_by_hostname(hostname)
+            find(:first, :params => { :hostname => hostname })
+        end
+
+        def hardware_pool
+            HardwarePool.find(hardware_pool_id)
+        end
+    end
+end
-- 
1.5.5.1


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]