Method: Beaker::Docker#provision

Defined in:
lib/beaker/hypervisor/docker.rb

#provisionObject



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/beaker/hypervisor/docker.rb', line 19

def provision
  @logger.notify "Provisioning docker"

  @hosts.each do |host|
    @logger.notify "provisioning #{host.name}"

    @logger.debug("Creating image")
    image = ::Docker::Image.build(dockerfile_for(host), { :rm => true })

    @logger.debug("Creating container from image #{image.id}")
    container = ::Docker::Container.create({
      'Image' => image.id,
      'Hostname' => host.name,
    })

    @logger.debug("Starting container #{container.id}")
    container.start({"PublishAllPorts" => true, "Privileged" => true})

    # Find out where the ssh port is from the container
    if ENV['DOCKER_HOST']
      ip = URI.parse(ENV['DOCKER_HOST']).host
      @logger.info("Using docker server at #{ip}")
    else
      ip = container.json["NetworkSettings"]["Ports"]["22/tcp"][0]["HostIp"]
    end
    port = container.json["NetworkSettings"]["Ports"]["22/tcp"][0]["HostPort"]

    forward_ssh_agent = @options[:forward_ssh_agent] || false

    # Update host metadata
    host['ip']  = ip
    host['port'] = port
    host['ssh']  = {
      :password => root_password,
      :port => port,
      :forward_agent => forward_ssh_agent,
    }

    @logger.debug("node available as  ssh -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no root@#{ip} -p #{port}")
    host['docker_container'] = container
    host['docker_image'] = image
  end
end