Class: Pkgman::Container

Inherits:
Object
  • Object
show all
Defined in:
lib/pkgman/container.rb

Instance Method Summary collapse

Constructor Details

#initialize(image, version) ⇒ Container

Returns a new instance of Container.



4
5
6
7
8
# 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

Instance Method Details

#destroyObject



49
50
51
52
# File 'lib/pkgman/container.rb', line 49

def destroy
  @container.kill
  @container.delete(:force => true)
end

#download(what, dst) ⇒ Object



30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# 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') ⇒ Object



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
# 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