2014-03-26 23:45:46 +00:00
|
|
|
module VagrantPlugins
|
|
|
|
module DockerProvider
|
|
|
|
module Action
|
|
|
|
class Create
|
|
|
|
def initialize(app, env)
|
|
|
|
@app = app
|
|
|
|
end
|
|
|
|
|
|
|
|
def call(env)
|
|
|
|
@env = env
|
|
|
|
@machine = env[:machine]
|
|
|
|
@provider_config = @machine.provider_config
|
|
|
|
@machine_config = @machine.config
|
|
|
|
@driver = @machine.provider.driver
|
|
|
|
|
2014-04-11 01:26:19 +00:00
|
|
|
params = create_params
|
|
|
|
|
2014-04-28 01:10:41 +00:00
|
|
|
# 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
|
|
|
|
|
2014-04-20 15:17:19 +00:00
|
|
|
env[:ui].output(I18n.t("docker_provider.creating"))
|
|
|
|
env[:ui].detail(" Name: #{params[:name]}")
|
|
|
|
env[:ui].detail(" Image: #{params[:image]}")
|
2014-04-28 01:10:41 +00:00
|
|
|
if params[:cmd]
|
|
|
|
env[:ui].detail(" Cmd: #{params[:cmd].join(" ")}")
|
|
|
|
end
|
2014-04-20 15:17:19 +00:00
|
|
|
params[:volumes].each do |volume|
|
|
|
|
env[:ui].detail("Volume: #{volume}")
|
2014-03-26 23:45:46 +00:00
|
|
|
end
|
2014-04-20 15:17:19 +00:00
|
|
|
params[:ports].each do |pair|
|
|
|
|
env[:ui].detail(" Port: #{pair}")
|
|
|
|
end
|
|
|
|
params[:links].each do |name, other|
|
|
|
|
env[:ui].detail(" Link: #{name}:#{other}")
|
|
|
|
end
|
|
|
|
|
2014-04-28 01:10:41 +00:00
|
|
|
if env[:machine_action] != :run_command
|
2014-04-28 01:23:31 +00:00
|
|
|
# For regular "ups" create it and get the CID
|
|
|
|
cid = @driver.create(params)
|
2014-04-28 01:10:41 +00:00
|
|
|
env[:ui].detail(" \n"+I18n.t(
|
|
|
|
"docker_provider.created", id: cid[0...16]))
|
|
|
|
@machine.id = cid
|
2014-04-28 01:23:31 +00:00
|
|
|
elsif params[:detach]
|
|
|
|
env[:ui].detail(" \n"+I18n.t("docker_provider.running_detached"))
|
|
|
|
else
|
|
|
|
# For run commands, we run it and stream back the output
|
2014-04-28 01:26:12 +00:00
|
|
|
env[:ui].detail(" \n"+I18n.t("docker_provider.running")+"\n ")
|
2014-04-28 01:23:31 +00:00
|
|
|
@driver.create(params) do |type, data|
|
|
|
|
env[:ui].detail(data)
|
|
|
|
end
|
2014-04-28 01:10:41 +00:00
|
|
|
end
|
|
|
|
|
2014-04-28 01:58:57 +00:00
|
|
|
@app.call(env)
|
2014-03-26 23:45:46 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def create_params
|
2014-04-18 01:40:04 +00:00
|
|
|
container_name = @provider_config.name
|
|
|
|
if !container_name
|
|
|
|
container_name = "#{@env[:root_path].basename.to_s}_#{@machine.name}"
|
|
|
|
container_name.gsub!(/[^-a-z0-9_]/i, "")
|
|
|
|
container_name << "_#{Time.now.to_i}"
|
|
|
|
end
|
2014-03-26 23:45:46 +00:00
|
|
|
|
2014-04-18 22:53:12 +00:00
|
|
|
image = @env[:create_image]
|
|
|
|
image ||= @provider_config.image
|
|
|
|
|
2014-04-18 01:33:46 +00:00
|
|
|
links = {}
|
|
|
|
@provider_config._links.each do |link|
|
|
|
|
parts = link.split(":", 2)
|
|
|
|
links[parts[0]] = parts[1]
|
|
|
|
end
|
|
|
|
|
2014-03-26 23:45:46 +00:00
|
|
|
{
|
|
|
|
cmd: @provider_config.cmd,
|
2014-04-28 01:10:41 +00:00
|
|
|
detach: true,
|
2014-04-18 01:07:57 +00:00
|
|
|
env: @provider_config.env,
|
2014-04-28 01:37:25 +00:00
|
|
|
expose: @provider_config.expose,
|
2014-04-11 01:26:19 +00:00
|
|
|
extra_args: @provider_config.create_args,
|
2014-03-26 23:45:46 +00:00
|
|
|
hostname: @machine_config.vm.hostname,
|
2014-04-18 22:53:12 +00:00
|
|
|
image: image,
|
2014-04-18 01:33:46 +00:00
|
|
|
links: links,
|
2014-04-11 01:26:19 +00:00
|
|
|
name: container_name,
|
|
|
|
ports: forwarded_ports,
|
|
|
|
privileged: @provider_config.privileged,
|
2014-03-26 23:45:46 +00:00
|
|
|
volumes: @provider_config.volumes,
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
|
|
|
def forwarded_ports
|
2014-04-17 21:42:20 +00:00
|
|
|
mappings = {}
|
|
|
|
@machine.config.vm.networks.each do |type, options|
|
2014-04-17 22:05:50 +00:00
|
|
|
if type == :forwarded_port
|
2014-04-17 21:42:20 +00:00
|
|
|
mappings[options[:host]] = options
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
mappings.values.map do |fp|
|
2014-03-26 23:45:46 +00:00
|
|
|
# TODO: Support for the protocol argument
|
|
|
|
"#{fp[:host]}:#{fp[:guest]}"
|
|
|
|
end.compact
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|