-
Notifications
You must be signed in to change notification settings - Fork 235
/
docker.rb
256 lines (225 loc) · 8.49 KB
/
docker.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
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
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
# -*- encoding: utf-8 -*-
#
# Copyright (C) 2014, Sean Porter
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
require 'kitchen'
require 'json'
require 'uri'
require File.join(File.dirname(__FILE__), 'docker', 'erb')
module Kitchen
module Driver
# Docker driver for Kitchen.
#
# @author Sean Porter <portertech@gmail.com>
class Docker < Kitchen::Driver::SSHBase
default_config :binary, 'docker'
default_config :socket, 'unix:///var/run/docker.sock'
default_config :privileged, false
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 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
default_config :use_sudo do |driver|
!driver.remote_socket?
end
default_config :image do |driver|
driver.default_image
end
default_config :platform do |driver|
driver.default_platform
end
default_config :disable_upstart, true
def verify_dependencies
run_command("#{config[:binary]} > /dev/null", :quiet => true)
rescue
raise UserError,
'You must first install the Docker CLI tool http://www.docker.io/gettingstarted/'
end
def default_image
platform, release = instance.platform.name.split('-')
if platform == "centos" && release
release = "centos" + release.split('.').first
end
release ? [platform, release].join(':') : platform
end
def default_platform
instance.platform.name.split('-').first
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] = remote_socket? ? socket_uri.host : 'localhost'
state[:port] = container_ssh_port(state)
wait_for_sshd(state[:hostname], nil, :port => state[:port])
end
def destroy(state)
rm_container(state) if container_exists?(state)
if config[:remove_images] && state[:image_id]
rm_image(state)
end
end
def remote_socket?
config[:socket] ? socket_uri.scheme == 'tcp' : false
end
protected
def socket_uri
URI.parse(config[:socket])
end
def docker_command(cmd, options={})
docker = config[:binary].dup
docker << " -H #{config[:socket]}" if config[:socket]
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?))
end
def build_dockerfile
from = "FROM #{config[:image]}"
platform = case config[:platform]
when 'debian', 'ubuntu'
disable_upstart = <<-eos
RUN dpkg-divert --local --rename --add /sbin/initctl
RUN ln -sf /bin/true /sbin/initctl
eos
packages = <<-eos
ENV DEBIAN_FRONTEND noninteractive
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'
<<-eos
RUN yum clean all
RUN yum install -y sudo openssh-server openssh-clients which curl
RUN ssh-keygen -t rsa -f /etc/ssh/ssh_host_rsa_key -N ''
RUN ssh-keygen -t dsa -f /etc/ssh/ssh_host_dsa_key -N ''
eos
when 'arch'
<<-eos
RUN pacman -Syu --noconfirm
RUN pacman -S --noconfirm openssh sudo curl
RUN ssh-keygen -A -t rsa -f /etc/ssh/ssh_host_rsa_key
RUN ssh-keygen -A -t dsa -f /etc/ssh/ssh_host_dsa_key
eos
else
raise ActionFailed,
"Unknown platform '#{config[:platform]}'"
end
username = config[:username]
password = config[:password]
base = <<-eos
RUN useradd -d /home/#{username} -m -s /bin/bash #{username}
RUN echo #{username}:#{password} | chpasswd
RUN echo '#{username} ALL=(ALL) NOPASSWD:ALL' >> /etc/sudoers
eos
custom = ''
Array(config[:provision_command]).each do |cmd|
custom << "RUN #{cmd}\n"
end
[from, platform, base, custom].join("\n")
end
def dockerfile
if config[:dockerfile]
template = IO.read(File.expand_path(config[:dockerfile]))
context = DockerERBContext.new(config.to_hash)
ERB.new(template).result(context.get_binding)
else
build_dockerfile
end
end
def parse_image_id(output)
output.each_line do |line|
if line =~ /image id|build successful|successfully built/i
return line.split(/\s+/).last
end
end
raise ActionFailed,
'Could not parse Docker build output for image ID'
end
def build_image(state)
cmd = "build"
cmd << " --no-cache" unless config[:use_cache]
output = docker_command("#{cmd} -", :input => dockerfile)
parse_image_id(output)
end
def parse_container_id(output)
container_id = output.chomp
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 = "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}"}
Array(config[:volumes_from]).each {|container| cmd << " --volumes-from #{container}"}
cmd << " -h #{config[:hostname]}" if config[:hostname]
cmd << " -m #{config[:memory]}" if config[:memory]
cmd << " -c #{config[:cpu]}" if config[:cpu]
cmd << " -privileged" if config[:privileged]
cmd << " -e http_proxy=#{config[:http_proxy]}" if config[:http_proxy]
cmd << " -e https_proxy=#{config[:https_proxy]}" if config[:https_proxy]
cmd << " #{image_id} #{config[:run_command]}"
cmd
end
def run_container(state)
cmd = build_run_command(state[:image_id])
output = docker_command(cmd)
parse_container_id(output)
end
def inspect_container(state)
container_id = state[:container_id]
docker_command("inspect #{container_id}")
end
def container_exists?(state)
!!inspect_container(state) rescue false
end
def parse_container_ssh_port(output)
begin
info = Array(::JSON.parse(output)).first
ports = info['NetworkSettings']['Ports'] || info['HostConfig']['PortBindings']
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 SSH port'
end
end
def container_ssh_port(state)
output = inspect_container(state)
parse_container_ssh_port(output)
end
def rm_container(state)
container_id = state[:container_id]
docker_command("stop #{container_id}")
docker_command("rm #{container_id}")
end
def rm_image(state)
image_id = state[:image_id]
docker_command("rmi #{image_id}")
end
end
end
end