Class: Chef::Knife::CloudSsh
- Inherits:
-
Chef::Knife
- Object
- Chef::Knife
- Chef::Knife::CloudSsh
- Includes:
- Mixin::ShellOut
- Defined in:
- lib/knife/undev/plugins/cloud_ssh.rb
Instance Attribute Summary collapse
-
#password ⇒ Object
writeonly
Sets the attribute password.
Instance Method Summary collapse
- #configure_attribute ⇒ Object
- #configure_gateway ⇒ Object
- #configure_identity_file ⇒ Object
- #configure_session ⇒ Object
- #configure_user ⇒ Object
- #extract_nested_value(data_structure, path_spec) ⇒ Object
- #fixup_sudo(command) ⇒ Object
- #get_password ⇒ Object
- #get_stripped_unfrozen_value(value) ⇒ Object
- #interactive ⇒ Object
- #print_all_status ⇒ Object
- #print_data(host, data) ⇒ Object
- #print_line(host, data) ⇒ Object
- #prompt_for_password(prompt = "Enter your password: ") ⇒ Object
- #read_line ⇒ Object
- #reader ⇒ Object
- #run ⇒ Object
- #session ⇒ Object
- #session_from_list(list) ⇒ Object
- #ssh_command(command, subsession = nil) ⇒ Object
Instance Attribute Details
#password=(value) ⇒ Object (writeonly)
Sets the attribute password
26 27 28 |
# File 'lib/knife/undev/plugins/cloud_ssh.rb', line 26 def password=(value) @password = value end |
Instance Method Details
#configure_attribute ⇒ Object
266 267 268 269 270 271 |
# File 'lib/knife/undev/plugins/cloud_ssh.rb', line 266 def configure_attribute config[:override_attribute] = config[:attribute] || Chef::Config[:knife][:ssh_attribute] config[:attribute] = (Chef::Config[:knife][:ssh_attribute] || config[:attribute] || "private_ipaddress").strip end |
#configure_gateway ⇒ Object
114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 |
# File 'lib/knife/undev/plugins/cloud_ssh.rb', line 114 def configure_gateway config[:ssh_gateway] ||= Chef::Config[:knife][:ssh_gateway] if config[:ssh_gateway] gw_host, gw_user = config[:ssh_gateway].split('@').reverse gw_host, gw_port = gw_host.split(':') gw_opts = gw_port ? { :port => gw_port } : {} session.via(gw_host, gw_user || config[:ssh_user], gw_opts) end rescue Net::SSH::AuthenticationFailed user = gw_user || config[:ssh_user] prompt = "Enter the password for #{user}@#{gw_host}: " gw_opts.merge!(:password => prompt_for_password(prompt)) session.via(gw_host, user, gw_opts) end |
#configure_identity_file ⇒ Object
333 334 335 336 |
# File 'lib/knife/undev/plugins/cloud_ssh.rb', line 333 def configure_identity_file config[:identity_file] = get_stripped_unfrozen_value(config[:identity_file] || Chef::Config[:knife][:ssh_identity_file]) end |
#configure_session ⇒ Object
130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 |
# File 'lib/knife/undev/plugins/cloud_ssh.rb', line 130 def configure_session list = case config[:manual] when true @all_nodes += @name_args[0].split(" ") @name_args[0].split(" ") when false r = Array.new q = Chef::Search::Query.new @action_nodes = q.search(:node, @name_args[0])[0] @action_nodes.each do |item| # we should skip the loop to next iteration if the item returned by the search is nil next if item.nil? # if a command line attribute was not passed, and we have a cloud public_hostname, use that. # see #configure_attribute for the source of config[:attribute] and config[:override_attribute] if !config[:override_attribute] && item[:cloud] and item[:cloud][:public_hostname] i = item[:cloud][:public_hostname] elsif config[:override_attribute] i = extract_nested_value(item, config[:override_attribute]) else i = extract_nested_value(item, config[:attribute]) end # next if we couldn't find the specified attribute in the returned node object next if i.nil? r.push(i) @all_nodes << item[:fqdn] end r end if list.length == 0 if @action_nodes.length == 0 ui.fatal("No nodes returned from search!") else ui.fatal("#{@action_nodes.length} #{@action_nodes.length > 1 ? "nodes":"node"} found, " + "but does not have the required attribute to establish the connection. " + "Try setting another attribute to open the connection using --attribute.") end exit 10 end session_from_list(list) end |
#configure_user ⇒ Object
328 329 330 331 |
# File 'lib/knife/undev/plugins/cloud_ssh.rb', line 328 def configure_user config[:ssh_user] = get_stripped_unfrozen_value(config[:ssh_user] || Chef::Config[:knife][:ssh_user]) end |
#extract_nested_value(data_structure, path_spec) ⇒ Object
338 339 340 |
# File 'lib/knife/undev/plugins/cloud_ssh.rb', line 338 def extract_nested_value(data_structure, path_spec) ui.presenter.extract_nested_value(data_structure, path_spec) end |
#fixup_sudo(command) ⇒ Object
201 202 203 |
# File 'lib/knife/undev/plugins/cloud_ssh.rb', line 201 def fixup_sudo(command) command.sub(/^sudo/, 'sudo -p \'knife sudo password: \'') end |
#get_password ⇒ Object
258 259 260 |
# File 'lib/knife/undev/plugins/cloud_ssh.rb', line 258 def get_password @password ||= prompt_for_password end |
#get_stripped_unfrozen_value(value) ⇒ Object
323 324 325 326 |
# File 'lib/knife/undev/plugins/cloud_ssh.rb', line 323 def get_stripped_unfrozen_value(value) return nil if value.nil? value.strip end |
#interactive ⇒ Object
294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 |
# File 'lib/knife/undev/plugins/cloud_ssh.rb', line 294 def interactive puts "Connected to #{ui.list(session.servers_for.collect { |s| ui.color(s.host, :cyan) }, :inline, " and ")}" puts puts "To run a command on a list of servers, do:" puts " on SERVER1 SERVER2 SERVER3; COMMAND" puts " Example: on latte foamy; echo foobar" puts puts "To exit interactive mode, use 'quit!'" puts while 1 command = read_line case command when 'quit!' puts 'Bye!' break when /^on (.+?); (.+)$/ raw_list = $1.split(" ") server_list = Array.new session.servers.each do |session_server| server_list << session_server if raw_list.include?(session_server.host) end command = $2 ssh_command(command, session.on(*server_list)) else ssh_command(command) end end end |
#print_all_status ⇒ Object
342 343 344 345 346 347 348 349 350 351 352 |
# File 'lib/knife/undev/plugins/cloud_ssh.rb', line 342 def print_all_status succ = @all_nodes.count - @failed_connect_nodes.count - @not_zerro_nodes.count failed = @failed_connect_nodes.count + @not_zerro_nodes.count @failed_connect_nodes.each { |msg| ui.error("#{msg['node']} #{msg['message']}") } @not_zerro_nodes.each { |msg| ui.error("#{msg['node']} #{msg['message']}") } if failed > 0 || @zerro_nodes.count != succ ui.warn "All: #{@all_nodes.count}, successful exit: #{@zerro_nodes.count}, connection problem: #{@failed_connect_nodes.count}, failed: #{@not_zerro_nodes.count}" else ui.info "All: #{@all_nodes.count}, successful: #{succ}" end end |
#print_data(host, data) ⇒ Object
205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 |
# File 'lib/knife/undev/plugins/cloud_ssh.rb', line 205 def print_data(host, data) @buffers ||= {} if leftover = @buffers[host] @buffers[host] = nil print_data(host, leftover + data) else if newline_index = data.index("\n") line = data.slice!(0...newline_index) data.slice!(0) print_line(host, line) print_data(host, data) else @buffers[host] = data end end end |
#print_line(host, data) ⇒ Object
222 223 224 225 226 |
# File 'lib/knife/undev/plugins/cloud_ssh.rb', line 222 def print_line(host, data) padding = @longest - host.length str = ui.color(host, :cyan) + (" " * (padding + 1)) + data ui.msg(str) end |
#prompt_for_password(prompt = "Enter your password: ") ⇒ Object
262 263 264 |
# File 'lib/knife/undev/plugins/cloud_ssh.rb', line 262 def prompt_for_password(prompt = "Enter your password: ") ui.ask(prompt) { |q| q.echo = false } end |
#read_line ⇒ Object
273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 |
# File 'lib/knife/undev/plugins/cloud_ssh.rb', line 273 def read_line loop do command = reader.readline("#{ui.color('knife-ssh>', :bold)} ", true) if command.nil? command = "exit" puts(command) else command.strip! end unless command.empty? return command end end end |
#reader ⇒ Object
290 291 292 |
# File 'lib/knife/undev/plugins/cloud_ssh.rb', line 290 def reader Readline end |
#run ⇒ Object
354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 |
# File 'lib/knife/undev/plugins/cloud_ssh.rb', line 354 def run extend Chef::Mixin::Command @longest = 0 @all_nodes = [] @failed_connect_nodes = [] @not_zerro_nodes = [] @zerro_nodes = [] configure_attribute configure_user configure_identity_file configure_gateway configure_session exit_status = case @name_args[1] when "interactive" interactive else ssh_command(@name_args[1..-1].join(" ")) end session.close print_all_status if (@not_zerro_nodes + @failed_connect_nodes).compact.empty? exit_status else my_status = exit_status == 0 ? 1 : exit_status exit my_status end end |
#session ⇒ Object
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/knife/undev/plugins/cloud_ssh.rb', line 89 def session config[:on_error] ||= :skip ssh_error_handler = Proc.new do |server| @failed_connect_nodes << { "node" => server.host , "message" => "#{$!.class.name}: #{$!.}" } if config[:manual] node_name = server.host else @action_nodes.each do |n| node_name = n if format_for_display(n)[config[:attribute]] == server.host end end case config[:on_error] when :skip ui.warn "Failed to connect to #{node_name} -- #{$!.class.name}: #{$!.}" $!.backtrace.each { |l| Chef::Log.error(l) } when :raise #Net::SSH::Multi magic to force exception to be re-raised. throw :go, :raise end end @session ||= Net::SSH::Multi.start(:concurrent_connections => config[:concurrency], :on_error => ssh_error_handler) end |
#session_from_list(list) ⇒ Object
171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 |
# File 'lib/knife/undev/plugins/cloud_ssh.rb', line 171 def session_from_list(list) list.each do |item| Chef::Log.debug("Adding #{item}") session_opts = {} ssh_config = Net::SSH.configuration_for(item) # Chef::Config[:knife][:ssh_user] is parsed in #configure_user and written to config[:ssh_user] user = config[:ssh_user] || ssh_config[:user] hostspec = user ? "#{user}@#{item}" : item session_opts[:keys] = File.(config[:identity_file]) if config[:identity_file] session_opts[:keys_only] = true if config[:identity_file] session_opts[:password] = config[:ssh_password] if config[:ssh_password] session_opts[:forward_agent] = config[:forward_agent] session_opts[:port] = config[:ssh_port] || Chef::Config[:knife][:ssh_port] || ssh_config[:port] session_opts[:logger] = Chef::Log.logger if Chef::Log.level == :debug if !config[:host_key_verify] session_opts[:paranoid] = false session_opts[:user_known_hosts_file] = "/dev/null" end session.use(hostspec, session_opts) @longest = item.length if item.length > @longest end session end |
#ssh_command(command, subsession = nil) ⇒ Object
228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 |
# File 'lib/knife/undev/plugins/cloud_ssh.rb', line 228 def ssh_command(command, subsession=nil) exit_status = 0 subsession ||= session command = fixup_sudo(command) command.force_encoding('binary') if command.respond_to?(:force_encoding) subsession.open_channel do |ch| ch.request_pty ch.exec command do |ch, success| raise ArgumentError, "Cannot execute #{command}" unless success ch.on_data do |ichannel, data| print_data(ichannel[:host], data) if data =~ /^knife sudo password: / print_data(ichannel[:host], "\n") ichannel.send_data("#{get_password}\n") end end ch.on_request "exit-status" do |ichannel, data| exit_status = [exit_status, data.read_long].max if exit_status != 0 @not_zerro_nodes << { 'node' => ichannel[:host], "message" => "exit status: #{exit_status}" } else @zerro_nodes << {'node' => ichannel[:host]} end end end end session.loop exit_status end |