Module: Capistrano::Configuration::RightScale

Included in:
Capistrano::Configuration
Defined in:
lib/cap-rightscale/configuration/rightscale.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#rs_confpathObject

Returns the value of attribute rs_confpath.



5
6
7
# File 'lib/cap-rightscale/configuration/rightscale.rb', line 5

def rs_confpath
  @rs_confpath
end

#rs_domainObject

Returns the value of attribute rs_domain.



5
6
7
# File 'lib/cap-rightscale/configuration/rightscale.rb', line 5

def rs_domain
  @rs_domain
end

#rs_lifetimeObject

Returns the value of attribute rs_lifetime.



5
6
7
# File 'lib/cap-rightscale/configuration/rightscale.rb', line 5

def rs_lifetime
  @rs_lifetime
end

#use_nickname=(value) ⇒ Object

Sets the attribute use_nickname

Parameters:

  • value

    the value to set the attribute use_nickname to.



5
6
7
# File 'lib/cap-rightscale/configuration/rightscale.rb', line 5

def use_nickname=(value)
  @use_nickname = value
end

#use_public_ip=(value) ⇒ Object

Sets the attribute use_public_ip

Parameters:

  • value

    the value to set the attribute use_public_ip to.



5
6
7
# File 'lib/cap-rightscale/configuration/rightscale.rb', line 5

def use_public_ip=(value)
  @use_public_ip = value
end

#use_rs_cache=(value) ⇒ Object

Sets the attribute use_rs_cache

Parameters:

  • value

    the value to set the attribute use_rs_cache to.



4
5
6
# File 'lib/cap-rightscale/configuration/rightscale.rb', line 4

def use_rs_cache=(value)
  @use_rs_cache = value
end

#validate_echo=(value) ⇒ Object

Sets the attribute validate_echo

Parameters:

  • value

    the value to set the attribute validate_echo to.



5
6
7
# File 'lib/cap-rightscale/configuration/rightscale.rb', line 5

def validate_echo=(value)
  @validate_echo = value
end

#validate_resolv=(value) ⇒ Object

Sets the attribute validate_resolv

Parameters:

  • value

    the value to set the attribute validate_resolv to.



5
6
7
# File 'lib/cap-rightscale/configuration/rightscale.rb', line 5

def validate_resolv=(value)
  @validate_resolv = value
end

Instance Method Details

#nickname(role, params) ⇒ Object

Get servers in deployment

Parameters

  • role - Capistrano role symbol (ex. :app, :web, :db)

  • params[:name_prefix] - ex. :name_prefix => “db” (RightScale instance nickname)

  • params[:deployment] - ex. :deployment => 1

  • params[:domain] domain name(user defined) - ex. :domain => “example.com”

  • params[:except_tags] except servers matching tags

    • ex. :except_tags => [“xx_app:state=broken”, “xx_app:state=out_of_service”]

  • params[:xxx] - ex. :user => “www”, :port => 2345, etc…

Examples

deployment_id = 1
nickname :db, :name_prefix => "db", :deployment => deployment_id, :user => "mysql"

Raises:

  • (ArgumentError)


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
114
115
116
117
118
119
120
121
122
123
# File 'lib/cap-rightscale/configuration/rightscale.rb', line 85

