Class: Kitchen::Driver::DockerCli
- Inherits:
-
Base
- Object
- Base
- Kitchen::Driver::DockerCli
- Defined in:
- lib/kitchen/driver/docker_cli.rb
Overview
Docker CLI driver for Kitchen.
Instance Method Summary collapse
- #build(state) ⇒ Object
- #container_name ⇒ Object
- #create(state) ⇒ Object
- #default_image ⇒ Object
- #default_platform ⇒ Object
- #destroy(state) ⇒ Object
- #docker_build_command ⇒ Object
- #docker_file ⇒ Object
- #docker_run_command(image) ⇒ Object
- #parse_container_id(output) ⇒ Object
- #parse_image_id(output) ⇒ Object
- #pre_create_command ⇒ Object
- #run(state) ⇒ Object
Instance Method Details
#build(state) ⇒ Object
96 97 98 99 100 101 102 |
# File 'lib/kitchen/driver/docker_cli.rb', line 96 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 ⇒ Object
147 148 149 150 151 152 153 |
# File 'lib/kitchen/driver/docker_cli.rb', line 147 def container_name if config[:container_name] config[:container_name] elsif config[:instance_container_name] instance.name end end |
#create(state) ⇒ Object
62 63 64 65 66 |
# File 'lib/kitchen/driver/docker_cli.rb', line 62 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 ⇒ Object
50 51 52 53 54 55 56 |
# File 'lib/kitchen/driver/docker_cli.rb', line 50 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 ⇒ Object
58 59 60 |
# File 'lib/kitchen/driver/docker_cli.rb', line 58 def default_platform instance.platform.name.split('-').first end |
#destroy(state) ⇒ Object
79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 |
# File 'lib/kitchen/driver/docker_cli.rb', line 79 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 ⇒ Object
112 113 114 115 116 117 118 119 120 121 |
# File 'lib/kitchen/driver/docker_cli.rb', line 112 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 ⇒ Object
169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 |
# File 'lib/kitchen/driver/docker_cli.rb', line 169 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) ⇒ Object
123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 |
# File 'lib/kitchen/driver/docker_cli.rb', line 123 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) ⇒ Object
162 163 164 165 166 167 |
# File 'lib/kitchen/driver/docker_cli.rb', line 162 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) ⇒ Object
155 156 157 158 159 160 |
# File 'lib/kitchen/driver/docker_cli.rb', line 155 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 ⇒ Object
68 69 70 71 72 73 74 75 76 77 |
# File 'lib/kitchen/driver/docker_cli.rb', line 68 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) ⇒ Object
104 105 106 107 108 109 110 |
# File 'lib/kitchen/driver/docker_cli.rb', line 104 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 |