Skip to content

Commit

Permalink
Merge pull request #3 from jubianchi/master
Browse files Browse the repository at this point in the history
Add more formatting to redirections list
  • Loading branch information
bewiwi committed Oct 8, 2013
2 parents 6f04174 + 32b473f commit f418561
Show file tree
Hide file tree
Showing 4 changed files with 72 additions and 32 deletions.
2 changes: 1 addition & 1 deletion bin/install.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
60 changes: 48 additions & 12 deletions lib/vagrant-getredirection/command.rb
Original file line number Diff line number Diff line change
@@ -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 <vm-name>"
opts.separator ""
opts = OptionParser.new do |opts|
opts.banner = 'Usage: vagrant redirection <name> [--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
40 changes: 22 additions & 18 deletions lib/vagrant-getredirection/controller.rb
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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

Expand All @@ -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*(?<name>.*?),.*?host port\s*=\s*(?<hostport>[0-9]+),.*?guest port\s*=\s*(?<guestport>[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
2 changes: 1 addition & 1 deletion lib/vagrant-getredirection/version.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
module VagrantGetredirection
VERSION = "0.2"
VERSION = "0.3"
end

0 comments on commit f418561

Please sign in to comment.