[DRE-commits] [vagrant-libvirt] 03/17: Describe new functionality in the README and fix some coding style issues.

Antonio Terceiro terceiro at moszumanska.debian.org
Sun Apr 24 13:56:16 UTC 2016


This is an automated email from the git hooks/post-receive script.

terceiro pushed a commit to annotated tag 0.0.15
in repository vagrant-libvirt.

commit d3e9188e736e2396d009002d1ab4c39302cf3f1a
Author: Ryan Petrello <lists at ryanpetrello.com>
Date:   Fri Jan 24 07:48:00 2014 -0800

    Describe new functionality in the README and fix some coding style issues.
---
 README.md                                   |  6 +++
 lib/vagrant-libvirt/action/forward_ports.rb | 64 ++++++++++++++++++-----------
 2 files changed, 47 insertions(+), 23 deletions(-)

diff --git a/README.md b/README.md
index 1fae222..1a9d0cf 100644
--- a/README.md
+++ b/README.md
@@ -271,6 +271,12 @@ Configurable ssh parameters in Vagrantfile after provider version 0.0.5 are:
 * `config.ssh.forward_agent` - Default is false.
 * `config.ssh.forward_x11` - Default is false.
 
+## Forwarded Ports
+
+vagrant-libvirt supports Forwarded Ports via ssh port forwarding.  For each
+`forwarded_port` directive you specify in your Vagrantfile, vagrant-libvirt
+will maintain an active ssh process for the lifetime of the VM.
+
 ## Synced Folders
 
 There is minimal support for synced folders. Upon `vagrant up`, the Libvirt
diff --git a/lib/vagrant-libvirt/action/forward_ports.rb b/lib/vagrant-libvirt/action/forward_ports.rb
index 82995a1..165b69c 100644
--- a/lib/vagrant-libvirt/action/forward_ports.rb
+++ b/lib/vagrant-libvirt/action/forward_ports.rb
@@ -1,10 +1,11 @@
 module VagrantPlugins
   module ProviderLibvirt
     module Action
+      # Adds support for vagrant's `forward_ports` configuration directive.
       class ForwardPorts
         def initialize(app, env)
           @app    = app
-          @logger = Log4r::Logger.new("vagrant_libvirt::action::forward_ports")
+          @logger = Log4r::Logger.new('vagrant_libvirt::action::forward_ports')
         end
 
         def call(env)
@@ -16,7 +17,9 @@ module VagrantPlugins
           # Warn if we're port forwarding to any privileged ports
           env[:forwarded_ports].each do |fp|
             if fp[:host] <= 1024
-              env[:ui].warn I18n.t("vagrant.actions.vm.forward_ports.privileged_ports")
+              env[:ui].warn I18n.t(
+                'vagrant.actions.vm.forward_ports.privileged_ports'
+              )
               break
             end
           end
@@ -25,7 +28,7 @@ module VagrantPlugins
           @app.call env
 
           if @env[:forwarded_ports].any?
-            env[:ui].info I18n.t("vagrant.actions.vm.forward_ports.forwarding")
+            env[:ui].info I18n.t('vagrant.actions.vm.forward_ports.forwarding')
             forward_ports
           end
         end
@@ -33,13 +36,15 @@ module VagrantPlugins
         def forward_ports
           @env[:forwarded_ports].each do |fp|
             message_attributes = {
-              :adapter    => 'eth0',
-              :guest_port => fp[:guest],
-              :host_port  => fp[:host]
+              adapter: 'eth0',
+              guest_port: fp[:guest],
+              host_port: fp[:host]
             }
 
