Skip to content

Commit 5597110

Browse files
committed
create an image, create a container, get its ip, let kitchen do its thing
1 parent f4c03cd commit 5597110

2 files changed

Lines changed: 69 additions & 0 deletions

File tree

lib/kitchen/driver/docker.rb

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,11 +27,67 @@ module Driver
2727
# @author Sean Porter <portertech@gmail.com>
2828
class Docker < Kitchen::Driver::SSHBase
2929

30+
default_config :image, "ubuntu"
31+
default_config :username, "kitchen"
32+
default_config :password, "kitchen"
33+
3034
def create(state)
35+
state[:image_id] = create_image(state)
36+
state[:container_id] = run_container(state)
37+
state[:hostname] = container_address(state)
38+
wait_for_sshd(state[:hostname])
3139
end
3240

3341
def destroy(state)
3442
end
43+
44+
protected
45+
46+
def dockerfile
47+
path = File.join(File.dirname(__FILE__), "docker", "Dockerfile")
48+
File.expand_path(path)
49+
end
50+
51+
def parse_image_id(output)
52+
output.each_line do |line|
53+
if line =~ /image id/i
54+
return line.split(/\s+/).last
55+
end
56+
end
57+
raise ActionFailed, "Could not parse Docker build output for image ID"
58+
end
59+
60+
def create_image(state)
61+
output = run_command("cat #{dockerfile} | docker build -")
62+
parse_image_id(output)
63+
end
64+
65+
def parse_container_id(output)
66+
container_id = output.chomp
67+
unless container_id.size == 12
68+
raise ActionFailed, "Could not parse Docker run output for container ID"
69+
end
70+
container_id
71+
end
72+
73+
def run_container(state)
74+
output = run_command("docker run -d #{state[:image_id]} /usr/sbin/sshd -D")
75+
parse_container_id(output)
76+
end
77+
78+
def parse_container_ip(output)
79+
begin
80+
info = JSON.parse(output)
81+
info["NetworkSettings"]["IpAddress"]
82+
rescue
83+
raise ActionFailed, "Could not parse Docker inspect output for container IP address"
84+
end
85+
end
86+
87+
def container_address(state)
88+
output = run_command("docker inspect #{state[:container_id]}")
89+
parse_container_ip(output)
90+
end
3591
end
3692
end
3793
end
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
FROM ubuntu
2+
3+
ENV DEBIAN_FRONTEND noninteractive
4+
5+
RUN apt-get update
6+
7+
RUN apt-get install -y openssh-server
8+
9+
RUN mkdir /var/run/sshd
10+
11+
RUN useradd -d /home/kitchen -m -s /bin/bash kitchen
12+
13+
RUN echo kitchen:kitchen | chpasswd

0 commit comments

Comments
 (0)