Statistics
| Branch: | Tag: | Revision:

one / src / oca / ruby / OpenNebula / Image.rb @ 6329d8b3

History | View | Annotate | Download (6.76 KB)

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
            :rmattr      => "image.rmattr",
30
            :enable      => "image.enable",
31
            :publish     => "image.publish",
32
            :persistent  => "image.persistent",
33
            :delete      => "image.delete"
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
        # Modifies an image attribute
96
        #
97
        # +name+ Name of the attribute to be changed
98
        #
99
        # +value+ New value for the attribute
100
        def update(name, value)
101
            super(IMAGE_METHODS[:update], name, value)
102
        end
103

    
104
        # Deletes an Image attribute
105
        #
106
        # +name+ Name of the attribute to be deleted
107
        def remove_attr(name)
108
            do_rm_attr(name)
109
        end
110

    
111
        # Enables an Image
112
        def enable
113
            set_enabled(true)
114
        end
115

    
116
        # Disables an Image
117
        def disable
118
            set_enabled(false)
119
        end
120

    
121
        # Publishes the Image, to be used by other users
122
        def publish
123
            set_publish(true)
124
        end
125

    
126
        # Unplubishes the Image
127
        def unpublish
128
            set_publish(false)
129
        end
130
        
131
        # Makes the Image persistent
132
        def persistent
133
            set_persistent(true)
134
        end
135

    
136
        # Makes the Image non persistent
137
        def nonpersistent
138
            set_persistent(false)
139
        end
140

    
141
        # Deletes the Image
142
        def delete()
143
            super(IMAGE_METHODS[:delete])
144
        end
145
    
146

    
147
        #######################################################################
148
        # Helpers to get Image information
149
        #######################################################################
150

    
151
        # Returns the state of the Image (numeric value)
152
        def state
153
            self['STATE'].to_i
154
        end
155

    
156
        # Returns the state of the Image (string value)
157
        def state_str
158
            IMAGE_STATES[state]
159
        end
160

    
161
        # Returns the state of the Image (string value)
162
        def short_state_str
163
            SHORT_IMAGE_STATES[state_str]
164
        end
165

    
166
        # Returns the type of the Image (numeric value)
167
        def type
168
            self['TYPE'].to_i
169
        end
170

    
171
        # Returns the type of the Image (string value)
172
        def type_str
173
            IMAGE_TYPES[type]
174
        end
175

    
176
        # Returns the state of the Image (string value)
177
        def short_type_str
178
            SHORT_IMAGE_TYPES[type_str]
179
        end
180

    
181
        # Returns the group identifier
182
        # [return] _Integer_ the element's group ID
183
        def gid
184
            self['GID'].to_i
185
        end
186

    
187
    private
188

    
189
        def set_enabled(enabled)
190
            return Error.new('ID not defined') if !@pe_id
191

    
192
            rc = @client.call(IMAGE_METHODS[:enable], @pe_id, enabled)
193
            rc = nil if !OpenNebula.is_error?(rc)
194

    
195
            return rc
196
        end
197

    
198
        def set_publish(published)
199
            return Error.new('ID not defined') if !@pe_id
200

    
201
            rc = @client.call(IMAGE_METHODS[:publish], @pe_id, published)
202
            rc = nil if !OpenNebula.is_error?(rc)
203

    
204
            return rc
205
        end
206
        
207
        def set_persistent(persistence)
208
            return Error.new('ID not defined') if !@pe_id
209

    
210
            rc = @client.call(IMAGE_METHODS[:persistent], @pe_id, persistence)
211
            rc = nil if !OpenNebula.is_error?(rc)
212

    
213
            return rc
214
        end
215

    
216
        def do_rm_attr(name)
217
            return Error.new('ID not defined') if !@pe_id
218

    
219
            rc = @client.call(IMAGE_METHODS[:rmattr], @pe_id, name)
220
            rc = nil if !OpenNebula.is_error?(rc)
221

    
222
            return rc
223
        end
224

    
225
    end
226
end