Class: Spurious::Server::State::Init

Inherits:
Base
  • Object
show all
Defined in:
lib/spurious/server/state/init.rb

Instance Attribute Summary collapse

Attributes inherited from Base

#config, #connection

Instance Method Summary collapse

Methods inherited from Base

#initialize

Constructor Details

This class inherits a constructor from Spurious::Server::State::Base

Instance Attribute Details

#completed_containersObject

Returns the value of attribute completed_containers.



11
12
13
# File 'lib/spurious/server/state/init.rb', line 11

def completed_containers
  @completed_containers
end

Instance Method Details

#execute!Object



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/spurious/server/state/init.rb', line 13

def execute!
  this = self
  completed_containers = 0

  app_config.each do |name, meta|
    image_meta = { 'fromImage' => meta[:image]}
    image_meta['Env'] = meta[:env] unless meta[:env].nil?

    container_operation = Proc.new do

      begin
        this.send "Creating container with name: #{name}"
        Docker::Container.create("name" => name, "Image" => meta[:image])
      rescue Exception => e
        case e.message
        when /409 Conflict/
          this.error "Container with name: #{name} already exists"
        else
          this.error "Error creating container: #{e.message}"
        end
      end

    end

    container_callback = Proc.new do
      completed_containers = completed_containers + 1
      this.send("#{config.app.length} containers successfully initialized", true) if completed_containers == app_config.length
    end

    image_operation = Proc.new do
      begin
        this.send "Pulling #{name} from the public repo..."
        Docker::Image.create(image_meta)
      rescue Exception => e
        this.error "Error pulling down image: #{e.message}"
      end

    end

    image_callback = Proc.new do
      EM.add_timer(1) do
        EM.defer(container_operation, container_callback)
      end
    end

    EM.add_timer(1) do
      EM.defer(image_operation, image_callback)
    end
  end

end