Skip to content

Commit

Permalink
F #3189: Fix LXD status report (#4210)
Browse files Browse the repository at this point in the history
* F #3189: Fix lxd net hook

* F #3189: Fix LXD transitions

* F #3189:  Prioritize transition flag over status

* M #: Lint

* M #: Remove WIP hook

* M #: C7 compat

* F #3189: Remove flag only on native containers

(cherry picked from commit e594c88)
  • Loading branch information
dann1 authored and rsmontero committed Feb 19, 2020
1 parent 7df413f commit 9986eee
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 41 deletions.
23 changes: 9 additions & 14 deletions share/examples/network_hooks/99-lxd_clean.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,17 +7,15 @@

def clean_host_nic(veth)
cmd = "ip link show #{veth}"

_o, _e, s = Open3.capture3(cmd)

return unless s == 0

cmd = "sudo ip link delete #{veth}"
OpenNebula.log "Found lingering nic #{veth}\n Running #{cmd}"

o, e, _s = Open3.capture3(cmd)

OpenNebula.log "#{o}\n#{e}"
o, e, s = Open3.capture3(cmd)
OpenNebula.log "#{o}\n#{e}" unless s == 0
end

###############
Expand All @@ -40,16 +38,13 @@ def clean_host_nic(veth)
nics << nic
end

# Detect if hotplug or shutdown
detach = nil
# Detect if shutdown/reboot or nic hotplug calls vnm clean.
# Assumes only 1 nic detachable at once.
nics.each do |nic|
next unless nic[:nic][:attach] == 'YES'

detach = nic[:nic][:target]
if nic[:nic][:attach] == 'YES'
clean_host_nic(nic[:nic][:target])
exit 0
end
end

if detach # only clean detached nic
clean_host_nic(detach)
else # clean all nics
nics.each {|nic| clean_host_nic(nic[:nic][:target]) }
end
nics.each {|nic| clean_host_nic(nic[:nic][:target]) }
22 changes: 9 additions & 13 deletions src/vmm_mad/remotes/lib/lxd/container.rb
Original file line number Diff line number Diff line change
Expand Up @@ -208,12 +208,7 @@ def show_log
def start(options = {})
OpenNebula.log '--- Starting container ---'

operation = change_state(__method__, options)

transition_end
update

operation
change_state(__method__, options)
end

def stop(options = { :timeout => 120 })
Expand All @@ -232,8 +227,8 @@ def check_stop(force)

begin
stop(:force => force)
rescue => exception
OpenNebula.log_error "LXD Error: #{exception}"
rescue => e
OpenNebula.log_error "LXD Error: #{e}"

real_status = 'Unknown'

Expand All @@ -248,8 +243,8 @@ def check_stop(force)

begin
stop(:force => true) if real_status == 'Running'
rescue => exception
error = "LXD Error: Cannot shut down container #{exception}"
rescue => e
error = "LXD Error: Cannot shut down container #{e}"

OpenNebula.log_error error
end
Expand All @@ -263,12 +258,12 @@ def reboot(force)
start

config['user.reboot_state'] = 'RUNNING'
transition_start
transition_end # container reached a final state
else
check_stop(force)

config['user.reboot_state'] = 'STOPPED'
transition_end
transition_start # container will be started later
end

update
Expand Down Expand Up @@ -555,8 +550,9 @@ def new_disk_mapper(disk)
when 'FILE', 'BLOCK'

ds = @one.disk_source(disk)
cmd = "#{Mapper::COMMANDS[:file]} #{ds}"

rc, out, err = Command.execute("#{Mapper::COMMANDS[:file]} #{ds}", false)
rc, out, err = Command.execute(cmd, false)

unless rc.zero?
OpenNebula.log_error("#{__method__} #{err}")
Expand Down
5 changes: 3 additions & 2 deletions src/vmm_mad/remotes/lxd/deploy
Original file line number Diff line number Diff line change
Expand Up @@ -69,13 +69,13 @@ else
begin
operation = container.start
raise operation if container.status != 'Running'
rescue StandardError => exception
rescue e
# TODO: Improve wait condition
sleep 5 # Wait for LXD to revert non-rootfs disks mountpoint

container.clean

raise exception
raise e
end

end
Expand All @@ -84,6 +84,7 @@ end
# Updates container configuration with the OpenNebulaVM description
# ------------------------------------------------------------------------------
container.config.update('user.xml' => xml)
container.transition_end unless container.wild?
container.update

container.vnc('start')
Expand Down
29 changes: 17 additions & 12 deletions src/vmm_mad/remotes/lxd/poll
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,8 @@ module LXD
# * 'stopped' container not running or in the process of shutting down.
# * 'failure' container have failed.
def get_state(container)
return '-' if container.config['user.one_status'] == '0'

begin
status = container.status.downcase
rescue StandardError
Expand All @@ -121,8 +123,6 @@ module LXD
state = 'p'
when 'stopped'
state = 'd'

state = '-' if container.config['user.one_status'] == '0'
when 'failure'
state = 'e'
else
Expand Down Expand Up @@ -227,6 +227,12 @@ module LXD
memory
end

def unindent(str)
m = str.match(/^(\s*)/)
spaces = m[1].size
str.gsub!(/^ {#{spaces}}/, '')
end

def to_one(container)
name = container.name
arch = container.architecture
Expand All @@ -248,16 +254,15 @@ module LXD
cpu = cpu.chomp('%').to_f / 100
mem = parse_memory(mem)

template = <<EOT
NAME="#{name}"
CPU=#{cpu}
VCPU=#{vcpu}
MEMORY=#{mem}
HYPERVISOR="lxd"
IMPORT_VM_ID="#{name}"
OS=[ARCH="#{arch}"]
EOT
template
unindent(<<-EOT)
NAME="#{name}"
CPU=#{cpu}
VCPU=#{vcpu}
MEMORY=#{mem}
HYPERVISOR="lxd"
IMPORT_VM_ID="#{name}"
OS=[ARCH="#{arch}"]
EOT
end

end
Expand Down

0 comments on commit 9986eee

Please sign in to comment.