forked from hashicorp/vagrant
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
provider/docker: Add docker-exec command
This adds a new core command, `docker-exec`, which allows the user to exec into an already-running container. - Fixes hashicorp#6566 - Fixes hashicorp#5193 - Fixes hashicorp#4904 - Fixes hashicorp#4057 - Fixes hashicorp#4179 - Fixes hashicorp#4903
- Loading branch information
Showing
8 changed files
with
234 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,93 @@ | ||
module VagrantPlugins | ||
module DockerProvider | ||
module Command | ||
class Exec < Vagrant.plugin("2", :command) | ||
def self.synopsis | ||
"attach to an already-running docker container" | ||
end | ||
|
||
def execute | ||
options = {} | ||
options[:detach] = false | ||
options[:pty] = false | ||
options[:prefix] = true | ||
|
||
opts = OptionParser.new do |o| | ||
o.banner = "Usage: vagrant docker-exec [options] [name] -- <command> [args]" | ||
o.separator "" | ||
o.separator "Options:" | ||
o.separator "" | ||
|
||
o.on("--[no-]detach", "Run in the background") do |d| | ||
options[:detach] = d | ||
end | ||
|
||
o.on("-t", "--[no-]tty", "Allocate a pty") do |t| | ||
options[:pty] = t | ||
end | ||
|
||
o.on("--[no-]prefix", "Prefix output with machine names") do |p| | ||
options[:prefix] = p | ||
end | ||
end | ||
|
||
# Parse out the extra args to send to SSH, which is everything | ||
# after the "--" | ||
command = nil | ||
split_index = @argv.index("--") | ||
if split_index | ||
command = @argv.drop(split_index + 1) | ||
@argv = @argv.take(split_index) | ||
end | ||
|
||
# Parse the options | ||
argv = parse_options(opts) | ||
return if !argv | ||
|
||
# Show the error if we don't have "--" _after_ parse_options | ||
# so that "-h" and "--help" work properly. | ||
if !split_index | ||
raise Errors::ExecCommandRequired | ||
end | ||
|
||
target_opts = { provider: :docker } | ||
target_opts[:single_target] = options[:pty] | ||
|
||
with_target_vms(argv, target_opts) do |machine| | ||
if machine.state.id != :running | ||
@env.ui.info("#{machine.id} is not running.") | ||
next | ||
end | ||
exec_command(machine, options, command) | ||
end | ||
|
||
return 0 | ||
end | ||
|
||
def exec_command(machine, options, command) | ||
exec_cmd = %w(docker exec) | ||
exec_cmd << "-it" if options[:pty] | ||
exec_cmd << machine.id | ||
exec_cmd += options[:extra_args] if options[:extra_args] | ||
exec_cmd += command | ||
|
||
# Run this interactively if asked. | ||
exec_options = options | ||
exec_options[:stdin] = true if options[:pty] | ||
|
||
output = "" | ||
machine.provider.driver.execute(*exec_cmd, exec_options) do |type, data| | ||
output += data | ||
end | ||
|
||
output_options = {} | ||
output_options[:prefix] = false if !options[:prefix] | ||
|
||
if !output.empty? | ||
machine.ui.output(output.chomp, **output_options) | ||
end | ||
end | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
require_relative "../../../../base" | ||
require_relative "../../../../../../plugins/providers/docker/command/exec" | ||
|
||
describe VagrantPlugins::DockerProvider::Command::Exec do | ||
include_context "unit" | ||
include_context "command plugin helpers" | ||
|
||
let(:sandbox) do | ||
isolated_environment | ||
end | ||
|
||
let(:argv) { [] } | ||
let(:env) { sandbox.create_vagrant_env } | ||
|
||
let(:vagrantfile_path) { File.join(env.cwd, "Vagrantfile") } | ||
|
||
subject { described_class.new(argv, env) } | ||
|
||
before(:all) do | ||
I18n.load_path << Vagrant.source_root.join("templates/locales/providers_docker.yml") | ||
I18n.reload! | ||
end | ||
|
||
before do | ||
Vagrant.plugin("2").manager.stub(commands: {}) | ||
allow(subject).to receive(:exec_command) | ||
end | ||
|
||
after do | ||
sandbox.close | ||
end | ||
|
||
describe "#execute" do | ||
describe "without a command" do | ||
let(:argv) { [] } | ||
|
||
it "raises an error" do | ||
expect { | ||
subject.execute | ||
}.to raise_error(VagrantPlugins::DockerProvider::Errors::ExecCommandRequired) | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters