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.



59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
# File 'lib/fog/opennebula/requests/compute/OpenNebulaVNC.rb', line 59

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.



57
58
59
# File 'lib/fog/opennebula/requests/compute/OpenNebulaVNC.rb', line 57

def proxy_port
  @proxy_port
end

Instance Method Details

#delete_token(filename) ⇒ Object

Delete proxy token file



182
183
184
185
186
187
# File 'lib/fog/opennebula/requests/compute/OpenNebulaVNC.rb', line 182

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

#proxy(vm_resource) ⇒ Object



135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
# File 'lib/fog/opennebula/requests/compute/OpenNebulaVNC.rb', line 135

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

  unless 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'].casecmp('vnc').zero?
    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']
  }

  [200, info]
end

#startObject



85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
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
# File 'lib/fog/opennebula/requests/compute/OpenNebulaVNC.rb', line 85

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.empty?
    proxy_options << ' --ssl-only' if @wss == 'only'
  end

  proxy_options << ' -6' if @proxy_ipv6

  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

  unless 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



223
224
225
226
227
228
229
230
231
# File 'lib/fog/opennebula/requests/compute/OpenNebulaVNC.rb', line 223

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



189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
# File 'lib/fog/opennebula/requests/compute/OpenNebulaVNC.rb', line 189

def stop(force = false)
  pid = get_pid

  if pid
    @logger.info 'Killing VNC proxy'

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

    sleep 1

    begin
      Process.getpgid(pid)

      Process.kill('KILL', pid)
    rescue StandardError
    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