2014-03-26 23:45:46 +00:00
|
|
|
module VagrantPlugins
|
|
|
|
module DockerProvider
|
|
|
|
module Action
|
|
|
|
class Create
|
|
|
|
def initialize(app, env)
|
|
|
|
@app = app
|
|
|
|
@@mutex ||= Mutex.new
|
|
|
|
end
|
|
|
|
|
|
|
|
def call(env)
|
|
|
|
@env = env
|
|
|
|
@machine = env[:machine]
|
|
|
|
@provider_config = @machine.provider_config
|
|
|
|
@machine_config = @machine.config
|
|
|
|
@driver = @machine.provider.driver
|
|
|
|
|
|
|
|
guard_cmd_configured!
|
|
|
|
|
2014-04-11 01:26:19 +00:00
|
|
|
params = create_params
|
|
|
|
|
2014-03-26 23:45:46 +00:00
|
|
|
cid = ''
|
|
|
|
@@mutex.synchronize do
|
2014-04-11 01:26:19 +00:00
|
|
|
env[:ui].output(I18n.t("docker_provider.creating"))
|
2014-04-16 18:59:08 +00:00
|
|
|
env[:ui].detail(" Name: #{params[:name]}")
|
|
|
|
env[:ui].detail(" Image: #{params[:image]}")
|
|
|
|
params[:volumes].each do |volume|
|
|
|
|
env[:ui].detail("Volume: #{volume}")
|
|
|
|
end
|
2014-04-17 21:42:20 +00:00
|
|
|
params[:ports].each do |pair|
|
|
|
|
env[:ui].detail(" Port: #{pair}")
|
|
|
|
end
|
2014-04-11 01:26:19 +00:00
|
|
|
|
|
|
|
cid = @driver.create(params)
|
2014-03-26 23:45:46 +00:00
|
|
|
end
|
|
|
|
|
2014-04-11 01:26:19 +00:00
|
|
|
env[:ui].detail(" \n"+I18n.t(
|
|
|
|
"docker_provider.created", id: cid[0...16]))
|
2014-03-26 23:45:46 +00:00
|
|
|
@machine.id = cid
|
|
|
|
@app.call(env)
|
|
|
|
end
|
|
|
|
|
|
|
|
def create_params
|
|
|
|
container_name = "#{@env[:root_path].basename.to_s}_#{@machine.name}"
|
|
|
|
container_name.gsub!(/[^-a-z0-9_]/i, "")
|
|
|
|
container_name << "_#{Time.now.to_i}"
|
|
|
|
|
|
|
|
{
|
|
|
|
cmd: @provider_config.cmd,
|
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-11 01:26:19 +00:00
|
|
|
image: @provider_config.image,
|
|
|
|
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|
|
|
|
|
if type == :forwarded_port && options[:id] != 'ssh'
|
|
|
|
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
|
|
|
|
|
|
|
|
def guard_cmd_configured!
|
|
|
|
if ! @provider_config.image
|
|
|
|
raise Errors::ImageNotConfiguredError, name: @machine.name
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|