Module: Dokken::Helpers

Defined in:
lib/kitchen/helpers.rb

Instance Method Summary collapse

Instance Method Details

#coerce_exposed_ports(v) ⇒ Object



177
178
179
180
181
182
183
184
185
186
187
188
# File 'lib/kitchen/helpers.rb', line 177

def coerce_exposed_ports(v)
  case v
  when Hash, nil
    v
  else
    x = Array(v).map { |a| parse_port(a) }
    x.flatten!
    x.each_with_object({}) do |y, h|
      h[y['container_port']] = {}
    end
  end
end

#coerce_port_bindings(v) ⇒ Object



190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
# File 'lib/kitchen/helpers.rb', line 190

def coerce_port_bindings(v)
  case v
  when Hash, nil
    v
  else
    x = Array(v).map { |a| parse_port(a) }
    x.flatten!
    x.each_with_object({}) do |y, h|
      h[y['container_port']] = [] unless h[y['container_port']]
      h[y['container_port']] << {
        'HostIp' => y['host_ip'],
        'HostPort' => y['host_port'],
      }
    end
  end
end

#create_data_imageObject



89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
# File 'lib/kitchen/helpers.rb', line 89

def create_data_image
  return if ::Docker::Image.exist?(data_image)

  tmpdir = Dir.tmpdir
  FileUtils.mkdir_p "#{tmpdir}/dokken"
  File.write("#{tmpdir}/dokken/Dockerfile", data_dockerfile)
  File.write("#{tmpdir}/dokken/authorized_keys", insecure_ssh_public_key)

  i = ::Docker::Image.build_from_dir(
    "#{tmpdir}/dokken",
    'nocache' => true,
    'rm' => true
  )
  i.tag('repo' => repo(data_image), 'tag' => tag(data_image), 'force' => true)
end

#create_sandboxObject



255
256
257
258
259
260
# File 'lib/kitchen/helpers.rb', line 255

def create_sandbox
  info("Creating kitchen sandbox in #{sandbox_path}")
  unless ::Dir.exist?(sandbox_path)
    FileUtils.mkdir_p(sandbox_path, :mode => 0o755)
  end
end

#data_dockerfileObject



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
# File 'lib/kitchen/helpers.rb', line 62

def data_dockerfile
  <<-EOF
FROM centos:7
MAINTAINER Sean OMeara \"[email protected]\"
ENV LANG en_US.UTF-8

RUN yum -y install tar rsync openssh-server passwd git
RUN ssh-keygen -t rsa -f /etc/ssh/ssh_host_rsa_key -N ''

# uncomment to debug cert issues
# RUN echo 'root:dokkendokkendokken' | chpasswd
# RUN sed -i 's/PermitRootLogin prohibit-password/PermitRootLogin yes/' /etc/ssh/sshd_config
# RUN sed 's@session\s*required\s*pam_loginuid.so@session optional pam_loginuid.so@g' -i /etc/pam.d/sshd

RUN mkdir -p /root/.ssh/
COPY authorized_keys /root/.ssh/authorized_keys
RUN chmod 700 /root/.ssh/
RUN chmod 600  /root/.ssh/authorized_keys

EXPOSE 22
CMD [ "/usr/sbin/sshd", "-D", "-p", "22", "-o", "UseDNS=no", "-o", "UsePrivilegeSeparation=no", "-o", "MaxAuthTries=60" ]

VOLUME /opt/kitchen
VOLUME /opt/verifier
EOF
end

#default_docker_hostObject



105
106
107
108
109
110
111
112
113
114
115
116
# File 'lib/kitchen/helpers.rb', line 105

def default_docker_host
  if ENV['DOCKER_HOST']
    ENV['DOCKER_HOST']
  elsif File.exist?('/var/run/docker.sock')
    'unix:///var/run/docker.sock'
  # TODO: Docker for Windows also operates over a named pipe at
  # //./pipe/docker_engine that can be used if named pipe support is
  # added to the docker-api gem.
  else
    'tcp://127.0.0.1:2375'
  end
