Statistics
| Branch: | Tag: | Revision:

one / src / sunstone / models / SunstoneServer.rb @ df6b93e6

History | View | Annotate | Download (11.9 KB)

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
require 'CloudServer'
18

    
19
require 'OpenNebulaJSON'
20
include OpenNebulaJSON
21

    
22
require 'OpenNebulaVNC'
23
require 'OpenNebulaJSON/JSONUtils'
24
include JSONUtils
25

    
26
class SunstoneServer < CloudServer
27
    # FLAG that will filter the elements retrieved from the Pools
28
    POOL_FILTER = Pool::INFO_ALL
29

    
30
    # Secs to sleep between checks to see if image upload to repo is finished
31
    IMAGE_POLL_SLEEP_TIME = 5
32

    
33
    def initialize(client, config, logger)
34
        super(config, logger)
35
        @client = client
36
    end
37

    
38
    ############################################################################
39
    #
40
    ############################################################################
41
    def get_pool(kind,gid)
42
        if gid == "0"
43
            user_flag = Pool::INFO_ALL
44
        else
45
            user_flag = POOL_FILTER
46
        end
47

    
48
        pool = case kind
49
            when "group"      then GroupPoolJSON.new(@client)
50
            when "cluster"    then ClusterPoolJSON.new(@client)
51
            when "host"       then HostPoolJSON.new(@client)
52
            when "image"      then ImagePoolJSON.new(@client, user_flag)
53
            when "vmtemplate" then TemplatePoolJSON.new(@client, user_flag)
54
            when "vm"         then VirtualMachinePoolJSON.new(@client, user_flag)
55
            when "vnet"       then VirtualNetworkPoolJSON.new(@client, user_flag)
56
            when "user"       then UserPoolJSON.new(@client)
57
            when "acl"        then AclPoolJSON.new(@client)
58
            when "datastore"  then DatastorePoolJSON.new(@client)
59
            else
60
                error = Error.new("Error: #{kind} resource not supported")
61
                return [404, error.to_json]
62
        end
63

    
64
        rc = pool.info
65

    
66
        if OpenNebula.is_error?(rc)
67
            return [500, rc.to_json]
68
        else
69
            return [200, pool.to_json]
70
        end
71
    end
72

    
73
    ############################################################################
74
    #
75
    ############################################################################
76
    def get_resource(kind, id)
77
        resource = retrieve_resource(kind, id)
78
        if OpenNebula.is_error?(resource)
79
            return [404, resource.to_json]
80
        else
81
            return [200, resource.to_json]
82
        end
83
    end
84

    
85
    ############################################################################
86
    #
87
    ############################################################################
88
    def get_template(kind,id)
89
        resource = retrieve_resource(kind,id)
90
        if OpenNebula.is_error?(resource)
91
            return [404, resource.to_json]
92
        else
93
            template_str = resource.template_str(true)
94
            return [200, {:template => template_str}.to_json]
95
        end
96
    end
97

    
98
    ############################################################################
99
    #
100
    ############################################################################
101
    def create_resource(kind, template)
102
        resource = case kind
103
            when "group"      then GroupJSON.new(Group.build_xml, @client)
104
            when "cluster"    then ClusterJSON.new(Group.build_xml, @client)
105
            when "host"       then HostJSON.new(Host.build_xml, @client)
106
            when "image"      then ImageJSON.new(Image.build_xml, @client)
107
            when "vmtemplate" then TemplateJSON.new(Template.build_xml, @client)
108
            when "vm"         then VirtualMachineJSON.new(VirtualMachine.build_xml,@client)
109
            when "vnet"       then VirtualNetworkJSON.new(VirtualNetwork.build_xml, @client)
110
            when "user"       then UserJSON.new(User.build_xml, @client)
111
            when "acl"        then AclJSON.new(Acl.build_xml, @client)
112
            when "datastore"  then DatastoreJSON.new(Acl.build_xml, @client)
113
            else
114
                error = Error.new("Error: #{kind} resource not supported")
115
                return [404, error.to_json]
116
        end
117

    
118
        rc = resource.create(template)
119
        if OpenNebula.is_error?(rc)
120
            return [500, rc.to_json]
121
        else
122
            resource.info
123
            return [201, resource.to_json]
124
        end
125
    end
126

    
127
    ############################################################################
128
    #
129
    ############################################################################
130
    def upload(template, file_path)
131
        image_hash = parse_json(template, 'image')
132
        if OpenNebula.is_error?(image_hash)
133
            return [500, image_hash.to_json]
134
        end
135

    
136
        image_hash['PATH'] = file_path
137

    
138
        ds_id = parse_json(template, 'ds_id')
139
        if OpenNebula.is_error?(ds_id)
140
            return [500, ds_id.to_json]
141
        end
142
        
143
        new_template = {
144
            :image => image_hash,
145
            :ds_id => ds_id,
146
        }.to_json
147

    
148
        image = ImageJSON.new(Image.build_xml, @client)
149

    
150
        rc = image.create(new_template)
151

    
152
        if OpenNebula.is_error?(rc)
153
            return [500, rc.to_json]
154
        end
155

    
156
        image.info
157
        #wait until image is ready to return
158
        while (image.state_str == 'LOCKED') && (image['RUNNING_VMS'] == '0') do
159
            sleep IMAGE_POLL_SLEEP_TIME
160
            image.info
161
        end
162
        return [201, image.to_json]
163
    end
164

    
165
    ############################################################################
