Sha256: fa2b457a1ffba6fbdec77dbd3393a794507a854b834d23bc0bddbb03448d80b9

Contents?: true

Size: 1.9 KB

Versions: 16

Compression:

Stored size: 1.9 KB

Contents

require 'net/ssh/multi'

module Mccloud
  module Command
    #http://stackoverflow.com/questions/1383282/netsshmulti-using-the-environment-exec-how-do-you-get-the-output-straight-away
    #https://gist.github.com/700730
    def multi(selection=nil,command="who am i",options=nil)
      trap("INT") { puts "we hit CTRL_C"; exit }


      Net::SSH::Multi.start do |environment|
        # Connect to remote machines
        ip2name=Hash.new
        on_selected_machines(selection) do |id,vm|
          instance=vm.instance
          if instance.state == "running"
            ip2name[instance.public_ip_address]=vm.name
            environment.use "#{instance.public_ip_address}", { :user => vm.user , :keys => [ vm.private_key ], :paranoid => false, :keys_only => true}
          end
        end

        unless options["sudo"].nil?
          #check vm.sudo
          sudo_cmd="sudo"
          command="#{sudo_cmd} #{command}"
        end

        puts "Executing #{command}"
        begin
          environment.exec("#{command}") do |ch, stream, data|
            #exit code
            #http://groups.google.com/group/comp.lang.ruby/browse_thread/thread/a806b0f5dae4e1e2

            ch.on_request("exit-status") do |ch, data|
              exit_code = data.read_long
              @status=exit_code
              if exit_code > 0
                puts "ERROR: exit code #{exit_code}"
              else
                puts "Successfully executed"
              end
            end
            if (ip2name.count > 1) || options.verbose?
              puts "[#{ip2name[ch[:host]]}] #{data}"
            else
              print "#{data}"              
            end
            # puts "[#{ch[:host]} : #{stream}] #{data}"

          end
        rescue Errno::ECONNREFUSED
          puts "oops - no connection"
        end
        # Tell Net::SSH to wait for output from the SSH server
        environment.loop  
      end

    end
  end
end

Version data entries

16 entries across 16 versions & 1 rubygems

Version Path
mccloud-0.1.1 lib/mccloud/provider/core/vm/old_multi.rb
mccloud-0.0.28 lib/mccloud/provider/core/vm/old_multi.rb
mccloud-0.0.27 lib/mccloud/provider/core/vm/old_multi.rb
mccloud-0.0.26 lib/mccloud/provider/core/vm/old_multi.rb
mccloud-0.0.25 lib/mccloud/provider/core/vm/old_multi.rb
mccloud-0.0.24 lib/mccloud/provider/core/vm/old_multi.rb
mccloud-0.0.23 lib/mccloud/provider/core/vm/old_multi.rb
mccloud-0.0.22 lib/mccloud/provider/core/vm/old_multi.rb
mccloud-0.0.21 lib/mccloud/provider/core/vm/old_multi.rb
mccloud-0.0.20 lib/mccloud/provider/core/vm/old_multi.rb
mccloud-0.0.19 lib/mccloud/provider/core/vm/old_multi.rb
mccloud-0.0.18 lib/mccloud/provider/core/vm/old_multi.rb
mccloud-0.0.17 lib/mccloud/provider/core/vm/old_multi.rb
mccloud-0.0.16 lib/mccloud/provider/core/vm/old_multi.rb
mccloud-0.0.15 lib/mccloud/provider/core/vm/old_multi.rb
mccloud-0.0.14 lib/mccloud/provider/core/vm/old_multi.rb