end

#docker_infoObject



118
119
120
# File 'lib/kitchen/helpers.rb', line 118

def docker_info
  @docker_info ||= ::Docker.info
end

#dokken_create_sandboxObject



122
123
124
125
126
127
128
# File 'lib/kitchen/helpers.rb', line 122

def dokken_create_sandbox
  info("Creating kitchen sandbox at #{dokken_kitchen_sandbox}")
  FileUtils.mkdir_p(dokken_kitchen_sandbox, :mode => 0o755)

  info("Creating verifier sandbox at #{dokken_verifier_sandbox}")
  FileUtils.mkdir_p(dokken_verifier_sandbox, :mode => 0o755)
end

#dokken_delete_sandboxObject



130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
# File 'lib/kitchen/helpers.rb', line 130

def dokken_delete_sandbox
  info("Deleting kitchen sandbox at #{dokken_kitchen_sandbox}")
  begin
    FileUtils.rm_rf(dokken_kitchen_sandbox)
  rescue Errno::ENOENT
    debug("Cannot delete #{dokken_kitchen_sandbox}. Does not exist")
  end

  info("Deleting verifier sandbox at #{dokken_verifier_sandbox}")
  begin
    FileUtils.rm_rf(dokken_verifier_sandbox)
  rescue Errno::ENOENT
    debug("Cannot delete #{dokken_verifier_sandbox}. Does not exist")
  end
end

#dokken_kitchen_sandboxObject



156
157
158
# File 'lib/kitchen/helpers.rb', line 156

def dokken_kitchen_sandbox
  "#{home_dir}/.dokken/kitchen_sandbox/#{instance_name}"
end

#dokken_verifier_sandboxObject



160
161
162
# File 'lib/kitchen/helpers.rb', line 160

def dokken_verifier_sandbox
  "#{home_dir}/.dokken/verifier_sandbox/#{instance_name}"
end

#exposed_portsObject



169
170
171
# File 'lib/kitchen/helpers.rb', line 169

def exposed_ports
  coerce_exposed_ports(config[:ports])
end

#home_dirObject



146
147
148
149
150
151
152
153
154
# File 'lib/kitchen/helpers.rb', line 146

def home_dir
  # while dokken_binds avoid invalid bind mount spec "C:/Users/..." error by
  # remote docker host virtual box shared folder on boot2docker created by docker-machine in Windows
  # refs:
  # https://github.com/docker/machine/issues/1814
  # https://github.com/docker/toolbox/issues/607
  return Dir.home.sub 'C:/Users', '/c/Users' if Dir.home =~ /^C:/ && remote_docker_host?
  Dir.home
end

#insecure_ssh_private_keyObject



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
# File 'lib/kitchen/helpers.rb', line 30

def insecure_ssh_private_key
  <<-EOF
-----BEGIN RSA PRIVATE KEY-----
MIIEpAIBAAKCAQEAqCcMlu6jYcPjU7jowuPkVaREpfiQV3m18weyasUYCgG8pqug
d/WsxAIjceTcx5FnEJjskDYRaShUgLn3oHF0UuHLUtS3BJRKF3vW6wwKihAUpJp1
2H2VzDSBtBJ8OPuVv62cRxQgJbFEgB2LWGRLPt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PcCgYBrvvyh5OuMIc++YYZXaRgvTueblLQc22CDBItI
FmUBmxqfTF3ycgJBlWVoFoENhq1eUMplctrx+hXeeSPLzM10VX1X79ZLdEYHv513
D58kDk7684mKwKotr34NfqkFl2ZJ8T+f8pVwmUNvtPtX0j8IO7/6bfIjPTFyNeFJ
1QjHuQKBgQC/LE05M4eeWXihZ7c7fyWHLyddcPdH48LRF9UH9yjKF84jXRT91uMv
XuIb2Qt4MLHABySsk653LDw/jTIGV26c068nZryq5OUPxk67Xgod54jKgOwjgjZS
X8N2N9ZNnORJqK374yGj1jWUU66mQhPvn49QpG8P2HEoh2RQjNvyHA==
-----END RSA PRIVATE KEY-----
EOF
end

