rbvmomi 1.1.3 → 1.1.4
Sign up to get free protection for your applications and to get access to all the features.
- data/README.html +76 -0
- data/VERSION +1 -1
- data/bin/rbvmomish +133 -0
- data/lib/rbvmomi/vim/Datastore.rb +2 -2
- data/lib/rbvmomi/vim/Folder.rb +2 -2
- metadata +8 -5
data/README.html
ADDED
@@ -0,0 +1,76 @@
|
|
1
|
+
<h1>RbVmomi</h1>
|
2
|
+
|
3
|
+
<h2>Introduction</h2>
|
4
|
+
|
5
|
+
<p>RbVmomi is a Ruby interface to the vSphere API. Like the Perl and Java SDKs,
|
6
|
+
you can use it to manage ESX and VirtualCenter servers. The current release
|
7
|
+
supports the vSphere 4.1 API.</p>
|
8
|
+
|
9
|
+
<h2>Usage</h2>
|
10
|
+
|
11
|
+
<p>A simple example of turning on a VM:</p>
|
12
|
+
|
13
|
+
<pre><code>require 'rbvmomi'
|
14
|
+
conn = RbVmomi.connect host: 'foo', user: 'bar', password: 'baz'
|
15
|
+
dc = conn.serviceInstance.find_datacenter("mydatacenter") or fail "datacenter not found"
|
16
|
+
vm = dc.find_vm("myvm") or fail "VM not found"
|
17
|
+
vm.PowerOn_Task.wait_for_completion
|
18
|
+
</code></pre>
|
19
|
+
|
20
|
+
<p>This code uses several RbVmomi extensions to the VI API for concision. The
|
21
|
+
expanded snippet below uses only standard API calls and should be familiar to
|
22
|
+
users of the Java SDK:</p>
|
23
|
+
|
24
|
+
<pre><code>require 'rbvmomi'
|
25
|
+
conn = RbVmomi.connect host: 'foo', user: 'bar', password: 'baz'
|
26
|
+
rootFolder = conn.serviceInstance.content.rootFolder
|
27
|
+
dc = rootFolder.childEntity.grep(RbVmomi::VIM::Datacenter).find { |x| x.name == "mydatacenter" } or fail "datacenter not found"
|
28
|
+
vm = dc.vmFolder.childEntity.grep(RbVmomi::VIM::VirtualMachine).find { |x| x.name == "myvm" } or fail "VM not found"
|
29
|
+
task = vm.PowerOn_Task
|
30
|
+
filter = conn.propertyCollector.CreateFilter(
|
31
|
+
spec: {
|
32
|
+
propSet: [{ type => 'Task', all: false, pathSet: ['info.state']}],
|
33
|
+
objectSet: [{ obj: task }]
|
34
|
+
},
|
35
|
+
partialUpdates: false
|
36
|
+
)
|
37
|
+
ver = ''
|
38
|
+
while true
|
39
|
+
result = conn.propertyCollector.WaitForUpdates(version: ver)
|
40
|
+
ver = result.version
|
41
|
+
break if ['success', ['error'].member? task.info.state
|
42
|
+
end
|
43
|
+
filter.DestroyPropertyFilter
|
44
|
+
raise task.info.error if task.info.state == 'error'
|
45
|
+
</code></pre>
|
46
|
+
|
47
|
+
<p>As you can see, the extensions RbVmomi adds can dramatically decrease the code
|
48
|
+
needed to perform simple tasks while still letting you use the full power of
|
49
|
+
the API when necessary. RbVmomi extensions are often more efficient than a
|
50
|
+
naive implementation; for example, the find_vm method on VIM::Datacenter used
|
51
|
+
in the first example uses the SearchIndex for fast lookups.</p>
|
52
|
+
|
53
|
+
<p>A few important points:</p>
|
54
|
+
|
55
|
+
<ul>
|
56
|
+
<li>Ruby 1.9 is required.</li>
|
57
|
+
<li>Properties are exposed as methods: vm.summary</li>
|
58
|
+
<li>All class, method, parameter, and property names match the <a href="http://www.vmware.com/support/developer/vc-sdk/visdk41pubs/ApiReference/index.html">official documentation</a>.</li>
|
59
|
+
<li>Data object types can usually be inferred from context, so you may simply use a hash instead.</li>
|
60
|
+
<li>Enumeration values are simply strings.</li>
|
61
|
+
<li>Example code is included in the examples/ directory.</li>
|
62
|
+
<li>A set of helper methods for Trollop is included to speed up development of
|
63
|
+
command line apps. See the included examples for usage.</li>
|
64
|
+
<li>This is a side project of a VMware developer and is entirely unsupported by VMware.</li>
|
65
|
+
</ul>
|
66
|
+
|
67
|
+
<p>Built-in extensions are in lib/rbvmomi/extensions.rb. You are encouraged to
|
68
|
+
reopen VIM classes in your applications and add extensions of your own. If you
|
69
|
+
write something generally useful please send it to me and I'll add it in. One
|
70
|
+
important point about extensions is that since VIM classes are lazily loaded,
|
71
|
+
you need to trigger this loading before you can reopen the class. Putting the
|
72
|
+
class name on a line by itself before reopening is enough.</p>
|
73
|
+
|
74
|
+
<h2>Development</h2>
|
75
|
+
|
76
|
+
<p>Send patches to rlane@vmware.com.</p>
|
data/VERSION
CHANGED
@@ -1 +1 @@
|
|
1
|
-
1.1.
|
1
|
+
1.1.4
|
data/bin/rbvmomish
ADDED
@@ -0,0 +1,133 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
# TODO keepalive
|
3
|
+
# TODO rc file
|
4
|
+
# TODO proxy support?
|
5
|
+
require 'trollop'
|
6
|
+
require 'readline'
|
7
|
+
require 'rbvmomi'
|
8
|
+
require 'rbvmomi/trollop'
|
9
|
+
|
10
|
+
VIM = RbVmomi::VIM
|
11
|
+
|
12
|
+
opts = Trollop.options do
|
13
|
+
banner <<-EOS
|
14
|
+
vSphere API console.
|
15
|
+
|
16
|
+
Usage:
|
17
|
+
rbvmomish [options]
|
18
|
+
|
19
|
+
Predefined methods:
|
20
|
+
si: Returns the ServiceInstance
|
21
|
+
help: Displays this text.
|
22
|
+
|
23
|
+
Special syntax:
|
24
|
+
Adding a '#' suffix to an expression displays information about the type of the
|
25
|
+
result, including its properties and methods, instead of the value.
|
26
|
+
|
27
|
+
VIM connection options:
|
28
|
+
EOS
|
29
|
+
|
30
|
+
rbvmomi_connection_opts
|
31
|
+
|
32
|
+
text <<-EOS
|
33
|
+
|
34
|
+
Other options:
|
35
|
+
EOS
|
36
|
+
|
37
|
+
$trollop = self
|
38
|
+
end
|
39
|
+
|
40
|
+
begin
|
41
|
+
$vim = VIM.connect opts
|
42
|
+
rescue Errno::EHOSTUNREACH
|
43
|
+
abort $!.message
|
44
|
+
end
|
45
|
+
|
46
|
+
typenames = VIM.instance_variable_get(:@typenames)
|
47
|
+
Readline.completion_append_character = " "
|
48
|
+
Readline.completion_proc = lambda do |word|
|
49
|
+
return unless word
|
50
|
+
prefix_regex = /^#{Regexp.escape(word)}/
|
51
|
+
candidates = typenames.sort
|
52
|
+
candidates.find_all { |e| e.match(prefix_regex) }
|
53
|
+
end
|
54
|
+
|
55
|
+
history_fn = "#{ENV['HOME']}/.rbvmomish-history"
|
56
|
+
IO.foreach(history_fn) { |l| Readline::HISTORY << l.chomp } rescue nil
|
57
|
+
history = File.open(history_fn, 'a')
|
58
|
+
|
59
|
+
def type name
|
60
|
+
klass = VIM.type(name) rescue err("invalid type #{name.inspect}")
|
61
|
+
q = lambda { |x| x =~ /^xsd:/ ? $' : x }
|
62
|
+
if klass < VIM::DataObject
|
63
|
+
puts "Data Object #{klass}"
|
64
|
+
klass.full_props_desc.each do |desc|
|
65
|
+
puts " #{desc['name']}: #{q[desc['wsdl_type']]}#{desc['is-array'] ? '[]' : ''}"
|
66
|
+
end
|
67
|
+
elsif klass < VIM::ManagedObject
|
68
|
+
puts "Managed Object #{klass}"
|
69
|
+
puts
|
70
|
+
puts "Properties:"
|
71
|
+
klass.full_props_desc.each do |desc|
|
72
|
+
puts " #{desc['name']}: #{q[desc['wsdl_type']]}#{desc['is-array'] ? '[]' : ''}"
|
73
|
+
end
|
74
|
+
puts
|
75
|
+
puts "Methods:"
|
76
|
+
klass.full_methods_desc.sort_by(&:first).each do |name,desc|
|
77
|
+
params = desc['params']
|
78
|
+
puts " #{name}(#{params.map { |x| "#{x['name']} : #{q[x['wsdl_type'] || 'void']}#{x['is-array'] ? '[]' : ''}" } * ', '}) : #{q[desc['result']['wsdl_type'] || 'void']}"
|
79
|
+
end
|
80
|
+
else
|
81
|
+
err("cannot introspect type #{klass}")
|
82
|
+
end
|
83
|
+
nil
|
84
|
+
end
|
85
|
+
|
86
|
+
class UserError < RuntimeError; end
|
87
|
+
def err msg
|
88
|
+
raise UserError.new(msg)
|
89
|
+
end
|
90
|
+
|
91
|
+
def cookie str
|
92
|
+
$vim.cookie = str
|
93
|
+
end
|
94
|
+
|
95
|
+
def si
|
96
|
+
$vim.serviceInstance
|
97
|
+
end
|
98
|
+
|
99
|
+
def help
|
100
|
+
$trollop.educate
|
101
|
+
:no_result
|
102
|
+
end
|
103
|
+
|
104
|
+
$binding = $vim.instance_eval { binding }
|
105
|
+
|
106
|
+
loop do
|
107
|
+
begin
|
108
|
+
input = Readline.readline("#{opts[:host]}> ", false) or break
|
109
|
+
input = input.strip
|
110
|
+
next if input.empty?
|
111
|
+
|
112
|
+
(history.puts input; Readline::HISTORY << input) unless input == Readline::HISTORY.to_a[-1]
|
113
|
+
|
114
|
+
result = eval(input, $binding)
|
115
|
+
if input =~ /\#$/
|
116
|
+
type result.class.wsdl_name
|
117
|
+
else
|
118
|
+
pp result unless result == :no_result
|
119
|
+
end
|
120
|
+
rescue SystemExit, IOError
|
121
|
+
raise
|
122
|
+
rescue RuntimeError, RbVmomi::Fault
|
123
|
+
puts "#{$!.class}: #{$!.message}"
|
124
|
+
puts $!.backtrace * "\n"
|
125
|
+
rescue UserError
|
126
|
+
puts $!.message
|
127
|
+
rescue Interrupt
|
128
|
+
puts
|
129
|
+
rescue Exception
|
130
|
+
puts "#{$!.class}: #{$!.message}"
|
131
|
+
puts $!.backtrace * "\n"
|
132
|
+
end
|
133
|
+
end
|
@@ -55,10 +55,10 @@ class RbVmomi::VIM::Datastore
|
|
55
55
|
def datacenter
|
56
56
|
return @datacenter if @datacenter
|
57
57
|
x = parent
|
58
|
-
while not x.is_a? Datacenter
|
58
|
+
while not x.is_a? RbVmomi::VIM::Datacenter
|
59
59
|
x = x.parent
|
60
60
|
end
|
61
|
-
fail unless x.is_a? Datacenter
|
61
|
+
fail unless x.is_a? RbVmomi::VIM::Datacenter
|
62
62
|
@datacenter = x
|
63
63
|
end
|
64
64
|
|
data/lib/rbvmomi/vim/Folder.rb
CHANGED
@@ -26,12 +26,12 @@ class RbVmomi::VIM::Folder
|
|
26
26
|
final = es.pop
|
27
27
|
|
28
28
|
p = es.inject(self) do |f,e|
|
29
|
-
f.find(e, Folder) || (create && f.CreateFolder(:name => e)) || return
|
29
|
+
f.find(e, RbVmomi::VIM::Folder) || (create && f.CreateFolder(:name => e)) || return
|
30
30
|
end
|
31
31
|
|
32
32
|
if x = p.find(final, type)
|
33
33
|
x
|
34
|
-
elsif create and type == Folder
|
34
|
+
elsif create and type == RbVmomi::VIM::Folder
|
35
35
|
p.CreateFolder(:name => final)
|
36
36
|
else
|
37
37
|
nil
|
metadata
CHANGED
@@ -5,8 +5,8 @@ version: !ruby/object:Gem::Version
|
|
5
5
|
segments:
|
6
6
|
- 1
|
7
7
|
- 1
|
8
|
-
-
|
9
|
-
version: 1.1.
|
8
|
+
- 4
|
9
|
+
version: 1.1.4
|
10
10
|
platform: ruby
|
11
11
|
authors:
|
12
12
|
- Rich Lane
|
@@ -15,7 +15,7 @@ bindir: bin
|
|
15
15
|
cert_chain: []
|
16
16
|
|
17
17
|
date: 2011-02-22 00:00:00 -08:00
|
18
|
-
default_executable:
|
18
|
+
default_executable: rbvmomish
|
19
19
|
dependencies:
|
20
20
|
- !ruby/object:Gem::Dependency
|
21
21
|
name: nokogiri
|
@@ -73,12 +73,13 @@ dependencies:
|
|
73
73
|
version_requirements: *id004
|
74
74
|
description:
|
75
75
|
email: rlane@vmware.com
|
76
|
-
executables:
|
77
|
-
|
76
|
+
executables:
|
77
|
+
- rbvmomish
|
78
78
|
extensions: []
|
79
79
|
|
80
80
|
extra_rdoc_files:
|
81
81
|
- LICENSE
|
82
|
+
- README.html
|
82
83
|
- README.rdoc
|
83
84
|
files:
|
84
85
|
- .yardopts
|
@@ -86,6 +87,7 @@ files:
|
|
86
87
|
- README.rdoc
|
87
88
|
- Rakefile
|
88
89
|
- VERSION
|
90
|
+
- bin/rbvmomish
|
89
91
|
- devel/analyze-vim-declarations.rb
|
90
92
|
- devel/analyze-xml.rb
|
91
93
|
- examples/create_vm-1.9.rb
|
@@ -125,6 +127,7 @@ files:
|
|
125
127
|
- test/test_parse_response.rb
|
126
128
|
- test/test_serialization.rb
|
127
129
|
- vmodl.cdb
|
130
|
+
- README.html
|
128
131
|
has_rdoc: true
|
129
132
|
homepage: https://github.com/rlane/rbvmomi
|
130
133
|
licenses: []
|