Sha256: 3739f747a69b7fe20bcda3c256caa0038c3be8fe9455790f5c927c3903c6bbe8

Contents?: true

Size: 1.15 KB

Versions: 6

Compression:

Stored size: 1.15 KB

Contents

module Vagrant
  module LXC
    module Action
      class ClearForwardedPorts
        def initialize(app, env)
          @app = app
          @logger = Log4r::Logger.new("vagrant::lxc::action::clear_forwarded_ports")
        end

        def call(env)
          @env = env

          if redir_pids.any?
            env[:ui].info I18n.t("vagrant.actions.vm.clear_forward_ports.deleting")
            redir_pids.each do |pid|
              next unless is_redir_pid?(pid)
              @logger.debug "Killing pid #{pid}"
              system "sudo pkill -9 -P #{pid}"
            end

            remove_redir_pids
          end

          @app.call env
        end

        protected

        def redir_pids
          @redir_pids = Dir[@env[:machine].data_dir.join('pids').to_s + "/redir_*.pid"].map do |file|
            File.read(file).strip.chomp
          end
        end

        def is_redir_pid?(pid)
          `ps -o cmd= #{pid}`.strip.chomp =~ /redir/
        end

        def remove_redir_pids
          Dir[@env[:machine].data_dir.join('pids').to_s + "/redir_*.pid"].each do |file|
            File.delete file
          end
        end
      end
    end
  end
end

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
vagrant-lxc-0.3.2 lib/vagrant-lxc/action/clear_forwarded_ports.rb
vagrant-lxc-0.3.1 lib/vagrant-lxc/action/clear_forwarded_ports.rb
vagrant-lxc-0.3.0 lib/vagrant-lxc/action/clear_forwarded_ports.rb
vagrant-lxc-0.2.0 lib/vagrant-lxc/action/clear_forwarded_ports.rb
vagrant-lxc-0.1.1 lib/vagrant-lxc/action/clear_forwarded_ports.rb
vagrant-lxc-0.1.0 lib/vagrant-lxc/action/clear_forwarded_ports.rb