opennebula 3.9.80.beta

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (42) hide show
  1. data/LICENSE +202 -0
  2. data/NOTICE +47 -0
  3. data/lib/opennebula.rb +58 -0
  4. data/lib/opennebula/acl.rb +266 -0
  5. data/lib/opennebula/acl_pool.rb +55 -0
  6. data/lib/opennebula/client.rb +119 -0
  7. data/lib/opennebula/cluster.rb +249 -0
  8. data/lib/opennebula/cluster_pool.rb +58 -0
  9. data/lib/opennebula/datastore.rb +171 -0
  10. data/lib/opennebula/datastore_pool.rb +55 -0
  11. data/lib/opennebula/document.rb +261 -0
  12. data/lib/opennebula/document_json.rb +131 -0
  13. data/lib/opennebula/document_pool.rb +102 -0
  14. data/lib/opennebula/document_pool_json.rb +58 -0
  15. data/lib/opennebula/error.rb +52 -0
  16. data/lib/opennebula/group.rb +163 -0
  17. data/lib/opennebula/group_pool.rb +56 -0
  18. data/lib/opennebula/host.rb +201 -0
  19. data/lib/opennebula/host_pool.rb +93 -0
  20. data/lib/opennebula/image.rb +297 -0
  21. data/lib/opennebula/image_pool.rb +79 -0
  22. data/lib/opennebula/ldap_auth.rb +99 -0
  23. data/lib/opennebula/ldap_auth_spec.rb +70 -0
  24. data/lib/opennebula/pool.rb +160 -0
  25. data/lib/opennebula/pool_element.rb +269 -0
  26. data/lib/opennebula/server_cipher_auth.rb +148 -0
  27. data/lib/opennebula/server_x509_auth.rb +104 -0
  28. data/lib/opennebula/ssh_auth.rb +139 -0
  29. data/lib/opennebula/system.rb +141 -0
  30. data/lib/opennebula/template.rb +213 -0
  31. data/lib/opennebula/template_pool.rb +79 -0
  32. data/lib/opennebula/user.rb +174 -0
  33. data/lib/opennebula/user_pool.rb +55 -0
  34. data/lib/opennebula/virtual_machine.rb +560 -0
  35. data/lib/opennebula/virtual_machine_pool.rb +323 -0
  36. data/lib/opennebula/virtual_network.rb +249 -0
  37. data/lib/opennebula/virtual_network_pool.rb +79 -0
  38. data/lib/opennebula/x509_auth.rb +288 -0
  39. data/lib/opennebula/xml_element.rb +427 -0
  40. data/lib/opennebula/xml_pool.rb +45 -0
  41. data/lib/opennebula/xml_utils.rb +34 -0
  42. metadata +118 -0
