Class: Construi::Container

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

Defined Under Namespace

Classes: RunError

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(container, options = {}) ⇒ Container

Returns a new instance of Container.



9
10
11
12
13
# File 'lib/construi/container.rb', line 9

def initialize(container, options = {})
  @container = container
  @name = options[:name] || @container.id
  @log_lifecycle = options[:log_lifecycle] || false
end

Instance Attribute Details

#nameObject (readonly)

Returns the value of attribute name.



7
8
9
# File 'lib/construi/container.rb', line 7

def name
  @name
end

Class Method Details

.create(image, options = {}) ⇒ Object



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
# File 'lib/construi/container.rb', line 68

def self.create(image, options = {})
  env = options[:env] || []
  privileged = options[:privileged] || false
  links = options[:links] || []
  volumes = options[:volumes] || []
  volumes_from = options[:volumes_from] || []

  host_config = {
    'Binds' => ["#{Dir.pwd}:/var/workspace"].concat(volumes),
    'Privileged' => privileged,
    'Links' => links,
    'VolumesFrom' => volumes_from
  }

  create_options = {
    'Image' => image.id,
    'Env' => env,
    'Tty' => false,
    'WorkingDir' => '/var/workspace',
    'HostConfig' => host_config
  }

  create_options['Cmd'] = options[:cmd].split if options.key?(:cmd)

  wrap Docker::Container.create(create_options), options
end

.run(image, options = {}) ⇒ Object



106
107
108
# File 'lib/construi/container.rb', line 106

def self.run(image, options = {})
  use image, options, &:run
end

.use(image, options = {}) ⇒ Object



99
100
101
102
103
104
# File 'lib/construi/container.rb', line 99

def self.use(image, options = {})
  container = create image, options
  yield container
ensure
  container.delete unless container.nil?
end

.wrap(container, options = {}) ⇒ Object



95
96
97
# File 'lib/construi/container.rb', line 95

def self.wrap(container, options = {})
  new container, options
end

Instance Method Details

#==(other) ⇒ Object



64
65
66
# File 'lib/construi/container.rb', line 64

def ==(other)
  other.is_a? Container and id == other.id
end

#attach_stdoutObject



37
38
39
40
41
# File 'lib/construi/container.rb', line 37

def attach_stdout
  Thread.new do
    @container.attach(:stream => true, :logs => true) { |_, c| Console.output name, c }
  end
end

#commitObject



51
52
53
# File 'lib/construi/container.rb', line 51

def commit
  Image.wrap(@container.commit)
end

#deleteObject



30
31
32
33
34
35
# File 'lib/construi/container.rb', line 30

def delete
  stop
  @container.kill
  @container.delete force: true, v: true
  log_lifecycle "Deleted container: '#{name}'"
end

#idObject



15
16
17
# File 'lib/construi/container.rb', line 15

def id
  @container.id
end

#log_lifecycle(msg) ⇒ Object



47
48
49
# File 'lib/construi/container.rb', line 47

def log_lifecycle(msg)
  Console.progress msg if log_lifecycle?
end

#log_lifecycle?Boolean

Returns:

  • (Boolean)


43
44
45
# File 'lib/construi/container.rb', line 43

def log_lifecycle?
  @log_lifecycle
end

#runObject

Raises:



55
56
57
58
59
60
61
62
# File 'lib/construi/container.rb', line 55

def run
  start
  status_code = @container.wait['StatusCode']

  raise RunError, "Cmd returned status code: #{status_code}" unless status_code == 0

  commit
end

#startObject



19
20
21
22
23
# File 'lib/construi/container.rb', line 19

def start
  log_lifecycle "Starting container: '#{name}'..."
  @container.start!
  attach_stdout
end

#stopObject



25
26
27
28
# File 'lib/construi/container.rb', line 25

def stop
  log_lifecycle "Stopping container: '#{name}'..."
  @container.stop
end