Class: Beaker::Docker

Inherits:
Hypervisor show all
Defined in:
lib/beaker/hypervisor/docker.rb

Constant Summary

Constants inherited from Hypervisor

Hypervisor::CHARMAP

Constants included from HostPrebuiltSteps

HostPrebuiltSteps::APT_CFG, HostPrebuiltSteps::CUMULUS_PACKAGES, HostPrebuiltSteps::DEBIAN_PACKAGES, HostPrebuiltSteps::ETC_HOSTS_PATH, HostPrebuiltSteps::ETC_HOSTS_PATH_SOLARIS, HostPrebuiltSteps::IPS_PKG_REPO, HostPrebuiltSteps::NTPSERVER, HostPrebuiltSteps::ROOT_KEYS_SCRIPT, HostPrebuiltSteps::ROOT_KEYS_SYNC_CMD, HostPrebuiltSteps::SLEEPWAIT, HostPrebuiltSteps::SLES_PACKAGES, HostPrebuiltSteps::TRIES, HostPrebuiltSteps::UNIX_PACKAGES, HostPrebuiltSteps::WINDOWS_PACKAGES

Instance Method Summary collapse

Methods inherited from Hypervisor

#configure, create, #generate_host_name, #proxy_package_manager, #validate

Methods included from HostPrebuiltSteps

#add_el_extras, #additive_hash_merge, #apt_get_update, #check_and_install_packages_if_needed, #construct_env, #copy_file_to_remote, #copy_ssh_to_root, #disable_iptables, #disable_se_linux, #echo_on_host, #enable_root_login, #epel_info_for, #get_domain_name, #get_ip, #hack_etc_hosts, #package_proxy, #proxy_config, #set_env, #set_etc_hosts, #sync_root_keys, #timesync, #validate_host

Methods included from Beaker::DSL::Patterns

#block_on

Constructor Details

#initialize(hosts, options) ⇒ Docker

Returns a new instance of Docker.



4
5
6
7
8
9
10
11
12
13
14
15
16
17
# File 'lib/beaker/hypervisor/docker.rb', line 4

def initialize(hosts, options)
  require 'docker'
  @options = options
  @logger = options[:logger]
  @hosts = hosts

  # increase the http timeouts as provisioning images can be slow
  ::Docker.options = { :write_timeout => 300, :read_timeout => 300 }.merge(::Docker.options || {})
  # assert that the docker-api gem can talk to your docker
  # enpoint.  Will raise if there is a version mismatch
  ::Docker.validate_version!
  # Pass on all the logging from docker-api to the beaker logger instance
  ::Docker.logger = @logger
end

Instance Method Details

#cleanupObject



63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
# File 'lib/beaker/hypervisor/docker.rb', line 63

def cleanup
  @logger.notify "Cleaning up docker"
  @hosts.each do |host|
    if container = host['docker_container']
      @logger.debug("stop container #{container.id}")
      begin
        container.stop
        sleep 2 # avoid a race condition where the root FS can't unmount
      rescue Excon::Errors::ClientError => e
        @logger.warn("stop of container #{container.id} failed: #{e.response.body}")
      end
      @logger.debug("delete container #{container.id}")
      begin
        container.delete
      rescue Excon::Errors::ClientError => e
        @logger.warn("deletion of container #{container.id} failed: #{e.response.body}")
      end
    end

    # Do not remove the image if docker_reserve_image is set to true, otherwise remove it
    if image = (host['docker_preserve_image'] ? nil : host['docker_image'])
      @logger.debug("delete image #{image.id}")
      begin
        image.delete
      rescue Excon::Errors::ClientError => e
        @logger.warn("deletion of image #{image.id} failed: #{e.response.body}")
      end
    end
  end
end

#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