lib/kitchen/driver/docker.rb in kitchen-docker-2.5.0 vs lib/kitchen/driver/docker.rb in kitchen-docker-2.6.0.rc.0

- old
+ new

@@ -19,20 +19,22 @@ require 'securerandom' require 'uri' require 'net/ssh' require 'tempfile' require 'shellwords' -require File.join(File.dirname(__FILE__), 'docker', 'erb') -module Kitchen +require 'kitchen/driver/base' - module Driver +require_relative './docker/erb' +module Kitchen + module Driver # Docker driver for Kitchen. # # @author Sean Porter <portertech@gmail.com> - class Docker < Kitchen::Driver::SSHBase + class Docker < Kitchen::Driver::Base + include ShellOut default_config :binary, 'docker' default_config :socket, ENV['DOCKER_HOST'] || 'unix:///var/run/docker.sock' default_config :privileged, false default_config :cap_add, nil @@ -41,11 +43,10 @@ default_config :use_cache, true default_config :remove_images, false default_config :run_command, '/usr/sbin/sshd -D -o UseDNS=no -o UsePAM=no -o PasswordAuthentication=yes ' + '-o UsePrivilegeSeparation=no -o PidFile=/tmp/sshd.pid' default_config :username, 'kitchen' - default_config :password, 'kitchen' default_config :tls, false default_config :tls_verify, false default_config :tls_cacert, nil default_config :tls_cert, nil default_config :tls_key, nil @@ -72,14 +73,24 @@ default_config :build_context do |driver| !driver.remote_socket? end + default_config :instance_name do |driver| + # Borrowed from kitchen-rackspace + [ + driver.instance.name.gsub(/\W/, ''), + (Etc.getlogin || 'nologin').gsub(/\W/, ''), + Socket.gethostname.gsub(/\W/, '')[0..20], + Array.new(8) { rand(36).to_s(36) }.join + ].join('-') + end + MUTEX_FOR_SSH_KEYS = Mutex.new def verify_dependencies - run_command("#{config[:binary]} >> #{dev_null} 2>&1", :quiet => true) + run_command("#{config[:binary]} >> #{dev_null} 2>&1", quiet: true, use_sudo: config[:use_sudo]) rescue raise UserError, 'You must first install the Docker CLI tool http://www.docker.io/gettingstarted/' end @@ -104,16 +115,21 @@ instance.platform.name.split('-').first end def create(state) 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[:port] = container_ssh_port(state) - wait_for_sshd(state[:hostname], nil, :port => state[:port]) if config[:wait_for_sshd] + if config[:wait_for_sshd] + instance.transport.connection(state) do |conn| + conn.wait_until_ready + end + end end def destroy(state) rm_container(state) if container_exists?(state) if config[:remove_images] && state[:image_id] @@ -137,11 +153,15 @@ docker << " --tls" if config[:tls] docker << " --tlsverify" if config[:tls_verify] docker << " --tlscacert=#{config[:tls_cacert]}" if config[:tls_cacert] docker << " --tlscert=#{config[:tls_cert]}" if config[:tls_cert] docker << " --tlskey=#{config[:tls_key]}" if config[:tls_key] - run_command("#{docker} #{cmd}", options.merge(:quiet => !logger.debug?)) + run_command("#{docker} #{cmd}", options.merge({ + quiet: !logger.debug?, + use_sudo: config[:use_sudo], + log_subject: Thor::Util.snake_case(self.class.to_s), + })) end def generate_keys MUTEX_FOR_SSH_KEYS.synchronize do if !File.exist?(config[:public_key]) || !File.exist?(config[:private_key]) @@ -185,10 +205,11 @@ RUN dpkg-divert --local --rename --add /sbin/initctl RUN ln -sf /bin/true /sbin/initctl eos packages = <<-eos ENV DEBIAN_FRONTEND noninteractive + 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' @@ -228,18 +249,16 @@ raise ActionFailed, "Unknown platform '#{config[:platform]}'" end username = config[:username] - password = config[:password] public_key = IO.read(config[:public_key]).strip homedir = username == 'root' ? '/root' : "/home/#{username}" base = <<-eos RUN if ! getent passwd #{username}; then \ - useradd -d #{homedir} -m -s /bin/bash #{username}; \ + useradd -d #{homedir} -m -s /bin/bash -p '*' #{username}; \ fi - RUN echo #{username}:#{password} | chpasswd RUN echo "#{username} ALL=(ALL) NOPASSWD: ALL" >> /etc/sudoers RUN echo "Defaults !requiretty" >> /etc/sudoers RUN mkdir -p #{homedir}/.ssh RUN chown -R #{username} #{homedir}/.ssh RUN chmod 0700 #{homedir}/.ssh