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

#connection_timeouts

Constructor Details

#initialize(connection, config, docker_host) ⇒ Init

Returns a new instance of Init.



12
13
14
15
16
# File 'lib/spurious/server/state/init.rb', line 12

def initialize(connection, config, docker_host)
  super(connection, config)
  connection_timeouts 2, 3600, 3600
  @docker_host = docker_host
end

Instance Attribute Details

#completed_containersObject

Returns the value of attribute completed_containers.



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

def completed_containers
  @completed_containers
end

#docker_hostObject

Returns the value of attribute docker_host.



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

def docker_host
  @docker_host
end

Instance Method Details

#execute!Object



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
# File 'lib/spurious/server/state/init.rb', line 18

def execute!

  raise "Containers have already been initilised, please run 'spurious start'" if spurious_containers.length > 0



  send("Pulling containers from the registry can take some time, please be patient...", :info, false, :blue)

  containers = app_config.length
  index = 0

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

    if meta[:image] == 'spurious/s3'
      container_cmd = ['-H', meta[:hostname]]
    end

    create_container = Proc.new do
      begin
        send "Creating #{name} container", :debug
        Docker::Container.create("name" => name, "Image" => meta[:image], 'Cmd' => container_cmd)
      rescue Docker::Error::ArgumentError, Docker::Error::NotFoundError
      rescue Excon::Errors::Conflict
        error "#{name} container already exists"
      end
      index = index + 1
      operation_complete(containers == index)
    end

    create_image = Proc.new do
      begin
        send "Pulling latest version of #{name} from registry", :debug
        Docker::Image.create(image_meta)
      rescue Docker::Error::ArgumentError, Docker::Error::NotFoundError, Excon::Errors::Timeout, Excon::Errors::SocketError
      end
    end

    EM.defer(create_image, create_container)
  end
end

#operation_complete(complete) ⇒ Object



64
65
66
67
68
69
# File 'lib/spurious/server/state/init.rb', line 64

def operation_complete(complete)
  if complete
    send("#{app_config.length} containers successfully initialized", :info, false, :green)
    send("Please add the following to your /etc/hosts file:\n\n#{docker_host} sqs.spurious.localhost s3.spurious.localhost dynamodb.spurious.localhost browser.spurious.localhost\n", :info, true, :blue)
  end
end