From e714720052e7e1d00b752005760b12cac7b39cfa Mon Sep 17 00:00:00 2001 From: Mitchell Hashimoto Date: Tue, 15 Apr 2014 19:58:28 -0700 Subject: [PATCH] providers/docker: `vagrant ssh` proxies through the host VM --- plugins/providers/docker/action.rb | 3 ++ .../providers/docker/action/prepare_ssh.rb | 41 +++++++++++++++++++ plugins/providers/docker/provider.rb | 12 ++---- 3 files changed, 48 insertions(+), 8 deletions(-) create mode 100644 plugins/providers/docker/action/prepare_ssh.rb diff --git a/plugins/providers/docker/action.rb b/plugins/providers/docker/action.rb index c1d96bf55..fc6dfc7c7 100644 --- a/plugins/providers/docker/action.rb +++ b/plugins/providers/docker/action.rb @@ -162,6 +162,8 @@ module VagrantPlugins b3.use Message, I18n.t("docker_provider.messages.not_running") next end + + b3.use PrepareSSH b3.use SSHExec end end @@ -222,6 +224,7 @@ module VagrantPlugins autoload :ForwardPorts, action_root.join("forward_ports") autoload :HasSSH, action_root.join("has_ssh") autoload :HostMachine, action_root.join("host_machine") + autoload :PrepareSSH, action_root.join("prepare_ssh") autoload :Stop, action_root.join("stop") autoload :PrepareNFSValidIds, action_root.join("prepare_nfs_valid_ids") autoload :PrepareNFSSettings, action_root.join("prepare_nfs_settings") diff --git a/plugins/providers/docker/action/prepare_ssh.rb b/plugins/providers/docker/action/prepare_ssh.rb new file mode 100644 index 000000000..692f46883 --- /dev/null +++ b/plugins/providers/docker/action/prepare_ssh.rb @@ -0,0 +1,41 @@ +module VagrantPlugins + module DockerProvider + module Action + class PrepareSSH + def initialize(app, env) + @app = app + end + + def call(env) + # If we aren't using a host VM, then don't worry about it + return @app.call(env) if !env[:machine].provider.host_vm? + + # Get the container's SSH info + info = env[:machine].ssh_info + info[:port] ||= 22 + + # Modify the SSH info to be the host VM's info + env[:ssh_info] = env[:machine].provider.host_vm.ssh_info + + # Modify the SSH options for when we `vagrant ssh`... + ssh_opts = env[:ssh_opts] || {} + + # Append "-t" to force a TTY allocation + ssh_opts[:extra_args] = Array(ssh_opts[:extra_args]) + ssh_opts[:extra_args] << "-t" + + # Append our real SSH command + ssh_opts[:extra_args] << + "ssh -i /home/vagrant/insecure " + + "-p#{info[:port]} " + + "#{info[:username]}@#{info[:host]}" + + # Set the opts + env[:ssh_opts] = ssh_opts + + @app.call(env) + end + end + end + end +end diff --git a/plugins/providers/docker/provider.rb b/plugins/providers/docker/provider.rb index 678e16865..3e1f1c423 100644 --- a/plugins/providers/docker/provider.rb +++ b/plugins/providers/docker/provider.rb @@ -123,21 +123,17 @@ module VagrantPlugins # Returns the SSH info for accessing the Container. def ssh_info - # If the Container is not created then we cannot possibly SSH into it, so - # we return nil. - return nil if state == :not_created + # If the container isn't running, we can't SSH into it + return nil if state.id != :running network = driver.inspect_container(@machine.id)['NetworkSettings'] ip = network['IPAddress'] # If we were not able to identify the container's IP, we return nil # here and we let Vagrant core deal with it ;) - return nil unless ip + return nil if !ip - { - :host => ip, - :port => @machine.config.ssh.guest_port - } + { host: ip } end def state