Class: Beaker::Docker
- Inherits:
-
Hypervisor
- Object
- Hypervisor
- Beaker::Docker
- Defined in:
- lib/beaker/hypervisor/docker.rb
Constant Summary
Constants inherited from Hypervisor
Constants included from HostPrebuiltSteps
HostPrebuiltSteps::APT_CFG, HostPrebuiltSteps::CUMULUS_PACKAGES, HostPrebuiltSteps::DEBIAN_PACKAGES, HostPrebuiltSteps::ETC_HOSTS_PATH, HostPrebuiltSteps::ETC_HOSTS_PATH_SOLARIS, HostPrebuiltSteps::FREEBSD_PACKAGES, HostPrebuiltSteps::IPS_PKG_REPO, HostPrebuiltSteps::NTPSERVER, HostPrebuiltSteps::OPENBSD_PACKAGES, HostPrebuiltSteps::PSWINDOWS_PACKAGES, HostPrebuiltSteps::ROOT_KEYS_SCRIPT, HostPrebuiltSteps::ROOT_KEYS_SYNC_CMD, HostPrebuiltSteps::ROOT_KEYS_SYNC_CMD_AIX, HostPrebuiltSteps::SLEEPWAIT, HostPrebuiltSteps::SLES10_PACKAGES, HostPrebuiltSteps::SLES_PACKAGES, HostPrebuiltSteps::SOLARIS10_PACKAGES, HostPrebuiltSteps::SOLARIS11_PACKAGES, HostPrebuiltSteps::TRIES, HostPrebuiltSteps::UNIX_PACKAGES, HostPrebuiltSteps::WINDOWS_PACKAGES
Instance Method Summary collapse
- #cleanup ⇒ Object
-
#initialize(hosts, options) ⇒ Docker
constructor
Docker hypvervisor initializtion Env variables supported: DOCKER_REGISTRY: Docker registry URL DOCKER_HOST: Remote docker host.
- #provision ⇒ Object
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, #disable_updates, #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
Constructor Details
#initialize(hosts, options) ⇒ Docker
Docker hypvervisor initializtion Env variables supported: DOCKER_REGISTRY: Docker registry URL DOCKER_HOST: Remote docker host
11 12 13 14 15 16 17 18 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 |
# File 'lib/beaker/hypervisor/docker.rb', line 11 def initialize(hosts, ) require 'docker' @options = @logger = [:logger] @hosts = hosts # increase the http timeouts as provisioning images can be slow = { :write_timeout => 300, :read_timeout => 300 }.merge(::Docker. || {}) # Merge docker options from the entry in hosts file ::Docker. = .merge(@options[:docker_options] || {}) # assert that the docker-api gem can talk to your docker # enpoint. Will raise if there is a version mismatch begin ::Docker.validate_version! rescue Excon::Errors::SocketError => e raise "Docker instance not connectable.\nError was: #{e}\nCheck your DOCKER_HOST variable has been set\nIf you are on OSX or Windows, you might not have Docker Machine setup correctly: https://docs.docker.com/machine/\n" end # Pass on all the logging from docker-api to the beaker logger instance ::Docker.logger = @logger # Find out what kind of remote instance we are talking against if ::Docker.version['Version'] =~ /swarm/ @docker_type = 'swarm' unless ENV['DOCKER_REGISTRY'] raise "Using Swarm with beaker requires a private registry. Please setup the private registry and set the 'DOCKER_REGISTRY' env var" else @registry = ENV['DOCKER_REGISTRY'] end else @docker_type = 'docker' end end |
Instance Method Details
#cleanup ⇒ Object
142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 |
# File 'lib/beaker/hypervisor/docker.rb', line 142 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}") rescue ::Docker::Error::DockerError => e @logger.warn("deletion of image #{image.id} caused internal Docker error: #{e.}") end end end end |
#provision ⇒ Object
46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 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 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 |
# File 'lib/beaker/hypervisor/docker.rb', line 46 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 }) if @docker_type == 'swarm' image_name = "#{@registry}/beaker/#{image.id}" ret = ::Docker::Image.search(:term => image_name) if ret.first.nil? @logger.debug("Image does not exist on registry. Pushing.") image.tag({:repo => image_name, :force => true}) image.push end else image_name = image.id end container_opts = { 'Image' => image_name, 'Hostname' => host.name, } container = find_container(host) # If the specified container exists, then use it rather creating a new one if container.nil? unless host['mount_folders'].nil? container_opts['HostConfig'] ||= {} container_opts['HostConfig']['Binds'] = host['mount_folders'].values.map do |mount| a = [ File.(mount['host_path']), mount['container_path'] ] a << mount['opts'] if mount.has_key?('opts') a.join(':') end end if @options[:provision] if host['docker_container_name'] container_opts['name'] = host['docker_container_name'] end @logger.debug("Creating container from image #{image_name}") container = ::Docker::Container.create(container_opts) end end if container.nil? raise RuntimeError, 'Cannot continue because no existing container ' + 'could be found and provisioning is disabled.' end fix_ssh(container) if @options[:provision] == false @logger.debug("Starting container #{container.id}") container.start({"PublishAllPorts" => true, "Privileged" => true}) # Find out where the ssh port is from the container # When running on swarm DOCKER_HOST points to the swarm manager so we have to get the # IP of the swarm slave via the container data # When we are talking to a normal docker instance DOCKER_HOST can point to a remote docker instance. # Talking against a remote docker host which is a normal docker host if @docker_type == 'docker' && ENV['DOCKER_HOST'] ip = URI.parse(ENV['DOCKER_HOST']).host else # Swarm or local docker host ip = container.json["NetworkSettings"]["Ports"]["22/tcp"][0]["HostIp"] end @logger.info("Using docker server at #{ip}") 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 host['vm_ip'] = container.json["NetworkSettings"]["IPAddress"].to_s end hack_etc_hosts @hosts, @options end |