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
|
# File 'lib/beaker/hypervisor/docker.rb', line 46
def provision
@logger.notify "Provisioning docker"
@hosts.each do |host|
@logger.notify "provisioning #{host.name}"
@logger.debug("Creating image")
image = ::Docker::Image.build(dockerfile_for(host), { :rm => true })
if @docker_type == 'swarm'
image_name = "#{@registry}/beaker/#{image.id}"
ret = ::Docker::Image.search(:term => image_name)
if ret.first.nil?
@logger.debug("Image does not exist on registry. Pushing.")
image.tag({:repo => image_name, :force => true})
image.push
end
else
image_name = image.id
end
container_opts = {
'Image' => image_name,
'Hostname' => host.name,
}
container = find_container(host)
if container.nil?
unless host['mount_folders'].nil?
container_opts['HostConfig'] ||= {}
container_opts['HostConfig']['Binds'] = host['mount_folders'].values.map do |mount|
a = [ File.expand_path(mount['host_path']), mount['container_path'] ]
a << mount['opts'] if mount.has_key?('opts')
a.join(':')
end
end
if @options[:provision]
if host['docker_container_name']
container_opts['name'] = host['docker_container_name']
end
@logger.debug("Creating container from image #{image_name}")
container = ::Docker::Container.create(container_opts)
end
end
if container.nil?
raise RuntimeError, 'Cannot continue because no existing container ' +
'could be found and provisioning is disabled.'
end
fix_ssh(container) if @options[:provision] == false
@logger.debug("Starting container #{container.id}")
container.start({"PublishAllPorts" => true, "Privileged" => true})
if @docker_type == 'docker' && ENV['DOCKER_HOST']
ip = URI.parse(ENV['DOCKER_HOST']).host
else
ip = container.json["NetworkSettings"]["Ports"]["22/tcp"][0]["HostIp"]
end
@logger.info("Using docker server at #{ip}")
port = container.json["NetworkSettings"]["Ports"]["22/tcp"][0]["HostPort"]
forward_ssh_agent = @options[:forward_ssh_agent] || false
host['ip'] = ip
host['port'] = port
host['ssh'] = {
:password => root_password,
:port => port,
:forward_agent => forward_ssh_agent,
}
@logger.debug("node available as ssh -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no root@#{ip} -p #{port}")
host['docker_container'] = container
host['docker_image'] = image
host['vm_ip'] = container.json["NetworkSettings"]["IPAddress"].to_s
end
hack_etc_hosts @hosts, @options
end
|