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.



164
165
166
167
168
169
# File 'lib/vagrant-libvirt/action/forward_ports.rb', line 164

def initialize(app, _env)
  @app = app
  @logger = Log4r::Logger.new(
    'vagrant_libvirt::action::clear_forward_ports'
  )
end

Instance Method Details

#call(env) ⇒ Object



171
172
173
174
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
# File 'lib/vagrant-libvirt/action/forward_ports.rb', line 171

def call(env)
  pids = ssh_pids(env[:machine])
  if pids.any?
    env[: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