Skip to content
nathan-opscode edited this page Sep 12, 2010 · 1 revision

You can tie iClassify in to Capistrano, giving you the power of it’s full text search engine for your Capistrano tasks.

Stick this in your Capfile, and go to town.


require ‘iclassify’

default_run_options[:pty] = true

set(:query, ENV[“QUERY”]) if ENV.has_key?(“QUERY”)
set(:query) do
Capistrano::CLI.ui.ask "iClassify Query: "
end unless exists?(:query)

set(:ic_password, ENV[“IC_PASSWORD”]) if ENV.has_key?(“IC_PASSWORD”)
set(:ic_password) do
Capistrano::CLI.password_prompt "iClassify Password: "
end unless exists?(:ic_password)

if ENV.has_key?(‘IC_USER’)
set(:ic_user, ENV[“IC_USER”])
else
set(:ic_user, ‘admin’);
end

if ENV.has_key?(‘IC_SERVER’)
set(:ic_server, ENV[“IC_SERVER”])
else
set(:ic_server, ‘https://iclassify/’)
end

ic= IClassify::Client.new(ic_server, ic_user, ic_password)
ic_nodes = ic.search(query, [ ‘fqdn’ ])

ic_nodes.each do |node|
role :nodes, node.attrib?(‘fqdn’)
end

desc <<-DESC
Invoke a single command on the remote servers. This is useful for performing \
one-off commands that may not require a full task to be written for them. \
Simply specify the command to execute via the COMMAND environment variable. \
To execute the command only on certain roles, specify the ROLES environment \
variable as a comma-delimited list of role names. Alternatively, you can \
specify the HOSTS environment variable as a comma-delimited list of hostnames \
to execute the task on those hosts, explicitly. Lastly, if you want to \
execute the command via sudo, specify a non-empty value for the SUDO \
environment variable.

Sample usage: $ cap COMMAND=uptime HOSTS=foo.capistano.test invoke $ cap ROLES=app,web SUDO=1 COMMAND=“tail -f /var/log/messages” invoke

DESC
task :invoke do
command = ENV[“COMMAND”] || ""
abort “Please specify a command to execute on the remote servers (via the COMMAND environment variable)” if command.empty?
method = ENV[“SUDO”] ? :sudo : :run
invoke_command(command, :via => method)
end

desc <<-DESC
Begin an interactive Capistrano session. This gives you an interactive \
terminal from which to execute tasks and commands on all of your servers. \
(This is still an experimental feature, and is subject to change without \
notice!)

Sample usage: $ cap shell

DESC
task :shell do
require ‘capistrano/shell’
Capistrano*Shell.run(self)
end

desc <<-DESC
Run puppet on each host that matches your query. Defaults to \
using the fqdn of the current host for the server. Override with \
SERVER.

You can specify a —tags value with TAGS.

DESC
task :puppet do
puppet_server = nil
if ENV.has_key?(“SERVER”)
puppet_server = ENV[“SERVER”]
else
puppet_server = `hostname f`.chomp!
end
tags = ""
if ENV.has_key?(“TAGS”)
tags = "
-tags #{ENV[“TAGS”]}"
end
sudo(“sleep $(expr $RANDOM \% 10); sudo puppetd —onetime —verbose —ignorecache —server #{puppet_server} #{tags}”)
end