Revision 6509e422 src/sunstone/models/OpenNebulaJSON/VirtualMachineJSON.rb

View differences:

src/sunstone/models/OpenNebulaJSON/VirtualMachineJSON.rb
17 17
require 'OpenNebulaJSON/JSONUtils'
18 18

  
19 19
module OpenNebulaJSON
20

  
20 21
    class VirtualMachineJSON < OpenNebula::VirtualMachine
21 22
        include JSONUtils
22 23

  
......
56 57
                when "saveas"       then self.save_as(action_hash['params'])
57 58
                when "shutdown"     then self.shutdown
58 59
                when "resubmit"     then self.resubmit
59
                when "startvnc"     then self.startvnc
60
                when "stopvnc"      then self.stopvnc
61 60
                else
62 61
                    error_msg = "#{action_hash['perform']} action not " <<
63 62
                                " available for this resource"
......
120 119

  
121 120
            super(params['disk_id'].to_i, image.id)
122 121
        end
123
        
124
        def startvnc(port)
125
            result = self.info();
126
            if OpenNebula.is_error?(result)
127
                return result
128
            end
129
            
130
            if self['LCM_STATE'] != "3"
131
                return OpenNebula::Error.new("VM is not running");
132
            end
133
            
134
            if self['TEMPLATE/GRAPHICS/TYPE'] != "vnc"
135
                return OpenNebula::Error.new("VM has no VNC configured");
136
            end
137
                      
138
            if self['TEMPLATE/GRAPHICS/PORT']
139
                vnc_port = self['TEMPLATE/GRAPHICS/PORT']
140
            else
141
                return OpenNebula::Error.new("VM has no VNC port set");
142
            end
143
            
144
            if self['TEMPLATE/GRAPHICS/PASSWD']
145
                vnc_pw = self['TEMPLATE/GRAPHICS/PASSWD']
146
            else
147
                vnc_pw = ""
148
            end
149
            
150
            host = self['HISTORY/HOSTNAME']
151
            
152
            #we are ready for the party
153
            
154
            final_port = 29876+port.to_i;
155
            
156
            # puts "Launch noVNC on #{final_port} listenting to #{host}:#{vnc_port}"
157
            # So here we launch the noVNC server listening on the final_port 
158
            # and serving as proxy for the VM host on the configured VNC port.
159
            # TODO - This path is in public...            
160
            pipe = IO.popen("#{File.dirname(__FILE__)}/../../public/vendor/noVNC/utils/launch.sh --listen #{final_port} --vnc #{host}:#{vnc_port}")
161
            
162
            return {:pipe => pipe, :port => final_port, :password => vnc_pw}
163
            
164
        end
165
        
166
        def stopvnc(pipe)
167
            #am I allowed to do something affecting this machine?
168
            result = self.info();
169
            if OpenNebula.is_error?(result)
170
                return result
171
            end
172
            pid = pipe.pid
173
            # puts "Killing noVNC with pid #{pid}"
174
            Process.kill('KILL',pid)
175
            pipe.close
176
            return
177
        end
178
        
179 122
    end
180 123
end

Also available in: Unified diff