lib/cap-rightscale/configuration/rightscale.rb in cap-rightscale-0.3.12 vs lib/cap-rightscale/configuration/rightscale.rb in cap-rightscale-0.3.13
- old
+ new
@@ -1,18 +1,24 @@
require 'cap-rightscale/utils/rs_utils'
+require 'cap-rightscale/configuration/rightscale/cache'
require 'cap-rightscale/configuration/rightscale/resource'
module Capistrano
class Configuration
module RightScale
+ attr_reader :domainname
attr_writer :validate_echo, :use_nickname, :use_public_ip, :use_rs_cache
- attr_accessor :domainname
+ 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)
@@ -30,13 +36,17 @@
__send__("#{arg}=".to_sym, false) if respond_to?("#{arg}=".to_sym)
end
end
def rs_cache_lifetime(time)
- @lifetime = time # seconds
+ get_cache_instance.lifetime = time # seconds
end
+ def set_domainname(domain)
+ @domainname = domain
+ end
+
# Get RightScale Server Array
# === Parameters
# * _role_ - Capistrano role symbol (ex. :app, :web, :db)
# * _params[:array_id]_ - ex. :array_id => 1[https://my.rightscale.com/server_arrays/{id}]
# * _params[:xxx]_ - ex. :user => "www", :port => 2345, etc...
@@ -53,24 +63,24 @@
# Set rightscale's parameters
_array_id = params[:array_id]
params.delete(:array_id) # remove rightscale's parameters
- host_list = use_rs_cache ? get_server_cache(role) : [] # Get cache
+ host_list = use_rs_cache ? get_cache_instance.load_server_cache(role, @caller) : [] # Get cache
if host_list && host_list.size > 0
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.__send__(:array, _array_id)
+ array = get_rs_instance.array(_array_id)
logger.info("querying rightscale for server_array #{array.nickname}...")
- dept = get_rs_instance.__send__(:deployment, array.deployment_href.match(/[0-9]+$/).to_s, :server_settings => 'true')
+ 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}:")
- host_list = get_rs_instance.__send__(:array_instances, array.id).select {|i| i[:state] == "operational"}.map do |instance|
+ host_list = get_rs_instance.array_instances(array.id).select {|i| i[:state] == "operational"}.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})")
@@ -78,11 +88,11 @@
end
host_list = RSUtils.valid_echo(host_list, logger) if validate_echo
if host_list && host_list.size > 0
role(role, params) { host_list }
- dump_server_cache(role, host_list) if use_rs_cache # Dump cache
+ get_cache_instance.dump_server_cache(role, host_list, @caller) if use_rs_cache # Dump cache
end
end
puts "Time: #{Time.now - start}"
host_list || []
end
@@ -109,18 +119,18 @@
_name_prefix = params[:name_prefix]
params.delete(:deployment)
params.delete(:name_prefix) if params.has_key?(:name_prefix)
- host_list = use_rs_cache ? get_server_cache(role) : [] # Get cache
+ host_list = use_rs_cache ? get_cache_instance.load_server_cache(role, @caller) : [] # Get cache
if host_list && host_list.size > 0
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')
+ dept = get_rs_instance.deployment(_dept_id, :server_settings => 'true')
logger.info("querying rightscale for servers #{_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
host_list = srvs.map do |server|
@@ -133,11 +143,11 @@
end
host_list = RSUtils.valid_echo(host_list, logger) if validate_echo
if host_list && host_list.size > 0
role(role, params) { host_list }
- dump_server_cache(role, host_list) if use_rs_cache # Dump cache
+ get_cache_instance.dump_server_cache(role, host_list, @caller) if use_rs_cache # Dump cache
end
end
puts "Time: #{Time.now - start}"
host_list || []
end
@@ -165,11 +175,11 @@
_tags = params[:tags]
params.delete(:deployment)
params.delete(:tags)
- host_list = use_rs_cache ? get_server_cache(role) : [] # Get cache
+ host_list = use_rs_cache ? get_cache_instance.load_server_cache(role, @caller) : [] # Get cache
if host_list && host_list.size > 0
logger.info("restore cache of servers:\n#{host_list.pretty_inspect}")
role(role, params) { host_list } # set cache to role()
else
@@ -200,11 +210,11 @@
host_list = RSUtils.valid_echo(host_list, logger) if validate_echo
end
if host_list && host_list.size > 0
role(role, params) { host_list }
- dump_server_cache(role, host_list) if use_rs_cache # Dump cache
+ get_cache_instance.dump_server_cache(role, host_list, @caller) if use_rs_cache # Dump cache
end
end
puts "Time: #{Time.now - start}"
host_list || []
end
@@ -212,59 +222,9 @@
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_server_cache(role)
- @lifetime ||= 86400
- server_cache = self.instance_variable_get("@#{role}_cache")
-
- begin
- cache_files = Dir.glob("#{Dir.tmpdir}/cap-rightscale-#{ENV['USER']}-*/#{@caller}*#{role}")
-
- if cache_files.size > 0 && !server_cache
- c = Marshal.load(open(cache_files.first) {|f| f.read})
- self.instance_variable_set("@#{role}_cache", c)
- end
- server_cache = self.instance_variable_get("@#{role}_cache")
- return [] unless server_cache # No cache entry
-
- # get servers
- if Time.now - server_cache[role][:cache] > @lifetime
- server_list = []
- elsif server_cache[role][:servers]
- server_list = server_cache[role][:servers]
- else
- server_list = []
- end
- rescue => e
- return [] unless server_cache
- end
-
- server_list
- end
-
- def dump_server_cache(role, servers)
- h = {role => {:servers => servers, :cache => Time.now}}
- obj_dump = Marshal.dump(h)
-
- # Get cache directory
- cache_dir = Dir.glob("#{Dir.tmpdir}/cap-rightscale-#{ENV['USER']}-*").first
- if cache_dir.nil?
- RSUtils.mk_rs_cache_dir
- cache_dir = Dir.glob("#{Dir.tmpdir}/cap-rightscale-#{ENV['USER']}-*").first
- exit if cache_dir.nil?
- end
- cache_file = File.join(cache_dir, "#{@caller}-#{role}")
-
- begin
- open(cache_file, "w") {|f| f.write(obj_dump)}
- rescue => e
- STDERR.puts("#{e.class}: #{e.pretty_inspect}")
- warn("Backtrace:\n#{e.backtrace.pretty_inspect}")
- end
end
def validate_echo
@validate_echo ||= false
end