lib/cap-rightscale/configuration/rightscale.rb in cap-rightscale-0.3.13 vs lib/cap-rightscale/configuration/rightscale.rb in cap-rightscale-0.4.0
- old
+ new
@@ -7,18 +7,10 @@
module RightScale
attr_reader :domainname
attr_writer :validate_echo, :use_nickname, :use_public_ip, :use_rs_cache
attr_accessor :rs_cache_lifetime
- def get_rs_instance
- @rs_instance ||= Capistrano::RightScale::Resource.instance
- end
-
- def get_cache_instance
- @cache_instance ||= Capistrano::RightScale::Cache.instance
- end
-
def get_rs_confpath
get_rs_instance.confpath
end
def set_rs_confpath(path)
@@ -59,38 +51,43 @@
@caller ||= File.basename(caller.map {|x| /(.*?):(\d+)/ =~ x; $1}.first, ".*")
start = Time.now
logger.info("SETTING ROLE: #{role}")
- # Set rightscale's parameters
- _array_id = params[:array_id]
- params.delete(:array_id) # remove rightscale's parameters
-
host_list = use_rs_cache ? get_cache_instance.load_server_cache(role, @caller) : [] # Get cache
if host_list && host_list.size > 0
+ [:array_id, :except_tags].each {|key| params.delete(key)} # remove rightscale's parameters
logger.info("restore cache of servers:\n#{host_list.pretty_inspect}")
role(role, params) { host_list } # set cache to role()
else
# Request RightScale API
- array = get_rs_instance.array(_array_id)
+ array = get_rs_instance.array(params[:array_id])
logger.info("querying rightscale for server_array #{array.nickname}...")
dept = get_rs_instance.deployment(array.deployment_href.match(/[0-9]+$/).to_s, :server_settings => 'true')
deployment_name = dept.nickname
logger.info("Deployment #{deployment_name}:")
+ srvs = get_rs_instance.array_instances(array.id).select {|i| i[:state] == "operational"}
- host_list = get_rs_instance.array_instances(array.id).select {|i| i[:state] == "operational"}.map do |instance|
+ if params.include?(:except_tags)
+ except_tags_params = {:resource_type => "ec2_instance", :tags => [params[:except_tags]]}
+ srvs = servers_with_tags_set(params[:deployment], srvs, except_tags_params, :minus)
+ return [] if srvs.size == 0 # Not found servers matching tag
+ end
+
+ host_list = srvs.map do |instance|
hostname = instance[:nickname].sub(/ #[0-9]+$/, "-%03d" % instance[:nickname].match(/[0-9]+$/).to_s.to_i)
hostname << ".#{domainname}" if domainname && hostname.match(/#{domainname}/).nil?
ip = use_public_ip ? instance[:ip_address] : instance[:private_ip_address]
logger.info("Found server: #{hostname}(#{ip})")
use_nickname ? hostname : ip
end
host_list = RSUtils.valid_echo(host_list, logger) if validate_echo
if host_list && host_list.size > 0
+ [:array_id, :except_tags].each {|key| params.delete(key)} # remove rightscale's parameters
role(role, params) { host_list }
get_cache_instance.dump_server_cache(role, host_list, @caller) if use_rs_cache # Dump cache
end
end
puts "Time: #{Time.now - start}"
@@ -112,40 +109,42 @@
@caller ||= File.basename(caller.map {|x| /(.*?):(\d+)/ =~ x; $1}.first, ".*")
start = Time.now
logger.info("SETTING ROLE: #{role}")
- # Set rightscale's parameters
- _dept_id = params[:deployment]
- _name_prefix = params[:name_prefix]
-
- params.delete(:deployment)
- params.delete(:name_prefix) if params.has_key?(:name_prefix)
-
host_list = use_rs_cache ? get_cache_instance.load_server_cache(role, @caller) : [] # Get cache
- if host_list && host_list.size > 0
+ if host_list.size > 0
+ [:deployment, :name_prefix, :except_tags].each {|key| params.delete(key)} # remove rightscale's parameters
logger.info("restore cache of servers:\n#{host_list.pretty_inspect}")
role(role, params) { host_list } # set cache to role()
else
+
# Request RightScale API
- dept = get_rs_instance.deployment(_dept_id, :server_settings => 'true')
- logger.info("querying rightscale for servers #{_name_prefix} in deployment #{dept.nickname}...")
+ dept = get_rs_instance.deployment(params[:deployment], :server_settings => 'true')
+ logger.info("querying rightscale for servers #{params[:name_prefix]} in deployment #{dept.nickname}...")
srvs = dept.servers.select {|s| s[:state] == "operational"}
- srvs = srvs.select {|s| /#{_name_prefix}/ =~ s[:nickname]} if _name_prefix
+ srvs = srvs.select {|s| /#{params[:name_prefix]}/ =~ s[:nickname]} if params[:name_prefix]
+ if params.include?(:except_tags)
+ except_tags_params = {:resource_type => "ec2_instance", :tags => [params[:except_tags]]}
+ srvs = servers_with_tags_set(params[:deployment], srvs, except_tags_params, :minus)
+ return [] if srvs.size == 0 # Not found servers matching tag
+ end
+
host_list = srvs.map do |server|
- hostname = server[:nickname]
+ hostname = server[:nickname].sub(/ #[0-9]+$/, "-%03d" % server[:nickname].match(/[0-9]+$/).to_s.to_i)
hostname << ".#{domainname}" if domainname && hostname.match(/#{domainname}/).nil?
ip = use_public_ip ? server[:settings][:ip_address] : server[:settings][:private_ip_address]
logger.info("Found server: #{hostname}(#{ip})")
use_nickname ? hostname : ip
end
host_list = RSUtils.valid_echo(host_list, logger) if validate_echo
if host_list && host_list.size > 0
+ [:array_id, :except_tags].each {|key| params.delete(key)} # remove rightscale's parameters
role(role, params) { host_list }
get_cache_instance.dump_server_cache(role, host_list, @caller) if use_rs_cache # Dump cache
end
end
puts "Time: #{Time.now - start}"
@@ -168,51 +167,45 @@
@caller ||= File.basename(caller.map {|x| /(.*?):(\d+)/ =~ x; $1}.first, ".*")
start = Time.now
logger.info("SETTING ROLE: #{role}")
- # Set rightscale's parameters
- _dept_id = params[:deployment]
- _tags = params[:tags]
-
- params.delete(:deployment)
- params.delete(:tags)
-
host_list = use_rs_cache ? get_cache_instance.load_server_cache(role, @caller) : [] # Get cache
if host_list && host_list.size > 0
+ [:deployment, :tags, :except_tags].each {|key| params.delete(key)} # remove rightscale's parameters
logger.info("restore cache of servers:\n#{host_list.pretty_inspect}")
role(role, params) { host_list } # set cache to role()
else
# Request RightScale API
- dept = get_rs_instance.__send__(:deployment, _dept_id, :server_settings => 'true')
- logger.info("querying rightscale for servers matching tags #{_tags} in deployment #{dept.nickname}...")
+ dept = get_rs_instance.deployment(params[:deployment], :server_settings => 'true')
+ logger.info("querying rightscale for servers matching tags #{params[:tags]} in deployment #{dept.nickname}...")
srvs = dept.servers.select {|s| s[:state] == "operational"}
- ts_params = {:resource_type => "ec2_instance", :tags => [_tags]}
- ts = get_rs_instance.__send__(:tag, ts_params).
- select {|s| s.state == "operational"}.
- select {|s| s.deployment_href.match(/[0-9]+$/).to_s == _dept_id.to_s}
+ ts_params = {:resource_type => "ec2_instance", :tags => [params[:tags]]}
+ srvs = servers_with_tags_set(params[:deployment], srvs, ts_params, :intersect)
+ return [] if srvs.size == 0 # Not found servers matching tag
- # diff servers in deployment and servers matching tags in deployment
- srvs_ids = srvs.map {|s| s[:href].match(/[0-9]+$/).to_s}
- ts_ids = ts.map {|s| s.href.sub("/current", "").match(/[0-9]+$/).to_s}
- found_ids = srvs_ids & ts_ids
+ if params.include?(:except_tags)
+ except_tags_params = {:resource_type => "ec2_instance", :tags => [params[:except_tags]]}
+ srvs = servers_with_tags_set(params[:deployment], srvs, except_tags_params, :minus)
+ return [] if srvs.size == 0 # Not found servers matching tag
+ end
- if found_ids.size > 0
- host_list = srvs.select {|s| found_ids.include?(s[:href].match(/[0-9]+$/).to_s)}.map do |server|
- hostname = server[:nickname]
- hostname << ".#{domainname}" if domainname && hostname.match(/#{domainname}/).nil?
- ip = use_public_ip ? server[:settings][:ip_address] : server[:settings][:private_ip_address]
+ host_list = srvs.map do |server|
+ hostname = server[:nickname]
+ hostname << ".#{domainname}" if domainname && hostname.match(/#{domainname}/).nil?
+ ip = use_public_ip ? server[:settings][:ip_address] : server[:settings][:private_ip_address]
- logger.info("Found server: #{hostname}(#{ip})")
- use_nickname ? hostname : ip
- end
- host_list = RSUtils.valid_echo(host_list, logger) if validate_echo
+ logger.info("Found server: #{hostname}(#{ip})")
+ use_nickname ? hostname : ip
end
+ host_list = RSUtils.valid_echo(host_list, logger) if validate_echo
+
if host_list && host_list.size > 0
+ [:deployment, :tags, :except_tags].each {|key| params.delete(key)} # remove rightscale's parameters
role(role, params) { host_list }
get_cache_instance.dump_server_cache(role, host_list, @caller) if use_rs_cache # Dump cache
end
end
puts "Time: #{Time.now - start}"
@@ -222,9 +215,56 @@
private
def check_role(role)
return false if ENV['HOSTS']
return false if ENV['ROLES'] && ENV['ROLES'].split(',').include?("#{role}") == false
return true
+ end
+
+ def get_rs_instance
+ @rs_instance ||= Capistrano::RightScale::Resource.instance
+ end
+
+ def get_cache_instance
+ @cache_instance ||= Capistrano::RightScale::Cache.instance
+ end
+
+ # set(union, intersect, minus) servers in deployment and servers matching tags in deployment
+ def servers_with_tags_set(deployment_id, servers, tags_params, operator)
+ servers_ids = servers.map {|s| s[:href].match(/[0-9]+$/).to_s}
+
+ ts = servers_with_tags_in_deployment(deployment_id, tags_params)
+ return [] if ts.size == 0
+
+ ts_ids = ts.map {|s| s.href.sub("/current", "").match(/[0-9]+$/).to_s}
+ case operator
+ when :intersect then
+ oper_ids = servers_ids & ts_ids
+ when :minus then
+ oper_ids = servers_ids - ts_ids
+ end
+ return [] if oper_ids.size == 0
+
+ servers.select {|s| oper_ids.include?(s[:href].match(/[0-9]+$/).to_s)} || []
+ end
+
+ def intersect_servers_with_tags(deployment, servers, tags_params, tags_api)
+ ts = tags_api.call
+ return [] if ts.size == 0
+
+ ts_ids = ts.map {|s| s.href.sub("/current", "").match(/[0-9]+$/).to_s}
+ intersect_ids = servers_ids & ts_ids
+ intersect_ids.size > 0 ? intersect_ids : []
+ end
+
+ def servers_with_tags_in_deployment(deployment_id, params)
+ begin
+ servers = get_rs_instance.tag(params).
+ select {|s| s.state == "operational"}.
+ select {|s| s.deployment_href.match(/[0-9]+$/).to_s == deployment_id.to_s}
+ rescue => e
+ {}
+ end
+ servers
end
def validate_echo
@validate_echo ||= false
end