oca 3.0.1

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,146 @@
1
+ # -------------------------------------------------------------------------- #
2
+ # Copyright 2002-2011, 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
+ require 'OpenNebula/Pool'
18
+
19
+ module OpenNebula
20
+ class Template < PoolElement
21
+ # ---------------------------------------------------------------------
22
+ # Constants and Class Methods
23
+ # ---------------------------------------------------------------------
24
+ TEMPLATE_METHODS = {
25
+ :allocate => "template.allocate",
26
+ :instantiate => "template.instantiate",
27
+ :info => "template.info",
28
+ :update => "template.update",
29
+ :publish => "template.publish",
30
+ :delete => "template.delete",
31
+ :chown => "template.chown"
32
+ }
33
+
34
+ # Creates a Template description with just its identifier
35
+ # this method should be used to create plain Template objects.
36
+ # +id+ the id of the user
37
+ #
38
+ # Example:
39
+ # template = Template.new(Template.build_xml(3),rpc_client)
40
+ #
41
+ def Template.build_xml(pe_id=nil)
42
+ if pe_id
43
+ obj_xml = "<VMTEMPLATE><ID>#{pe_id}</ID></VMTEMPLATE>"
44
+ else
45
+ obj_xml = "<VMTEMPLATE></VMTEMPLATE>"
46
+ end
47
+
48
+ XMLElement.build_xml(obj_xml,'VMTEMPLATE')
49
+ end
50
+
51
+ # ---------------------------------------------------------------------
52
+ # Class constructor
53
+ # ---------------------------------------------------------------------
54
+ def initialize(xml, client)
55
+ super(xml,client)
56
+
57
+ @client = client
58
+ end
59
+
60
+ # ---------------------------------------------------------------------
61
+ # XML-RPC Methods for the Template Object
62
+ # ---------------------------------------------------------------------
63
+
64
+ # Retrieves the information of the given Template.
65
+ def info()
66
+ super(TEMPLATE_METHODS[:info], 'VMTEMPLATE')
67
+ end
68
+
69
+ # Allocates a new Template in OpenNebula
70
+ #
71
+ # +templatename+ A string containing the name of the Template.
72
+ def allocate(templatename)
73
+ super(TEMPLATE_METHODS[:allocate], templatename)
74
+ end
75
+
76
+ # Deletes the Template
77
+ def delete()
78
+ super(TEMPLATE_METHODS[:delete])
79
+ end
80
+
81
+ # Creates a VM instance from a Template
82
+ #
83
+ # +name+ A string containing the name of the VM instance.
84
+ # [return] The new VM Instance ID, or an Error object
85
+ def instantiate(name="")
86
+ return Error.new('ID not defined') if !@pe_id
87
+
88
+ name ||= ""
89
+ rc = @client.call(TEMPLATE_METHODS[:instantiate], @pe_id, name)
90
+
91
+ return rc
92
+ end
93
+
94
+ # Replaces the template contents
95
+ #
96
+ # +new_template+ New template contents
97
+ def update(new_template)
98
+ return Error.new('ID not defined') if !@pe_id
99
+
100
+ super(TEMPLATE_METHODS[:update], new_template)
101
+ end
102
+
103
+ # Publishes the Template, to be used by other users
104
+ def publish
105
+ set_publish(true)
106
+ end
107
+
108
+ # Unplubishes the Image
109
+ def unpublish
110
+ set_publish(false)
111
+ end
112
+
113
+ # Changes the owner/group
114
+ # uid:: _Integer_ the new owner id. Set to -1 to leave the current one
115
+ # gid:: _Integer_ the new group id. Set to -1 to leave the current one
116
+ # [return] nil in case of success or an Error object
117
+ def chown(uid, gid)
118
+ super(TEMPLATE_METHODS[:chown], uid, gid)
119
+ end
120
+
121
+ # ---------------------------------------------------------------------
122
+ # Helpers to get Template information
123
+ # ---------------------------------------------------------------------
124
+
125
+ # Returns the group identifier
126
+ # [return] _Integer_ the element's group ID
127
+ def gid
128
+ self['GID'].to_i
129
+ end
130
+
131
+ def owner_id
132
+ self['UID'].to_i
133
+ end
134
+
135
+ private
136
+
137
+ def set_publish(published)
138
+ return Error.new('ID not defined') if !@pe_id
139
+
140
+ rc = @client.call(TEMPLATE_METHODS[:publish], @pe_id, published)
141
+ rc = nil if !OpenNebula.is_error?(rc)
142
+
143
+ return rc
144
+ end
145
+ end
146
+ end
@@ -0,0 +1,71 @@
1
+ # -------------------------------------------------------------------------- #
2
+ # Copyright 2002-2011, 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
+ require 'OpenNebula/Pool'
18
+
19
+ module OpenNebula
20
+ class TemplatePool < Pool
21
+ # ---------------------------------------------------------------------
22
+ # Constants and Class attribute accessors
23
+ # ---------------------------------------------------------------------
24
+
25
+ TEMPLATE_POOL_METHODS = {
26
+ :info => "templatepool.info"
27
+ }
28
+
29
+ # ---------------------------------------------------------------------
30
+ # Class constructor & Pool Methods
31
+ # ---------------------------------------------------------------------
32
+
33
+ # +client+ a Client object that represents an XML-RPC connection
34
+ # +user_id+ used to refer to a Pool with Templates from that user
35
+ def initialize(client, user_id=-1)
36
+ super('VMTEMPLATE_POOL','VMTEMPLATE',client)
37
+
38
+ @user_id = user_id
39
+ end
40
+
41
+ # Factory method to create Template objects
42
+ def factory(element_xml)
43
+ OpenNebula::Template.new(element_xml,@client)
44
+ end
45
+
46
+ # ---------------------------------------------------------------------
47
+ # XML-RPC Methods for the Template Object
48
+ # ---------------------------------------------------------------------
49
+ # Retrieves all or part of the VirtualMachines in the pool.
50
+ def info(*args)
51
+ case args.size
52
+ when 0
53
+ info_filter(TEMPLATE_POOL_METHODS[:info],@user_id,-1,-1)
54
+ when 3
55
+ info_filter(TEMPLATE_POOL_METHODS[:info],args[0],args[1],args[2])
56
+ end
57
+ end
58
+
59
+ def info_all()
60
+ return super(TEMPLATE_POOL_METHODS[:info])
61
+ end
62
+
63
+ def info_mine()
64
+ return super(TEMPLATE_POOL_METHODS[:info])
65
+ end
66
+
67
+ def info_group()
68
+ return super(TEMPLATE_POOL_METHODS[:info])
69
+ end
70
+ end
71
+ end
@@ -0,0 +1,117 @@
1
+ # -------------------------------------------------------------------------- #
2
+ # Copyright 2002-2011, 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
+ require 'OpenNebula/Pool'
18
+
19
+ module OpenNebula
20
+ class User < PoolElement
21
+ # ---------------------------------------------------------------------
22
+ # Constants and Class Methods
23
+ # ---------------------------------------------------------------------
24
+ USER_METHODS = {
25
+ :info => "user.info",
26
+ :allocate => "user.allocate",
27
+ :delete => "user.delete",
28
+ :passwd => "user.passwd",
29
+ :chgrp => "user.chgrp"
30
+ }
31
+
32
+ SELF = -1
33
+
34
+ # Creates a User description with just its identifier
35
+ # this method should be used to create plain User objects.
36
+ # +id+ the id of the user
37
+ #
38
+ # Example:
39
+ # user = User.new(User.build_xml(3),rpc_client)
40
+ #
41
+ def User.build_xml(pe_id=nil)
42
+ if pe_id
43
+ user_xml = "<USER><ID>#{pe_id}</ID></USER>"
44
+ else
45
+ user_xml = "<USER></USER>"
46
+ end
47
+
48
+ XMLElement.build_xml(user_xml, 'USER')
49
+ end
50
+
51
+ # ---------------------------------------------------------------------
52
+ # Class constructor
53
+ # ---------------------------------------------------------------------
54
+ def initialize(xml, client)
55
+ super(xml,client)
56
+
57
+ @client = client
58
+ end
59
+
60
+ # ---------------------------------------------------------------------
61
+ # XML-RPC Methods for the User Object
62
+ # ---------------------------------------------------------------------
63
+
64
+ # Retrieves the information of the given User.
65
+ def info()
66
+ super(USER_METHODS[:info], 'USER')
67
+ end
68
+
69
+ # Allocates a new User in OpenNebula
70
+ #
71
+ # +username+ Name of the new user.
72
+ #
73
+ # +password+ Password for the new user
74
+ def allocate(username, password)
75
+ super(USER_METHODS[:allocate], username, password)
76
+ end
77
+
78
+ # Deletes the User
79
+ def delete()
80
+ super(USER_METHODS[:delete])
81
+ end
82
+
83
+ # Changes the password of the given User
84
+ #
85
+ # +password+ String containing the new password
86
+ def passwd(password)
87
+ return Error.new('ID not defined') if !@pe_id
88
+
89
+ rc = @client.call(USER_METHODS[:passwd], @pe_id, password)
90
+ rc = nil if !OpenNebula.is_error?(rc)
91
+
92
+ return rc
93
+ end
94
+
95
+ # Changes the main group
96
+ # gid:: _Integer_ the new group id. Set to -1 to leave the current one
97
+ # [return] nil in case of success or an Error object
98
+ def chgrp(gid)
99
+ return Error.new('ID not defined') if !@pe_id
100
+
101
+ rc = @client.call(USER_METHODS[:chgrp],@pe_id, gid)
102
+ rc = nil if !OpenNebula.is_error?(rc)
103
+
104
+ return rc
105
+ end
106
+
107
+ # ---------------------------------------------------------------------
108
+ # Helpers to get User information
109
+ # ---------------------------------------------------------------------
110
+
111
+ # Returns the group identifier
112
+ # [return] _Integer_ the element's group ID
113
+ def gid
114
+ self['GID'].to_i
115
+ end
116
+ end
117
+ end
@@ -0,0 +1,52 @@
1
+ # -------------------------------------------------------------------------- #
2
+ # Copyright 2002-2011, 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
+ require 'OpenNebula/Pool'
18
+
19
+ module OpenNebula
20
+ class UserPool < Pool
21
+ # ---------------------------------------------------------------------
22
+ # Constants and Class attribute accessors
23
+ # ---------------------------------------------------------------------
24
+
25
+ USER_POOL_METHODS = {
26
+ :info => "userpool.info"
27
+ }
28
+
29
+ # ---------------------------------------------------------------------
30
+ # Class constructor & Pool Methods
31
+ # ---------------------------------------------------------------------
32
+
33
+ # +client+ a Client object that represents a XML-RPC connection
34
+ def initialize(client)
35
+ super('USER_POOL','USER',client)
36
+ end
37
+
38
+ # Factory method to create User objects
39
+ def factory(element_xml)
40
+ OpenNebula::User.new(element_xml,@client)
41
+ end
42
+
43
+ # ---------------------------------------------------------------------
44
+ # XML-RPC Methods for the User Object
45
+ # ---------------------------------------------------------------------
46
+
47
+ # Retrieves all the Users in the pool.
48
+ def info()
49
+ super(USER_POOL_METHODS[:info])
50
+ end
51
+ end
52
+ end
@@ -0,0 +1,289 @@
1
+ # -------------------------------------------------------------------------- #
2
+ # Copyright 2002-2011, 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
+ require 'OpenNebula/Pool'
18
+
19
+ module OpenNebula
20
+ class VirtualMachine < PoolElement
21
+ #######################################################################
22
+ # Constants and Class Methods
23
+ #######################################################################
24
+ VM_METHODS = {
25
+ :info => "vm.info",
26
+ :allocate => "vm.allocate",
27
+ :action => "vm.action",
28
+ :migrate => "vm.migrate",
29
+ :deploy => "vm.deploy",
30
+ :savedisk => "vm.savedisk",
31
+ :chown => "vm.chown"
32
+ }
33
+
34
+ VM_STATE=%w{INIT PENDING HOLD ACTIVE STOPPED SUSPENDED DONE FAILED}
35
+
36
+ LCM_STATE=%w{LCM_INIT PROLOG BOOT RUNNING MIGRATE SAVE_STOP SAVE_SUSPEND
37
+ SAVE_MIGRATE PROLOG_MIGRATE PROLOG_RESUME EPILOG_STOP EPILOG
38
+ SHUTDOWN CANCEL FAILURE CLEANUP UNKNOWN}
39
+
40
+ SHORT_VM_STATES={
41
+ "INIT" => "init",
42
+ "PENDING" => "pend",
43
+ "HOLD" => "hold",
44
+ "ACTIVE" => "actv",
45
+ "STOPPED" => "stop",
46
+ "SUSPENDED" => "susp",
47
+ "DONE" => "done",
48
+ "FAILED" => "fail"
49
+ }
50
+
51
+ SHORT_LCM_STATES={
52
+ "PROLOG" => "prol",
53
+ "BOOT" => "boot",
54
+ "RUNNING" => "runn",
55
+ "MIGRATE" => "migr",
56
+ "SAVE_STOP" => "save",
57
+ "SAVE_SUSPEND" => "save",
58
+ "SAVE_MIGRATE" => "save",
59
+ "PROLOG_MIGRATE"=> "migr",
60
+ "PROLOG_RESUME" => "prol",
61
+ "EPILOG_STOP" => "epil",
62
+ "EPILOG" => "epil",
63
+ "SHUTDOWN" => "shut",
64
+ "CANCEL" => "shut",
65
+ "FAILURE" => "fail",
66
+ "CLEANUP" => "clea",
67
+ "UNKNOWN" => "unkn"
68
+ }
69
+
70
+ MIGRATE_REASON=%w{NONE ERROR STOP_RESUME USER CANCEL}
71
+
72
+ SHORT_MIGRATE_REASON={
73
+ "NONE" => "none",
74
+ "ERROR" => "erro",
75
+ "STOP_RESUME" => "stop",
76
+ "USER" => "user",
77
+ "CANCEL" => "canc"
78
+ }
79
+
80
+ # Creates a VirtualMachine description with just its identifier
81
+ # this method should be used to create plain VirtualMachine objects.
82
+ # +id+ the id of the vm
83
+ #
84
+ # Example:
85
+ # vnet = VirtualMachine.new(VirtualMachine.build_xml(3),rpc_client)
86
+ #
87
+ def VirtualMachine.build_xml(pe_id=nil)
88
+ if pe_id
89
+ vm_xml = "<VM><ID>#{pe_id}</ID></VM>"
90
+ else
91
+ vm_xml = "<VM></VM>"
92
+ end
93
+
94
+ XMLElement.build_xml(vm_xml, 'VM')
95
+ end
96
+
97
+ def VirtualMachine.get_reason(reason)
98
+ reason=MIGRATE_REASON[reason.to_i]
99
+ reason_str=SHORT_MIGRATE_REASON[reason]
100
+
101
+ reason_str
102
+ end
103
+
104
+ #######################################################################
105
+ # Class constructor
106
+ #######################################################################
107
+ def initialize(xml, client)
108
+ super(xml,client)
109
+
110
+ @element_name = "VM"
111
+ @client = client
112
+ end
113
+
114
+ #######################################################################
115
+ # XML-RPC Methods for the Virtual Machine Object
116
+ #######################################################################
117
+
118
+ # Retrieves the information of the given VirtualMachine.
119
+ def info()
120
+ super(VM_METHODS[:info], 'VM')
121
+ end
122
+
123
+ # Allocates a new VirtualMachine in OpenNebula
124
+ #
125
+ # +description+ A string containing the template of the VirtualMachine.
126
+ def allocate(description)
127
+ super(VM_METHODS[:allocate],description)
128
+ end
129
+
130
+ # Initiates the instance of the VM on the target host.
131
+ #
132
+ # +host_id+ The host id (hid) of the target host where
133
+ # the VM will be instantiated.
134
+ def deploy(host_id)
135
+ return Error.new('ID not defined') if !@pe_id
136
+
137
+ rc = @client.call(VM_METHODS[:deploy], @pe_id, host_id.to_i)
138
+ rc = nil if !OpenNebula.is_error?(rc)
139
+
140
+ return rc
141
+ end
142
+
143
+ # Shutdowns an already deployed VM
144
+ def shutdown
145
+ action('shutdown')
146
+ end
147
+
148
+ # Cancels a running VM
149
+ def cancel
150
+ action('cancel')
151
+ end
152
+
153
+ # Sets a VM to hold state, scheduler will not deploy it
154
+ def hold
155
+ action('hold')
156
+ end
157
+
158
+ # Releases a VM from hold state
159
+ def release
160
+ action('release')
161
+ end
162
+
163
+ # Stops a running VM
164
+ def stop
165
+ action('stop')
166
+ end
167
+
168
+ # Saves a running VM
169
+ def suspend
170
+ action('suspend')
171
+ end
172
+
173
+ # Resumes the execution of a saved VM
174
+ def resume
175
+ action('resume')
176
+ end
177
+
178
+ # Deletes a VM from the pool
179
+ def finalize
180
+ action('finalize')
181
+ end
182
+
183
+ # Forces a re-deployment of a VM in UNKNOWN or BOOT state
184
+ def restart
185
+ action('restart')
186
+ end
187
+
188
+ # Resubmits a VM to PENDING state
189
+ def resubmit
190
+ action('resubmit')
191
+ end
192
+
193
+ # Saves a running VM and starts it again in the specified host
194
+ def migrate(host_id)
195
+ return Error.new('ID not defined') if !@pe_id
196
+
197
+ rc = @client.call(VM_METHODS[:migrate], @pe_id, host_id.to_i, false)
198
+ rc = nil if !OpenNebula.is_error?(rc)
199
+
200
+ return rc
201
+ end
202
+
203
+ # Migrates a running VM to another host without downtime
204
+ def live_migrate(host_id)
205
+ return Error.new('ID not defined') if !@pe_id
206
+
207
+ rc = @client.call(VM_METHODS[:migrate], @pe_id, host_id.to_i, true)
208
+ rc = nil if !OpenNebula.is_error?(rc)
209
+
210
+ return rc
211
+ end
212
+
213
+ # Set the specified vm's disk to be saved in a new image
214
+ # when the VirtualMachine shutdowns
215
+ #
216
+ # @param disk_id [Integer] ID of the disk to be saved
217
+ # @param image_name [String] Name for the new image where the
218
+ # disk will be saved
219
+ #
220
+ # @return [Integer, OpenNebula::Error] the new Image ID in case of
221
+ # success, error otherwise
222
+ def save_as(disk_id, image_name)
223
+ return Error.new('ID not defined') if !@pe_id
224
+
225
+ rc = @client.call(VM_METHODS[:savedisk], @pe_id, disk_id, image_name)
226
+
227
+ return rc
228
+ end
229
+
230
+ # Changes the owner/group
231
+ # uid:: _Integer_ the new owner id. Set to -1 to leave the current one
232
+ # gid:: _Integer_ the new group id. Set to -1 to leave the current one
233
+ # [return] nil in case of success or an Error object
234
+ def chown(uid, gid)
235
+ super(VM_METHODS[:chown], uid, gid)
236
+ end
237
+
238
+ #######################################################################
239
+ # Helpers to get VirtualMachine information
240
+ #######################################################################
241
+
242
+ # Returns the VM state of the VirtualMachine (numeric value)
243
+ def state
244
+ self['STATE'].to_i
245
+ end
246
+
247
+ # Returns the VM state of the VirtualMachine (string value)
248
+ def state_str
249
+ VM_STATE[state]
250
+ end
251
+
252
+ # Returns the LCM state of the VirtualMachine (numeric value)
253
+ def lcm_state
254
+ self['LCM_STATE'].to_i
255
+ end
256
+
257
+ # Returns the LCM state of the VirtualMachine (string value)
258
+ def lcm_state_str
259
+ LCM_STATE[lcm_state]
260
+ end
261
+
262
+ # Returns the short status string for the VirtualMachine
263
+ def status
264
+ short_state_str=SHORT_VM_STATES[state_str]
265
+
266
+ if short_state_str=="actv"
267
+ short_state_str=SHORT_LCM_STATES[lcm_state_str]
268
+ end
269
+
270
+ short_state_str
271
+ end
272
+
273
+ # Returns the group identifier
274
+ # [return] _Integer_ the element's group ID
275
+ def gid
276
+ self['GID'].to_i
277
+ end
278
+
279
+ private
280
+ def action(name)
281
+ return Error.new('ID not defined') if !@pe_id
282
+
283
+ rc = @client.call(VM_METHODS[:action], name, @pe_id)
284
+ rc = nil if !OpenNebula.is_error?(rc)
285
+
286
+ return rc
287
+ end
288
+ end
289
+ end