166
    #
167
    ############################################################################
168
    def delete_resource(kind, id)
169
        resource = retrieve_resource(kind, id)
170
        if OpenNebula.is_error?(resource)
171
            return [404, resource.to_json]
172
        end
173

    
174
        rc = resource.delete
175
        if OpenNebula.is_error?(rc)
176
            return [500, rc.to_json]
177
        else
178
            return [204, resource.to_json]
179
        end
180
    end
181

    
182
    ############################################################################
183
    #
184
    ############################################################################
185
    def perform_action(kind, id, action_json)
186
        resource = retrieve_resource(kind, id)
187
        if OpenNebula.is_error?(resource)
188
            return [404, resource.to_json]
189
        end
190

    
191
        rc = resource.perform_action(action_json)
192
        if OpenNebula.is_error?(rc)
193
            return [500, rc.to_json]
194
        else
195
            return [204, resource.to_json]
196
        end
197
    end
198

    
199
    ############################################################################
200
    # Unused
201
    ############################################################################
202
    def get_vm_log(id)
203
        resource = retrieve_resource("vm", id)
204
        if OpenNebula.is_error?(resource)
205
            return [404, nil]
206
        else
207
            if !ONE_LOCATION
208
                vm_log_file = LOG_LOCATION + "/#{id}.log"
209
            else
210
                vm_log_file = LOG_LOCATION + "/#{id}/vm.log"
211
            end
212

    
213
            begin
214
                log = File.read(vm_log_file)
215
            rescue Exception => e
216
                msg = "Log for VM #{id} not available"
217
                return [200, {:vm_log => msg}.to_json]
218
            end
219

    
220
            return [200, {:vm_log => log}.to_json]
221
        end
222
    end
223

    
224
    ########################################################################
225
    # VNC
226
    ########################################################################
227
    def startvnc(id, config)
228
        resource = retrieve_resource("vm", id)
229
        if OpenNebula.is_error?(resource)
230
            return [404, resource.to_json]
231
        end
232

    
233
        vnc_proxy = OpenNebulaVNC.new(config, logger)
234
        return vnc_proxy.start(resource)
235
    end
236

    
237
    ############################################################################
238
    #
239
    ############################################################################
240
    def stopvnc(pipe)
241
        begin
242
            OpenNebulaVNC.stop(pipe)
243
        rescue Exception => e
244
            logger.error {e.message}
245
            error = Error.new("Error stopping VNC. Please check server logs.")
246
            return [500, error.to_json]
247
        end
248

    
249
        return [200, nil]
250
    end
251

    
252
    ############################################################################
253
    #
254
    ############################################################################
255
    def get_pool_monitoring(resource, meters)
256
        #pool_element
257
        pool = case resource
258
            when "vm", "VM"
259
                VirtualMachinePool.new(@client)
260
            when "host", "HOST"
261
                HostPool.new(@client)
262
            else
263
                error = Error.new("Monitoring not supported for #{resource}")
264
                return [200, error.to_json]
265
            end
266

    
267
        meters_a = meters.split(',')
268

    
269
        rc = pool.monitoring(meters_a)
270

    
271
        if OpenNebula.is_error?(rc)
272
            error = Error.new(rc,message)
273
            return [500, error.to_json]
274
        end
275

    
276
        rc[:resource] = resource
277

    
278
        return [200, rc.to_json]
279
    end
280

    
281
    def get_resource_monitoring(id, resource, meters)
282
        pool_element = case resource
283
            when "vm", "VM"
284
                VirtualMachine.new_with_id(id, @client)
285
            when "host", "HOST"
286
                Host.new_with_id(id, @client)
287
            else
288
                error = Error.new("Monitoring not supported for #{resource}")
289
                return [200, error.to_json]
290
            end
291

    
292
        meters_a = meters.split(',')
293

    
294
        rc = pool_element.monitoring(meters_a)
295

    
296
        if OpenNebula.is_error?(rc)
297
            error = Error.new(rc,message)
298
            return [500, error.to_json]
299
        end
300

    
301
        meters_h = Hash.new
302
        meters_h[:resource]   = resource
303
        meters_h[:id]         = id
304
        meters_h[:monitoring] = rc
305

    
306
        return [200, meters_h.to_json]
307
    end
308

    
309
    private
310

    
311
    ############################################################################
312
    #
313
    ############################################################################
314
    def retrieve_resource(kind, id)
315
        resource = case kind
316
            when "group"      then GroupJSON.new_with_id(id, @client)
317
            when "cluster"    then ClusterJSON.new_with_id(id, @client)
318
            when "host"       then HostJSON.new_with_id(id, @client)
319
            when "image"      then ImageJSON.new_with_id(id, @client)
320
            when "vmtemplate" then TemplateJSON.new_with_id(id, @client)
321
            when "vm"         then VirtualMachineJSON.new_with_id(id, @client)
322
            when "vnet"       then VirtualNetworkJSON.new_with_id(id, @client)
323
            when "user"       then UserJSON.new_with_id(id, @client)
324
            when "acl"        then AclJSON.new_with_id(id, @client)
325
            when "datastore"  then DatastoreJSON.new_with_id(id, @client)
326
            else
327
                error = Error.new("Error: #{kind} resource not supported")
328
                return error
329
        end
330

    
331
        rc = resource.info
332
        if OpenNebula.is_error?(rc)
333
            return rc
334
        else
335
            return resource
336
        end
337
    end
338
end