Class: Capistrano::DockerCloud::Stack

Inherits:
Base
  • Object
show all
Defined in:
lib/capistrano/docker-cloud/stack.rb

Instance Method Summary collapse

Methods inherited from Base

belongs_to, configure, #connection, connection, find, find_by, find_by_name, redeploy, #redeploy, #reload!, resource, subclass_name

Methods included from Helpers::CollectionHelper

#first_or_raise_error!

Constructor Details

#initialize(stack) ⇒ Stack

Returns a new instance of Stack.



5
6
7
8
# File 'lib/capistrano/docker-cloud/stack.rb', line 5

def initialize(stack)
  @original_stack = stack
  sync_services_from_stack
end

Instance Method Details

#build_service(options = {}) ⇒ Object



28
29
30
31
32
33
34
# File 'lib/capistrano/docker-cloud/stack.rb', line 28

def build_service(options = {})
  services = find_service_by_image_name(options[:image], tag: false)
  service = first_or_raise_error!(services, options[:image])
  service = Capistrano::DockerCloud::Service.new(service)
  service.update(image: options[:image], name: options[:name])
  @services << service
end

#find_service_by_image_name(name, options = {}) ⇒ Object



20
21
22
23
24
25
26
# File 'lib/capistrano/docker-cloud/stack.rb', line 20

def find_service_by_image_name(name, options = {})
  options[:tag] = true unless options.key?(:tag)

  @original_stack.services.select do |service|
    compare(service.image_name, name, tag: options[:tag])
  end
end

#find_service_by_name(name) ⇒ Object

TODO : Refactor this code in order to move it in the Service class



14
15
16
17
18
# File 'lib/capistrano/docker-cloud/stack.rb', line 14

def find_service_by_name(name)
  @original_stack.services.select do |service|
    compare(service.name, name)
  end
end

#has_a_load_balancer?Boolean

Returns:

  • (Boolean)


43
44
45
46
47
48
# File 'lib/capistrano/docker-cloud/stack.rb', line 43

def has_a_load_balancer?
  # TODO : Refactor this code in order to move it in the Service class
  @load_balancer_service = @original_stack.services.detect do |service|
    service.image_name =~ /^dockercloud\/haproxy\:/
  end
end

#load_balancer_serviceObject



50
51
52
53
54
# File 'lib/capistrano/docker-cloud/stack.rb', line 50

def load_balancer_service
  service = Capistrano::DockerCloud::Service.new(@load_balancer_service)
  service.stack = self
  service
end

#saveObject



36
37
38
39
40
41
# File 'lib/capistrano/docker-cloud/stack.rb', line 36

def save
  connection.stacks.update(@original_stack.uuid, build_update_body)
  reload!
rescue RestClient::BadRequest => error
  puts "ERROR: #{error.response}"
end

#start_service(options) ⇒ Object



56
57
58
59
60
# File 'lib/capistrano/docker-cloud/stack.rb', line 56

def start_service(options)
  services = find_service_by_name(options[:name])
  service = first_or_raise_error!(services, options[:name])
  service.start
end

#uuidObject



62
63
64
# File 'lib/capistrano/docker-cloud/stack.rb', line 62

def uuid
  @original_stack.uuid
end

#waiting_service_application_boot(options) ⇒ Object



83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
# File 'lib/capistrano/docker-cloud/stack.rb', line 83

def waiting_service_application_boot(options)
  options[:timeout] ||= 60
  services = find_service_by_name(options[:name])
  service = first_or_raise_error!(services, options[:name])

  uri = service.containers.flat_map do |container|
    container.info[:container_ports].map do |container_port|
      next unless container_port[:endpoint_uri]
      container_port[:endpoint_uri].gsub(/^tcp\:\/\//, 'http://')
    end
  end

  result = 0

  Timeout.timeout(options[:timeout]) do
    while result != 200 do
      sleep 0.5
      result = uri.map do |url|
        begin
          response = RestClient.get(url)
          response.code
        rescue Errno::ECONNREFUSED
          500
        end
      end.reduce(0, :+) / uri.size
    end
  end
rescue Timeout::Error
  raise "The service #{options[:name]} application was not able to boot" \
        " in less than #{options[:timeout]} seconds."
end

#waiting_service_boot(options) ⇒ Object



66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
# File 'lib/capistrano/docker-cloud/stack.rb', line 66

def waiting_service_boot(options)
  options[:timeout] ||= 60
  services = find_service_by_name(options[:name])
  service = first_or_raise_error!(services, options[:name])

  Timeout.timeout(options[:timeout]) do
    begin
      sleep 0.5
      service.reload
    end until service.state == 'Running'
  end
rescue Timeout::Error
  raise "The service #{options[:name]} was not able to boot in less " \
        "than #{options[:timeout]} seconds. (Service state was " \
        "#{service.state.inspect})"
end