Skip to content

Instantly share code, notes, and snippets.

@arika
Created August 25, 2010 12:04
Show Gist options
  • Select an option

  • Save arika/549369 to your computer and use it in GitHub Desktop.

Select an option

Save arika/549369 to your computer and use it in GitHub Desktop.
#!/usr/bin/ruby
require 'rubygems'
gem 'capistrano'
require 'capistrano/cli'
class Capdo < Capistrano::CLI
def option_parser
return @option_parser if @option_parser
super
@option_parser.on(
"-u USER",
"Specify username to run the command line"
) do |user|
options[:user] = user
end
@option_parser.on(
"-H host,...", Array,
"Specify target hosts"
) do |hosts|
options[:hosts] = hosts
end
@option_parser.on(
"-L role,...", Array,
"Specify target roles"
) do |roles|
options[:roles] = roles
end
options[:env] ||= {}
@option_parser.on(
"-E NAME=VALUE",
"Set a environment variable"
) do |pair|
name, value = pair.split(/=/, 2)
options[:env][name] = value
end
@option_parser
end
def execute_requested_actions(config)
cmd = options[:actions].join(' ')
user = options[:user]
if options[:hosts]
opt = {:hosts => options[:hosts]}
elsif options[:roles]
opt = {:roles => options[:roles]}
end
env = nil
unless options[:env].empty?
env = options[:env]
end
config.task(:"command line", opt) do
if user
sudo cmd, :as => user, :pty => true, :env => env
else
run cmd, :env => env
end
end
options[:actions].replace ["command line"]
super
end
end
Capdo.execute
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment