From a975508be41e80c5c4b226314923d1139d08eafe Mon Sep 17 00:00:00 2001 From: Daniel Clavijo Coca Date: Wed, 3 Apr 2019 10:49:28 -0600 Subject: [PATCH 1/5] Remove useless container attr --- src/vmm_mad/remotes/lib/lxd/container.rb | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/src/vmm_mad/remotes/lib/lxd/container.rb b/src/vmm_mad/remotes/lib/lxd/container.rb index 890f37183a1..c09f74abf62 100644 --- a/src/vmm_mad/remotes/lib/lxd/container.rb +++ b/src/vmm_mad/remotes/lib/lxd/container.rb @@ -68,11 +68,12 @@ def initialize(lxc, one, client) @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" + @rootfs_dir = "#{@client.lxd_path}/storage-pools/default/containers/\ + #{name}/rootfs" @context_path = "#{@rootfs_dir}/context" end From bd341df177ea403f5aa93f9241b3bfc70da6f47f Mon Sep 17 00:00:00 2001 From: Daniel Clavijo Coca Date: Wed, 3 Apr 2019 11:13:48 -0600 Subject: [PATCH 2/5] B #3175 Fix container shutdown --- src/vmm_mad/remotes/lib/lxd/container.rb | 23 +++++++++++++++++---- src/vmm_mad/remotes/lxd/shutdown | 26 ++++++------------------ 2 files changed, 25 insertions(+), 24 deletions(-) diff --git a/src/vmm_mad/remotes/lib/lxd/container.rb b/src/vmm_mad/remotes/lib/lxd/container.rb index c09f74abf62..a0f3fca16c9 100644 --- a/src/vmm_mad/remotes/lib/lxd/container.rb +++ b/src/vmm_mad/remotes/lib/lxd/container.rb @@ -197,10 +197,25 @@ def stop(options = { :timeout => 120 }) def check_stop return if status != 'Running' - if ARGV[-1] == '-f' - stop(:force => true) - else - stop + begin + if ARGV[-1] == '-f' + stop(:force => true) + else + stop + end + rescue => exception + OpenNebula.log_error exception + + 2.times do + # This call may return an operation output instead of a + # container data in case of timeout. The call breaks + # the container attributes. It needs to be read again + container = Container.get(vm_name, xml, client) + + break if %w[Running Stopped].include? container.status + end + + container.stop(:force => true) if container.status == 'Running' end end diff --git a/src/vmm_mad/remotes/lxd/shutdown b/src/vmm_mad/remotes/lxd/shutdown index bf5885d58f5..866322ffe5f 100755 --- a/src/vmm_mad/remotes/lxd/shutdown +++ b/src/vmm_mad/remotes/lxd/shutdown @@ -36,26 +36,12 @@ container = Container.get(vm_name, xml, client) # ------------------------------------------------------------------------------ # Stop the container & unmap devices if not a wild container # ------------------------------------------------------------------------------ -begin - container.check_stop -rescue => exception - OpenNebula.log_error exception -end - -# This call may return an operation output instead of a container data -# in case of timeout. The call breaks the container attributes -# it needs to be read again -container = Container.get(vm_name, xml, client) # :status => "Sucess" -# Fixer call -container = Container.get(vm_name, xml, client) unless %w[Running Stopped].include? container.status - -if !container.wild? - container.stop(:force => true) if container.status == 'Running' +container.vnc('stop') +container.check_stop - e = 'Failed to dismantle container storage' - raise "#{e}\n#{container}" unless container.setup_storage('unmap') +return if container.wild? - container.delete -end +raise 'Failed to dismantle container storage' unless \ +container.setup_storage('unmap') -container.vnc('stop') +container.delete From 7c85bc4dbe3d6596a081a3498e4956988fc5f242 Mon Sep 17 00:00:00 2001 From: Daniel Clavijo Coca Date: Wed, 3 Apr 2019 11:43:34 -0600 Subject: [PATCH 3/5] B #3175 Fixed string --- src/vmm_mad/remotes/lib/lxd/container.rb | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/vmm_mad/remotes/lib/lxd/container.rb b/src/vmm_mad/remotes/lib/lxd/container.rb index a0f3fca16c9..c203d6c5410 100644 --- a/src/vmm_mad/remotes/lib/lxd/container.rb +++ b/src/vmm_mad/remotes/lib/lxd/container.rb @@ -72,8 +72,8 @@ def initialize(lxc, one, client) @lxc_command = 'lxc' @lxc_command.prepend 'sudo ' if client.snap - @rootfs_dir = "#{@client.lxd_path}/storage-pools/default/containers/\ - #{name}/rootfs" + @rootfs_dir = "#{@client.lxd_path}/storage-pools/default/containers/"\ + "#{name}/rootfs" @context_path = "#{@rootfs_dir}/context" end From eb67491084f83febd72a68c24d312df0572e6072 Mon Sep 17 00:00:00 2001 From: Daniel Clavijo Coca Date: Thu, 4 Apr 2019 23:09:47 -0600 Subject: [PATCH 4/5] B #3175 Updated comments --- src/vmm_mad/remotes/lib/lxd/container.rb | 5 ++--- src/vmm_mad/remotes/lxd/deploy | 5 ++--- src/vmm_mad/remotes/lxd/shutdown | 2 +- 3 files changed, 5 insertions(+), 7 deletions(-) diff --git a/src/vmm_mad/remotes/lib/lxd/container.rb b/src/vmm_mad/remotes/lib/lxd/container.rb index c203d6c5410..73248283359 100644 --- a/src/vmm_mad/remotes/lib/lxd/container.rb +++ b/src/vmm_mad/remotes/lib/lxd/container.rb @@ -150,7 +150,7 @@ def update(wait: true, timeout: '') wait?(@client.put("#{CONTAINERS}/#{name}", @lxc), wait, timeout) end - # Returns the container current state + # Returns the container live state def monitor @client.get("#{CONTAINERS}/#{name}/state") end @@ -209,8 +209,7 @@ def check_stop 2.times do # This call may return an operation output instead of a # container data in case of timeout. The call breaks - # the container attributes. It needs to be read again - container = Container.get(vm_name, xml, client) + # the container info. It needs to be read again break if %w[Running Stopped].include? container.status end diff --git a/src/vmm_mad/remotes/lxd/deploy b/src/vmm_mad/remotes/lxd/deploy index 3d67455b87a..bf8de789a9c 100755 --- a/src/vmm_mad/remotes/lxd/deploy +++ b/src/vmm_mad/remotes/lxd/deploy @@ -38,9 +38,8 @@ container = Container.new_from_xml(xml, client) # ------------------------------------------------------------------------------ # Create Container in LXD -# - Already exists: gets container metadata from LXD and set OpenNebula -# configurations to update existing container. -# - Not exists. Creates new container in LXD. +# - Already exists: Raise error if container is detected running +# - Doesn't exist. Creates new container in LXD. # ------------------------------------------------------------------------------ if Container.exist?(container.name, client) OpenNebula.log_info('Overriding container') diff --git a/src/vmm_mad/remotes/lxd/shutdown b/src/vmm_mad/remotes/lxd/shutdown index 866322ffe5f..b5aad709ecc 100755 --- a/src/vmm_mad/remotes/lxd/shutdown +++ b/src/vmm_mad/remotes/lxd/shutdown @@ -34,7 +34,7 @@ client = LXDClient.new container = Container.get(vm_name, xml, client) # ------------------------------------------------------------------------------ -# Stop the container & unmap devices if not a wild container +# Stop vnc connection and container & unmap devices if not a wild container # ------------------------------------------------------------------------------ container.vnc('stop') container.check_stop From 72b9d8a8d453a77733f6e04773f0a60db1754ac4 Mon Sep 17 00:00:00 2001 From: Daniel Clavijo Coca Date: Thu, 4 Apr 2019 23:10:40 -0600 Subject: [PATCH 5/5] B #3175 Use live status calls when validating deply and shutdown --- src/vmm_mad/remotes/lib/lxd/container.rb | 11 +++++++++-- src/vmm_mad/remotes/lxd/deploy | 15 +++------------ 2 files changed, 12 insertions(+), 14 deletions(-) diff --git a/src/vmm_mad/remotes/lib/lxd/container.rb b/src/vmm_mad/remotes/lib/lxd/container.rb index 73248283359..302b136c4af 100644 --- a/src/vmm_mad/remotes/lib/lxd/container.rb +++ b/src/vmm_mad/remotes/lib/lxd/container.rb @@ -155,6 +155,10 @@ def monitor @client.get("#{CONTAINERS}/#{name}/state") end + def check_status + monitor['metadata']['status'] if Container.exist?(name, @client) + end + # Retreive metadata for the container def get_metadata @lxc = @client.get("#{CONTAINERS}/#{name}")['metadata'] @@ -206,15 +210,18 @@ def check_stop rescue => exception OpenNebula.log_error exception + real_status = 'Unknown' + 2.times do # This call may return an operation output instead of a # container data in case of timeout. The call breaks # the container info. It needs to be read again - break if %w[Running Stopped].include? container.status + real_status = check_status + break if %w[Running Stopped].include? real_status end - container.stop(:force => true) if container.status == 'Running' + container.stop(:force => true) if real_status == 'Running' end end diff --git a/src/vmm_mad/remotes/lxd/deploy b/src/vmm_mad/remotes/lxd/deploy index bf8de789a9c..72dfa602586 100755 --- a/src/vmm_mad/remotes/lxd/deploy +++ b/src/vmm_mad/remotes/lxd/deploy @@ -42,19 +42,10 @@ container = Container.new_from_xml(xml, client) # - Doesn't exist. Creates new container in LXD. # ------------------------------------------------------------------------------ if Container.exist?(container.name, client) - OpenNebula.log_info('Overriding container') - - config = container.config - devices = container.devices - - container.get_metadata + running = container.check_status == 'Running' + raise 'A container with the same ID is already running' if running - err_msg = 'A container with the same ID is already running' - raise err_msg if container.status == 'Running' - - container.config = config - - container.devices = devices + OpenNebula.log_info('Overriding container') container.update else