opennebula-oca 3.8.0.beta1

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,172 @@
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 User < PoolElement
22
+ #######################################################################
23
+ # Constants and Class Methods
24
+ #######################################################################
25
+
26
+ USER_METHODS = {
27
+ :info => "user.info",
28
+ :allocate => "user.allocate",
29
+ :delete => "user.delete",
30
+ :passwd => "user.passwd",
31
+ :chgrp => "user.chgrp",
32
+ :update => "user.update",
33
+ :chauth => "user.chauth",
34
+ :quota => "user.quota"
35
+ }
36
+
37
+ SELF = -1
38
+
39
+ # Driver name for default core authentication
40
+ CORE_AUTH = "core"
41
+
42
+ # Driver name for default core authentication
43
+ CIPHER_AUTH = "server_cipher"
44
+
45
+ # Driver name for ssh authentication
46
+ SSH_AUTH = "ssh"
47
+
48
+ # Driver name for x509 authentication
49
+ X509_AUTH = "x509"
50
+
51
+ # Driver name for x509 proxy authentication
52
+ X509_PROXY_AUTH = "x509_proxy"
53
+
54
+ # Creates a User description with just its identifier
55
+ # this method should be used to create plain User objects.
56
+ # +id+ the id of the user
57
+ #
58
+ # Example:
59
+ # user = User.new(User.build_xml(3),rpc_client)
60
+ #
61
+ def User.build_xml(pe_id=nil)
62
+ if pe_id
63
+ user_xml = "<USER><ID>#{pe_id}</ID></USER>"
64
+ else
65
+ user_xml = "<USER></USER>"
66
+ end
67
+
68
+ XMLElement.build_xml(user_xml, 'USER')
69
+ end
70
+
71
+ # Class constructor
72
+ def initialize(xml, client)
73
+ super(xml,client)
74
+
75
+ @client = client
76
+ end
77
+
78
+ #######################################################################
79
+ # XML-RPC Methods for the User Object
80
+ #######################################################################
81
+
82
+ # Retrieves the information of the given User.
83
+ def info()
84
+ super(USER_METHODS[:info], 'USER')
85
+ end
86
+
87
+ # Allocates a new User in OpenNebula
88
+ #
89
+ # +username+ Name of the new user.
90
+ #
91
+ # +password+ Password for the new user
92
+ def allocate(username, password, driver=CORE_AUTH)
93
+ super(USER_METHODS[:allocate], username, password, driver)
94
+ end
95
+
96
+ # Replaces the template contents
97
+ #
98
+ # +new_template+ New template contents
99
+ def update(new_template)
100
+ super(USER_METHODS[:update], new_template)
101
+ end
102
+
103
+ # Deletes the User
104
+ def delete()
105
+ super(USER_METHODS[:delete])
106
+ end
107
+
108
+ # Changes the password of the given User
109
+ #
110
+ # +password+ String containing the new password
111
+ def passwd(password)
112
+ return Error.new('ID not defined') if !@pe_id
113
+
114
+ rc = @client.call(USER_METHODS[:passwd], @pe_id, password)
115
+ rc = nil if !OpenNebula.is_error?(rc)
116
+
117
+ return rc
118
+ end
119
+
120
+ # Changes the main group
121
+ # gid:: _Integer_ the new group id. Set to -1 to leave the current one
122
+ # [return] nil in case of success or an Error object
123
+ def chgrp(gid)
124
+ return Error.new('ID not defined') if !@pe_id
125
+
126
+ rc = @client.call(USER_METHODS[:chgrp],@pe_id, gid)
127
+ rc = nil if !OpenNebula.is_error?(rc)
128
+
129
+ return rc
130
+ end
131
+
132
+ # Changes the auth driver and the password of the given User
133
+ #
134
+ # @param auth [String] the new auth driver
135
+ # @param password [String] the new password. If it is an empty string,
136
+ # the user password is not changed
137
+ # @return [nil, OpenNebula::Error] nil in case of success, Error
138
+ # otherwise
139
+ def chauth(auth, password="")
140
+ return Error.new('ID not defined') if !@pe_id
141
+
142
+ rc = @client.call(USER_METHODS[:chauth],@pe_id, auth, password)
143
+ rc = nil if !OpenNebula.is_error?(rc)
144
+
145
+ return rc
146
+ end
147
+
148
+ # Sets the user quota limits
149
+ # @param quota [String] a template (XML or txt) with the new quota limits
150
+ #
151
+ # @return [nil, OpenNebula::Error] nil in case of success, Error
152
+ # otherwise
153
+ def set_quota(quota)
154
+ return Error.new('ID not defined') if !@pe_id
155
+
156
+ rc = @client.call(USER_METHODS[:quota],@pe_id, quota)
157
+ rc = nil if !OpenNebula.is_error?(rc)
158
+
159
+ return rc
160
+ end
161
+
162
+ #######################################################################
163
+ # Helpers to get User information
164
+ #######################################################################
165
+
166
+ # Returns the group identifier
167
+ # [return] _Integer_ the element's group ID
168
+ def gid
169
+ self['GID'].to_i
170
+ end
171
+ end
172
+ end
@@ -0,0 +1,53 @@
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 UserPool < Pool
22
+ #######################################################################
23
+ # Constants and Class attribute accessors
24
+ #######################################################################
25
+
26
+ USER_POOL_METHODS = {
27
+ :info => "userpool.info"
28
+ }
29
+
30
+ #######################################################################
31
+ # Class constructor & Pool Methods
32
+ #######################################################################
33
+
34
+ # +client+ a Client object that represents a XML-RPC connection
35
+ def initialize(client)
36
+ super('USER_POOL','USER',client)
37
+ end
38
+
39
+ # Factory method to create User objects
40
+ def factory(element_xml)
41
+ OpenNebula::User.new(element_xml,@client)
42
+ end
43
+
44
+ #######################################################################
45
+ # XML-RPC Methods for the User Object
46
+ #######################################################################
47
+
48
+ # Retrieves all the Users in the pool.
49
+ def info()
50
+ super(USER_POOL_METHODS[:info])
51
+ end
52
+ end
53
+ end
@@ -0,0 +1,406 @@
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 VirtualMachine < PoolElement
22
+ #######################################################################
23
+ # Constants and Class Methods
24
+ #######################################################################
25
+
26
+
27
+ VM_METHODS = {
28
+ :info => "vm.info",
29
+ :allocate => "vm.allocate",
30
+ :action => "vm.action",
31
+ :migrate => "vm.migrate",
32
+ :deploy => "vm.deploy",
33
+ :savedisk => "vm.savedisk",
34
+ :chown => "vm.chown",
35
+ :chmod => "vm.chmod",
36
+ :monitoring => "vm.monitoring",
37
+ :attach => "vm.attach",
38
+ :detach => "vm.detach"
39
+ }
40
+
41
+ VM_STATE=%w{INIT PENDING HOLD ACTIVE STOPPED SUSPENDED DONE FAILED
42
+ POWEROFF}
43
+
44
+ LCM_STATE=%w{LCM_INIT PROLOG BOOT RUNNING MIGRATE SAVE_STOP SAVE_SUSPEND
45
+ SAVE_MIGRATE PROLOG_MIGRATE PROLOG_RESUME EPILOG_STOP EPILOG
46
+ SHUTDOWN CANCEL FAILURE CLEANUP UNKNOWN HOTPLUG SHUTDOWN_POWEROFF
47
+ BOOT_UNKNOWN BOOT_POWEROFF BOOT_SUSPENDED BOOT_STOPPED}
48
+
49
+ SHORT_VM_STATES={
50
+ "INIT" => "init",
51
+ "PENDING" => "pend",
52
+ "HOLD" => "hold",
53
+ "ACTIVE" => "actv",
54
+ "STOPPED" => "stop",
55
+ "SUSPENDED" => "susp",
56
+ "DONE" => "done",
57
+ "FAILED" => "fail",
58
+ "POWEROFF" => "poff"
59
+ }
60
+
61
+ SHORT_LCM_STATES={
62
+ "PROLOG" => "prol",
63
+ "BOOT" => "boot",
64
+ "RUNNING" => "runn",
65
+ "MIGRATE" => "migr",
66
+ "SAVE_STOP" => "save",
67
+ "SAVE_SUSPEND" => "save",
68
+ "SAVE_MIGRATE" => "save",
69
+ "PROLOG_MIGRATE"=> "migr",
70
+ "PROLOG_RESUME" => "prol",
71
+ "EPILOG_STOP" => "epil",
72
+ "EPILOG" => "epil",
73
+ "SHUTDOWN" => "shut",
74
+ "CANCEL" => "shut",
75
+ "FAILURE" => "fail",
76
+ "CLEANUP" => "clea",
77
+ "UNKNOWN" => "unkn",
78
+ "HOTPLUG" => "hotp",
79
+ "SHUTDOWN_POWEROFF" => "shut",
80
+ "BOOT_UNKNOWN" => "boot",
81
+ "BOOT_POWEROFF" => "boot",
82
+ "BOOT_SUSPENDED" => "boot",
83
+ "BOOT_STOPPED" => "boot"
84
+ }
85
+
86
+ MIGRATE_REASON=%w{NONE ERROR STOP_RESUME USER CANCEL}
87
+
88
+ SHORT_MIGRATE_REASON={
89
+ "NONE" => "none",
90
+ "ERROR" => "erro",
91
+ "STOP_RESUME" => "stop",
92
+ "USER" => "user",
93
+ "CANCEL" => "canc"
94
+ }
95
+
96
+ # Creates a VirtualMachine description with just its identifier
97
+ # this method should be used to create plain VirtualMachine objects.
98
+ # +id+ the id of the vm
99
+ #
100
+ # Example:
101
+ # vnet = VirtualMachine.new(VirtualMachine.build_xml(3),rpc_client)
102
+ #
103
+ def VirtualMachine.build_xml(pe_id=nil)
104
+ if pe_id
105
+ vm_xml = "<VM><ID>#{pe_id}</ID></VM>"
106
+ else
107
+ vm_xml = "<VM></VM>"
108
+ end
109
+
110
+ XMLElement.build_xml(vm_xml, 'VM')
111
+ end
112
+
113
+ def VirtualMachine.get_reason(reason)
114
+ reason=MIGRATE_REASON[reason.to_i]
115
+ reason_str=SHORT_MIGRATE_REASON[reason]
116
+
117
+ reason_str
118
+ end
119
+
120
+ # Class constructor
121
+ def initialize(xml, client)
122
+ super(xml,client)
123
+ end
124
+
125
+ #######################################################################
126
+ # XML-RPC Methods for the Virtual Machine Object
127
+ #######################################################################
128
+
129
+ # Retrieves the information of the given VirtualMachine.
130
+ def info()
131
+ super(VM_METHODS[:info], 'VM')
132
+ end
133
+
134
+ # Allocates a new VirtualMachine in OpenNebula
135
+ #
136
+ # +description+ A string containing the template of the VirtualMachine.
137
+ def allocate(description)
138
+ super(VM_METHODS[:allocate],description)
139
+ end
140
+
141
+ # Initiates the instance of the VM on the target host.
142
+ #
143
+ # +host_id+ The host id (hid) of the target host where
144
+ # the VM will be instantiated.
145
+ def deploy(host_id)
146
+ return Error.new('ID not defined') if !@pe_id
147
+
148
+ rc = @client.call(VM_METHODS[:deploy], @pe_id, host_id.to_i)
149
+ rc = nil if !OpenNebula.is_error?(rc)
150
+
151
+ return rc
152
+ end
153
+
154
+ # Shutdowns an already deployed VM
155
+ def shutdown
156
+ action('shutdown')
157
+ end
158
+
159
+ # Powers off a running VM
160
+ def poweroff
161
+ action('poweroff')
162
+ end
163
+
164
+ # Reboots an already deployed VM
165
+ def reboot
166
+ action('reboot')
167
+ end
168
+
169
+ # Resets an already deployed VM
170
+ def reset
171
+ action('reset')
172
+ end
173
+
174
+ # Cancels a running VM
175
+ def cancel
176
+ action('cancel')
177
+ end
178
+
179
+ # Sets a VM to hold state, scheduler will not deploy it
180
+ def hold
181
+ action('hold')
182
+ end
183
+
184
+ # Releases a VM from hold state
185
+ def release
186
+ action('release')
187
+ end
188
+
189
+ # Stops a running VM
190
+ def stop
191
+ action('stop')
192
+ end
193
+
194
+ # Saves a running VM
195
+ def suspend
196
+ action('suspend')
197
+ end
198
+
199
+ # Resumes the execution of a saved VM
200
+ def resume
201
+ action('resume')
202
+ end
203
+
204
+ # Attaches a disk to a running VM
205
+ def attachdisk(disk)
206
+ return Error.new('ID not defined') if !@pe_id
207
+
208
+ rc = @client.call(VM_METHODS[:attach], @pe_id, disk)
209
+ rc = nil if !OpenNebula.is_error?(rc)
210
+
211
+ return rc
212
+ end
213
+
214
+ # Detaches a disk from a running VM
215
+ def detachdisk(disk)
216
+ return Error.new('ID not defined') if !@pe_id
217
+
218
+ rc = @client.call(VM_METHODS[:detach], @pe_id, disk)
219
+ rc = nil if !OpenNebula.is_error?(rc)
220
+
221
+ return rc
222
+ end
223
+
224
+ # Deletes a VM from the pool
225
+ def finalize
226
+ action('finalize')
227
+ end
228
+
229
+ # Forces a re-deployment of a VM in UNKNOWN or BOOT state
230
+ def restart
231
+ action('restart')
232
+ end
233
+
234
+ # Resubmits a VM to PENDING state
235
+ def resubmit
236
+ action('resubmit')
237
+ end
238
+
239
+ # Sets the re-scheduling flag for the VM
240
+ def resched
241
+ action('resched')
242
+ end
243
+
244
+ # Unsets the re-scheduling flag for the VM
245
+ def unresched
246
+ action('unresched')
247
+ end
248
+
249
+ # Saves a running VM and starts it again in the specified host
250
+ def migrate(host_id)
251
+ return Error.new('ID not defined') if !@pe_id
252
+
253
+ rc = @client.call(VM_METHODS[:migrate], @pe_id, host_id.to_i, false)
254
+ rc = nil if !OpenNebula.is_error?(rc)
255
+
256
+ return rc
257
+ end
258
+
259
+ # Migrates a running VM to another host without downtime
260
+ def live_migrate(host_id)
261
+ return Error.new('ID not defined') if !@pe_id
262
+
263
+ rc = @client.call(VM_METHODS[:migrate], @pe_id, host_id.to_i, true)
264
+ rc = nil if !OpenNebula.is_error?(rc)
265
+
266
+ return rc
267
+ end
268
+
269
+ # Set the specified vm's disk to be saved in a new image
270
+ # when the VirtualMachine shutdowns
271
+ #
272
+ # @param disk_id [Integer] ID of the disk to be saved
273
+ # @param image_name [String] Name for the new image where the
274
+ # disk will be saved
275
+ # @param image_type [String] Type of the new image. Set to empty string
276
+ # to use the default type
277
+ #
278
+ # @return [Integer, OpenNebula::Error] the new Image ID in case of
279
+ # success, error otherwise
280
+ def save_as(disk_id, image_name, image_type="")
281
+ return Error.new('ID not defined') if !@pe_id
282
+
283
+ rc = @client.call(VM_METHODS[:savedisk],
284
+ @pe_id,
285
+ disk_id,
286
+ image_name,
287
+ image_type)
288
+
289
+ return rc
290
+ end
291
+
292
+ # Changes the owner/group
293
+ # uid:: _Integer_ the new owner id. Set to -1 to leave the current one
294
+ # gid:: _Integer_ the new group id. Set to -1 to leave the current one
295
+ # [return] nil in case of success or an Error object
296
+ def chown(uid, gid)
297
+ super(VM_METHODS[:chown], uid, gid)
298
+ end
299
+
300
+ # Changes the permissions.
301
+ #
302
+ # @param octet [String] Permissions octed , e.g. 640
303
+ # @return [nil, OpenNebula::Error] nil in case of success, Error
304
+ # otherwise
305
+ def chmod_octet(octet)
306
+ super(VM_METHODS[:chmod], octet)
307
+ end
308
+
309
+ # Changes the permissions.
310
+ # Each [Integer] argument must be 1 to allow, 0 deny, -1 do not change
311
+ #
312
+ # @return [nil, OpenNebula::Error] nil in case of success, Error
313
+ # otherwise
314
+ def chmod(owner_u, owner_m, owner_a, group_u, group_m, group_a, other_u,
315
+ other_m, other_a)
316
+ super(VM_METHODS[:chmod], owner_u, owner_m, owner_a, group_u,
317
+ group_m, group_a, other_u, other_m, other_a)
318
+ end
319
+
320
+ # Retrieves this VM's monitoring data from OpenNebula
321
+ #
322
+ # @param [Array<String>] xpath_expressions Elements to retrieve.
323
+ #
324
+ # @return [Hash<String, Array<Array<int>>>, OpenNebula::Error] Hash with
325
+ # the requested xpath expressions, and an Array of 'timestamp, value'.
326
+ #
327
+ # @example
328
+ # vm.monitoring( ['CPU', 'NET_TX', 'TEMPLATE/CUSTOM_PROBE'] )
329
+ #
330
+ # { "NET_TX" =>
331
+ # [["1337264510", "210"],
332
+ # ["1337264553", "220"],
333
+ # ["1337264584", "230"]],
334
+ # "TEMPLATE/CUSTOM_PROBE" =>
335
+ # [],
336
+ # "CPU" =>
337
+ # [["1337264510", "0"],
338
+ # ["1337264553", "0"],
339
+ # ["1337264584", "0"]]
340
+ # }
341
+ def monitoring(xpath_expressions)
342
+ return super(VM_METHODS[:monitoring], 'VM',
343
+ 'LAST_POLL', xpath_expressions)
344
+ end
345
+
346
+ # Retrieves this VM's monitoring data from OpenNebula, in XML
347
+ #
348
+ # @return [String] VM monitoring data, in XML
349
+ def monitoring_xml()
350
+ return Error.new('ID not defined') if !@pe_id
351
+
352
+ return @client.call(VM_METHODS[:monitoring], @pe_id)
353
+ end
354
+
355
+ #######################################################################
356
+ # Helpers to get VirtualMachine information
357
+ #######################################################################
358
+
359
+ # Returns the VM state of the VirtualMachine (numeric value)
360
+ def state
361
+ self['STATE'].to_i
362
+ end
363
+
364
+ # Returns the VM state of the VirtualMachine (string value)
365
+ def state_str
366
+ VM_STATE[state]
367
+ end
368
+
369
+ # Returns the LCM state of the VirtualMachine (numeric value)
370
+ def lcm_state
371
+ self['LCM_STATE'].to_i
372
+ end
373
+
374
+ # Returns the LCM state of the VirtualMachine (string value)
375
+ def lcm_state_str
376
+ LCM_STATE[lcm_state]
377
+ end
378
+
379
+ # Returns the short status string for the VirtualMachine
380
+ def status
381
+ short_state_str=SHORT_VM_STATES[state_str]
382
+
383
+ if short_state_str=="actv"
384
+ short_state_str=SHORT_LCM_STATES[lcm_state_str]
385
+ end
386
+
387
+ short_state_str
388
+ end
389
+
390
+ # Returns the group identifier
391
+ # [return] _Integer_ the element's group ID
392
+ def gid
393
+ self['GID'].to_i
394
+ end
395
+
396
+ private
397
+ def action(name)
398
+ return Error.new('ID not defined') if !@pe_id
399
+
400
+ rc = @client.call(VM_METHODS[:action], name, @pe_id)
401
+ rc = nil if !OpenNebula.is_error?(rc)
402
+
403
+ return rc
404
+ end
405
+ end
406
+ end