Class: VagrantPlugins::ProviderLibvirt::Action::ClearForwardedPorts

Inherits:
Object
  • Object
show all
Defined in:
lib/vagrant-libvirt/action/forward_ports.rb

Overview

Cleans up ssh-forwarded ports on VM halt/destroy.

Constant Summary collapse

@@lock =
Mutex.new

Instance Method Summary collapse

Constructor Details

#initialize(app, env) ⇒ ClearForwardedPorts

Returns a new instance of ClearForwardedPorts.



167
168
169
170
171
172
173
# File 'lib/vagrant-libvirt/action/forward_ports.rb', line 167

def initialize(app, env)
  @app = app
  @logger = Log4r::Logger.new(
    'vagrant_libvirt::action::clear_forward_ports'
  )
  @ui = env[:ui]
end

Instance Method Details

#call(env) ⇒ Object



175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
# File 'lib/vagrant-libvirt/action/forward_ports.rb', line 175

def call(env)
  pids = ssh_pids(env[:machine])
  if pids.any?
    @ui.info I18n.t(
      'vagrant.actions.vm.clear_forward_ports.deleting'
    )
    pids.each do |tag|
      next unless ssh_pid?(tag[:pid])
      @logger.debug "Killing pid #{tag[:pid]}"
      kill_cmd = ''

      if tag[:port] <= 1024
        kill_cmd += 'sudo ' # add sudo prefix
      end

      kill_cmd += "kill #{tag[:pid]}"
      @@lock.synchronize do
        system(kill_cmd)
      end
    end

    @logger.info 'Removing ssh pid files'
    remove_ssh_pids(env[:machine])
  else
    @logger.info 'No ssh pids found'
  end

  @app.call env
end