Class: OpenNebulaVNC

Inherits:
Object
  • Object
show all
Defined in:
lib/fog/opennebula/requests/compute/OpenNebulaVNC.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(config, logger, options = {}) ⇒ OpenNebulaVNC

Returns a new instance of OpenNebulaVNC.



80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
# File 'lib/fog/opennebula/requests/compute/OpenNebulaVNC.rb', line 80

def initialize(config, logger, options = {})
    opts={ :json_errors => true,
           :token_folder_name => 'sunstone_vnc_tokens'}.merge(options)

    @pipe = nil
    @token_folder = File.join(VAR_LOCATION, opts[:token_folder_name])
    @proxy_path   = File.join(SHARE_LOCATION, "websockify/websocketproxy.py")
    @proxy_port   = config[:vnc_proxy_port]

    @proxy_ipv6   = config[:vnc_proxy_ipv6]

    @wss = config[:vnc_proxy_support_wss]

    @lock_file = NOVNC_LOCK_FILE

    if (@wss == "yes") || (@wss == "only") || (@wss == true)
        @enable_wss = true
        @cert       = config[:vnc_proxy_cert]
        @key        = config[:vnc_proxy_key]
    else
        @enable_wss = false
    end
    @options = opts
    @logger = logger
end

Instance Attribute Details

#proxy_portObject (readonly)

Returns the value of attribute proxy_port.



78
79
80
# File 'lib/fog/opennebula/requests/compute/OpenNebulaVNC.rb', line 78

def proxy_port
  @proxy_port
end

Instance Method Details

#delete_token(filename) ⇒ Object

Delete proxy token file



205
206
207
208
209
210
211
212
# File 'lib/fog/opennebula/requests/compute/OpenNebulaVNC.rb', line 205

def delete_token(filename)
    begin
        File.delete(File.join(@token_folder, filename))
    rescue => e
        @logger.error "Error deleting token file for VM #{vm_id}"
        @logger.error e.message
    end
end

#proxy(vm_resource) ⇒ Object



158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
# File 'lib/fog/opennebula/requests/compute/OpenNebulaVNC.rb', line 158

def proxy(vm_resource)
    # Check configurations and VM attributes
    #if !is_running?
    #    return error(400, "VNC Proxy is not running")
    #end

    if !VNC_STATES.include?(vm_resource['LCM_STATE'])
        return error(400,"Wrong state (#{vm_resource['LCM_STATE']}) to open a VNC session")
    end

    if vm_resource['TEMPLATE/GRAPHICS/TYPE'].nil? ||
       vm_resource['TEMPLATE/GRAPHICS/TYPE'].downcase != "vnc"
        return error(400,"VM has no VNC configured")
    end

    # Proxy data
    host     = vm_resource['HISTORY_RECORDS/HISTORY[last()]/HOSTNAME']
    vnc_port = vm_resource['TEMPLATE/GRAPHICS/PORT']
    vnc_pw = vm_resource['TEMPLATE/GRAPHICS/PASSWD']

    # Generate token random_str: host:port
    random_str = rand(36**20).to_s(36) #random string a-z0-9 length 20
    token = "#{random_str}: #{host}:#{vnc_port}"
    token_file = 'one-'+vm_resource['ID']

    # Create token file
    
	begin
        f = File.open(File.join(@token_folder, token_file), 'w')
        f.write(token)
        f.close
    rescue Exception => e
#            @logger.error e.message
        return error(500, "Cannot create VNC proxy token")
    end

    info   = {
 :proxy_port => "29876",
        :password => vnc_pw,
        :token => random_str,
        :vm_name => vm_resource['NAME']
    }

    return [200, info]
end

#startObject



106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
# File 'lib/fog/opennebula/requests/compute/OpenNebulaVNC.rb', line 106

def start
    if is_running?
        message="VNC server already running"
        STDERR.puts message
        @logger.info message
        return false
    end

    create_token_dir

    proxy_options = "--target-config=#{@token_folder} "

    if @enable_wss
        proxy_options << " --cert #{@cert}"
        proxy_options << " --key #{@key}" if @key && @key.size > 0
        proxy_options << " --ssl-only" if @wss == "only"
    end

    if @proxy_ipv6
        proxy_options << " -6"
    end

    cmd ="python #{@proxy_path} #{proxy_options} #{@proxy_port}"

    begin
        @logger.info { "Starting VNC proxy: #{cmd}" }
        pid=start_daemon(cmd, VNC_LOG)
    rescue Exception => e
        @logger.error e.message
        return false
    end

    File.open(@lock_file, "w") do |f|
        f.write(pid.to_s)
    end

    sleep 1

    if !is_running?
        message="Error starting VNC proxy"
        STDERR.puts message
        @logger.error message
        File.delete(@lock_file) if File.exist?(@lock_file)

        return false
    end

    STDOUT.puts "VNC proxy started"

    true
end

#statusObject



248
249
250
251
252
253
254
255
256
# File 'lib/fog/opennebula/requests/compute/OpenNebulaVNC.rb', line 248

def status
    if is_running?
        STDOUT.puts "VNC is running"
        true
    else
        STDOUT.puts "VNC is NOT running"
        false
    end
end

#stop(force = false) ⇒ Object



214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
# File 'lib/fog/opennebula/requests/compute/OpenNebulaVNC.rb', line 214

def stop(force=false)
    pid=get_pid

    if pid
        @logger.info "Killing VNC proxy"

        signal=(force ? 'KILL' : 'TERM')
        Process.kill(signal ,pid)

        sleep 1

        begin
            Process.getpgid(pid)

            Process.kill('KILL', pid)
        rescue
        end

        if is_running?
            message="VNC server is still running"
            STDERR.puts message
            logger.error message
            return false
        end

        delete_token_dir
    else
        message="VNC server is not running"
        @logger.info message
        STDERR.puts message
    end
    true
end