Module: Dokken::Helpers

Defined in:
lib/kitchen/helpers.rb

Instance Method Summary collapse

Instance Method Details

#coerce_exposed_ports(v) ⇒ Object


199
200
201
202
203
204
205
206
207
208
209
210
# File 'lib/kitchen/helpers.rb', line 199

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


212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
# File 'lib/kitchen/helpers.rb', line 212

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_image(registry) ⇒ Object


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

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

  tmpdir = Dir.tmpdir
  FileUtils.mkdir_p "#{tmpdir}/dokken"
  File.write("#{tmpdir}/dokken/Dockerfile", data_dockerfile(registry))
  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


279
280
281
282
283
284
# File 'lib/kitchen/helpers.rb', line 279

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_dockerfile(registry) ⇒ Object


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

def data_dockerfile(registry)
  from = "centos:7"
  if registry
    from = "#{registry}/#{from}"
  end
  <<-EOF
FROM #{from}
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 '[email protected]\s*required\s*[email protected] optional [email protected]' -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


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

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


119
120
121
122
123
124
125
126
# File 'lib/kitchen/helpers.rb', line 119

def docker_info
  ::Docker.url = default_docker_host

  @docker_info ||= ::Docker.info
rescue Excon::Error::Socket
  puts "kitchen-dokken could not connect to the docker host at #{default_docker_host}. Is docker running?"
  exit!
end

#dokken_create_sandboxObject


128
129
130
131
132
133
134
# File 'lib/kitchen/helpers.rb', line 128

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


136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
# File 'lib/kitchen/helpers.rb', line 136

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


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

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

#dokken_verifier_sandboxObject


167
168
169
# File 'lib/kitchen/helpers.rb', line 167

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

#exposed_portsObject


176
177
178
# File 'lib/kitchen/helpers.rb', line 176

def exposed_ports
  coerce_exposed_ports(config[:ports])
end

#home_dirObject


152
153
154
155
156
157
158
159
160
161
# File 'lib/kitchen/helpers.rb', line 152

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


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

def insecure_ssh_private_key
  <<-EOF
-----BEGIN RSA PRIVATE KEY-----
MIIEpAIBAA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NpYK344Eey1/bF
DntyHKN+fglPGReldM7Dh4gBabgZid2nP+N5XtQaIpPKeQyLqgfckhEecTau68dA
Dh4Gcs6pq394GFmkbotrcPMJ2SgpySlXi1fCWrvvlbON8IiDqWxdiop74wmArFOm
I86ZmzBYXeo+IV869vAFcPcCgYBrvvyh5OuMIc++YYZXaRgvTueblLQc22CDBItI
FmUBmxqfTF3ycgJBlWVoFoENhq1eUMplctrx+hXeeSPLzM10VX1X79ZLdEYHv513
D58kDk7684mKwKotr34NfqkFl2ZJ8T+f8pVwmUNvtPtX0j8IO7/6bfIjPTFyNeFJ
1QjHuQKBgQC/LE05M4eeWXihZ7c7fyWHLyddcPdH48LRF9UH9yjKF84jXRT91uMv
XuIb2Qt4MLHABySsk653LDw/jTIGV26c068nZryq5OUPxk67Xgod54jKgOwjgjZS
X8N2N9ZNnORJqK374yGj1jWUU66mQhPvn49QpG8P2HEoh2RQjNvyHA==
-----END RSA PRIVATE KEY-----
  EOF
end

#insecure_ssh_public_keyObject


21
22
23
24
25
# File 'lib/kitchen/helpers.rb', line 21

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

#instance_nameObject


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

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

#network_settingsObject


180
181
182
183
184
185
186
187
188
189
190
191
192
193
# File 'lib/kitchen/helpers.rb', line 180

def network_settings
  if self[:ipv6]
    {
      "EnableIPv6" => true,
      "IPAM" => {
        "Config" => [{
          "Subnet" => self[:ipv6_subnet],
        }],
      },
    }
  else
    {}
  end
end

#parse_port(v) ⇒ Object


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
257
258
259
260
261
# File 'lib/kitchen/helpers.rb', line 229

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


195
196
197
# File 'lib/kitchen/helpers.rb', line 195

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

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

#remote_docker_host?Boolean

Returns:

  • (Boolean)

263
264
265
266
267
268
269
# File 'lib/kitchen/helpers.rb', line 263

def remote_docker_host?
  return false if config[:docker_info]["OperatingSystem"].include?("Docker Desktop")
  return false if config[:docker_info]["OperatingSystem"].include?("Boot2Docker")
  return true if /^tcp:/.match?(config[:docker_host_url])

  false
end

#sandbox_dirsObject


275
276
277
# File 'lib/kitchen/helpers.rb', line 275

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

#sandbox_pathObject


271
272
273
# File 'lib/kitchen/helpers.rb', line 271

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