1
0
mirror of https://github.com/OpenNebula/one.git synced 2025-03-22 18:50:08 +03:00

B #3019: Fix VNC access when lxd is installed via snaps

(cherry picked from commit 58aff07f9d5be1da196852bb078969be0ac483d7)
This commit is contained in:
Daniel Clavijo Coca 2019-03-12 09:11:17 -06:00 committed by Ruben S. Montero
parent 672f9f8134
commit 3869ac6afe
3 changed files with 14 additions and 18 deletions

View File

@ -31,15 +31,16 @@ class LXDClient
API = '/1.0'.freeze
HEADER = { 'Host' => 'localhost' }.freeze
attr_reader :lxd_path
attr_reader :lxd_path, :snap
def initialize
paths = ['/var/lib/lxd', '/var/snap/lxd/common/lxd']
snap_path = '/var/snap/lxd/common/lxd'
apt_path = '/var/lib/lxd'
@socket = nil
@lxd_path = nil
paths.each do |path|
[apt_path, snap_path].each do |path|
begin
@socket = socket(path)
@lxd_path = path
@ -50,6 +51,8 @@ class LXDClient
end
raise 'Failed to open LXD socket' unless @socket
@snap = @lxd_path == snap_path
end
# Performs HTTP::Get
@ -133,6 +136,7 @@ class LXDClient
response
end
end
# Error used for raising LXDClient exception when response is error return value

View File

@ -35,7 +35,6 @@ class Container
# Class Constants API and Containers Paths
#---------------------------------------------------------------------------
CONTAINERS = 'containers'.freeze
LXC_COMMAND = 'lxc'
#---------------------------------------------------------------------------
# Methods to access container attributes
@ -69,6 +68,8 @@ class Container
@lxc = lxc
@one = one
@lxc_command = 'lxc'
@lxc_command.prepend 'sudo' if client.snap
@containers = "#{@client.lxd_path}/storage-pools/default/containers"
@rootfs_dir = "#{@containers}/#{name}/rootfs"
@ -163,17 +164,8 @@ class Container
# Runs command inside container
# @param command [String] to execute through lxc exec
def exec(command)
cmd = "#{LXC_COMMAND} exec #{@one.vm_name} -- #{command}"
rc, o, e = Command.execute(cmd, true)
# TODO: this should be removed when Snap bug is fixed
err = 'cannot create user data directory:'
rc, o, e = Command.execute("sudo #{cmd}", true) if e.include?(err)
log = "Failed to run command #{cmd}: #{e}"
OpenNebula.log_error("#{__method__}: #{log}") unless rc.zero?
[rc, o, e]
cmd = "#{@lxc_command} exec #{@one.vm_name} -- #{command}"
Command.execute(cmd, true)
end
#---------------------------------------------------------------------------
@ -361,7 +353,7 @@ class Container
# Start the svncterm server if it is down.
def vnc(signal)
command = @one.vnc_command(signal)
command = @one.vnc_command(signal, @lxc_command)
return if command.nil?
w = @one.lxdrc[:vnc][:width]

View File

@ -375,7 +375,7 @@ class OpenNebulaVM
# Creates container vnc connection
# Creates or closes a connection to a container rfb port depending on signal
def vnc_command(signal)
def vnc_command(signal, lxc_command)
data = @xml.element('//TEMPLATE/GRAPHICS')
return unless data && data['TYPE'].casecmp('vnc').zero?
@ -387,7 +387,7 @@ class OpenNebulaVM
command = @lxdrc[:vnc][:command]
command = data['COMMAND'] unless data['COMMAND'].empty?
"#{data['PORT']} #{pass} lxc exec #{@vm_name} #{command}\n"
"#{data['PORT']} #{pass} #{lxc_command} exec #{@vm_name} #{command}\n"
when 'stop'
"-#{data['PORT']}\n"
end