class Pkgman::Container
Public Class Methods
new(image, version)
click to toggle source
# File lib/pkgman/container.rb, line 4 def initialize(image, version) @image = Docker::Image.create('fromImage' => "#{image}:#{version}") @container = Docker::Container.create('Image' => @image.id, 'Tty' => true) @container.start end
Public Instance Methods
destroy()
click to toggle source
# File lib/pkgman/container.rb, line 49 def destroy @container.kill @container.delete(:force => true) end
download(what, dst)
click to toggle source
# File lib/pkgman/container.rb, line 30 def download(what, dst) pth = File.join(dst, "export.rpm.tar") File.open(pth, 'w') do |file| @container.copy(what) do |chunk| file.write(chunk) end end Gem::Package::TarReader.new(File.open(pth)) do |tar| tar.each do |entry| File.open(File.join(dst, entry.full_name), 'w') do |file| file.write(entry.read) end end end File.delete(pth) end
execute(command, cwd = '/root')
click to toggle source
# File lib/pkgman/container.rb, line 10 def execute(command, cwd = '/root') puts "Executing #{command} in #{cwd}" options = {} options['WorkingDir'] = cwd if cwd command = ['/bin/bash', '-c', command] result = @container.exec(command, options) do |stream, chunk| puts "#{stream}: #{chunk}" end if result[2] > 0 self.destroy raise Exception.new('Execution failed') end result end