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.



139
140
141
142
143
144
# File 'lib/vagrant-libvirt/action/forward_ports.rb', line 139

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

Instance Method Details

#call(env) ⇒ Object



146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
# File 'lib/vagrant-libvirt/action/forward_ports.rb', line 146

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