diff --git a/src/vmm_mad/remotes/lib/lxd/container.rb b/src/vmm_mad/remotes/lib/lxd/container.rb index 890f37183a..302b136c4a 100644 --- a/src/vmm_mad/remotes/lib/lxd/container.rb +++ b/src/vmm_mad/remotes/lib/lxd/container.rb @@ -68,11 +68,12 @@ 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" + @rootfs_dir = "#{@client.lxd_path}/storage-pools/default/containers/"\ + "#{name}/rootfs" @context_path = "#{@rootfs_dir}/context" end @@ -149,11 +150,15 @@ class Container 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 + 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'] @@ -196,10 +201,27 @@ class Container 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 + + 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 + + real_status = check_status + break if %w[Running Stopped].include? real_status + end + + 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 3d67455b87..72dfa60258 100755 --- a/src/vmm_mad/remotes/lxd/deploy +++ b/src/vmm_mad/remotes/lxd/deploy @@ -38,25 +38,15 @@ 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) + running = container.check_status == 'Running' + raise 'A container with the same ID is already running' if running + OpenNebula.log_info('Overriding container') - config = container.config - devices = container.devices - - container.get_metadata - - err_msg = 'A container with the same ID is already running' - raise err_msg if container.status == 'Running' - - container.config = config - - container.devices = devices - container.update else container.create diff --git a/src/vmm_mad/remotes/lxd/shutdown b/src/vmm_mad/remotes/lxd/shutdown index bf5885d58f..b5aad709ec 100755 --- a/src/vmm_mad/remotes/lxd/shutdown +++ b/src/vmm_mad/remotes/lxd/shutdown @@ -34,28 +34,14 @@ 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 # ------------------------------------------------------------------------------ -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' - - e = 'Failed to dismantle container storage' - raise "#{e}\n#{container}" unless container.setup_storage('unmap') - - container.delete -end - container.vnc('stop') +container.check_stop + +return if container.wild? + +raise 'Failed to dismantle container storage' unless \ +container.setup_storage('unmap') + +container.delete