Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Added support for caching deployments

  • Loading branch information...
commit f7ac94ce20c163a9c447e82416b889afdf115ce5 1 parent a01d6b9
@lox lox authored
Showing with 40 additions and 15 deletions.
  1. +2 −1  README.markdown
  2. +38 −14 lib/capistrano/rightscale.rb
View
3  README.markdown
@@ -9,7 +9,8 @@ Introduction
[RightScale](http://www.rightscale.com) provides a service for managing deployments of servers in various clouds. Servers can be tagged with
machine tags. This plugin allows for specific tags in specific deployments to be mapped to Capistrano roles.
-At present these mappings require several api calls, which are slow.
+At present these mappings require several api calls, which are slow. On the first call, a cache file is written in the users
+home directory. This can be disabled with an ENV variable RIGHTSCALE_CACHE=false.
Installation
============
View
52 lib/capistrano/rightscale.rb
@@ -1,35 +1,59 @@
require 'rightscale-api'
+require 'json'
+require 'fileutils'
module Capistrano
class Configuration
module Tags
- # Associate a tag in a specific deployment with a role
- # Examples:
+ def deployment(deployment)
+ logger.info "querying rightscale for deployment %s" % deployment
+ deployment = rightscale.deployments.index.find { |d| d['href'] == rightscale_url("/deployments/%d" % deployment) }
+ servers = []
+ deployment['servers'].each do |server|
+ if server['state'] == 'operational'
+ settings = rightscale.servers.settings(server['href'])
+ servers.push << {
+ 'dns_name' => settings['dns_name'],
+ 'tags' => tags_for_server(server)
+ }
+ end
+ end
+ servers
+ end
+
+ # associate a tag in a specific deployment with a role
+ # e.g:
# tag "x99:role=app", :app, :deployment => 45678
def tag(which, *args)
- @rightscale ||= RightScale::Client.new(fetch(:rightscale_account), fetch(:rightscale_username), fetch(:rightscale_password))
- @deployments ||= @rightscale.deployments
- @servers ||= @rightscale.servers
+ cache_file = '%s/.rightscale%d' % [ ENV['HOME'], args[1][:deployment] ]
- logger.info "querying rightscale for deployment %s" % args[1][:deployment]
- deployment = @deployments.index.find { |d| d['href'] == rightscale_url("/deployments/%d" % args[1][:deployment]) }
+ if File.exist?(cache_file) and ENV.fetch('RIGHTSCALE_CACHE', 'true') == 'true'
+ logger.info("cache file found for deployment %d" % args[1][:deployment]);
+ servers = JSON.parse(File.open(cache_file, "r").read)
+ else
+ servers = deployment(args[1][:deployment])
+ File.open(cache_file, 'w') {|f| f.write(servers.to_json) }
+ end
- deployment['servers'].each do |server|
- if server['state'] == 'operational' && tags_for_server(server).include?(which)
- settings = @servers.settings(server['href'])
- logger.info "found server %s (%s) with tag %s" % [ settings['dns_name'], server['nickname'], which ]
- server(settings['dns_name'], *args)
- end
+ servers.each do |server|
+ server(server['dns_name'], *args) if server['tags'].include?(which)
end
end
+ # query rightscale for tags for a running instance
def tags_for_server(server)
instance_id = server["current_instance_href"].split('/').last
- tags = @rightscale.get(rightscale_url("/tags/search.js?resource_href=/ec2_instances/%d" % instance_id))
+ tags = rightscale.get(rightscale_url("/tags/search.js?resource_href=/ec2_instances/%d" % instance_id))
tags.collect { |x| x["name"] }
end
+ # rightscale client
+ def rightscale
+ @rightscale ||= RightScale::Client.new(
+ fetch(:rightscale_account), fetch(:rightscale_username), fetch(:rightscale_password))
+ end
+
def rightscale_url(path)
"https://my.rightscale.com/api/acct/%d%s" % [ fetch(:rightscale_account), path ]
end
Please sign in to comment.
Something went wrong with that request. Please try again.