lib/kitchen/driver/docker.rb in kitchen-docker-0.8.1 vs lib/kitchen/driver/docker.rb in kitchen-docker-0.9.0

- old
+ new

@@ -24,19 +24,20 @@ # Docker driver for Kitchen. # # @author Sean Porter <portertech@gmail.com> class Docker < Kitchen::Driver::SSHBase - default_config :port, '22' default_config :username, 'kitchen' default_config :password, 'kitchen' default_config :require_chef_omnibus, true default_config :remove_images, false default_config :use_sudo, true + default_config :image do |driver| driver.default_image end + default_config :platform do |driver| driver.default_platform end def verify_dependencies @@ -56,12 +57,13 @@ end def create(state) state[:image_id] = build_image(state) unless state[:image_id] state[:container_id] = run_container(state) unless state[:container_id] - state[:hostname] = container_address(state) unless state[:hostname] - wait_for_sshd(state[:hostname]) + state[:hostname] = remote_socket? ? socket_uri.host : 'localhost' + state[:port] = container_ssh_port(state) + wait_for_sshd(state[:hostname], :port => state[:port]) end def destroy(state) rm_container(state) if state[:container_id] if config[:remove_images] && state[:image_id] @@ -69,10 +71,24 @@ end end protected + def socket_uri + URI.parse(config[:socket]) + end + + def remote_socket? + config[:socket] ? socket_uri.scheme == 'tcp' : false + end + + def docker_command(cmd, options={}) + docker = "docker" + docker << " -H #{config[:socket]}" if config[:socket] + run_command("#{docker} #{cmd}", options) + end + def dockerfile from = "FROM #{config[:image]}" platform = case config[:platform] when 'debian', 'ubuntu' <<-eos @@ -117,25 +133,25 @@ raise ActionFailed, 'Could not parse Docker build output for image ID' end def build_image(state) - output = run_command("docker build -", :input => dockerfile) + output = docker_command("build -", :input => dockerfile) parse_image_id(output) end def parse_container_id(output) container_id = output.chomp - unless container_id.size == 12 + unless [12, 64].include?(container_id.size) raise ActionFailed, 'Could not parse Docker run output for container ID' end container_id end def build_run_command(image_id) - cmd = 'docker run -d' + cmd = "run -d -p 22" Array(config[:forward]).each {|port| cmd << " -p #{port}"} Array(config[:dns]).each {|dns| cmd << " -dns #{dns}"} Array(config[:volume]).each {|volume| cmd << " -v #{volume}"} cmd << " -m #{config[:memory]}" if config[:memory] cmd << " -c #{config[:cpu]}" if config[:cpu] @@ -143,39 +159,40 @@ cmd end def run_container(state) cmd = build_run_command(state[:image_id]) - output = run_command(cmd) + output = docker_command(cmd) parse_container_id(output) end - def parse_container_ip(output) + def parse_container_ssh_port(output) begin info = Array(::JSON.parse(output)).first - settings = info['NetworkSettings'] - settings['IpAddress'] || settings['IPAddress'] + ports = info['NetworkSettings']['Ports'] + ssh_port = ports['22/tcp'].detect {|port| port['HostIp'] == '0.0.0.0'} + ssh_port['HostPort'].to_i rescue raise ActionFailed, - 'Could not parse Docker inspect output for container IP address' + 'Could not parse Docker inspect output for container SSH port' end end - def container_address(state) + def container_ssh_port(state) container_id = state[:container_id] - output = run_command("docker inspect #{container_id}") - parse_container_ip(output) + output = docker_command("inspect #{container_id}") + parse_container_ssh_port(output) end def rm_container(state) container_id = state[:container_id] - run_command("docker stop #{container_id}") - run_command("docker rm #{container_id}") + docker_command("stop #{container_id}") + docker_command("rm #{container_id}") end def rm_image(state) image_id = state[:image_id] - run_command("docker rmi #{image_id}") + docker_command("rmi #{image_id}") end end end end