Skip to content

Commit

Permalink
Clear and create forwarded ports on suspend/resume
Browse files Browse the repository at this point in the history
Ensure forwarded ports are cleared on suspend and recreated on resume.
Included tests to exercise the entire up/start behaviour to ensure the
expected actions are called in various scenarios, moving config validate
to only occur at the entrypoints and dropping it from being called
during start. Also eliminate a duplicate SetupComplete call.

Fixes: #1115
  • Loading branch information
electrofelix committed Nov 23, 2022
1 parent 6c4b775 commit 281d028
Show file tree
Hide file tree
Showing 2 changed files with 402 additions and 18 deletions.
29 changes: 16 additions & 13 deletions lib/vagrant-libvirt/action.rb
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,6 @@ module Action
autoload :StartDomain, action_root.join('start_domain')
autoload :StartShutdownTimer, action_root.join('shutdown_domain')
autoload :SuspendDomain, action_root.join('suspend_domain')
autoload :TimedProvision, action_root.join('timed_provision')
autoload :WaitTillUp, action_root.join('wait_till_up')

autoload :Package, 'vagrant/action/general/package'
Expand Down Expand Up @@ -94,8 +93,6 @@ def self.action_up
b2.use CreateNetworkInterfaces

b2.use action_start

b2.use SetupComplete
else
b2.use HandleStoragePool
require 'vagrant/action/builtin/handle_box'
Expand All @@ -112,6 +109,7 @@ def self.action_up
b2.use SetHostname
end
else
# start VM if halted
env[:halt_on_error] = true
b2.use ResolveDiskSettings
b2.use CreateNetworks
Expand All @@ -129,7 +127,6 @@ def self.action_up
# poweroff state.
private_class_method def self.action_start
Vagrant::Action::Builder.new.tap do |b|
b.use ConfigValidate
b.use Call, IsRunning do |env, b2|
# If the VM is running, run the necessary provisioners
if env[:result]
Expand All @@ -138,16 +135,19 @@ def self.action_up
end

b2.use Call, IsSuspended do |env2, b3|
# if vm is suspended resume it then exit
# if vm is suspended resume it
if env2[:result]
b3.use ResumeDomain
next
end

if !env[:machine].config.vm.box
# if there was a box, want to wait until the communicator is
# available and then forward ports
next if !env[:machine].config.vm.box
elsif !env[:machine].config.vm.box
# With no box, we just care about network creation and starting it
b3.use SetBootOrder
b3.use StartDomain

next
else
# VM is not running or suspended.
b3.use PrepareNFSValidIds
Expand All @@ -165,11 +165,12 @@ def self.action_up
# Machine should gain IP address when coming up,
# so wait for dhcp lease and store IP into machines data_dir.
b3.use WaitTillUp
require 'vagrant/action/builtin/wait_for_communicator'
b3.use WaitForCommunicator, [:running]

b3.use ForwardPorts
end

require 'vagrant/action/builtin/wait_for_communicator'
b3.use WaitForCommunicator, [:running]

b3.use ForwardPorts
end
end
end
Expand Down Expand Up @@ -216,13 +217,13 @@ def self.action_halt
# It uses the halt and start actions
def self.action_reload
Vagrant::Action::Builder.new.tap do |b|
b.use ConfigValidate
b.use Call, IsCreated do |env, b2|
unless env[:result]
b2.use MessageNotCreated
next
end

b2.use ConfigValidate
b2.use Provision
b2.use action_halt
b2.use action_start
Expand Down Expand Up @@ -339,6 +340,7 @@ def self.action_suspend
b3.use MessageNotRunning
next
end
b3.use ClearForwardedPorts
b3.use SuspendDomain
end
end
Expand Down Expand Up @@ -366,6 +368,7 @@ def self.action_resume
b3.use Provision
require 'vagrant/action/builtin/wait_for_communicator'
b3.use WaitForCommunicator, [:running]
b3.use ForwardPorts
end
end
end
Expand Down

0 comments on commit 281d028

Please sign in to comment.