From 32b473f2bc99b23d96494ab80c6b49d853d047ed Mon Sep 17 00:00:00 2001 From: "julien.bianchi" Date: Mon, 7 Oct 2013 18:01:14 +0200 Subject: [PATCH] Add more formatting to redirections list --- bin/install.sh | 2 +- lib/vagrant-getredirection/command.rb | 60 +++++++++++++++++++----- lib/vagrant-getredirection/controller.rb | 40 +++++++++------- lib/vagrant-getredirection/version.rb | 2 +- 4 files changed, 72 insertions(+), 32 deletions(-) diff --git a/bin/install.sh b/bin/install.sh index 3ae4d89..23fb15e 100755 --- a/bin/install.sh +++ b/bin/install.sh @@ -9,7 +9,7 @@ cd $ROOT rake -T --trace || exit_code=1 rake build --trace || exit_code=1 vagrant plugin uninstall vagrant-getredirection || true -vagrant plugin install pkg/vagrant-getredirection-0.2.gem || exit_code=1 +vagrant plugin install pkg/vagrant-getredirection-0.3.gem || exit_code=1 vagrant plugin list exit $exit_code diff --git a/lib/vagrant-getredirection/command.rb b/lib/vagrant-getredirection/command.rb index 5dcf4d0..26e33f4 100644 --- a/lib/vagrant-getredirection/command.rb +++ b/lib/vagrant-getredirection/command.rb @@ -1,27 +1,63 @@ require 'optparse' +require 'json' module VagrantGetredirection class Command < Vagrant.plugin("2", :command) def execute - options = {} - opts = OptionParser.new do |opts| - opts.banner = "Usage: vagrant redirection " - opts.separator "" + opts = OptionParser.new do |opts| + opts.banner = 'Usage: vagrant redirection [--json] [--port]' + opts.separator '' + + opts.on('-j', '--json', 'Output redirections as JSON') do |j| + options['json'] = j + end + + opts.on('-p', '--port port', 'Output redirection for selected guest port') do |p| + options['port'] = p + end + end + + # Parse the options + argv = parse_options(opts) + return if !argv + + contr = VagrantGetredirection::Controller.new(@app, @env) + redirects = {} + + with_target_vms(argv, :reverse => true) do |machine| + contr.run(machine, redirects) + end + if options['port'] + + redirects.each_value do |rules| + rules.each_key do |guest| + unless guest == options['port'] + rules.delete(guest) + end + end end + end - # Parse the options - argv = parse_options(opts) - return if !argv + if options['json'] + @env.ui.info(JSON.pretty_generate(redirects)) + else + redirects.each_pair do |machine, rules| + if redirects.count > 1 + @env.ui.info("[#{machine}] - Redirect :") + end - contr = VagrantGetredirection::Controller.new(@app, @env) - - with_target_vms(argv, :reverse => true) do |machine| - contr.run(machine) + rules.each_pair do |guest, host| + if options['port'] + @env.ui.info("#{host}") + else + @env.ui.info("#{host} => #{guest}") + end + end end + end end - end end diff --git a/lib/vagrant-getredirection/controller.rb b/lib/vagrant-getredirection/controller.rb index 87f7cec..8783d69 100644 --- a/lib/vagrant-getredirection/controller.rb +++ b/lib/vagrant-getredirection/controller.rb @@ -1,13 +1,11 @@ require 'pp' -module VagrantGetredirection - +module VagrantGetredirection class Controller - def initialize(app, env) @app = app @env = env - @vboxcmd=determine_vboxcmd + @vboxcmd = determine_vboxcmd end def determine_vboxcmd @@ -16,11 +14,12 @@ def determine_vboxcmd if ENV.has_key?("VBOX_INSTALL_PATH") # The path usually ends with a \ but we make sure here path = File.join(ENV["VBOX_INSTALL_PATH"], "VBoxManage.exe") - return "\"#{path}\"" + + "\"#{path}\"" end else # for other platforms assume it is on the PATH - return "VBoxManage" + 'VBoxManage' end end @@ -31,24 +30,29 @@ def windows? false end - def run(machine) + def run(machine, redirects) instance_id = machine.id instance_name = machine.name - results=command("#{@vboxcmd} showvminfo #{instance_id} |grep \"^NIC [0-9] Rule\" | sed 's/^.*host port = \\([0-9]*\\).*guest port = \\([0-9]*\\)/\\1 => \\2/g' ") - puts "[#{instance_name}] - Redirect : " - #puts "#{result}" - results.each { |result| puts "#{result}" } + redirects[instance_name] = {} + + result=command("#{@vboxcmd} showvminfo #{instance_id}") + + result.each do |line| + data = /^NIC [0-9]+ Rule\([0-9]+\):\s*name\s*=\s*(?.*?),.*?host port\s*=\s*(?[0-9]+),.*?guest port\s*=\s*(?[0-9]+)/.match(line) + + unless data == nil + redirects[instance_name][data['guestport']] = data['hostport'] + end + end end - def command(command,options = {}) + def command(command) output=nil - result=IO.popen("#{command}") {|f| output=f.readlines} - return output - end + IO.popen("#{command}") do |f| + output = f.readlines + end - def is_vm_created?(machine) - return !machine.id.nil? + output end - end end diff --git a/lib/vagrant-getredirection/version.rb b/lib/vagrant-getredirection/version.rb index 04a31d9..4aa70d9 100644 --- a/lib/vagrant-getredirection/version.rb +++ b/lib/vagrant-getredirection/version.rb @@ -1,3 +1,3 @@ module VagrantGetredirection - VERSION = "0.2" + VERSION = "0.3" end