Module: Capistrano::Configuration::Tags

Included in:
Capistrano::Configuration
Defined in:
lib/capistrano/rightscale.rb

Instance Method Summary collapse

Instance Method Details

#deployment(deployment) ⇒ Object



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
# File 'lib/capistrano/rightscale.rb', line 9

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

#rightscaleObject

rightscale client



52
53
54
55
# File 'lib/capistrano/rightscale.rb', line 52

def rightscale
   @rightscale ||= RightScale::Client.new(
     fetch(:rightscale_account), fetch(:rightscale_username), fetch(:rightscale_password))
end

#rightscale_url(path) ⇒ Object



57
58
59
# File 'lib/capistrano/rightscale.rb', line 57

def rightscale_url(path)
  "https://my.rightscale.com/api/acct/%d%s" % [ fetch(:rightscale_account), path ]
end

#tag(which, *args) ⇒ Object

associate a tag in a specific deployment with a role e.g:

tag "x99:role=app", :app, :deployment => 45678


28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/capistrano/rightscale.rb', line 28

def tag(which, *args)
  cache_file = '%s/.rightscale%d' % [ ENV['HOME'], 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

  servers.each do |server|
    server(server['dns_name'], *args) if server['tags'].include?(which)
  end
end

#tags_for_server(server) ⇒ Object

query rightscale for tags for a running instance



45
46
47
48
49
# File 'lib/capistrano/rightscale.rb', line 45

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.collect { |x| x["name"] }
end