Class: Chef::Knife::ClusterSsh

Inherits:
Ssh
  • Object
show all
Includes:
Ironfan::KnifeCommon
Defined in:
lib/chef/knife/cluster_ssh.rb

Direct Known Subclasses

ClusterKick

Instance Attribute Summary

Attributes included from Ironfan::KnifeCommon

#broker, #problems

Instance Method Summary collapse

Methods included from Ironfan::KnifeCommon

#all_computers, #bootstrapper, #configure_dry_run, #confirm_execution, #confirm_or_exit, #die, #discover_computers, #display, #exit_if_unhealthy!, #gemfile, #get_relevant_slice, #get_slice, #has_problem, #healthy?, included, load_deps, #load_ironfan, #pick_apart, #predicate_str, #progressbar_for_threads, #relevant?, #run, #run_bootstrap, #section, #sub_command, #wait_for_ssh

Instance Method Details

#_runObject



125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
# File 'lib/chef/knife/cluster_ssh.rb', line 125

def _run
  load_ironfan
  die(banner) if @name_args.empty?
  extend Chef::Mixin::Command

  @longest = 0
  configure_session

  case @name_args[1]
  when "screen",nil   then screen
  when "interactive"  then interactive
  when "tmux"         then tmux
  when "macterm"      then macterm
  when "cssh"         then cssh
  else
    ssh_command(@name_args[1..-1].join(" "))
  end

  session.close
  exit_if_unhealthy!
end

#configure_sessionObject



47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/chef/knife/cluster_ssh.rb', line 47

def configure_session
  target = get_slice(@name_args[0]).select(&:running?)

  display(target) if config[:verbose] || config[:display_target]

  config[:attribute]     ||= Chef::Config[:knife][:ssh_address_attribute] || "fqdn"
  config[:ssh_user]      ||= Chef::Config[:knife][:ssh_user]

  target = target.select {|t| not t.bogus? }
  addresses = target.map {|c| c.machine.vpc_id.nil? ? c.machine.public_hostname : c.machine.public_ip_address }.compact

  (ui.fatal("No nodes returned from search!"); exit 10) if addresses.nil? || addresses.length == 0

  # Need to include both public host and public ip; sometimes these are different
  @hostname_to_ironfan_hostname = target.to_a.inject({}) do |remap, c|
    remap[c.machine.public_hostname]   = c.machine.tags['Name'] || c.name
    remap[c.machine.public_ip_address] = c.machine.tags['Name'] || c.name
    remap
  end

  @longest_ironfan_hostname = @hostname_to_ironfan_hostname.values.group_by(&:size).max.last[0].size

  @action_nodes = Chef::Search::Query.new.search(:node, "node_name:#{@name_args[0]}*")[0]

  session_from_list(addresses)
end

#csshObject



121
122
123
# File 'lib/chef/knife/cluster_ssh.rb', line 121

def cssh
  exec "cssh "+session.servers_for.map{|server| server.user ? "#{server.user}@#{server.host}" : server.host}.join(" ")
end

Override the one in Chef::Knife::Ssh to allow an err flag (prints in red if non-null)



78
79
80
81
82
83
84
85
86
87
# File 'lib/chef/knife/cluster_ssh.rb', line 78

def print_data(host, data, err=nil)
  display_hostname = @hostname_to_ironfan_hostname[host]
  if data =~ /\n/
    data.split(/\n/).each { |d| print_data(host, d, err) }
  else
    padding = @longest_ironfan_hostname - display_hostname.length
    str = ui.color(display_hostname, :cyan) + (" " * (padding + 1)) + (err ? ui.color(data, :red) : data)
    ui.msg(str)
  end
end

#ssh_command(command, subsession = nil) ⇒ Object

Override the one in Chef::Knife::Ssh to give a big red warning if the process executes with badness



93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
# File 'lib/chef/knife/cluster_ssh.rb', line 93

def ssh_command(command, subsession=nil)
  subsession ||= session
  command = fixup_sudo(command)
  #
  subsession.open_channel do |ch|
    ch.request_pty
    ch.exec command do |ch, success|
      raise ArgumentError, "Cannot execute #{command}" unless success
      # note: you can't do the stderr calback because requesting a pty
      # squashes stderr and stdout together
      ch.on_data do |ichannel, data|
        print_data(ichannel[:host], data)
        if data =~ /^knife sudo password: /
          ichannel.send_data("#{get_password}\n")
        end
      end
      ch.on_request "exit-status" do |ichannel, data|
        exit_status = data.read_long
        if exit_status != 0
          command_snippet = (command.length < 70) ? command : (command[0..45] + ' ... ' + command[-19..-1])
          has_problem ->{ print_data(ichannel[:host], "'#{command_snippet.gsub(/[\r\n]+/, "; ")}' terminated with error status #{exit_status}", :err) }
        end
      end
    end
  end
  session.loop
end