opennebula-oca 3.8.0.beta1

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.
@@ -0,0 +1,318 @@
1
+ # -------------------------------------------------------------------------- #
2
+ # Copyright 2002-2012, OpenNebula Project Leads (OpenNebula.org) #
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
+ # Retrieves the monitoring data for all the VMs in the pool
117
+ #
118
+ # @param [Array<String>] xpath_expressions Elements to retrieve.
119
+ # @param [Integer] filter_flag Optional filter flag to retrieve all or
120
+ # part of the Pool. Possible values: INFO_ALL, INFO_GROUP, INFO_MINE.
121
+ #
122
+ # @return [Hash<String, <Hash<String, Array<Array<int>>>>>,
123
+ # OpenNebula::Error] The first level hash uses the VM ID as keys, and
124
+ # as value a Hash with the requested xpath expressions,
125
+ # and an Array of 'timestamp, value'.
126
+ #
127
+ # @example
128
+ # vm_pool.monitoring( ['CPU', 'NET_TX', 'TEMPLATE/CUSTOM_PROBE'] )
129
+ #
130
+ # {"1"=>
131
+ # {"CPU"=>
132
+ # [["1337608271", "0"], ["1337608301", "0"], ["1337608331", "0"]],
133
+ # "NET_TX"=>
134
+ # [["1337608271", "510"], ["1337608301", "510"], ["1337608331", "520"]],
135
+ # "TEMPLATE/CUSTOM_PROBE"=>
136
+ # []},
137
+ #
138
+ # "0"=>
139
+ # {"CPU"=>
140
+ # [["1337608271", "0"], ["1337608301", "0"], ["1337608331", "0"]],
141
+ # "NET_TX"=>
142
+ # [["1337608271", "510"], ["1337608301", "510"], ["1337608331", "520"]],
143
+ # "TEMPLATE/CUSTOM_PROBE"=>
144
+ # []}}
145
+ def monitoring(xpath_expressions, filter_flag=INFO_ALL)
146
+ return super(VM_POOL_METHODS[:monitoring],
147
+ 'VM', 'LAST_POLL', xpath_expressions, filter_flag)
148
+ end
149
+
150
+ # Retrieves the monitoring data for all the VMs in the pool, in XML
151
+ #
152
+ # @param [Integer] filter_flag Optional filter flag to retrieve all or
153
+ # part of the Pool. Possible values: INFO_ALL, INFO_GROUP, INFO_MINE.
154
+ #
155
+ # @return [String] VM monitoring data, in XML
156
+ def monitoring_xml(filter_flag=INFO_ALL)
157
+ return @client.call(VM_POOL_METHODS[:monitoring], filter_flag)
158
+ end
159
+
160
+ # Retrieves the accounting data for all the VMs in the pool
161
+ #
162
+ # @param [Integer] filter_flag Optional filter flag to retrieve all or
163
+ # part of the Pool. Possible values: INFO_ALL, INFO_GROUP, INFO_MINE
164
+ # or user_id
165
+ # @param [Hash] options
166
+ # @option params [Integer] :start_time Start date and time to take into account,
167
+ # if no start_time is required use -1
168
+ # @option params [Integer] :end_time End date and time to take into account,
169
+ # if no end_time is required use -1
170
+ # @option params [Integer] :host Host id to filter the results
171
+ # @option params [Integer] :group Group id to filter the results
172
+ # @option params [String] :xpath Xpath expression to filter the results.
173
+ # For example: HISTORY[ETIME>0]
174
+ # @option params [String] :order_by_1 Xpath expression to group the
175
+ # @option params [String] :order_by_2 Xpath expression to group the
176
+ # returned hash. This will be the second level of the hash
177
+ #
178
+ # @return [Hash, OpenNebula::Error]
179
+ # The first level hash uses the :order_by_1 values as keys, and
180
+ # as value a Hash with the :order_by_2 values and the HISTORY_RECORDS
181
+ #
182
+ # @example
183
+ # {"HISTORY_RECORDS"=>
184
+ # {"HISTORY"=> [
185
+ # {"OID"=>"0",
186
+ # "SEQ"=>"0",
187
+ # "HOSTNAME"=>"dummy",
188
+ # ...
189
+ # },
190
+ # {"OID"=>"0",
191
+ # "SEQ"=>"0",
192
+ # "HOSTNAME"=>"dummy",
193
+ #
194
+ # @example :order_by_1 => VM/UID
195
+ # {"0"=>
196
+ # {"HISTORY_RECORDS"=>
197
+ # {"HISTORY"=> [
198
+ # {"OID"=>"0",
199
+ # "SEQ"=>"0",
200
+ # "HOSTNAME"=>"dummy",
201
+ # ...
202
+ # },
203
+ # {"OID"=>"0",
204
+ # "SEQ"=>"0",
205
+ # "HOSTNAME"=>"dummy",
206
+ #
207
+ # @example :order_by_1 => VM/UID, :order_by_2 => VM/ID
208
+ # {"0"=>
209
+ # {"0"=>
210
+ # {"HISTORY_RECORDS"=>
211
+ # {"HISTORY"=> [
212
+ # {"OID"=>"0",
213
+ # "SEQ"=>"0",
214
+ # "HOSTNAME"=>"dummy",
215
+ # ...
216
+ # },
217
+ # {"OID"=>"0",
218
+ # "SEQ"=>"0",
219
+ # "HOSTNAME"=>"dummy",
220
+ #
221
+ def accounting(filter_flag=INFO_ALL, options={})
222
+ acct_hash = Hash.new
223
+
224
+ rc = build_accounting(filter_flag, options) do |history|
225
+ hash = acct_hash
226
+
227
+ if options[:order_by_1]
228
+ id_1 = history[options[:order_by_1]]
229
+ acct_hash[id_1] ||= Hash.new
230
+
231
+ if options[:order_by_2]
232
+ id_2 = history[options[:order_by_2]]
233
+ acct_hash[id_1][id_2] ||= Hash.new
234
+
235
+ hash = acct_hash[id_1][id_2]
236
+ else
237
+ hash = acct_hash[id_1]
238
+ end
239
+ end
240
+
241
+ hash["HISTORY_RECORDS"] ||= Hash.new
242
+ hash["HISTORY_RECORDS"]["HISTORY"] ||= Array.new
243
+ hash["HISTORY_RECORDS"]["HISTORY"] << history.to_hash['HISTORY']
244
+ end
245
+
246
+ return rc if OpenNebula.is_error?(rc)
247
+
248
+ acct_hash
249
+ end
250
+
251
+ # Retrieves the accounting data for all the VMs in the pool in xml
252
+ #
253
+ # @param [Integer] filter_flag Optional filter flag to retrieve all or
254
+ # part of the Pool. Possible values: INFO_ALL, INFO_GROUP, INFO_MINE
255
+ # or user_id
256
+ # @param [Hash] options
257
+ # @option params [Integer] :start_time Start date and time to take into account,
258
+ # if no start_time is required use -1
259
+ # @option params [Integer] :end_time End date and time to take into account,
260
+ # if no end_time is required use -1
261
+ # @option params [Integer] :host Host id to filter the results
262
+ # @option params [Integer] :group Group id to filter the results
263
+ # @option params [String] :xpath Xpath expression to filter the results.
264
+ # For example: HISTORY[ETIME>0]
265
+ #
266
+ # @return [String] the xml representing the accounting data
267
+ def accounting_xml(filter_flag=INFO_ALL, options={})
268
+ acct_hash = Hash.new
269
+ xml_str = "<HISTORY_RECORDS>\n"
270
+
271
+ rc = build_accounting(filter_flag, options) do |history|
272
+ xml_str << history.to_xml
273
+ end
274
+
275
+ return rc if OpenNebula.is_error?(rc)
276
+
277
+ xml_str << "\n</HISTORY_RECORDS>"
278
+ xml_str
279
+ end
280
+
281
+ private
282
+
283
+ def build_accounting(filter_flag, options, &block)
284
+ xml_str = @client.call(VM_POOL_METHODS[:accounting],
285
+ filter_flag,
286
+ options[:start_time],
287
+ options[:end_time])
288
+
289
+ return xml_str if OpenNebula.is_error?(xml_str)
290
+
291
+ xmldoc = XMLElement.new
292
+ xmldoc.initialize_xml(xml_str, 'HISTORY_RECORDS')
293
+
294
+ xpath_array = Array.new
295
+ xpath_array << "HISTORY[HID=#{options[:host]}]" if options[:host]
296
+ xpath_array << "HISTORY[VM/GID=#{options[:group]}]" if options[:group]
297
+ xpath_array << options[:xpath] if options[:xpath]
298
+
299
+ if xpath_array.empty?
300
+ xpath_str = "HISTORY"
301
+ else
302
+ xpath_str = xpath_array.join(' | ')
303
+ end
304
+
305
+ acct_hash = Hash.new
306
+
307
+ xmldoc.each(xpath_str) do |history|
308
+ block.call(history)
309
+ end
310
+
311
+ acct_hash
312
+ end
313
+
314
+ def info_filter(xml_method, who, start_id, end_id, state)
315
+ return xmlrpc_info(xml_method, who, start_id, end_id, state)
316
+ end
317
+ end
318
+ end
@@ -0,0 +1,236 @@
1
+ # -------------------------------------------------------------------------- #
2
+ # Copyright 2002-2012, OpenNebula Project Leads (OpenNebula.org) #
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 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
+ }
39
+
40
+ VN_TYPES=%w{RANGED FIXED}
41
+
42
+ SHORT_VN_TYPES={
43
+ "RANGED" => "R",
44
+ "FIXED" => "F"
45
+ }
46
+
47
+ # Creates a VirtualNetwork description with just its identifier
48
+ # this method should be used to create plain VirtualNetwork objects.
49
+ # +id+ the id of the network
50
+ #
51
+ # Example:
52
+ # vnet = VirtualNetwork.new(VirtualNetwork.build_xml(3),rpc_client)
53
+ #
54
+ def VirtualNetwork.build_xml(pe_id=nil)
55
+ if pe_id
56
+ vn_xml = "<VNET><ID>#{pe_id}</ID></VNET>"
57
+ else
58
+ vn_xml = "<VNET></VNET>"
59
+ end
60
+
61
+ XMLElement.build_xml(vn_xml, 'VNET')
62
+ end
63
+
64
+ # Class constructor
65
+ def initialize(xml, client)
66
+ super(xml,client)
67
+ end
68
+
69
+ #######################################################################
70
+ # XML-RPC Methods for the Virtual Network Object
71
+ #######################################################################
72
+
73
+ # Retrieves the information of the given VirtualNetwork.
74
+ def info()
75
+ super(VN_METHODS[:info], 'VNET')
76
+ end
77
+
78
+ # Allocates a new VirtualNetwork in OpenNebula
79
+ #
80
+ # @param description [String] The template of the VirtualNetwork.
81
+ # @param cluster_id [Integer] Id of the cluster
82
+ #
83
+ # @return [Integer, OpenNebula::Error] the new ID in case of
84
+ # success, error otherwise
85
+ def allocate(description,cluster_id=ClusterPool::NONE_CLUSTER_ID)
86
+ super(VN_METHODS[:allocate], description, cluster_id)
87
+ end
88
+
89
+ # Replaces the template contents
90
+ #
91
+ # +new_template+ New template contents. If no argument is provided
92
+ # the object will be updated using the @xml variable
93
+ def update(new_template=nil)
94
+ super(VN_METHODS[:update], new_template)
95
+ end
96
+
97
+ # Publishes the VirtualNetwork, to be used by other users
98
+ def publish
99
+ set_publish(true)
100
+ end
101
+
102
+ # Unplubishes the VirtualNetwork
103
+ def unpublish
104
+ set_publish(false)
105
+ end
106
+
107
+ # Deletes the VirtualNetwork
108
+ def delete()
109
+ super(VN_METHODS[:delete])
110
+ end
111
+
112
+ # Adds a lease to the VirtualNetwork
113
+ def addleases(ip, mac = nil)
114
+ return Error.new('ID not defined') if !@pe_id
115
+
116
+ lease_template = "LEASES = [ IP = #{ip}"
117
+ lease_template << ", MAC = #{mac}" if mac
118
+ lease_template << " ]"
119
+
120
+ rc = @client.call(VN_METHODS[:addleases], @pe_id, lease_template)
121
+ rc = nil if !OpenNebula.is_error?(rc)
122
+
123
+ return rc
124
+ end
125
+
126
+ # Removes a lease from the VirtualNetwork
127
+ def rmleases(ip)
128
+ return Error.new('ID not defined') if !@pe_id
129
+
130
+ lease_template = "LEASES = [ IP = #{ip} ]"
131
+
132
+ rc = @client.call(VN_METHODS[:rmleases], @pe_id, lease_template)
133
+ rc = nil if !OpenNebula.is_error?(rc)
134
+
135
+ return rc
136
+ end
137
+
138
+ # Holds a virtual network Lease as used
139
+ # @param ip [String] IP to hold
140
+ def hold(ip)
141
+ return Error.new('ID not defined') if !@pe_id
142
+
143
+ lease_template = "LEASES = [ IP = #{ip} ]"
144
+
145
+ rc = @client.call(VN_METHODS[:hold], @pe_id, lease_template)
146
+ rc = nil if !OpenNebula.is_error?(rc)
147
+
148
+ return rc
149
+ end
150
+
151
+ # Releases a virtual network Lease on hold
152
+ # @param ip [String] IP to release
153
+ def release(ip)
154
+ return Error.new('ID not defined') if !@pe_id
155
+
156
+ lease_template = "LEASES = [ IP = #{ip} ]"
157
+
158
+ rc = @client.call(VN_METHODS[:release], @pe_id, lease_template)
159
+ rc = nil if !OpenNebula.is_error?(rc)
160
+
161
+ return rc
162
+ end
163
+
164
+ # Changes the owner/group
165
+ #
166
+ # @param uid [Integer] the new owner id. Set to -1 to leave the current one
167
+ # @param gid [Integer] the new group id. Set to -1 to leave the current one
168
+ #
169
+ # @return [nil, OpenNebula::Error] nil in case of success, Error
170
+ # otherwise
171
+ def chown(uid, gid)
172
+ super(VN_METHODS[:chown], uid, gid)
173
+ end
174
+
175
+ # Changes the virtual network permissions.
176
+ #
177
+ # @param octet [String] Permissions octed , e.g. 640
178
+ # @return [nil, OpenNebula::Error] nil in case of success, Error
179
+ # otherwise
180
+ def chmod_octet(octet)
181
+ super(VN_METHODS[:chmod], octet)
182
+ end
183
+
184
+ # Changes the virtual network permissions.
185
+ # Each [Integer] argument must be 1 to allow, 0 deny, -1 do not change
186
+ #
187
+ # @return [nil, OpenNebula::Error] nil in case of success, Error
188
+ # otherwise
189
+ def chmod(owner_u, owner_m, owner_a, group_u, group_m, group_a, other_u,
190
+ other_m, other_a)
191
+ super(VN_METHODS[:chmod], owner_u, owner_m, owner_a, group_u,
192
+ group_m, group_a, other_u, other_m, other_a)
193
+ end
194
+
195
+ #######################################################################
196
+ # Helpers to get VirtualNetwork information
197
+ #######################################################################
198
+
199
+ # Returns the group identifier
200
+ # [return] _Integer_ the element's group ID
201
+ def gid
202
+ self['GID'].to_i
203
+ end
204
+
205
+ # Returns the type of the Virtual Network (numeric value)
206
+ def type
207
+ self['TYPE'].to_i
208
+ end
209
+
210
+ # Returns the type of the Virtual Network (string value)
211
+ def type_str
212
+ VN_TYPES[type]
213
+ end
214
+
215
+ # Returns the state of the Virtual Network (string value)
216
+ def short_type_str
217
+ SHORT_VN_TYPES[type_str]
218
+ end
219
+
220
+ def public?
221
+ if self['PERMISSIONS/GROUP_U'] == "1" || self['PERMISSIONS/OTHER_U'] == "1"
222
+ true
223
+ else
224
+ false
225
+ end
226
+ end
227
+
228
+ private
229
+ def set_publish(published)
230
+ group_u = published ? 1 : 0
231
+
232
+ chmod(-1, -1, -1, group_u, -1, -1, -1, -1, -1)
233
+ end
234
+
235
+ end
236
+ end
@@ -0,0 +1,74 @@
1
+ # -------------------------------------------------------------------------- #
2
+ # Copyright 2002-2012, OpenNebula Project Leads (OpenNebula.org) #
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 VirtualNetworkPool < Pool
22
+ #######################################################################
23
+ # Constants and Class attribute accessors
24
+ #######################################################################
25
+
26
+
27
+ VN_POOL_METHODS = {
28
+ :info => "vnpool.info"
29
+ }
30
+
31
+ #######################################################################
32
+ # Class constructor & Pool Methods
33
+ #######################################################################
34
+
35
+ # +client+ a Client object that represents a XML-RPC connection
36
+ # +user_id+ is to refer to a Pool with VirtualNetworks from that user
37
+ def initialize(client, user_id=0)
38
+ super('VNET_POOL','VNET',client)
39
+
40
+ @user_id = user_id
41
+ end
42
+
43
+ # Default Factory Method for the Pools
44
+ def factory(element_xml)
45
+ OpenNebula::VirtualNetwork.new(element_xml,@client)
46
+ end
47
+
48
+ #######################################################################
49
+ # XML-RPC Methods for the Virtual Network Object
50
+ #######################################################################
51
+
52
+ # Retrieves all or part of the VirtualMachines in the pool.
53
+ def info(*args)
54
+ case args.size
55
+ when 0
56
+ info_filter(VN_POOL_METHODS[:info],@user_id,-1,-1)
57
+ when 3
58
+ info_filter(VN_POOL_METHODS[:info],args[0],args[1],args[2])
59
+ end
60
+ end
61
+
62
+ def info_all()
63
+ return super(VN_POOL_METHODS[:info])
64
+ end
65
+
66
+ def info_mine()
67
+ return super(VN_POOL_METHODS[:info])
68
+ end
69
+
70
+ def info_group()
71
+ return super(VN_POOL_METHODS[:info])
72
+ end
73
+ end
74
+ end