From 404353dcf345122c0f04555a572efc5417f1b661 Mon Sep 17 00:00:00 2001 From: elijah Date: Tue, 11 Jun 2013 09:41:26 -0700 Subject: added --ip and --port override flags to deploy and init. --- lib/leap_cli/commands/deploy.rb | 8 +++++++- lib/leap_cli/commands/node.rb | 22 +++++++++++++++------- lib/leap_cli/commands/shell.rb | 16 ++++++++++++++++ lib/leap_cli/util/remote_command.rb | 8 +++++--- 4 files changed, 43 insertions(+), 11 deletions(-) (limited to 'lib') diff --git a/lib/leap_cli/commands/deploy.rb b/lib/leap_cli/commands/deploy.rb index 12e8294..8130111 100644 --- a/lib/leap_cli/commands/deploy.rb +++ b/lib/leap_cli/commands/deploy.rb @@ -18,6 +18,12 @@ module LeapCli c.flag :tags, :desc => 'Specify tags to pass through to puppet (overriding the default).', :default_value => DEFAULT_TAGS.join(','), :arg_name => 'TAG[,TAG]' + c.flag :port, :desc => 'Override the default SSH port.', + :arg_name => 'PORT' + + c.flag :ip, :desc => 'Override the default SSH IP address.', + :arg_name => 'IPADDRESS' + c.action do |global,options,args| init_submodules @@ -31,7 +37,7 @@ module LeapCli compile_hiera_files(nodes) - ssh_connect(nodes) do |ssh| + ssh_connect(nodes, connect_options(options)) do |ssh| ssh.leap.log :checking, 'node' do ssh.leap.assert_initialized end diff --git a/lib/leap_cli/commands/node.rb b/lib/leap_cli/commands/node.rb index 12c9500..4b5ea9e 100644 --- a/lib/leap_cli/commands/node.rb +++ b/lib/leap_cli/commands/node.rb @@ -51,14 +51,19 @@ module LeapCli; module Commands node.arg_name 'FILTER' #, :optional => false, :multiple => false node.command :init do |init| init.switch 'echo', :desc => 'If set, passwords are visible as you type them (default is hidden)', :negatable => false + init.switch 'noping', :desc => 'If set, skip initial ping of node (in case ICMP is being blocked).', :negatable => false + init.flag :port, :desc => 'Override the default SSH port.', :arg_name => 'PORT' + init.flag :ip, :desc => 'Override the default SSH IP address.', :arg_name => 'IPADDRESS' + init.action do |global,options,args| assert! args.any?, 'You must specify a FILTER' finished = [] manager.filter!(args).each_node do |node| - ping_node(node) - save_public_host_key(node, global) + ping_node(node, options) unless options[:noping] + save_public_host_key(node, global, options) update_compiled_ssh_configs - ssh_connect(node, :bootstrap => true, :echo => options[:echo]) do |ssh| + ssh_connect_options = connect_options(options).merge({:bootstrap => true, :echo => options[:echo]}) + ssh_connect(node, ssh_connect_options) do |ssh| ssh.install_authorized_keys ssh.install_prerequisites ssh.leap.capture(facter_cmd) do |response| @@ -148,9 +153,11 @@ module LeapCli; module Commands # # see `man sshd` for the format of known_hosts # - def save_public_host_key(node, global) + def save_public_host_key(node, global, options) log :fetching, "public SSH host key for #{node.name}" - public_key = get_public_key_for_ip(node.ip_address, node.ssh.port) + address = options[:ip] || node.ip_address + port = options[:port] || node.ssh.port + public_key = get_public_key_for_ip(address, port) pub_key_path = Path.named_path([:node_ssh_pub_key, node.name]) if Path.exists?(pub_key_path) if public_key == SshKey.load_from_file(pub_key_path) @@ -187,9 +194,10 @@ module LeapCli; module Commands return SshKey.load(public_key, key_type) end - def ping_node(node) + def ping_node(node, options) + ip = options[:ip] || node.ip_address log :pinging, node.name - assert_run!("ping -W 1 -c 1 #{node.ip_address}", "Could not ping #{node.name} (address #{node.ip_address}). Try again, we only send a single ping.") + assert_run!("ping -W 1 -c 1 #{ip}", "Could not ping #{node.name} (address #{ip}). Try again, we only send a single ping.") end def seed_node_data(node, args) diff --git a/lib/leap_cli/commands/shell.rb b/lib/leap_cli/commands/shell.rb index 2822481..822ef05 100644 --- a/lib/leap_cli/commands/shell.rb +++ b/lib/leap_cli/commands/shell.rb @@ -16,6 +16,22 @@ module LeapCli; module Commands end end + protected + + # + # allow for ssh overrides of all commands that use ssh_connect + # + def connect_options(options) + connect_options = {:ssh_options=>{}} + if options[:port] + connect_options[:ssh_options][:port] = options[:port] + end + if options[:ip] + connect_options[:ssh_options][:host_name] = options[:ip] + end + return connect_options + end + private def exec_ssh(cmd, args) diff --git a/lib/leap_cli/util/remote_command.rb b/lib/leap_cli/util/remote_command.rb index db02037..2c77196 100644 --- a/lib/leap_cli/util/remote_command.rb +++ b/lib/leap_cli/util/remote_command.rb @@ -9,6 +9,7 @@ module LeapCli; module Util; module RemoteCommand # Capistrano::Logger::TRACE = 3 # def ssh_connect(nodes, options={}, &block) + options ||= {} node_list = parse_node_list(nodes) cap = new_capistrano @@ -30,7 +31,7 @@ module LeapCli; module Util; module RemoteCommand end node_list.each do |name, node| - cap.server node.name, :dummy_arg, node_options(node) + cap.server node.name, :dummy_arg, node_options(node, options[:ssh_options]) end yield cap @@ -58,13 +59,14 @@ module LeapCli; module Util; module RemoteCommand # password_proc = Proc.new {Capistrano::CLI.password_prompt "Root SSH password for #{node.name}"} # return {:password => password_proc} # - def node_options(node) + def node_options(node, ssh_options_override=nil) + ssh_options_override ||= {} { :ssh_options => { :host_key_alias => node.name, :host_name => node.ip_address, :port => node.ssh.port - }.merge(contingent_ssh_options_for_node(node)) + }.merge(contingent_ssh_options_for_node(node)).merge(ssh_options_override) } end -- cgit v1.2.3