lib/kitchen/driver/docker.rb in kitchen-docker-2.7.0 vs lib/kitchen/driver/docker.rb in kitchen-docker-2.8.0

- old
+ new

@@ -19,10 +19,11 @@ require 'securerandom' require 'uri' require 'net/ssh' require 'tempfile' require 'shellwords' +require 'base64' require 'kitchen/driver/base' require_relative './docker/erb' @@ -54,10 +55,11 @@ default_config :wait_for_sshd, true default_config :private_key, File.join(Dir.pwd, '.kitchen', 'docker_id_rsa') default_config :public_key, File.join(Dir.pwd, '.kitchen', 'docker_id_rsa.pub') default_config :build_options, nil default_config :run_options, nil + default_config :use_internal_docker_network, false default_config :use_sudo, false default_config :image do |driver| driver.default_image @@ -117,11 +119,16 @@ generate_keys state[:username] = config[:username] state[:ssh_key] = config[:private_key] state[:image_id] = build_image(state) unless state[:image_id] state[:container_id] = run_container(state) unless state[:container_id] - state[:hostname] = remote_socket? ? socket_uri.host : 'localhost' + state[:hostname] = 'localhost' + if remote_socket? + state[:hostname] = socket_uri.host + elsif config[:use_internal_docker_network] + state[:hostname] = container_ip(state) + end state[:port] = container_ssh_port(state) if config[:wait_for_sshd] instance.transport.connection(state) do |conn| conn.wait_until_ready end @@ -129,11 +136,11 @@ end def destroy(state) rm_container(state) if container_exists?(state) if config[:remove_images] && state[:image_id] - rm_image(state) + rm_image(state) if image_exists?(state) end end def remote_socket? config[:socket] ? socket_uri.scheme == 'tcp' : false @@ -207,11 +214,11 @@ ENV container docker RUN apt-get update RUN apt-get install -y sudo openssh-server curl lsb-release eos config[:disable_upstart] ? disable_upstart + packages : packages - when 'rhel', 'centos', 'fedora' + when 'rhel', 'centos', 'fedora', 'oraclelinux' <<-eos ENV container docker RUN yum clean all RUN yum install -y sudo openssh-server openssh-clients which curl RUN [ -f "/etc/ssh/ssh_host_rsa_key" ] || ssh-keygen -t rsa -f /etc/ssh/ssh_host_rsa_key -N '' @@ -228,11 +235,11 @@ # See https://bugs.archlinux.org/task/47052 for why we # blank out limits.conf. <<-eos RUN pacman --noconfirm -Sy archlinux-keyring RUN pacman-db-upgrade - RUN pacman --noconfirm -Sy openssl openssh sudo curl + RUN pacman --noconfirm -Syu openssl openssh sudo curl RUN [ -f "/etc/ssh/ssh_host_rsa_key" ] || ssh-keygen -A -t rsa -f /etc/ssh/ssh_host_rsa_key RUN [ -f "/etc/ssh/ssh_host_dsa_key" ] || ssh-keygen -A -t dsa -f /etc/ssh/ssh_host_dsa_key RUN echo >/etc/security/limits.conf eos when 'gentoo' @@ -268,10 +275,11 @@ RUN chown -R #{username} #{homedir}/.ssh RUN chmod 0700 #{homedir}/.ssh RUN touch #{homedir}/.ssh/authorized_keys RUN chown #{username} #{homedir}/.ssh/authorized_keys RUN chmod 0600 #{homedir}/.ssh/authorized_keys + RUN mkdir -p /run/sshd eos custom = '' Array(config[:provision_command]).each do |cmd| custom << "RUN #{cmd}\n" end @@ -362,10 +370,14 @@ def container_exists?(state) state[:container_id] && !!docker_command("top #{state[:container_id]}") rescue false end + def image_exists?(state) + state[:image_id] && !!docker_command("docker inspect --type=image #{state[:image_id]}") rescue false + end + def parse_container_ssh_port(output) begin _host, port = output.split(':') port.to_i rescue @@ -374,14 +386,28 @@ end end def container_ssh_port(state) begin + if config[:use_internal_docker_network] + return 22 + end output = docker_command("port #{state[:container_id]} 22/tcp") parse_container_ssh_port(output) rescue raise ActionFailed, 'Docker reports container has no ssh port mapped' + end + end + + def container_ip(state) + begin + cmd = "inspect --format '{{ .NetworkSettings.IPAddress }}'" + cmd << " #{state[:container_id]}" + docker_command(cmd).strip + rescue + raise ActionFailed, + 'Error getting internal IP of Docker container' end end def rm_container(state) container_id = state[:container_id]