Class: Capistrano::DockerCloud::Service

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

Constant Summary collapse

ATTRIBUTES =
[
  :autorestart, :autodestroy, :container_envvars, :container_ports,
  :cpu_shares, :entrypoint, :image, :linked_to_service, :memory, :net,
  :privileged, :roles, :run_command, :sequential_deployment, :tags,
  :target_num_containers, :deployment_strategy, :autoredeploy, :pid,
  :working_dir, :nickname,
  # Special keys
  :name, :image_name
]

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(service) ⇒ Service

Returns a new instance of Service.



20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/capistrano/docker-cloud/service.rb', line 20

def initialize(service)
  ATTRIBUTES.each do |attribute|
    value = default_value_for(attribute)
    value = service.info[attribute] if service
    self.send("#{attribute.to_s}=", value)
  end
  if service
    # The Service API update is expecting an `image` attribute instead of
    # `image_name`
    self.image = service.info[:image_name]
    self.uuid = service.uuid
  end
end

Instance Method Details

#build_update_hashObject



87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
# File 'lib/capistrano/docker-cloud/service.rb', line 87

def build_update_hash
  attributes = {}
  ignored_attributes = [:nickname, :image_name]
  calling = caller.first.match(/^.*\/([a-z\_\-]+)\.rb\:.*$/)[1]
  ignored_attributes << :name unless calling == 'stack'
  ATTRIBUTES.each do |attribute|
    next if ignored_attributes.include?(attribute)

    value = self.send("#{attribute.to_s}")
    next if value.nil? || value == []
    next if default_value_for(attribute) == value

    case attribute
    when :linked_to_service
      attributes[attribute] = linked_to_service_for_update
    when :container_ports
      attributes[attribute] = container_ports_for_update
    else
      attributes[attribute] = value
    end
  end
  attributes
end

#container_ports_for_updateObject



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

def container_ports_for_update
  ports = container_ports.dup
  ports.each do |port|
    port.delete(:endpoint_uri)
    port.delete(:port_name)
  end
end

#default_value_for(attribute) ⇒ Object



64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
# File 'lib/capistrano/docker-cloud/service.rb', line 64

def default_value_for(attribute)
  case attribute
  when :privileged, :autoredeploy, :sequential_deployment
    false
  when :autorestart, :autodestroy
    'OFF'
  when :net
    'bridge'
  when :deployment_strategy
    'EMPTIEST_NODE'
  when :pid
    'none'
  when :target_num_containers
    1
  when :tags
    []
  when :nickname
    ''
  when :image, :name, :image_name
    nil
  end
end

#linked_to_service_for_updateObject



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

def linked_to_service_for_update
  links = linked_to_service.dup
  links.each { |link| link.delete(:from_service) }
end

#load_balancer?Boolean

Returns:

  • (Boolean)


34
35
36
# File 'lib/capistrano/docker-cloud/service.rb', line 34

def load_balancer?
  image_name =~ /^dockercloud\/haproxy\:/
end

#saveObject



57
58
59
60
61
62
# File 'lib/capistrano/docker-cloud/service.rb', line 57

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

#switch!(options) ⇒ Object



111
112
113
114
115
116
117
118
119
# File 'lib/capistrano/docker-cloud/service.rb', line 111

def switch!(options)
  unless load_balancer?
    raise 'This service is not a Load Balancer, therefor you cannot ' \
          'call switch.'
  end

  update_links_with_service!(name: options[:service_name])
  save
end

#update(options) ⇒ Object



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

def update(options)
  options.each do |key, value|
    self.send("#{key.to_s}=", value)
  end
end


121
122
123
124
125
126
127
128
129
130
131
132
133
# File 'lib/capistrano/docker-cloud/service.rb', line 121

def update_links_with_service!(options)
  links = linked_to_service.dup

  services = stack.find_service_by_name(options[:name])
  switch_to = first_or_raise_error!(services, options[:name])

  to_be_replaced = remove_service_with_same_image_than(switch_to)

  links.delete_if { |link| link[:name] == to_be_replaced.name }
  links << { name: options[:name], to_service: "/api/app/v1/yourcursus/service/#{switch_to.uuid}/" }

  self.linked_to_service = links
end