mirror of
https://github.com/OpenNebula/one.git
synced 2025-03-22 18:50:08 +03:00
B #3175 Fix container shutdown
Co-authored-by: Daniel Clavijo Coca <dclavijo@opennebula.systems> (cherry picked from commit 103cada4150e76b6649ea62b73e8f6f31133b308)
This commit is contained in:
parent
b59ee42750
commit
83bec40cbe
@ -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
|
||||
|
||||
|
@ -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
|
||||
|
@ -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
|
||||
|
Loading…
x
Reference in New Issue
Block a user