Class: Chef::Knife::ClusterSsh

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

Direct Known Subclasses

ClusterKick

Instance Method Summary collapse

Methods included from ClusterChef::KnifeCommon

#bootstrapper, #configure_dry_run, #confirm_execution, #confirm_or_exit, #die, #display, #get_relevant_slice, #get_slice, included, #load_cluster_chef, load_deps, #predicate_str, #progressbar_for_threads, #relevant?, #run_bootstrap, #section, #sub_command

Instance Method Details

#configure_sessionObject



43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/chef/knife/cluster_ssh.rb', line 43

def configure_session
  predicate = @name_args[0].split(/[\s\-]+/,3).map(&:strip)

  target = get_slice(*predicate).select(&:sshable?)

  display(target) if config[:verbose]

  config[:attribute]     ||= Chef::Config[:knife][:ssh_address_attribute] || "fqdn"
  config[:ssh_user]      ||= Chef::Config[:knife][:ssh_user]
  config[:identity_file] ||= target.ssh_identity_file

  @action_nodes = target.chef_nodes
  addresses = target.servers.map do |svr|
    if (svr.cloud.public_ip)             then address = svr.cloud.public_ip ; end
    if (not address) && (svr.fog_server) then address = svr.fog_server.public_ip_address ; end
    if (not address) && (svr.chef_node)  then address = format_for_display( svr.chef_node )[config[:attribute]] ; end
    address
  end.compact

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

  session_from_list(addresses)
end

#csshObject



114
115
116
# File 'lib/chef/knife/cluster_ssh.rb', line 114

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)



71
72
73
74
75
76
77
78
79
# File 'lib/chef/knife/cluster_ssh.rb', line 71

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

#runObject



118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
# File 'lib/chef/knife/cluster_ssh.rb', line 118

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

  @longest = 0
  configure_session

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

  session.close
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



85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
# File 'lib/chef/knife/cluster_ssh.rb', line 85

def ssh_command(command, subsession=nil)
  subsession ||= session
  command = fixup_sudo(command)
  notifications = []
  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])
          notifications << [ichannel[:host], "'#{command_snippet.gsub(/[\r\n]+/, "; ")}' terminated with error status #{exit_status}", :err]
        end
      end
    end
  end
  session.loop
  notifications.each{|args| print_data(*args) }
end