Integrating Capistrano 2.x

This is a sample showing how to use the information of your hosts registered to Mackerel as deploy target hosts for Capistrano 2.x.

We will use the mackerel-client gemfile.

Appending the gemfile:

gem 'mackerel-client'

From Capistrano

config/deploy.rb (Capfile):

load 'deploy'

require 'mackerel/client'

set :mackerel_api_key, MACKEREL_API_KEY
set :service, SERVICE_NAME

@client = mackerel_api_key)

def host_ip_addrs(role)
  hosts = @client.get_hosts(service: service, roles: role).select do |host|
    host.status === 'standby' || host.status === 'working' do |host|
    interface = host.interfaces.find { |i| /^eth/ === i['name'] }
    interface['ipAddress'] if interface {|ipaddr| ipaddr != nil }

role :app do

role :db do

Designate the name of the service registered to Mackerel in SERVICE_NAME.

The hosts that belong to that service in Mackerel can be assigned as Capistrano roles.

  • Hosts whose statuses are either working or standby can be listed.
  • As for the host IP addresses, information collected by the agent will be used.

Here we will define the task.

desc "Mackerel integration test"
namespace :deploy do
  desc "echo message"
  task :echo , roles => :app do
    run "echo \"Integration Test\""

Executing the task will yield the following results.

# bundle exec cap deploy:echo
  * 2014-09-23 21:36:28 executing `deploy:echo'
  * executing "echo \"Integration Test\""
    servers: [""]
    [] executing command
 ** [out ::] Integration Test
    command finished in 435ms