Revision 6509e422 src/sunstone/models/SunstoneServer.rb

View differences:

src/sunstone/models/SunstoneServer.rb
33 33
include OpenNebulaJSON
34 34

  
35 35
class SunstoneServer
36
    VNC_PROXY_BASE_PORT = 29876
37

  
36 38
    def initialize(username, password)
37 39
        # TBD one_client_user(name) from CloudServer
38 40
        @client = Client.new("dummy:dummy")
......
219 221
    ########################################################################
220 222

  
221 223
    def startvnc(id)
222
        rc = retrieve_resource("vm", id)
223
        if OpenNebula.is_error?(rc)
224
            return rc
224
        resource = retrieve_resource("vm", id)
225
        if OpenNebula.is_error?(resource)
226
            return [404, resource.to_json]
227
        end
228

  
229
        if resource['LCM_STATE'] != "3"
230
            error = OpenNebula::Error.new("VM is not running")
231
            return [403, error.to_json]
232
        end
233

  
234
        if resource['TEMPLATE/GRAPHICS/TYPE'] != "vnc"
235
            error = OpenNebula::Error.new("VM has no VNC configured")
236
            return [403, error.to_json]
237
        end
238

  
239
        vnc_port = resource['TEMPLATE/GRAPHICS/PORT']
240
        host = resource['HISTORY/HOSTNAME']
241

  
242
        proxy_port = VNC_PROXY_BASE_PORT + vnc_port.to_i
243

  
244
        # puts "Launch noVNC on #{final_port} listenting to #{host}:#{vnc_port}"
245
        # So here we launch the noVNC server listening on the final_port
246
        # and serving as proxy for the VM host on the configured VNC port.
247
        # TODO - This path is in public...
248
        begin
249
            pipe = IO.popen("#{File.dirname(__FILE__)}/../public/vendor/noVNC/utils/launch.sh --listen #{proxy_port} --vnc #{host}:#{vnc_port}")
250
        rescue Exception => e
251
            error = Error.new(e.message)
252
            return [500, error.to_json]
225 253
        end
226
        return rc.startvnc(id)
254

  
255
        vnc_pw = resource['TEMPLATE/GRAPHICS/PASSWD']
256

  
257
        info = {:pipe => pipe, :port => proxy_port, :password => vnc_pw}
258
        return [200, info]
227 259
    end
228
    
260

  
229 261
    def stopvnc(id,pipe)
230
       rc = retrieve_resource("vm", id)
231
       if OpenNebula.is_error?(rc)
232
           return rc
233
       end
234
       return rc.stopvnc(pipe)
235
       
262
        resource = retrieve_resource("vm", id)
263
        if OpenNebula.is_error?(resource)
264
            return [404, resource.to_json]
265
        end
266

  
267
        # puts "Killing noVNC with pid #{pid}"
268
        begin
269
            Process.kill('KILL',pipe.pid)
270
            pipe.close
271
        rescue Exception => e
272
            error = Error.new(e.message)
273
            return [500, error.to_json]
274
        end
275

  
276
        return [200, nil]
236 277
    end
237 278

  
238 279

  

Also available in: Unified diff