def nickname(role, params)
start = Time.now
  _init unless initialized?
  return [] unless check_role(role)
  raise ArgumentError, ":deployment is not included in params!![#{params}]" unless params.has_key?(:deployment)
  @caller ||= File.basename(caller.map {|x| /(.*?):(\d+)/ =~ x; $1 }.first, ".*")
  @rs_server_keys ||= [:array_id, :name_prefix, :domain, :except_tags]

  logger.debug("SETTING ROLE: #{role}")

  servers = use_rs_cache ? role_with_load_cache(role, @rs_server_keys, params) : []

  if servers.size == 0
    # Request RightScale API
    deployment = rs_instance.deployment(params[:deployment], :server_settings => 'true')
    logger.debug(
      "querying rightscale for servers #{params[:name_prefix]} in deployment #{deployment.nickname}...")
    servers = deployment.servers.select {|s| s[:state] == "operational" }
    servers = servers.select {|s| /#{params[:name_prefix]}/ =~ s[:nickname] } if params[:name_prefix]
    servers = servers_with_tags_set(
      params[:deployment], servers, params[:except_tags], :minus) if params.include?(:except_tags)

    servers = servers.map do |server|
      hostname = server[:nickname]
      dom = params[:domain] || rs_domain
      hostname += ".#{dom}" if dom
      ip = use_public_ip ? server[:settings][:ip_address] : server[:settings][:private_ip_address]

      logger.debug("Found server: #{server[:nickname]}(#{ip})")
      use_nickname ? hostname : ip
    end
    servers = RSUtils.valid_resolv(servers, logger) if validate_resolv && use_nickname
    servers = RSUtils.valid_echo(servers, logger) if validate_echo

    role_with_dump_cache(role, servers, @rs_server_keys, params, use_rs_cache) if servers.size > 0
  end
logger.trace("Time: #{Time.now - start}")
  servers || []
end

#rs_array_number_format(format) ⇒ Object



7
8
9
# File 'lib/cap-rightscale/configuration/rightscale.rb', line 7

def rs_array_number_format(format)
  @array_num_format = format
end

#server_array(role, params) ⇒ Object

Get RightScale Server Array RightScale nickname registerd deploy host’s /etc/hosts OR dns record(replace ‘s/ #/-000/’ to ServerArray name)

Parameters

  • role - Capistrano role symbol (ex. :app, :web, :db)

  • params[:array_id] - ex. :array_id => 1

  • params[:domain] domain name(user defined) - ex. :domain => “example.com”

  • params[:except_tags] except servers matching tags

    • ex. :except_tags => [“xx_app:state=broken”, “xx_app:state=out_of_service”]

  • params[:xxx] - ex. :user => “www”, :port => 2345, etc…

Examples

array_id = 1
server_array :app, :array_id => array_id, :port => 1234

Raises:

  • (ArgumentError)


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
64
65
66
67
68
69
70
71
# File 'lib/cap-rightscale/configuration/rightscale.rb', line 31

def server_array(role, params)
start = Time.now
  _init unless initialized?
  return [] unless check_role(role)
  raise ArgumentError, ":array_id is not included in params!![#{params}]" unless params.has_key?(:array_id)
  @caller ||= File.basename(caller.map {|x| /(.*?):(\d+)/ =~ x; $1}.first, ".*")
  @rs_array_keys ||= [:array_id, :domain, :except_tags]

  logger.debug("SETTING ROLE: #{role}")

  servers = use_rs_cache ? role_with_load_cache(role, @rs_array_keys, params) : []

  if servers.size == 0
    # Request RightScale API
    array = rs_instance.array(params[:array_id])
    logger.debug("querying rightscale for server_array #{array.nickname}...")
    deployment = rs_instance.deployment(array.deployment_href.match(/[0-9]+$/).to_s, :server_settings => 'true')
    deployment_name = deployment.nickname
    logger.debug("Deployment #{deployment_name}:")
    servers = rs_instance.array_instances(array.id).select {|i| i[:state] == "operational" }
    servers = servers_with_tags_set(
      deployment.id, servers, params[:except_tags], :minus) if params.include?(:except_tags)

    servers = servers.map do |instance|
      hostname = instance[:nickname].sub(
        / #[0-9]+$/, array_number_format % instance[:nickname].match(/[0-9]+$/).to_s.to_i)
      dom = params[:domain] || rs_domain
      hostname += ".#{dom}" if dom
      ip = use_public_ip ? instance[:ip_address] : instance[:private_ip_address]

      logger.debug("Found server: #{instance[:nickname]}(#{ip})")
      use_nickname ? hostname : ip
    end
    servers = RSUtils.valid_resolv(servers, logger) if validate_resolv && use_nickname
    servers = RSUtils.valid_echo(servers, logger) if validate_echo

    role_with_dump_cache(role, servers, @rs_array_keys, params, use_rs_cache) if servers.size > 0
  end
logger.trace("Time: #{Time.now - start}")
  servers || []
end

#tag(role, params) ⇒ Object

Get servers matching tags in deployment

Parameters

  • role - Capistrano role symbol (ex. :app, :web, :db)

  • params[:deployment] - ex. :deployment => 1

  • params[:tags] - ex. :tags => “xx_db:role=master”,

    "xx_web:role", "xx_lb" (RightScale tags partial matchs 'namespece:predicate=value')
    
  • params[:name_prefix] - ex. :name_prefix => “db” (RightScale instance nickname)

  • params[:domain] domain name(user defined) - ex. :domain => “example.com”

  • params[:except_tags] except servers matching tags

    • ex. :except_tags => [“xx_app:state=broken”, “xx_app:state=out_of_service”]

  • params[:xxx] - ex. :user => “www”, :port => 2345, etc…

Examples

deployment_id = 1
nickname :db, :name_prefix => "db",  :tags => "xx_db:role", :deployment => deployment_id, :port => 3306

Raises:

  • (ArgumentError)


139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
# File 'lib/cap-rightscale/configuration/rightscale.rb', line 139

def tag(role, params)
start = Time.now
  _init unless initialized?
  return [] unless check_role(role)
  raise ArgumentError, ":tags is not included in params!![#{params}]" unless params.has_key?(:tags)
  raise ArgumentError, ":deployment is not included in params!![#{params}]" unless params.has_key?(:deployment)
  @caller ||= File.basename(caller.map {|x| /(.*?):(\d+)/ =~ x; $1 }.first, ".*")
  @rs_tag_keys ||= [:deployment, :tags, :name_prefix, :domain, :except_tags]

  logger.debug("SETTING ROLE: #{role}")

  servers = use_rs_cache ? role_with_load_cache(role, @rs_tag_keys, params) : []

  if servers.size == 0
    # Request RightScale API
    deployment = rs_instance.deployment(params[:deployment], :server_settings => 'true')
    logger.debug("querying rightscale for servers #{params[:name_prefix]} " +
      "matching tags #{params[:tags]} in deployment #{deployment.nickname}...")
    servers = deployment.servers.select {|s| s[:state] == "operational" }
    servers = servers.select {|s| /#{params[:name_prefix]}/ =~ s[:nickname] } if params[:name_prefix]

    servers = servers_with_tags_set(params[:deployment], servers, params[:tags], :intersect)
    if params.include?(:except_tags) && servers.size > 0
      servers = servers_with_tags_set(params[:deployment], servers, params[:except_tags], :minus)
    end

    servers = servers.map do |server|
      hostname = server[:nickname]
      dom = params[:domain] || rs_domain
      hostname += ".#{dom}" if dom
      ip = use_public_ip ? server[:settings][:ip_address] : server[:settings][:private_ip_address]

      logger.debug("Found server: #{server[:nickname]}(#{ip})")
      use_nickname ? hostname : ip
    end
    servers = RSUtils.valid_resolv(servers, logger) if validate_resolv && use_nickname
    servers = RSUtils.valid_echo(servers, logger) if validate_echo

    role_with_dump_cache(role, servers, @rs_tag_keys, params, use_rs_cache) if servers.size > 0
  end
logger.trace("Time: #{Time.now - start}")
  servers || []
end