Class: VagrantPlugins::DockerProvider::Action::Create

Inherits:
Object
  • Object
show all
Defined in:
lib/docker-provider/action/create.rb

Instance Method Summary collapse

Constructor Details

#initialize(app, env) ⇒ Create

Returns a new instance of Create.



5
6
7
8
# File 'lib/docker-provider/action/create.rb', line 5

def initialize(app, env)
  @app = app
  @@mutex ||= Mutex.new
end

Instance Method Details

#call(env) ⇒ Object



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/docker-provider/action/create.rb', line 10

def call(env)
  @env             = env
  @machine         = env[:machine]
  @provider_config = @machine.provider_config
  @machine_config  = @machine.config
  @driver          = @machine.provider.driver

  guard_cmd_configured!

  cid = ''
  @@mutex.synchronize do
    cid = @driver.create(create_params)
  end

  @machine.id = cid
  @app.call(env)
end

#create_paramsObject



28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/docker-provider/action/create.rb', line 28

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}"

  {
    image:      @provider_config.image,
    cmd:        @provider_config.cmd,
    ports:      forwarded_ports,
    name:       container_name,
    hostname:   @machine_config.vm.hostname,
    volumes:    @provider_config.volumes,
    privileged: @provider_config.privileged
  }
end

#forwarded_portsObject



44
45
46
47
48
49
# File 'lib/docker-provider/action/create.rb', line 44

def forwarded_ports
  @env[:forwarded_ports].map do |fp|
    # TODO: Support for the protocol argument
    "#{fp[:host]}:#{fp[:guest]}"
  end.compact
end

#guard_cmd_configured!Object



51
52
53
54
55
# File 'lib/docker-provider/action/create.rb', line 51

def guard_cmd_configured!
  if ! @provider_config.image
    raise Errors::ImageNotConfiguredError, name: @machine.name
  end
end