#insecure_ssh_public_keyObject



24
25
26
27
28
# File 'lib/kitchen/helpers.rb', line 24

def insecure_ssh_public_key
  <<-EOF
ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQCoJwyW7qNhw+NTuOjC4+RVpESl+JBXebXzB7JqxRgKAbymq6B39azEAiNx5NzHkWcQmOyQNhFpKFSAufegcXRS4ctS1LcElEoXe9brDAqKEBSkmnXYfZXMNIG0Enw4+5W/rZxHFCAlsUSAHYtYZEs+3CgbIWuHhZ95C8UC6nGLWHNZOjcbsYZFrnFfO0qg0ene2w8LKhxqj5X0MRSdCIn1IwyxIbl5NND5Yk1Hx8JKsJtTiNTdxssiMgmM5bvTbYQUSf8pbGrRI30VQKBgQ8/UkidZbaTfvzWXYpwcDUERSbzEYCvkUytTemZIv6uhpPxqkfjl6KEOOml/iGqquPEr test-kitchen-rsa
EOF
end

#instance_nameObject



164
165
166
167
# File 'lib/kitchen/helpers.rb', line 164

def instance_name
  prefix = (Digest::SHA2.hexdigest FileUtils.pwd)[0, 10]
  "#{prefix}-#{instance.name}"
end

#parse_port(v) ⇒ Object



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
# File 'lib/kitchen/helpers.rb', line 207

def parse_port(v)
  parts = v.split(':')
  case parts.length
  when 3
    host_ip = parts[0]
    host_port = parts[1]
    container_port = parts[2]
  when 2
    host_ip = '0.0.0.0'
    host_port = parts[0]
    container_port = parts[1]
  when 1
    host_ip = ''
    host_port = ''
    container_port = parts[0]
  end
  port_range, protocol = container_port.split('/')
  if port_range.include?('-')
    port_range = container_port.split('-')
    port_range.map!(&:to_i)
    Chef::Log.fatal("FATAL: Invalid port range! #{container_port}") if port_range[0] > port_range[1]
    port_range = (port_range[0]..port_range[1]).to_a
  end
  # qualify the port-binding protocol even when it is implicitly tcp #427.
  protocol = 'tcp' if protocol.nil?
  Array(port_range).map do |port|
    {
      'host_ip' => host_ip,
      'host_port' => host_port,
      'container_port' => "#{port}/#{protocol}",
    }
  end
end

#port_bindingsObject



173
174
175
# File 'lib/kitchen/helpers.rb', line 173

def port_bindings
  coerce_port_bindings(config[:ports])
end

#port_open?(ip, port) ⇒ Boolean

Returns:

  • (Boolean)


7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
# File 'lib/kitchen/helpers.rb', line 7

def port_open?(ip, port)
  begin
    Timeout.timeout(1) do
      begin
        s = TCPSocket.new(ip, port)
        s.close
        return true
      rescue Errno::ECONNREFUSED, Errno::EHOSTUNREACH
        return false
      end
    end
  rescue Timeout::Error
  end

  false
end

#remote_docker_host?Boolean

Returns:

  • (Boolean)


241
242
243
244
245
# File 'lib/kitchen/helpers.rb', line 241

def remote_docker_host?
  return false if config[:docker_info]['OperatingSystem'].include?('Boot2Docker')
  return true if config[:docker_host_url] =~ /^tcp:/
  false
end

#sandbox_dirsObject



251
252
253
# File 'lib/kitchen/helpers.rb', line 251

def sandbox_dirs
  Dir.glob(File.join(sandbox_path, '*'))
end

#sandbox_pathObject



247
248
249
# File 'lib/kitchen/helpers.rb', line 247

def sandbox_path
  "#{Dir.home}/.dokken/verifier_sandbox/#{instance_name}"
end