providers/docker: docker-run command starting points

This commit is contained in:
Mitchell Hashimoto 2014-04-27 18:10:41 -07:00
parent 2694f746a7
commit abf7c0526b
6 changed files with 159 additions and 19 deletions

View File

@ -4,6 +4,27 @@ module VagrantPlugins
# Include the built-in modules so we can use them as top-level things.
include Vagrant::Action::Builtin
# This action starts another container just like the real one running
# but only for the purpose of running a single command rather than
# to exist long-running.
def self.action_run_command
Vagrant::Action::Builder.new.tap do |b|
b.use ConfigValidate
b.use HostMachine
b.use Call, IsState, :not_created do |env, b2|
if env[:result]
b2.use Message,
I18n.t("docker_provider.messages.not_created_original")
next
else
end
end
b.use action_start
end
end
# This action brings the "machine" up from nothing, including creating the
# container, configuring metadata, and booting.
def self.action_up
@ -198,16 +219,18 @@ module VagrantPlugins
def self.action_start
Vagrant::Action::Builder.new.tap do |b|
b.use Call, IsState, :running do |env, b2|
# If the container is running, then our work here is done, exit
next if env[:result]
# If the container is running and we're doing a run, we're done
next if env[:result] && env[:machine_action] != :run_command
b2.use Call, HasSSH do |env2, b3|
if env2[:result]
b3.use Provision
else
b3.use Message,
I18n.t("docker_provider.messages.provision_no_ssh"),
post: true
if env[:machine_action] != :run_command
b2.use Call, HasSSH do |env2, b3|
if env2[:result]
b3.use Provision
else
b3.use Message,
I18n.t("docker_provider.messages.provision_no_ssh"),
post: true
end
end
end
@ -224,7 +247,7 @@ module VagrantPlugins
b2.use PrepareNFSSettings
b2.use Build
# If the VM is NOT created yet, then do some setup steps
# If the container is NOT created yet, then do some setup steps
# necessary for creating it.
b2.use Call, IsState, :not_created do |env2, b3|
if env2[:result]
@ -240,12 +263,18 @@ module VagrantPlugins
end
end
b2.use Start
b2.use WaitForRunning
# If we're doing a one-off command, then we create
if env[:machine_action] == :run_command
b2.use SyncedFolders
b2.use Create
else
b2.use Start
b2.use WaitForRunning
b2.use Call, HasSSH do |env2, b3|
if env2[:result]
b3.use WaitForCommunicator
b2.use Call, HasSSH do |env2, b3|
if env2[:result]
b3.use WaitForCommunicator
end
end
end
end

View File

@ -15,9 +15,27 @@ module VagrantPlugins
params = create_params
# If we're running a single command, we modify the params a bit
if env[:machine_action] == :run_command
# Use the command that is given to us
params[:cmd] = env[:run_command]
# Don't detach, we want to watch the command run
params[:detach] = false
# No ports should be shared to the host
params[:ports] = []
# We link to our original container
# TODO
end
env[:ui].output(I18n.t("docker_provider.creating"))
env[:ui].detail(" Name: #{params[:name]}")
env[:ui].detail(" Image: #{params[:image]}")
if params[:cmd]
env[:ui].detail(" Cmd: #{params[:cmd].join(" ")}")
end
params[:volumes].each do |volume|
env[:ui].detail("Volume: #{volume}")
end
@ -30,10 +48,14 @@ module VagrantPlugins
cid = @driver.create(params)
env[:ui].detail(" \n"+I18n.t(
"docker_provider.created", id: cid[0...16]))
@machine.id = cid
@app.call(env)
# If this isn't just a one-off command, then save the ID
if env[:machine_action] != :run_command
env[:ui].detail(" \n"+I18n.t(
"docker_provider.created", id: cid[0...16]))
@machine.id = cid
end
@app.call(env)
end
def create_params
@ -55,6 +77,7 @@ module VagrantPlugins
{
cmd: @provider_config.cmd,
detach: true,
env: @provider_config.env,
extra_args: @provider_config.create_args,
hostname: @machine_config.vm.hostname,

View File

@ -0,0 +1,70 @@
module VagrantPlugins
module DockerProvider
module Command
class Run < Vagrant.plugin("2", :command)
def self.synopsis
"run a one-off command in the context of a container"
end
def execute
options = {}
options[:detach] = false
opts = OptionParser.new do |o|
o.banner = "Usage: vagrant docker-run [command...]"
o.separator ""
o.separator "Options:"
o.separator ""
o.on("--[no-]detach", "Run in the background") do |d|
options[:detach] = d
end
end
# Parse out the extra args to send to SSH, which is everything
# after the "--"
split_index = @argv.index("--")
if !split_index
@env.ui.error(I18n.t("docker_provider.run_command_required"))
return 1
end
command = @argv.drop(split_index + 1)
@argv = @argv.take(split_index)
# Parse the options
argv = parse_options(opts)
return if !argv
any_success = false
with_target_vms(argv) do |machine|
if machine.provider_name != :docker
machine.ui.output(I18n.t("docker_provider.not_docker_provider"))
next
end
state = machine.state
if state == :host_state_unknown
machine.ui.output(I18n.t("docker_provider.logs_host_state_unknown"))
next
elsif state == :not_created
machine.ui.output(I18n.t("docker_provider.not_created_skip"))
next
end
# At least one was run!
any_success = true
# Run it!
machine.action(
:run_command,
run_command: command,
run_detach: options[:detach])
end
return any_success ? 0 : 1
end
end
end
end
end

View File

@ -35,6 +35,7 @@ module VagrantPlugins
env = params.fetch(:env)
run_cmd = %W(docker run --name #{name} -d)
run_cmd << "-d" if params[:detach]
run_cmd += env.map { |k,v| ['-e', "#{k}=#{v}"] }
run_cmd += links.map { |k, v| ['--link', "#{k}:#{v}"] }
run_cmd += ports.map { |p| ['-p', p.to_s] }

View File

@ -28,6 +28,12 @@ module VagrantPlugins
Command::Logs
end
command("docker-run", primary: false) do
require_relative "command/run"
init!
Command::Run
end
communicator(:docker_hostvm) do
require_relative "communicator"
init!

View File

@ -37,6 +37,14 @@ en:
Container not created. Skipping.
not_docker_provider: |-
Not backed by Docker provider. Skipping.
run_command_required: |-
`vagrant docker-run` requires a command to execute. This command
must be specified after a `--` in the command line. This is used
to separate possible machine names and options from the actual
command to execute. An example is shown below:
vagrant docker-run web -- rails new .
ssh_through_host_vm: |-
SSH will be proxied through the Docker virtual machine since we're
not running Docker natively. This is just a notice, and not an error.
@ -52,6 +60,9 @@ en:
Deleting the container...
not_created: |-
The container hasn't been created yet.
not_created_original: |-
The original container hasn't been created yet. Run `vagrant up`
for this machine first.
not_running: |-
The container is not currently running.
provision_no_ssh: |-