Module: Dokken::Helpers
- Defined in:
- lib/kitchen/helpers.rb
Instance Method Summary collapse
- #coerce_exposed_ports(v) ⇒ Object
- #coerce_port_bindings(v) ⇒ Object
- #create_data_image ⇒ Object
- #create_sandbox ⇒ Object
- #data_dockerfile ⇒ Object
- #default_docker_host ⇒ Object
- #docker_info ⇒ Object
- #dokken_create_sandbox ⇒ Object
- #dokken_delete_sandbox ⇒ Object
- #dokken_kitchen_sandbox ⇒ Object
- #dokken_verifier_sandbox ⇒ Object
- #exposed_ports ⇒ Object
- #home_dir ⇒ Object
- #insecure_ssh_private_key ⇒ Object
- #insecure_ssh_public_key ⇒ Object
- #instance_name ⇒ Object
- #parse_port(v) ⇒ Object
- #port_bindings ⇒ Object
- #port_open?(ip, port) ⇒ Boolean
- #remote_docker_host? ⇒ Boolean
- #sandbox_dirs ⇒ Object
- #sandbox_path ⇒ Object
Instance Method Details
#coerce_exposed_ports(v) ⇒ Object
180 181 182 183 184 185 186 187 188 189 190 191 |
# File 'lib/kitchen/helpers.rb', line 180 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
193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 |
# File 'lib/kitchen/helpers.rb', line 193 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 ⇒ Object
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_sandbox ⇒ Object
258 259 260 261 262 263 |
# File 'lib/kitchen/helpers.rb', line 258 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 ⇒ Object
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 "FROM centos:7\nMAINTAINER Sean OMeara \\\"[email protected]\\\"\nENV LANG en_US.UTF-8\n\nRUN yum -y install tar rsync openssh-server passwd git\nRUN ssh-keygen -t rsa -f /etc/ssh/ssh_host_rsa_key -N ''\n\n# uncomment to debug cert issues\n# RUN echo 'root:dokkendokkendokken' | chpasswd\n# RUN sed -i 's/PermitRootLogin prohibit-password/PermitRootLogin yes/' /etc/ssh/sshd_config\n# RUN sed 's@session\\s*required\\s*pam_loginuid.so@session optional pam_loginuid.so@g' -i /etc/pam.d/sshd\n\nRUN mkdir -p /root/.ssh/\nCOPY authorized_keys /root/.ssh/authorized_keys\nRUN chmod 700 /root/.ssh/\nRUN chmod 600 /root/.ssh/authorized_keys\n\nEXPOSE 22\nCMD [ \"/usr/sbin/sshd\", \"-D\", \"-p\", \"22\", \"-o\", \"UseDNS=no\", \"-o\", \"UsePrivilegeSeparation=no\", \"-o\", \"MaxAuthTries=60\" ]\n\nVOLUME /opt/kitchen\nVOLUME /opt/verifier\n EOF\nend\n" |
#default_docker_host ⇒ Object
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_info ⇒ Object
118 119 120 121 122 123 |
# File 'lib/kitchen/helpers.rb', line 118 def docker_info @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_sandbox ⇒ Object
125 126 127 128 129 130 131 |
# File 'lib/kitchen/helpers.rb', line 125 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_sandbox ⇒ Object
133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 |
# File 'lib/kitchen/helpers.rb', line 133 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_sandbox ⇒ Object
159 160 161 |
# File 'lib/kitchen/helpers.rb', line 159 def dokken_kitchen_sandbox "#{home_dir}/.dokken/kitchen_sandbox/#{instance_name}" end |
#dokken_verifier_sandbox ⇒ Object
163 164 165 |
# File 'lib/kitchen/helpers.rb', line 163 def dokken_verifier_sandbox "#{home_dir}/.dokken/verifier_sandbox/#{instance_name}" end |
#exposed_ports ⇒ Object
172 173 174 |
# File 'lib/kitchen/helpers.rb', line 172 def exposed_ports coerce_exposed_ports(config[:ports]) end |
#home_dir ⇒ Object
149 150 151 152 153 154 155 156 157 |
# File 'lib/kitchen/helpers.rb', line 149 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_key ⇒ Object
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 "-----BEGIN RSA PRIVATE KEY-----\nMIIEpAIBAAKCAQEAqCcMlu6jYcPjU7jowuPkVaREpfiQV3m18weyasUYCgG8pqug\nd/WsxAIjceTcx5FnEJjskDYRaShUgLn3oHF0UuHLUtS3BJRKF3vW6wwKihAUpJp1\n2H2VzDSBtBJ8OPuVv62cRxQgJbFEgB2LWGRLPtwoGyFrh4WfeQvFAupxi1hzWTo3\nG7GGRa5xXztKoNHp3tsPCyocao+V9DEUnQiJ9SMMsSG5eTTQ+WJNR8fCSrCbU4jU\n3cbLIjIJjOW7022EFEn/KWxq0SN9FUCgYEPP1JInWW2k3781l2KcHA1BEUm8xGAr\n5FMrU3pmSL+roaT8apH45eihDjppf4hqqrjxKwIDAQABAoIBAEj7Cb/IOykHd/ay\nXnOXrVZuQU03oI4WyR19zbYBbPmK33IHM1JdUmqP8wpPpnMHbJALj0DX9p6JXoOw\nMwVzuGTwkuqUYAqgwbeHjDPfugNKD2uRjmwztXw3ncOl8jxZFRloJFfFKF6znWNt\nbzkh7naN3upHiv/6wsgqj4tAbZ9oRC1crO6bsNr3P6YooiG5RRNpHepiyXphyhN6\nWe1p5ZOQ/pUSE0Ca4wTlUhJHTUPMz7VFs/8CH0loRIsGPBROarPkoLVF+/UNyX8e\n+BGMhoUtQH2XvjEzWUl5jKJOnvKRIV+0j/upWXsPQKF5glVPmPrTVUAxThfu6rAa\n4Z3JveECgYEA0Pz3Hl0SlPR79r2qofh1ZNa8zvQDL+iLopULwDiil5qlUxJ+DgOl\n1kWXLhjdg/NfoTBHvBjdJu274YJgaGQOfCy5747YDVsakKOm4bI9+Jr2agshPyE6\nf1RNmGL8K8fNtpGq4G14o+pSQOPNrEfcFKgm3sosZWJAWaA64hmtiXcCgYEAzfp6\nFbodfUypAV5Zd6PCO2eJMjLdvGaNuH/Umo80WNV7XZ6iJ6MUeQe+YwxFJigjC3ii\nifLUj3kL7+wu7sEtkzS3zNd34KfhQ5fLADttfFgjjfm7IxlDD4ABaUgjwZM2gfXi\nxCwRhwwNgilF6qABJ1CLt8JSqKubkqvO1P1gQu0CgYEA0GA6AcNpYK344Eey1/bF\nDntyHKN+fglPGReldM7Dh4gBabgZid2nP+N5XtQaIpPKeQyLqgfckhEecTau68dA\nDh4Gcs6pq394GFmkbotrcPMJ2SgpySlXi1fCWrvvlbON8IiDqWxdiop74wmArFOm\nI86ZmzBYXeo+IV869vAFcPcCgYBrvvyh5OuMIc++YYZXaRgvTueblLQc22CDBItI\nFmUBmxqfTF3ycgJBlWVoFoENhq1eUMplctrx+hXeeSPLzM10VX1X79ZLdEYHv513\nD58kDk7684mKwKotr34NfqkFl2ZJ8T+f8pVwmUNvtPtX0j8IO7/6bfIjPTFyNeFJ\n1QjHuQKBgQC/LE05M4eeWXihZ7c7fyWHLyddcPdH48LRF9UH9yjKF84jXRT91uMv\nXuIb2Qt4MLHABySsk653LDw/jTIGV26c068nZryq5OUPxk67Xgod54jKgOwjgjZS\nX8N2N9ZNnORJqK374yGj1jWUU66mQhPvn49QpG8P2HEoh2RQjNvyHA==\n-----END RSA PRIVATE KEY-----\n EOF\nend\n" |
#insecure_ssh_public_key ⇒ Object
24 25 26 27 28 |
# File 'lib/kitchen/helpers.rb', line 24 def insecure_ssh_public_key "ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQCoJwyW7qNhw+NTuOjC4+RVpESl+JBXebXzB7JqxRgKAbymq6B39azEAiNx5NzHkWcQmOyQNhFpKFSAufegcXRS4ctS1LcElEoXe9brDAqKEBSkmnXYfZXMNIG0Enw4+5W/rZxHFCAlsUSAHYtYZEs+3CgbIWuHhZ95C8UC6nGLWHNZOjcbsYZFrnFfO0qg0ene2w8LKhxqj5X0MRSdCIn1IwyxIbl5NND5Yk1Hx8JKsJtTiNTdxssiMgmM5bvTbYQUSf8pbGrRI30VQKBgQ8/UkidZbaTfvzWXYpwcDUERSbzEYCvkUytTemZIv6uhpPxqkfjl6KEOOml/iGqquPEr test-kitchen-rsa\n EOF\nend\n" |
#instance_name ⇒ Object
167 168 169 170 |
# File 'lib/kitchen/helpers.rb', line 167 def instance_name prefix = (Digest::SHA2.hexdigest FileUtils.pwd)[0, 10] "#{prefix}-#{instance.name}" end |
#parse_port(v) ⇒ Object
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 |
# File 'lib/kitchen/helpers.rb', line 210 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_bindings ⇒ Object
176 177 178 |
# File 'lib/kitchen/helpers.rb', line 176 def port_bindings coerce_port_bindings(config[:ports]) end |
#port_open?(ip, port) ⇒ 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
244 245 246 247 248 |
# File 'lib/kitchen/helpers.rb', line 244 def remote_docker_host? return false if config[:docker_info]['OperatingSystem'].include?('Boot2Docker') return true if config[:docker_host_url] =~ /^tcp:/ false end |
#sandbox_dirs ⇒ Object
254 255 256 |
# File 'lib/kitchen/helpers.rb', line 254 def sandbox_dirs Dir.glob(File.join(sandbox_path, '*')) end |
#sandbox_path ⇒ Object
250 251 252 |
# File 'lib/kitchen/helpers.rb', line 250 def sandbox_path "#{Dir.home}/.dokken/verifier_sandbox/#{instance_name}" end |