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,279 @@
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
+ require 'fileutils'
20
+
21
+ module OpenNebula
22
+ class Image < PoolElement
23
+ #######################################################################
24
+ # Constants and Class Methods
25
+ #######################################################################
26
+
27
+
28
+ IMAGE_METHODS = {
29
+ :info => "image.info",
30
+ :allocate => "image.allocate",
31
+ :update => "image.update",
32
+ :enable => "image.enable",
33
+ :persistent => "image.persistent",
34
+ :delete => "image.delete",
35
+ :chown => "image.chown",
36
+ :chmod => "image.chmod",
37
+ :chtype => "image.chtype",
38
+ :clone => "image.clone"
39
+ }
40
+
41
+ IMAGE_STATES=%w{INIT READY USED DISABLED LOCKED ERROR CLONE DELETE USED_PERS}
42
+
43
+ SHORT_IMAGE_STATES={
44
+ "INIT" => "init",
45
+ "READY" => "rdy",
46
+ "USED" => "used",
47
+ "DISABLED" => "disa",
48
+ "LOCKED" => "lock",
49
+ "ERROR" => "err",
50
+ "CLONE" => "clon",
51
+ "DELETE" => "dele",
52
+ "USED_PERS" => "used"
53
+ }
54
+
55
+ IMAGE_TYPES=%w{OS CDROM DATABLOCK}
56
+
57
+ SHORT_IMAGE_TYPES={
58
+ "OS" => "OS",
59
+ "CDROM" => "CD",
60
+ "DATABLOCK" => "DB"
61
+ }
62
+
63
+ # Creates an Image description with just its identifier
64
+ # this method should be used to create plain Image objects.
65
+ # +id+ the id of the image
66
+ #
67
+ # Example:
68
+ # image = Image.new(Image.build_xml(3),rpc_client)
69
+ #
70
+ def Image.build_xml(pe_id=nil)
71
+ if pe_id
72
+ image_xml = "<IMAGE><ID>#{pe_id}</ID></IMAGE>"
73
+ else
74
+ image_xml = "<IMAGE></IMAGE>"
75
+ end
76
+
77
+ XMLElement.build_xml(image_xml,'IMAGE')
78
+ end
79
+
80
+ # Class constructor
81
+ def initialize(xml, client)
82
+ super(xml,client)
83
+
84
+ @client = client
85
+ end
86
+
87
+ #######################################################################
88
+ # XML-RPC Methods for the Image Object
89
+ #######################################################################
90
+
91
+ # Retrieves the information of the given Image.
92
+ def info()
93
+ super(IMAGE_METHODS[:info], 'IMAGE')
94
+ end
95
+
96
+ # Allocates a new Image in OpenNebula
97
+ #
98
+ # @param description [String] A string containing the template of the Image.
99
+ # @param ds_id [Integer] the target datastore ID
100
+ #
101
+ # @return [nil, OpenNebula::Error] nil in case of success, Error
102
+ # otherwise
103
+ def allocate(description, ds_id)
104
+ super(IMAGE_METHODS[:allocate],description, ds_id)
105
+ end
106
+
107
+ # Replaces the template contents
108
+ #
109
+ # +new_template+ New template contents. If no argument is provided
110
+ # the object will be updated using the @xml variable
111
+ def update(new_template=nil)
112
+ super(IMAGE_METHODS[:update], new_template)
113
+ end
114
+
115
+ # Enables an Image
116
+ def enable
117
+ set_enabled(true)
118
+ end
119
+
120
+ # Disables an Image
121
+ def disable
122
+ set_enabled(false)
123
+ end
124
+
125
+ # Publishes the Image, to be used by other users
126
+ def publish
127
+ set_publish(true)
128
+ end
129
+
130
+ # Unplubishes the Image
131
+ def unpublish
132
+ set_publish(false)
133
+ end
134
+
135
+ # Makes the Image persistent
136
+ def persistent
137
+ set_persistent(true)
138
+ end
139
+
140
+ # Makes the Image non persistent
141
+ def nonpersistent
142
+ set_persistent(false)
143
+ end
144
+
145
+ # Deletes the Image
146
+ def delete()
147
+ super(IMAGE_METHODS[:delete])
148
+ end
149
+
150
+ # Changes the owner/group
151
+ # uid:: _Integer_ the new owner id. Set to -1 to leave the current one
152
+ # gid:: _Integer_ the new group id. Set to -1 to leave the current one
153
+ # [return] nil in case of success or an Error object
154
+ def chown(uid, gid)
155
+ super(IMAGE_METHODS[:chown], uid, gid)
156
+ end
157
+
158
+ # Changes the Image permissions.
159
+ #
160
+ # @param octet [String] Permissions octed , e.g. 640
161
+ # @return [nil, OpenNebula::Error] nil in case of success, Error
162
+ # otherwise
163
+ def chmod_octet(octet)
164
+ super(IMAGE_METHODS[:chmod], octet)
165
+ end
166
+
167
+ # Changes the Image permissions.
168
+ # Each [Integer] argument must be 1 to allow, 0 deny, -1 do not change
169
+ #
170
+ # @return [nil, OpenNebula::Error] nil in case of success, Error
171
+ # otherwise
172
+ def chmod(owner_u, owner_m, owner_a, group_u, group_m, group_a, other_u,
173
+ other_m, other_a)
174
+ super(IMAGE_METHODS[:chmod], owner_u, owner_m, owner_a, group_u,
175
+ group_m, group_a, other_u, other_m, other_a)
176
+ end
177
+
178
+ # Changes the Image type
179
+ # @param type [String] new Image type
180
+ # @return [nil, OpenNebula::Error] nil in case of success, Error
181
+ # otherwise
182
+ def chtype(type)
183
+ return Error.new('ID not defined') if !@pe_id
184
+
185
+ rc = @client.call(IMAGE_METHODS[:chtype], @pe_id, type)
186
+ rc = nil if !OpenNebula.is_error?(rc)
187
+
188
+ return rc
189
+ end
190
+
191
+ # Clones this Image into a new one
192
+ #
193
+ # @param [String] name for the new Image.
194
+ #
195
+ # @return [Integer, OpenNebula::Error] The new Image ID in case
196
+ # of success, Error otherwise
197
+ def clone(name)
198
+ return Error.new('ID not defined') if !@pe_id
199
+
200
+ rc = @client.call(IMAGE_METHODS[:clone], @pe_id, name)
201
+
202
+ return rc
203
+ end
204
+
205
+ #######################################################################
206
+ # Helpers to get Image information
207
+ #######################################################################
208
+
209
+ # Returns the state of the Image (numeric value)
210
+ def state
211
+ self['STATE'].to_i
212
+ end
213
+
214
+ # Returns the state of the Image (string value)
215
+ def state_str
216
+ IMAGE_STATES[state]
217
+ end
218
+
219
+ # Returns the state of the Image (string value)
220
+ def short_state_str
221
+ SHORT_IMAGE_STATES[state_str]
222
+ end
223
+
224
+ # Returns the type of the Image (numeric value)
225
+ def type
226
+ self['TYPE'].to_i
227
+ end
228
+
229
+ # Returns the type of the Image (string value)
230
+ def type_str
231
+ IMAGE_TYPES[type]
232
+ end
233
+
234
+ # Returns the state of the Image (string value)
235
+ def short_type_str
236
+ SHORT_IMAGE_TYPES[type_str]
237
+ end
238
+
239
+ # Returns the group identifier
240
+ # [return] _Integer_ the element's group ID
241
+ def gid
242
+ self['GID'].to_i
243
+ end
244
+
245
+ def public?
246
+ if self['PERMISSIONS/GROUP_U'] == "1" || self['PERMISSIONS/OTHER_U'] == "1"
247
+ true
248
+ else
249
+ false
250
+ end
251
+ end
252
+
253
+ private
254
+
255
+ def set_enabled(enabled)
256
+ return Error.new('ID not defined') if !@pe_id
257
+
258
+ rc = @client.call(IMAGE_METHODS[:enable], @pe_id, enabled)
259
+ rc = nil if !OpenNebula.is_error?(rc)
260
+
261
+ return rc
262
+ end
263
+
264
+ def set_publish(published)
265
+ group_u = published ? 1 : 0
266
+
267
+ chmod(-1, -1, -1, group_u, -1, -1, -1, -1, -1)
268
+ end
269
+
270
+ def set_persistent(persistence)
271
+ return Error.new('ID not defined') if !@pe_id
272
+
273
+ rc = @client.call(IMAGE_METHODS[:persistent], @pe_id, persistence)
274
+ rc = nil if !OpenNebula.is_error?(rc)
275
+
276
+ return rc
277
+ end
278
+ end
279
+ 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 ImagePool < Pool
22
+ #######################################################################
23
+ # Constants and Class attribute accessors
24
+ #######################################################################
25
+
26
+
27
+ IMAGE_POOL_METHODS = {
28
+ :info => "imagepool.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 Images from that user
37
+ def initialize(client, user_id=-1)
38
+ super('IMAGE_POOL','IMAGE',client)
39
+
40
+ @user_id = user_id
41
+ end
42
+
43
+ # Default Factory Method for the Pools
44
+ def factory(element_xml)
45
+ OpenNebula::Image.new(element_xml,@client)
46
+ end
47
+
48
+ #######################################################################
49
+ # XML-RPC Methods for the Image 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(IMAGE_POOL_METHODS[:info],@user_id,-1,-1)
57
+ when 3
58
+ info_filter(IMAGE_POOL_METHODS[:info],args[0],args[1],args[2])
59
+ end
60
+ end
61
+
62
+ def info_all()
63
+ return super(IMAGE_POOL_METHODS[:info])
64
+ end
65
+
66
+ def info_mine()
67
+ return super(IMAGE_POOL_METHODS[:info])
68
+ end
69
+
70
+ def info_group()
71
+ return super(IMAGE_POOL_METHODS[:info])
72
+ end
73
+ end
74
+ end