Class: Construi::Container
- Inherits:
-
Object
- Object
- Construi::Container
- Defined in:
- lib/construi/container.rb
Defined Under Namespace
Classes: RunError
Instance Attribute Summary collapse
-
#name ⇒ Object
readonly
Returns the value of attribute name.
Class Method Summary collapse
- .create(image, options = {}) ⇒ Object
- .run(image, options = {}) ⇒ Object
- .use(image, options = {}) ⇒ Object
- .wrap(container, options = {}) ⇒ Object
Instance Method Summary collapse
- #==(other) ⇒ Object
- #attach_stdout ⇒ Object
- #commit ⇒ Object
- #delete ⇒ Object
- #id ⇒ Object
-
#initialize(container, options = {}) ⇒ Container
constructor
A new instance of Container.
- #log_lifecycle(msg) ⇒ Object
- #log_lifecycle? ⇒ Boolean
- #run ⇒ Object
- #start ⇒ Object
- #stop ⇒ Object
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, = {}) @container = container @name = [:name] || @container.id @log_lifecycle = [:log_lifecycle] || false end |
Instance Attribute Details
#name ⇒ Object (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, = {}) env = [:env] || [] privileged = [:privileged] || false links = [:links] || [] volumes = [:volumes] || [] volumes_from = [:volumes_from] || [] host_config = { 'Binds' => ["#{Dir.pwd}:/var/workspace"].concat(volumes), 'Privileged' => privileged, 'Links' => links, 'VolumesFrom' => volumes_from } = { 'Image' => image.id, 'Env' => env, 'Tty' => false, 'WorkingDir' => '/var/workspace', 'HostConfig' => host_config } ['Cmd'] = [:cmd].split if .key?(:cmd) wrap Docker::Container.create(), end |
.run(image, options = {}) ⇒ Object
106 107 108 |
# File 'lib/construi/container.rb', line 106 def self.run(image, = {}) use image, , &:run end |
.use(image, options = {}) ⇒ Object
99 100 101 102 103 104 |
# File 'lib/construi/container.rb', line 99 def self.use(image, = {}) container = create image, 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, = {}) new container, 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_stdout ⇒ Object
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 |
#commit ⇒ Object
51 52 53 |
# File 'lib/construi/container.rb', line 51 def commit Image.wrap(@container.commit) end |
#delete ⇒ Object
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 |
#id ⇒ Object
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
43 44 45 |
# File 'lib/construi/container.rb', line 43 def log_lifecycle? @log_lifecycle end |
#run ⇒ Object
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 |
#start ⇒ Object
19 20 21 22 23 |
# File 'lib/construi/container.rb', line 19 def start log_lifecycle "Starting container: '#{name}'..." @container.start! attach_stdout end |
#stop ⇒ Object
25 26 27 28 |
# File 'lib/construi/container.rb', line 25 def stop log_lifecycle "Stopping container: '#{name}'..." @container.stop end |