@@ -0,0 +1,323 @@
1
+ # -------------------------------------------------------------------------- #
2
+ # Copyright 2002-2013, OpenNebula Project (OpenNebula.org), C12G Labs #
3
+ # #
4
+ # Licensed under the Apache License, Version 2.0 (the "License"); you may #
5
+ # not use this file except in compliance with the License. You may obtain #
6
+ # a copy of the License at #
7
+ # #
8
+ # http://www.apache.org/licenses/LICENSE-2.0 #
9
+ # #
10
+ # Unless required by applicable law or agreed to in writing, software #
11
+ # distributed under the License is distributed on an "AS IS" BASIS, #
12
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. #
13
+ # See the License for the specific language governing permissions and #
14
+ # limitations under the License. #
15
+ #--------------------------------------------------------------------------- #
16
+
17
+
18
+ require 'opennebula/pool'
19
+
20
+ module OpenNebula
21
+ class VirtualMachinePool < Pool
22
+ #######################################################################
23
+ # Constants and Class attribute accessors
24
+ #######################################################################
25
+
26
+
27
+ VM_POOL_METHODS = {
28
+ :info => "vmpool.info",
29
+ :monitoring => "vmpool.monitoring",
30
+ :accounting => "vmpool.accounting"
31
+ }
32
+
33
+ # Constants for info queries (include/RequestManagerPoolInfoFilter.h)
34
+ INFO_NOT_DONE = -1
35
+ INFO_ALL_VM = -2
36
+
37
+ #######################################################################
38
+ # Class constructor & Pool Methods
39
+ #######################################################################
40
+
41
+
42
+ # +client+ a Client object that represents a XML-RPC connection
43
+ # +user_id+ is to refer to a Pool with VirtualMachines from that user
44
+ def initialize(client, user_id=0)
45
+ super('VM_POOL','VM',client)
46
+
47
+ @user_id = user_id
48
+ end
49
+
50
+ # Default Factory Method for the Pools
51
+ def factory(element_xml)
52
+ OpenNebula::VirtualMachine.new(element_xml,@client)
53
+ end
54
+
55
+ #######################################################################
56
+ # XML-RPC Methods for the Virtual Network Object
57
+ #######################################################################
58
+
59
+ # Retrieves all or part of the VirtualMachines in the pool.
60
+ # No arguments, returns the not-in-done VMs for the user
61
+ # [user_id, start_id, end_id]
62
+ # [user_id, start_id, end_id, state]
63
+ def info(*args)
64
+ case args.size
65
+ when 0
66
+ info_filter(VM_POOL_METHODS[:info],
67
+ @user_id,
68
+ -1,
69
+ -1,
70
+ INFO_NOT_DONE)
71
+ when 1
72
+ info_filter(VM_POOL_METHODS[:info],
73
+ args[0],
74
+ -1,
75
+ -1,
76
+ INFO_NOT_DONE)
77
+ when 3
78
+ info_filter(VM_POOL_METHODS[:info],
79
+ args[0],
80
+ args[1],
81
+ args[2],
82
+ INFO_NOT_DONE)
83
+ when 4
84
+ info_filter(VM_POOL_METHODS[:info],
85
+ args[0],
86
+ args[1],
87
+ args[2],
88
+ args[3])
89
+ end
90
+ end
91
+
92
+ def info_all()
93
+ return info_filter(VM_POOL_METHODS[:info],
94
+ INFO_ALL,
95
+ -1,
96
+ -1,
97
+ INFO_NOT_DONE)
98
+ end
99
+
100
+ def info_mine()
101
+ return info_filter(VM_POOL_METHODS[:info],
102
+ INFO_MINE,
103
+ -1,
104
+ -1,
105
+ INFO_NOT_DONE)
106
+ end
107
+
108
+ def info_group()
109
+ return info_filter(VM_POOL_METHODS[:info],
110
+ INFO_GROUP,
111
+ -1,
112
+ -1,
113
+ INFO_NOT_DONE)
114
+ end
115
+
116
+ alias_method :info!, :info
117
+ alias_method :info_all!, :info_all
118
+ alias_method :info_mine!, :info_mine
119
+ alias_method :info_group!, :info_group
120
+
121
+ # Retrieves the monitoring data for all the VMs in the pool
122
+ #
123
+ # @param [Array<String>] xpath_expressions Elements to retrieve.
124
+ # @param [Integer] filter_flag Optional filter flag to retrieve all or
125
+ # part of the Pool. Possible values: INFO_ALL, INFO_GROUP, INFO_MINE.
126
+ #
127
+ # @return [Hash<String, <Hash<String, Array<Array<int>>>>>,
128
+ # OpenNebula::Error] The first level hash uses the VM ID as keys, and
129
+ # as value a Hash with the requested xpath expressions,
130
+ # and an Array of 'timestamp, value'.
131
+ #
132
+ # @example
133
+ # vm_pool.monitoring( ['CPU', 'NET_TX', 'TEMPLATE/CUSTOM_PROBE'] )
134
+ #
135
+ # {"1"=>
136
+ # {"CPU"=>
137
+ # [["1337608271", "0"], ["1337608301", "0"], ["1337608331", "0"]],
138
+ # "NET_TX"=>
139
+ # [["1337608271", "510"], ["1337608301", "510"], ["1337608331", "520"]],
140
+ # "TEMPLATE/CUSTOM_PROBE"=>
141
+ # []},
142
+ #
143
+ # "0"=>
144
+ # {"CPU"=>
145
+ # [["1337608271", "0"], ["1337608301", "0"], ["1337608331", "0"]],
146
+ # "NET_TX"=>
147
+ # [["1337608271", "510"], ["1337608301", "510"], ["1337608331", "520"]],
148
+ # "TEMPLATE/CUSTOM_PROBE"=>
149
+ # []}}
150
+ def monitoring(xpath_expressions, filter_flag=INFO_ALL)
151
+ return super(VM_POOL_METHODS[:monitoring],
152
+ 'VM', 'LAST_POLL', xpath_expressions, filter_flag)
153
+ end
154
+
155
+ # Retrieves the monitoring data for all the VMs in the pool, in XML
156
+ #
157
+ # @param [Integer] filter_flag Optional filter flag to retrieve all or
158
+ # part of the Pool. Possible values: INFO_ALL, INFO_GROUP, INFO_MINE.
159
+ #
160
+ # @return [String] VM monitoring data, in XML
161
+ def monitoring_xml(filter_flag=INFO_ALL)
162
+ return @client.call(VM_POOL_METHODS[:monitoring], filter_flag)
163
+ end
164
+
165
+ # Retrieves the accounting data for all the VMs in the pool
166
+ #
167
+ # @param [Integer] filter_flag Optional filter flag to retrieve all or
168
+ # part of the Pool. Possible values: INFO_ALL, INFO_GROUP, INFO_MINE
169
+ # or user_id
170
+ # @param [Hash] options
171
+ # @option params [Integer] :start_time Start date and time to take into account,
172
+ # if no start_time is required use -1
173
+ # @option params [Integer] :end_time End date and time to take into account,
174
+ # if no end_time is required use -1
175
+ # @option params [Integer] :host Host id to filter the results
176
+ # @option params [Integer] :group Group id to filter the results
177
+ # @option params [String] :xpath Xpath expression to filter the results.
178
+ # For example: HISTORY[ETIME>0]
179
+ # @option params [String] :order_by_1 Xpath expression to group the
180
+ # @option params [String] :order_by_2 Xpath expression to group the
181
+ # returned hash. This will be the second level of the hash
182
+ #
183
+ # @return [Hash, OpenNebula::Error]
184
+ # The first level hash uses the :order_by_1 values as keys, and
185
+ # as value a Hash with the :order_by_2 values and the HISTORY_RECORDS
186
+ #
187
+ # @example
188
+ # {"HISTORY_RECORDS"=>
189
+ # {"HISTORY"=> [
190
+ # {"OID"=>"0",
191
+ # "SEQ"=>"0",
192
+ # "HOSTNAME"=>"dummy",
193
+ # ...
194
+ # },
195
+ # {"OID"=>"0",
196
+ # "SEQ"=>"0",
197
+ # "HOSTNAME"=>"dummy",
198
+ #
199
+ # @example :order_by_1 => VM/UID
200
+ # {"0"=>
201
+ # {"HISTORY_RECORDS"=>
202
+ # {"HISTORY"=> [
203
+ # {"OID"=>"0",
204
+ # "SEQ"=>"0",
205
+ # "HOSTNAME"=>"dummy",
206
+ # ...
207
+ # },
208
+ # {"OID"=>"0",
209
+ # "SEQ"=>"0",
210
+ # "HOSTNAME"=>"dummy",
211
+ #
212
+ # @example :order_by_1 => VM/UID, :order_by_2 => VM/ID
213
+ # {"0"=>
214
+ # {"0"=>
215
+ # {"HISTORY_RECORDS"=>
216
+ # {"HISTORY"=> [
217
+ # {"OID"=>"0",
218
+ # "SEQ"=>"0",
219
+ # "HOSTNAME"=>"dummy",
220
+ # ...
221
+ # },
222
+ # {"OID"=>"0",
223
+ # "SEQ"=>"0",
224
+ # "HOSTNAME"=>"dummy",
225
+ #
226
+ def accounting(filter_flag=INFO_ALL, options={})
227
+ acct_hash = Hash.new
228
+
229
+ rc = build_accounting(filter_flag, options) do |history|
230
+ hash = acct_hash
231
+
232
+ if options[:order_by_1]
233
+ id_1 = history[options[:order_by_1]]
234
+ acct_hash[id_1] ||= Hash.new
235
+
236
+ if options[:order_by_2]
237
+ id_2 = history[options[:order_by_2]]
238
+ acct_hash[id_1][id_2] ||= Hash.new
239
+
240
+ hash = acct_hash[id_1][id_2]
241
+ else
242
+ hash = acct_hash[id_1]
243
+ end
244
+ end
245
+
246
+ hash["HISTORY_RECORDS"] ||= Hash.new
247
+ hash["HISTORY_RECORDS"]["HISTORY"] ||= Array.new
248
+ hash["HISTORY_RECORDS"]["HISTORY"] << history.to_hash['HISTORY']
249
+ end
250
+
251
+ return rc if OpenNebula.is_error?(rc)
252
+
253
+ acct_hash
254
+ end
255
+
256
+ # Retrieves the accounting data for all the VMs in the pool in xml
257
+ #
258
+ # @param [Integer] filter_flag Optional filter flag to retrieve all or
259
+ # part of the Pool. Possible values: INFO_ALL, INFO_GROUP, INFO_MINE
260
+ # or user_id
261
+ # @param [Hash] options
262
+ # @option params [Integer] :start_time Start date and time to take into account,
263
+ # if no start_time is required use -1
264
+ # @option params [Integer] :end_time End date and time to take into account,
265
+ # if no end_time is required use -1
266
+ # @option params [Integer] :host Host id to filter the results
267
+ # @option params [Integer] :group Group id to filter the results
268
+ # @option params [String] :xpath Xpath expression to filter the results.
269
+ # For example: HISTORY[ETIME>0]
270
+ #
271
+ # @return [String] the xml representing the accounting data
272
+ def accounting_xml(filter_flag=INFO_ALL, options={})
273
+ acct_hash = Hash.new
274
+ xml_str = "<HISTORY_RECORDS>\n"
275
+
276
+ rc = build_accounting(filter_flag, options) do |history|
277
+ xml_str << history.to_xml
278
+ end
279
+
280
+ return rc if OpenNebula.is_error?(rc)
281
+
282
+ xml_str << "\n</HISTORY_RECORDS>"
283
+ xml_str
284
+ end
285
+
286
+ private
287
+
288
+ def build_accounting(filter_flag, options, &block)
289
+ xml_str = @client.call(VM_POOL_METHODS[:accounting],
290
+ filter_flag,
291
+ options[:start_time],
292
+ options[:end_time])
293
+
294
+ return xml_str if OpenNebula.is_error?(xml_str)
295
+
296
+ xmldoc = XMLElement.new
297
+ xmldoc.initialize_xml(xml_str, 'HISTORY_RECORDS')
298
+
299
+ xpath_array = Array.new
300
+ xpath_array << "HISTORY[HID=#{options[:host]}]" if options[:host]
301
+ xpath_array << "HISTORY[VM/GID=#{options[:group]}]" if options[:group]
302
+ xpath_array << options[:xpath] if options[:xpath]
303
+
304
+ if xpath_array.empty?
305
+ xpath_str = "HISTORY"
306
+ else
307
+ xpath_str = xpath_array.join(' | ')
308
+ end
309
+
310
+ acct_hash = Hash.new
311
+
312
+ xmldoc.each(xpath_str) do |history|
313
+ block.call(history)
314
+ end
315
+
316
+ acct_hash
317
+ end
318
+
319
+ def info_filter(xml_method, who, start_id, end_id, state)
320
+ return xmlrpc_info(xml_method, who, start_id, end_id, state)
321
+ end
322
+ end
323
+ end
@@ -0,0 +1,249 @@
1
+ # -------------------------------------------------------------------------- #
2
+ # Copyright 2002-2013, OpenNebula Project (OpenNebula.org), C12G Labs #
3
+ # #
4
+ # Licensed under the Apache License, Version 2.0 (the "License"); you may #
5
+ # not use this file except in compliance with the License. You may obtain #
6
+ # a copy of the License at #
7
+ # #
8
+ # http://www.apache.org/licenses/LICENSE-2.0 #
9
+ # #
10
+ # Unless required by applicable law or agreed to in writing, software #
11
+ # distributed under the License is distributed on an "AS IS" BASIS, #
12
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. #
13
+ # See the License for the specific language governing permissions and #
14
+ # limitations under the License. #
15
+ #--------------------------------------------------------------------------- #
16
+
17
+
18
+ require 'opennebula/pool_element'
19
+
20
+ module OpenNebula
21
+ class VirtualNetwork < PoolElement
22
+ #######################################################################
23
+ # Constants and Class Methods
24
+ #######################################################################
25
+
26
+
27
+ VN_METHODS = {
28
+ :info => "vn.info",
29
+ :allocate => "vn.allocate",
30
+ :delete => "vn.delete",
31
+ :addleases => "vn.addleases",
32
+ :rmleases => "vn.rmleases",
33
+ :chown => "vn.chown",
34
+ :chmod => "vn.chmod",
35
+ :update => "vn.update",
36
+ :hold => "vn.hold",
37
+ :release => "vn.release",
38
+ :rename => "vn.rename"
39
+ }
40
+
41
+ VN_TYPES=%w{RANGED FIXED}
42
+
43
+ SHORT_VN_TYPES={
44
+ "RANGED" => "R",
45
+ "FIXED" => "F"
46
+ }
47
+
48
+ # Creates a VirtualNetwork description with just its identifier
49
+ # this method should be used to create plain VirtualNetwork objects.
50
+ # +id+ the id of the network
51
+ #
52
+ # Example:
53
+ # vnet = VirtualNetwork.new(VirtualNetwork.build_xml(3),rpc_client)
54
+ #
55
+ def VirtualNetwork.build_xml(pe_id=nil)
56
+ if pe_id
57
+ vn_xml = "<VNET><ID>#{pe_id}</ID></VNET>"
58
+ else
59
+ vn_xml = "<VNET></VNET>"
60
+ end
61
+
62
+ XMLElement.build_xml(vn_xml, 'VNET')
63
+ end
64
+
65
+ # Class constructor
66
+ def initialize(xml, client)
67
+ super(xml,client)
68
+ end
69
+
70
+ #######################################################################
71
+ # XML-RPC Methods for the Virtual Network Object
72
+ #######################################################################
73
+
74
+ # Retrieves the information of the given VirtualNetwork.
75
+ def info()
76
+ super(VN_METHODS[:info], 'VNET')
77
+ end
78
+
79
+ alias_method :info!, :info
80
+
81
+ # Allocates a new VirtualNetwork in OpenNebula
82
+ #
83
+ # @param description [String] The template of the VirtualNetwork.
84
+ # @param cluster_id [Integer] Id of the cluster
85
+ #
86
+ # @return [Integer, OpenNebula::Error] the new ID in case of
87
+ # success, error otherwise
88
+ def allocate(description,cluster_id=ClusterPool::NONE_CLUSTER_ID)
89
+ super(VN_METHODS[:allocate], description, cluster_id)
90
+ end
91
+
92
+ # Replaces the template contents
93
+ #
94
+ # +new_template+ New template contents. If no argument is provided
95
+ # the object will be updated using the @xml variable
96
+ def update(new_template=nil)
97
+ super(VN_METHODS[:update], new_template)
98
+ end
99
+
100
+ # Publishes the VirtualNetwork, to be used by other users
101
+ def publish
102
+ set_publish(true)
103
+ end
104
+
105
+ # Unplubishes the VirtualNetwork
106
+ def unpublish
107
+ set_publish(false)
108
+ end
109
+
110
+ # Deletes the VirtualNetwork
111
+ def delete()
112
+ super(VN_METHODS[:delete])
113
+ end
114
+
115
+ # Adds a lease to the VirtualNetwork
116
+ def addleases(ip, mac = nil)
117
+ return Error.new('ID not defined') if !@pe_id
118
+
119
+ lease_template = "LEASES = [ IP = #{ip}"
120
+ lease_template << ", MAC = #{mac}" if mac
121
+ lease_template << " ]"
122
+
123
+ rc = @client.call(VN_METHODS[:addleases], @pe_id, lease_template)
124
+ rc = nil if !OpenNebula.is_error?(rc)
125
+
126
+ return rc
127
+ end
128
+
129
+ # Removes a lease from the VirtualNetwork
130
+ def rmleases(ip)
131
+ return Error.new('ID not defined') if !@pe_id
132
+
133
+ lease_template = "LEASES = [ IP = #{ip} ]"
134
+
135
+ rc = @client.call(VN_METHODS[:rmleases], @pe_id, lease_template)
136
+ rc = nil if !OpenNebula.is_error?(rc)
137
+
138
+ return rc
139
+ end
140
+
141
+ # Holds a virtual network Lease as used
142
+ # @param ip [String] IP to hold
143
+ def hold(ip)
144
+ return Error.new('ID not defined') if !@pe_id
145
+
146
+ lease_template = "LEASES = [ IP = #{ip} ]"
147
+
148
+ rc = @client.call(VN_METHODS[:hold], @pe_id, lease_template)
149
+ rc = nil if !OpenNebula.is_error?(rc)
150
+
151
+ return rc
152
+ end
153
+
154
+ # Releases a virtual network Lease on hold
155
+ # @param ip [String] IP to release
156
+ def release(ip)
157
+ return Error.new('ID not defined') if !@pe_id
158
+
159
+ lease_template = "LEASES = [ IP = #{ip} ]"
160
+
161
+ rc = @client.call(VN_METHODS[:release], @pe_id, lease_template)
162
+ rc = nil if !OpenNebula.is_error?(rc)
163
+
164
+ return rc
165
+ end
166
+
167
+ # Changes the owner/group
168
+ #
169
+ # @param uid [Integer] the new owner id. Set to -1 to leave the current one
170
+ # @param gid [Integer] the new group id. Set to -1 to leave the current one
171
+ #
172
+ # @return [nil, OpenNebula::Error] nil in case of success, Error
173
+ # otherwise
174
+ def chown(uid, gid)
175
+ super(VN_METHODS[:chown], uid, gid)
176
+ end
177
+
178
+ # Changes the virtual network permissions.
179
+ #
180
+ # @param octet [String] Permissions octed , e.g. 640
181
+ # @return [nil, OpenNebula::Error] nil in case of success, Error
182
+ # otherwise
183
+ def chmod_octet(octet)
184
+ super(VN_METHODS[:chmod], octet)
185
+ end
186
+
187
+ # Changes the virtual network permissions.
188
+ # Each [Integer] argument must be 1 to allow, 0 deny, -1 do not change
189
+ #
190
+ # @return [nil, OpenNebula::Error] nil in case of success, Error
191
+ # otherwise
192
+ def chmod(owner_u, owner_m, owner_a, group_u, group_m, group_a, other_u,
193
+ other_m, other_a)
194
+ super(VN_METHODS[:chmod], owner_u, owner_m, owner_a, group_u,
195
+ group_m, group_a, other_u, other_m, other_a)
196
+ end
197
+
198
+ # Renames this virtual network
199
+ #
200
+ # @param name [String] New name for the virtual network.
201
+ #
202
+ # @return [nil, OpenNebula::Error] nil in case of success, Error
203
+ # otherwise
204
+ def rename(name)
205
+ return call(VN_METHODS[:rename], @pe_id, name)
206
+ end
207
+
208
+ #######################################################################
209
+ # Helpers to get VirtualNetwork information
210
+ #######################################################################
211
+
212
+ # Returns the group identifier
213
+ # [return] _Integer_ the element's group ID
214
+ def gid
215
+ self['GID'].to_i
216
+ end
217
+
218
+ # Returns the type of the Virtual Network (numeric value)
219
+ def type
220
+ self['TYPE'].to_i
221
+ end
222
+
223
+ # Returns the type of the Virtual Network (string value)
224
+ def type_str
225
+ VN_TYPES[type]
226
+ end
227
+
228
+ # Returns the state of the Virtual Network (string value)
229
+ def short_type_str
230
+ SHORT_VN_TYPES[type_str]
231
+ end
232
+
233
+ def public?
234
+ if self['PERMISSIONS/GROUP_U'] == "1" || self['PERMISSIONS/OTHER_U'] == "1"
235
+ true
236
+ else
237
+ false
238
+ end
239
+ end
240
+
241
+ private
242
+ def set_publish(published)
243
+ group_u = published ? 1 : 0
244
+
245
+ chmod(-1, -1, -1, group_u, -1, -1, -1, -1, -1)
246
+ end
247
+
248
+ end
249
+ end