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.


158
159
160
161
162
163
# File 'lib/vagrant-libvirt/action/forward_ports.rb', line 158

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

Instance Method Details

#call(env) ⇒ Object


165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
# File 'lib/vagrant-libvirt/action/forward_ports.rb', line 165

def call(env)
  @env = env

  if ssh_pids.any?
    env[:ui].info I18n.t(
      'vagrant.actions.vm.clear_forward_ports.deleting'
    )
    ssh_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
  else
    @logger.info 'No ssh pids found'
  end

  @app.call env
end