aboutsummaryrefslogtreecommitdiff
path: root/lib/leap_cli/util/remote_command.rb
diff options
context:
space:
mode:
authorelijah <elijah@riseup.net>2012-11-09 01:22:48 -0800
committerelijah <elijah@riseup.net>2012-11-09 01:22:48 -0800
commitfebeb64a801f3b4c72193bc93ee0400dee3a2a0a (patch)
treea31d0efc4f9ef563c3f9e9023d09e9dfb8b954af /lib/leap_cli/util/remote_command.rb
parente0471e278c3baf7fc74f288281c7219cbcf0172c (diff)
downloadleap_cli-febeb64a801f3b4c72193bc93ee0400dee3a2a0a.tar.gz
leap_cli-febeb64a801f3b4c72193bc93ee0400dee3a2a0a.tar.bz2
vagrant support
Diffstat (limited to 'lib/leap_cli/util/remote_command.rb')
-rw-r--r--lib/leap_cli/util/remote_command.rb98
1 files changed, 98 insertions, 0 deletions
diff --git a/lib/leap_cli/util/remote_command.rb b/lib/leap_cli/util/remote_command.rb
new file mode 100644
index 0000000..118a65e
--- /dev/null
+++ b/lib/leap_cli/util/remote_command.rb
@@ -0,0 +1,98 @@
+module LeapCli; module Util; module RemoteCommand
+ extend self
+
+ #
+ # FYI
+ # Capistrano::Logger::IMPORTANT = 0
+ # Capistrano::Logger::INFO = 1
+ # Capistrano::Logger::DEBUG = 2
+ # Capistrano::Logger::TRACE = 3
+ #
+ def ssh_connect(nodes, options={}, &block)
+ node_list = parse_node_list(nodes)
+
+ cap = new_capistrano
+ cap.logger.level = LeapCli.log_level
+ user = options[:user] || 'root'
+ cap.set :user, user
+ cap.set :ssh_options, ssh_options # ssh options common to all nodes
+ cap.set :use_sudo, false # we may want to change this in the future
+
+ # Allow password authentication when we are bootstraping a single node
+ # (and key authentication fails).
+ if options[:bootstrap] && node_list.size == 1
+ hostname = node_list.values.first.name
+ if options[:echo]
+ cap.set(:password) { ask "Root SSH password for #{user}@#{hostname}> " }
+ else
+ cap.set(:password) { Capistrano::CLI.password_prompt " * Typed password will be hidden (use --echo to make it visible)\nRoot SSH password for #{user}@#{hostname}> " }
+ end
+ end
+
+ node_list.each do |name, node|
+ cap.server node.name, :dummy_arg, node_options(node)
+ end
+
+ yield cap
+ end
+
+ private
+
+ #
+ # For available options, see http://net-ssh.github.com/net-ssh/classes/Net/SSH.html#method-c-start
+ #
+ def ssh_options
+ {
+ :config => false,
+ :global_known_hosts_file => path(:known_hosts),
+ :paranoid => true
+ }
+ end
+
+ #
+ # For notes on advanced ways to set server-specific options, see
+ # http://railsware.com/blog/2011/11/02/advanced-server-definitions-in-capistrano/
+ #
+ # if, in the future, we want to do per-node password options, it would be done like so:
+ #
+ # password_proc = Proc.new {Capistrano::CLI.password_prompt "Root SSH password for #{node.name}"}
+ # return {:password => password_proc}
+ #
+ def node_options(node)
+ {
+ :ssh_options => {
+ :host_key_alias => node.name,
+ :host_name => node.ip_address,
+ :port => node.ssh.port
+ }.merge(contingent_ssh_options_for_node(node))
+ }
+ end
+
+ def new_capistrano
+ # load once the library files
+ @capistrano_enabled ||= begin
+ require 'capistrano'
+ require 'capistrano/cli'
+ require 'leap_cli/remote/plugin'
+ Capistrano.plugin :leap, LeapCli::Remote::Plugin
+ true
+ end
+
+ # create capistrano instance
+ cap = Capistrano::Configuration.new
+
+ # add tasks to capistrano instance
+ cap.load File.dirname(__FILE__) + '/../remote/tasks.rb'
+
+ return cap
+ end
+
+ def contingent_ssh_options_for_node(node)
+ if node.vagrant?
+ {:keys => [vagrant_ssh_key_file]}
+ else
+ {}
+ end
+ end
+
+end; end; end \ No newline at end of file