class Kitchen::Driver::DockerCli
Docker CLI driver for Kitchen
.
@author Masashi Terui <marcy9114@gmail.com>
Public Instance Methods
build(state)
click to toggle source
# File lib/kitchen/driver/docker_cli.rb, line 95 def build(state) output = "" instance.transport.connection(state) do |conn| output = conn.run_docker(docker_build_command, :input => docker_file) end parse_image_id(output) end
container_name()
click to toggle source
# File lib/kitchen/driver/docker_cli.rb, line 146 def container_name if config[:container_name] config[:container_name] elsif config[:instance_container_name] instance.name end end
create(state)
click to toggle source
# File lib/kitchen/driver/docker_cli.rb, line 61 def create(state) pre_create_command state[:image] = build(state) unless state[:image] state[:container_id] = run(state) unless state[:container_id] end
default_image()
click to toggle source
# File lib/kitchen/driver/docker_cli.rb, line 49 def default_image platform, version = instance.platform.name.split('-') if platform == 'centos' && version version = "centos#{version.split('.').first}" end version ? [platform, version].join(':') : platform end
default_platform()
click to toggle source
# File lib/kitchen/driver/docker_cli.rb, line 57 def default_platform instance.platform.name.split('-').first end
destroy(state)
click to toggle source
# File lib/kitchen/driver/docker_cli.rb, line 78 def destroy(state) instance.transport.connection(state) do |conn| begin if state[:container_id] output = conn.run_docker("ps -a -q -f id=#{state[:container_id]}").chomp conn.run_docker("rm -f #{state[:container_id]}") unless output.empty? end if config[:destroy_container_name] && container_name output = conn.run_docker("ps -a -q -f name=#{container_name}").chomp conn.run_docker("rm -f #{container_name}") unless output.empty? end rescue => e raise e unless conn.send(:options)[:lxc_driver] end end end
docker_build_command()
click to toggle source
# File lib/kitchen/driver/docker_cli.rb, line 111 def docker_build_command cmd = String.new('build') cmd << " --pull=#{config[:build_pull]}" if config[:build_pull] cmd << ' --no-cache' if config[:no_cache] if config[:build_context] cmd << ' .' else cmd << ' -' end end
docker_file()
click to toggle source
# File lib/kitchen/driver/docker_cli.rb, line 168 def docker_file if config[:dockerfile] file = ::Kitchen::DockerCli::DockerfileTemplate.new( config[:dockerfile_vars], config.to_hash ).result else file = ["FROM #{config[:image]}"] unless config[:skip_preparation] case config[:platform] when 'debian', 'ubuntu' file << 'RUN apt-get update' file << 'RUN apt-get -y install sudo curl tar' when 'rhel', 'centos', 'fedora' file << 'RUN yum clean all' file << 'RUN yum -y install sudo curl tar' file << 'RUN echo "Defaults:root !requiretty" >> /etc/sudoers' else # TODO: Support other distribution end end Array(config[:environment]).each { |env, value| file << "ENV #{env}=\"#{value}\"" } Array(config[:run_command]).each { |cmd| file << "RUN #{cmd}" } file.join("\n") end end
docker_run_command(image)
click to toggle source
# File lib/kitchen/driver/docker_cli.rb, line 122 def docker_run_command(image) cmd = String.new("run -d -t") cmd << " --name #{container_name}" if container_name cmd << ' -P' if config[:publish_all] cmd << " -m #{config[:memory_limit]}" if config[:memory_limit] cmd << " -c #{config[:cpu_shares]}" if config[:cpu_shares] cmd << " --security-opt #{config[:security_opt]}" if config[:security_opt] cmd << ' --privileged' if config[:privileged] cmd << " --net #{config[:network]}" if config[:network] if config[:hostname] cmd << " -h #{config[:hostname]}" elsif config[:instance_host_name] cmd << " -h #{instance.name}" end Array(config[:publish]).each { |pub| cmd << " -p #{pub}" } Array(config[:volume]).each { |vol| cmd << " -v #{vol}" } Array(config[:volumes_from]).each { |vf| cmd << " --volumes-from #{vf}" } Array(config[:link]).each { |link| cmd << " --link #{link}" } Array(config[:expose]).each { |exp| cmd << " --expose #{exp}" } Array(config[:dns]).each {|dns| cmd << " --dns #{dns}"} Array(config[:add_host]).each {|mapping| cmd << " --add-host #{mapping}"} cmd << " #{image} #{config[:command]}" end
parse_container_id(output)
click to toggle source
# File lib/kitchen/driver/docker_cli.rb, line 161 def parse_container_id(output) unless output.chomp.match(/([0-9a-z]{64})$/) raise ActionFailed, 'Could not parse CONTAINER ID.' end $1 end
parse_image_id(output)
click to toggle source
# File lib/kitchen/driver/docker_cli.rb, line 154 def parse_image_id(output) unless output.chomp.match(/Successfully built ([0-9a-z]{12})$/) raise ActionFailed, 'Could not parse IMAGE ID.' end $1 end
pre_create_command()
click to toggle source
# File lib/kitchen/driver/docker_cli.rb, line 67 def pre_create_command if config[:pre_create_command] system(config[:pre_create_command]) if $?.exitstatus.nonzero? raise ActionFailed, "pre_create_command '#{config[:pre_create_command]}' failed to execute \ (exit status #{$?.exitstatus})" end end end
run(state)
click to toggle source
# File lib/kitchen/driver/docker_cli.rb, line 103 def run(state) output = "" instance.transport.connection(state) do |conn| output = conn.run_docker(docker_run_command(state[:image])) end parse_container_id(output) end