providers/docker: implement port checker for remote machine

This commit is contained in:
Mitchell Hashimoto 2014-04-17 15:05:50 -07:00
parent 623386f13c
commit e578e91e3a
5 changed files with 53 additions and 10 deletions

View File

@ -0,0 +1,11 @@
module VagrantPlugins
module GuestLinux
module Cap
class Port
def self.port_open_check(machine, port)
machine.communicate.test("nc -z 127.0.0.1 #{port}")
end
end
end
end
end

View File

@ -51,6 +51,12 @@ module VagrantPlugins
Cap::NFSClient
end
# For the Docker provider
guest_capability("linux", "port_open_check") do
require_relative "cap/port"
Cap::Port
end
guest_capability("linux", "read_ip_address") do
require_relative "cap/read_ip_address"
Cap::ReadIPAddress

View File

@ -15,7 +15,7 @@ module VagrantPlugins
b.use Call, IsState, :not_created do |env, b2|
# If the VM is NOT created yet, then do the setup steps
if env[:result]
b2.use EnvSet, :port_collision_repair => true
b2.use EnvSet, port_collision_repair: true
b2.use Call, HasSSH do |env2, b3|
if env2[:result]
@ -27,14 +27,8 @@ module VagrantPlugins
end
end
b2.use Call, HostMachineRequired do |env2, b3|
if !env[:result]
# We're not using a proxy host VM, so just handle
# port collisions like we would any other system.
b3.use HandleForwardedPortCollisions
end
end
b2.use HostMachinePortChecker
b2.use HandleForwardedPortCollisions
b2.use PrepareNFSValidIds
b2.use SyncedFolderCleanup
b2.use SyncedFolders
@ -225,6 +219,7 @@ module VagrantPlugins
autoload :Destroy, action_root.join("destroy")
autoload :HasSSH, action_root.join("has_ssh")
autoload :HostMachine, action_root.join("host_machine")
autoload :HostMachinePortChecker, action_root.join("host_machine_port_checker")
autoload :HostMachineRequired, action_root.join("host_machine_required")
autoload :HostMachineSyncFolders, action_root.join("host_machine_sync_folders")
autoload :HostMachineSyncFoldersDisable, action_root.join("host_machine_sync_folders_disable")

View File

@ -59,7 +59,7 @@ module VagrantPlugins
def forwarded_ports
mappings = {}
@machine.config.vm.networks.each do |type, options|
if type == :forwarded_port && options[:id] != 'ssh'
if type == :forwarded_port
mappings[options[:host]] = options
end
end

View File

@ -0,0 +1,31 @@
require "log4r"
module VagrantPlugins
module DockerProvider
module Action
# This sets up the middleware env var to check for ports in use.
class HostMachinePortChecker
def initialize(app, env)
@app = app
@logger = Log4r::Logger.new("vagrant::docker::hostmachineportchecker")
end
def call(env)
return @app.call(env) if !env[:machine].provider.host_vm?
@machine = env[:machine]
env[:port_collision_port_check] = method(:port_check)
@app.call(env)
end
protected
def port_check(port)
host_machine = @machine.provider.host_vm
host_machine.guest.capability(:port_open_check, port)
end
end
end
end
end