oca 3.0.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -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 HostPool < Pool
21
+ #######################################################################
22
+ # Constants and Class attribute accessors
23
+ #######################################################################
24
+
25
+ HOST_POOL_METHODS = {
26
+ :info => "hostpool.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('HOST_POOL','HOST',client)
36
+ end
37
+
38
+ # Factory Method for the Host Pool
39
+ def factory(element_xml)
40
+ OpenNebula::Host.new(element_xml,@client)
41
+ end
42
+
43
+ #######################################################################
44
+ # XML-RPC Methods for the Host Pool
45
+ #######################################################################
46
+
47
+ # Retrieves all the Hosts in the pool.
48
+ def info()
49
+ super(HOST_POOL_METHODS[:info])
50
+ end
51
+ end
52
+ end
@@ -0,0 +1,214 @@
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
+ require 'fileutils'
19
+
20
+ module OpenNebula
21
+ class Image < PoolElement
22
+ # ---------------------------------------------------------------------
23
+ # Constants and Class Methods
24
+ # ---------------------------------------------------------------------
25
+ IMAGE_METHODS = {
26
+ :info => "image.info",
27
+ :allocate => "image.allocate",
28
+ :update => "image.update",
29
+ :enable => "image.enable",
30
+ :publish => "image.publish",
31
+ :persistent => "image.persistent",
32
+ :delete => "image.delete",
33
+ :chown => "image.chown"
34
+ }
35
+
36
+ IMAGE_STATES=%w{INIT READY USED DISABLED LOCKED ERROR}
37
+
38
+ SHORT_IMAGE_STATES={
39
+ "INIT" => "init",
40
+ "READY" => "rdy",
41
+ "USED" => "used",
42
+ "DISABLED" => "disa",
43
+ "LOCKED" => "lock",
44
+ "ERROR" => "err"
45
+ }
46
+
47
+ IMAGE_TYPES=%w{OS CDROM DATABLOCK}
48
+
49
+ SHORT_IMAGE_TYPES={
50
+ "OS" => "OS",
51
+ "CDROM" => "CD",
52
+ "DATABLOCK" => "DB"
53
+ }
54
+
55
+ # Creates an Image description with just its identifier
56
+ # this method should be used to create plain Image objects.
57
+ # +id+ the id of the image
58
+ #
59
+ # Example:
60
+ # image = Image.new(Image.build_xml(3),rpc_client)
61
+ #
62
+ def Image.build_xml(pe_id=nil)
63
+ if pe_id
64
+ image_xml = "<IMAGE><ID>#{pe_id}</ID></IMAGE>"
65
+ else
66
+ image_xml = "<IMAGE></IMAGE>"
67
+ end
68
+
69
+ XMLElement.build_xml(image_xml,'IMAGE')
70
+ end
71
+
72
+ # Class constructor
73
+ def initialize(xml, client)
74
+ super(xml,client)
75
+
76
+ @client = client
77
+ end
78
+
79
+ #######################################################################
80
+ # XML-RPC Methods for the Image Object
81
+ #######################################################################
82
+
83
+ # Retrieves the information of the given Image.
84
+ def info()
85
+ super(IMAGE_METHODS[:info], 'IMAGE')
86
+ end
87
+
88
+ # Allocates a new Image in OpenNebula
89
+ #
90
+ # +description+ A string containing the template of the Image.
91
+ def allocate(description)
92
+ super(IMAGE_METHODS[:allocate],description)
93
+ end
94
+
95
+ # Replaces the template contents
96
+ #
97
+ # +new_template+ New template contents
98
+ def update(new_template)
99
+ super(IMAGE_METHODS[:update], new_template)
100
+ end
101
+
102
+ # Enables an Image
103
+ def enable
104
+ set_enabled(true)
105
+ end
106
+
107
+ # Disables an Image
108
+ def disable
109
+ set_enabled(false)
110
+ end
111
+
112
+ # Publishes the Image, to be used by other users
113
+ def publish
114
+ set_publish(true)
115
+ end
116
+
117
+ # Unplubishes the Image
118
+ def unpublish
119
+ set_publish(false)
120
+ end
121
+
122
+ # Makes the Image persistent
123
+ def persistent
124
+ set_persistent(true)
125
+ end
126
+
127
+ # Makes the Image non persistent
128
+ def nonpersistent
129
+ set_persistent(false)
130
+ end
131
+
132
+ # Deletes the Image
133
+ def delete()
134
+ super(IMAGE_METHODS[:delete])
135
+ end
136
+
137
+ # Changes the owner/group
138
+ # uid:: _Integer_ the new owner id. Set to -1 to leave the current one
139
+ # gid:: _Integer_ the new group id. Set to -1 to leave the current one
140
+ # [return] nil in case of success or an Error object
141
+ def chown(uid, gid)
142
+ super(IMAGE_METHODS[:chown], uid, gid)
143
+ end
144
+
145
+ #######################################################################
146
+ # Helpers to get Image information
147
+ #######################################################################
148
+
149
+ # Returns the state of the Image (numeric value)
150
+ def state
151
+ self['STATE'].to_i
152
+ end
153
+
154
+ # Returns the state of the Image (string value)
155
+ def state_str
156
+ IMAGE_STATES[state]
157
+ end
158
+
159
+ # Returns the state of the Image (string value)
160
+ def short_state_str
161
+ SHORT_IMAGE_STATES[state_str]
162
+ end
163
+
164
+ # Returns the type of the Image (numeric value)
165
+ def type
166
+ self['TYPE'].to_i
167
+ end
168
+
169
+ # Returns the type of the Image (string value)
170
+ def type_str
171
+ IMAGE_TYPES[type]
172
+ end
173
+
174
+ # Returns the state of the Image (string value)
175
+ def short_type_str
176
+ SHORT_IMAGE_TYPES[type_str]
177
+ end
178
+
179
+ # Returns the group identifier
180
+ # [return] _Integer_ the element's group ID
181
+ def gid
182
+ self['GID'].to_i
183
+ end
184
+
185
+ private
186
+
187
+ def set_enabled(enabled)
188
+ return Error.new('ID not defined') if !@pe_id
189
+
190
+ rc = @client.call(IMAGE_METHODS[:enable], @pe_id, enabled)
191
+ rc = nil if !OpenNebula.is_error?(rc)
192
+
193
+ return rc
194
+ end
195
+
196
+ def set_publish(published)
197
+ return Error.new('ID not defined') if !@pe_id
198
+
199
+ rc = @client.call(IMAGE_METHODS[:publish], @pe_id, published)
200
+ rc = nil if !OpenNebula.is_error?(rc)
201
+
202
+ return rc
203
+ end
204
+
205
+ def set_persistent(persistence)
206
+ return Error.new('ID not defined') if !@pe_id
207
+
208
+ rc = @client.call(IMAGE_METHODS[:persistent], @pe_id, persistence)
209
+ rc = nil if !OpenNebula.is_error?(rc)
210
+
211
+ return rc
212
+ end
213
+ end
214
+ end
@@ -0,0 +1,72 @@
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 ImagePool < Pool
21
+ #######################################################################
22
+ # Constants and Class attribute accessors
23
+ #######################################################################
24
+
25
+ IMAGE_POOL_METHODS = {
26
+ :info => "imagepool.info"
27
+ }
28
+
29
+ #######################################################################
30
+ # Class constructor & Pool Methods
31
+ #######################################################################
32
+
33
+ # +client+ a Client object that represents a XML-RPC connection
34
+ # +user_id+ is to refer to a Pool with Images from that user
35
+ def initialize(client, user_id=-1)
36
+ super('IMAGE_POOL','IMAGE',client)
37
+
38
+ @user_id = user_id
39
+ end
40
+
41
+ # Default Factory Method for the Pools
42
+ def factory(element_xml)
43
+ OpenNebula::Image.new(element_xml,@client)
44
+ end
45
+
46
+ #######################################################################
47
+ # XML-RPC Methods for the Image Object
48
+ #######################################################################
49
+
50
+ # Retrieves all or part of the VirtualMachines in the pool.
51
+ def info(*args)
52
+ case args.size
53
+ when 0
54
+ info_filter(IMAGE_POOL_METHODS[:info],@user_id,-1,-1)
55
+ when 3
56
+ info_filter(IMAGE_POOL_METHODS[:info],args[0],args[1],args[2])
57
+ end
58
+ end
59
+
60
+ def info_all()
61
+ return super(IMAGE_POOL_METHODS[:info])
62
+ end
63
+
64
+ def info_mine()
65
+ return super(IMAGE_POOL_METHODS[:info])
66
+ end
67
+
68
+ def info_group()
69
+ return super(IMAGE_POOL_METHODS[:info])
70
+ end
71
+ end
72
+ end
@@ -0,0 +1,245 @@
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
+ module OpenNebula
18
+ # The Pool class represents a generic OpenNebula Pool in XML format
19
+ # and provides the basic functionality to handle the Pool elements
20
+
21
+ class Pool < XMLPool
22
+ include Enumerable
23
+
24
+ protected
25
+ #pool:: _String_ XML name of the root element
26
+ #element:: _String_ XML name of the Pool elements
27
+ #client:: _Client_ represents a XML-RPC connection
28
+ def initialize(pool,element,client)
29
+ super(nil)
30
+
31
+ @pool_name = pool.upcase
32
+ @element_name = element.upcase
33
+
34
+ @client = client
35
+ @hash = nil
36
+ end
37
+
38
+ # Default Factory Method for the Pools. The factory method returns an
39
+ # suitable PoolElement object. Each Pool MUST implement the
40
+ # corresponding factory method
41
+ # element_xml:: _XML_ XML element describing the pool element
42
+ # [return] a PoolElement object
43
+ def factory(element_xml)
44
+ OpenNebula::PoolElement.new(element_xml,client)
45
+ end
46
+
47
+ #######################################################################
48
+ # Common XML-RPC Methods for all the Pool Types
49
+ #######################################################################
50
+
51
+ #Gets the pool without any filter. Host, Group and User Pools
52
+ # xml_method:: _String_ the name of the XML-RPC method
53
+ def info(xml_method)
54
+ return xmlrpc_info(xml_method)
55
+ end
56
+
57
+ def info_all(xml_method)
58
+ return xmlrpc_info(xml_method,INFO_ALL,-1,-1)
59
+ end
60
+
61
+ def info_mine(xml_method)
62
+ return xmlrpc_info(xml_method,INFO_MINE,-1,-1)
63
+ end
64
+
65
+ def info_group(xml_method)
66
+ return xmlrpc_info(xml_method,INFO_GROUP,-1,-1)
67
+ end
68
+
69
+ def info_filter(xml_method, who, start_id, end_id)
70
+ return xmlrpc_info(xml_method,who, start_id, end_id)
71
+ end
72
+
73
+ private
74
+ # Calls to the corresponding info method to retreive the pool
75
+ # representation in XML format
76
+ # xml_method:: _String_ the name of the XML-RPC method
77
+ # args:: _Array_ with additional arguments for the info call
78
+ # [return] nil in case of success or an Error object
79
+ def xmlrpc_info(xml_method,*args)
80
+ rc = @client.call(xml_method,*args)
81
+
82
+ if !OpenNebula.is_error?(rc)
83
+ initialize_xml(rc,@pool_name)
84
+ rc = nil
85
+ end
86
+
87
+ return rc
88
+ end
89
+
90
+ public
91
+ # Constants for info queries (include/RequestManagerPoolInfoFilter.h)
92
+ INFO_GROUP = -1
93
+ INFO_ALL = -2
94
+ INFO_MINE = -3
95
+
96
+ # Iterates over every PoolElement in the Pool and calls the block with a
97
+ # a PoolElement obtained calling the factory method
98
+ # block:: _Block_
99
+ def each(&block)
100
+ each_element(block) if @xml
101
+ end
102
+
103
+ # DO NOT USE - ONLY REXML BACKEND
104
+ def to_str
105
+ str = ""
106
+ REXML::Formatters::Pretty.new(1).write(@xml,str)
107
+
108
+ return str
109
+ end
110
+ end
111
+
112
+ # The PoolElement Class represents a generic element of a Pool in
113
+ # XML format
114
+ class PoolElement < XMLElement
115
+
116
+ protected
117
+ # node:: _XML_is a XML element that represents the Pool element
118
+ # client:: _Client_ represents a XML-RPC connection
119
+ def initialize(node, client)
120
+ @xml = node
121
+ @client = client
122
+ @hash = nil
123
+
124
+ if self['ID']
125
+ @pe_id = self['ID'].to_i
126
+ else
127
+ @pe_id = nil
128
+ end
129
+ @name = self['NAME'] if self['NAME']
130
+ end
131
+
132
+ #######################################################################
133
+ # Common XML-RPC Methods for all the Pool Element Types
134
+ #######################################################################
135
+
136
+ # Calls to the corresponding info method to retreive the element
137
+ # detailed information in XML format
138
+ # xml_method:: _String_ the name of the XML-RPC method
139
+ # root_element:: _String_ Base XML element
140
+ # [return] nil in case of success or an Error object
141
+ def info(xml_method, root_element)
142
+ return Error.new('ID not defined') if !@pe_id
143
+
144
+ rc = @client.call(xml_method,@pe_id)
145
+
146
+ if !OpenNebula.is_error?(rc)
147
+ initialize_xml(rc, root_element)
148
+ rc = nil
149
+
150
+ @pe_id = self['ID'].to_i if self['ID']
151
+ @name = self['NAME'] if self['NAME']
152
+ end
153
+
154
+ return rc
155
+ end
156
+
157
+ # Calls to the corresponding allocate method to create a new element
158
+ # in the OpenNebula core
159
+ # xml_method:: _String_ the name of the XML-RPC method
160
+ # args:: _Array_ additional arguments including the template for the
161
+ # new element
162
+ # [return] nil in case of success or an Error object
163
+ def allocate(xml_method, *args)
164
+ rc = @client.call(xml_method, *args)
165
+
166
+ if !OpenNebula.is_error?(rc)
167
+ @pe_id = rc
168
+ rc = nil
169
+ end
170
+
171
+ return rc
172
+ end
173
+
174
+ # Calls to the corresponding update method to modify
175
+ # the object's template
176
+ # xml_method:: _String_ the name of the XML-RPC method
177
+ # new_template:: _String_ the new template contents
178
+ # [return] nil in case of success or an Error object
179
+ def update(xml_method, new_template)
180
+ return Error.new('ID not defined') if !@pe_id
181
+
182
+ rc = @client.call(xml_method,@pe_id, new_template)
183
+ rc = nil if !OpenNebula.is_error?(rc)
184
+
185
+ return rc
186
+ end
187
+
188
+ # Calls to the corresponding delete method to remove this element
189
+ # from the OpenNebula core
190
+ # xml_method:: _String_ the name of the XML-RPC method
191
+ # [return] nil in case of success or an Error object
192
+ def delete(xml_method)
193
+ return Error.new('ID not defined') if !@pe_id
194
+
195
+ rc = @client.call(xml_method,@pe_id)
196
+ rc = nil if !OpenNebula.is_error?(rc)
197
+
198
+ return rc
199
+ end
200
+
201
+ # Calls to the corresponding chown method to modify
202
+ # the object's owner and group
203
+ # xml_method:: _String_ the name of the XML-RPC method
204
+ # uid:: _Integer_ the new owner id. Set to -1 to leave the current one
205
+ # gid:: _Integer_ the new group id. Set to -1 to leave the current one
206
+ # [return] nil in case of success or an Error object
207
+ def chown(xml_method, uid, gid)
208
+ return Error.new('ID not defined') if !@pe_id
209
+
210
+ rc = @client.call(xml_method,@pe_id, uid, gid)
211
+ rc = nil if !OpenNebula.is_error?(rc)
212
+
213
+ return rc
214
+ end
215
+
216
+ public
217
+
218
+ # Creates new element specifying its id
219
+ # id:: identifyier of the element
220
+ # client:: initialized OpenNebula::Client object
221
+ def self.new_with_id(id, client=nil)
222
+ self.new(self.build_xml(id), client)
223
+ end
224
+
225
+ # Returns element identifier
226
+ # [return] _Integer_ the PoolElement ID
227
+ def id
228
+ @pe_id
229
+ end
230
+
231
+ # Gets element name
232
+ # [return] _String_ the PoolElement name
233
+ def name
234
+ @name
235
+ end
236
+
237
+ # DO NOT USE - ONLY REXML BACKEND
238
+ def to_str
239
+ str = ""
240
+ REXML::Formatters::Pretty.new(1).write(@xml,str)
241
+
242
+ return str
243
+ end
244
+ end
245
+ end