-            @env[:ui].info(I18n.t("vagrant.actions.vm.forward_ports.forwarding_entry",
-                                  message_attributes))
+            @env[:ui].info(I18n.t(
+                'vagrant.actions.vm.forward_ports.forwarding_entry',
+                message_attributes
+            ))
 
             ssh_pid = redirect_port(
               @env[:machine].name,
@@ -60,9 +65,10 @@ module VagrantPlugins
           config.vm.networks.each do |type, options|
             next if options[:disabled]
 
-            # TODO: Deprecate this behavior of "automagically" skipping ssh forwarded ports
             if type == :forwarded_port && options[:id] != 'ssh'
-              options.delete(:host_ip) if options.fetch(:host_ip, '').to_s.strip.empty?
+              if options.fetch(:host_ip, '').to_s.strip.empty?
+                options.delete(:host_ip)
+              end
               mappings[options[:host]] = options
             end
           end
@@ -70,9 +76,14 @@ module VagrantPlugins
           mappings.values
         end
 
-        def redirect_port(machine_name, host_ip, host_port, guest_ip, guest_port)
-          params = %W( #{machine_name} -L #{host_ip}:#{host_port}:#{guest_ip}:#{guest_port} -N ).join(" ")
-          ssh_cmd = "ssh $(vagrant ssh-config #{machine_name} | awk '{print \" -o \"$1\"=\"$2}') #{params} 2>/dev/null"
+        def redirect_port(machine, host_ip, host_port, guest_ip, guest_port)
+          params = %W(
+            #{machine}
+            -L #{host_ip}:#{host_port}:#{guest_ip}:#{guest_port}
+            -N
+          ).join(' ')
+          ssh_cmd = "ssh $(vagrant ssh-config #{machine}"\
+              "| awk '{print \" -o \"$1\"=\"$2}') #{params} 2>/dev/null"
 
           @logger.debug "Forwarding port with `#{ssh_cmd}`"
           spawn ssh_cmd
@@ -86,7 +97,6 @@ module VagrantPlugins
             pid_file.write(ssh_pid)
           end
         end
-
       end
     end
   end
@@ -95,27 +105,32 @@ end
 module VagrantPlugins
   module ProviderLibvirt
     module Action
+      # Cleans up ssh-forwarded ports on VM halt/destroy.
       class ClearForwardedPorts
         def initialize(app, env)
           @app = app
-          @logger = Log4r::Logger.new("vagrant_libvirt::action::clear_forward_ports")
+          @logger = Log4r::Logger.new(
+            'vagrant_libvirt::action::clear_forward_ports'
+          )
         end
 
         def call(env)
           @env = env
 
           if ssh_pids.any?
-            env[:ui].info I18n.t("vagrant.actions.vm.clear_forward_ports.deleting")
+            env[:ui].info I18n.t(
+              'vagrant.actions.vm.clear_forward_ports.deleting'
+            )
             ssh_pids.each do |pid|
-              next unless is_ssh_pid?(pid)
+              next unless ssh_pid?(pid)
               @logger.debug "Killing pid #{pid}"
               system "pkill -TERM -P #{pid}"
             end
 
-            @logger.info "Removing ssh pid files"
+            @logger.info 'Removing ssh pid files'
             remove_ssh_pids
           else
-            @logger.info "No ssh pids found"
+            @logger.info 'No ssh pids found'
           end
 
           @app.call env
@@ -124,18 +139,21 @@ module VagrantPlugins
         protected
 
         def ssh_pids
-          @ssh_pids = Dir[@env[:machine].data_dir.join('pids').to_s + "/ssh_*.pid"].map do |file|
+          glob = @env[:machine].data_dir.join('pids').to_s + '/ssh_*.pid'
+          @ssh_pids = Dir[glob].map do |file|
             File.read(file).strip.chomp
           end
         end
 
-        def is_ssh_pid?(pid)
-          @logger.debug "Checking if #{pid} is an ssh process with `ps -o cmd= #{pid}`"
+        def ssh_pid?(pid)
+          @logger.debug 'Checking if #{pid} is an ssh process '\
+                        'with `ps -o cmd= #{pid}`'
           `ps -o cmd= #{pid}`.strip.chomp =~ /ssh/
         end
 
         def remove_ssh_pids
-          Dir[@env[:machine].data_dir.join('pids').to_s + "/ssh_*.pid"].each do |file|
+          glob = @env[:machine].data_dir.join('pids').to_s + '/ssh_*.pid'
+          Dir[glob].each do |file|
             File.delete file
           end
         end

-- 
Alioth's /usr/local/bin/git-commit-notice on /srv/git.debian.org/git/pkg-ruby-extras/vagrant-libvirt.git



More information about the Pkg-ruby-extras-commits mailing list