# Flatten the image by piping a tar export into a tar import.
# Flattening essentially destroys the history of the image. By default docker
# will however stack image revisions ontop of one another. Namely if we have
# abc and create a new image edf, edf will be an AUFS ontop of abc. While this
# is probably useful if one doesn't commit containers repeatedly for us this
# is pretty crap as we have massive turn around on images.
@log.warn 'Flattening latest image by exporting and importing it.' \
' This can take a while.'
require 'thwait'
rd, wr = IO.pipe
@i = nil
Thread.abort_on_exception = true
read_thread = Thread.new do
@i = Docker::Image.import_stream do
rd.read(1000).to_s
end
@log.warn 'Import complete'
rd.close
end
write_thread = Thread.new do
c.export do |chunk|
wr.write(chunk)
end
@log.warn 'Export complete'
wr.close
end
ThreadsWait.all_waits(read_thread, write_thread)