Module: Chee::DSL

Included in:
Chee
Defined in:
lib/Chee.rb

Instance Method Summary collapse

Instance Method Details



32
33
34
35
# File 'lib/Chee.rb', line 32

def print_data d
  print d
  STDOUT.flush
end

#server(*args) ⇒ Object



25
26
27
28
29
30
# File 'lib/Chee.rb', line 25

def server *args
  return @server if args.empty?
  server_array << args
  server_array.uniq!
  @server = args
end

#server_arrayObject



21
22
23
# File 'lib/Chee.rb', line 21

def server_array
  @server_array ||= []
end

#ssh(raw_cmd) ⇒ Object



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
73
74
75
76
77
78
79
80
81
82
83
84
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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
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
170
171
172
173
174
175
# File 'lib/Chee.rb', line 48

def ssh raw_cmd
  command = raw_cmd.strip
  if command["\n"]
    return command.split("\n").map(&:strip).map { |s|
      ssh s
    }
  end
  stdout = ""
  stderr = ""
  t      = nil # used to store a Thread

  ip, user, new_opts = *server
  opts = {:timeout=>3}.merge(new_opts || {})

  begin

    get_input = true
    @channel  = nil
    cmd       = ''
    prev_cmd  = ''
    prev_data = ''
    result    = Result.new

    t = Thread.new { 

      while get_input do

        cmd = begin
                Readline.readline("", true).strip
              rescue Interrupt => e # Send CTRL-C:
                get_input = false
                "^C"
              end

        if @channel
          @channel.process
        else
          print "Connection closed. Could not send: #{cmd}\n"
        end

      end 

    }

    Net::SSH.start(ip, user, opts) { |ssh|

      @channel = ssh.open_channel do |ch1|

        ch1.on_extended_data do |ch, type, d|
          stderr << d
        end

        ch1.on_request 'exit-status' do |ch, d|
          result.exit_status d.read_long
        end

        ch1.on_open_failed { |ch, code, desc|
          stderr << "Failure to open channel: #{code.inspect}: #{desc}"
        }

        ch1.on_process do |ch|
          if cmd.strip == '^C'
            #ch.close
            ch.send_data( Net::SSH::Buffer.from(:byte, 3, :raw, "\n").to_s )
            stderr << "User requested interrupt."
          else
            if cmd.strip.empty?
              # ignore it
            else
              ch.send_data( "#{cmd}\n" ) 
              prev_cmd = cmd
              cmd = ''
            end
          end
        end

        ch1.on_data do |ch, d|

          stdout << d # .sub(%r!\r?\n\Z!,'')

          unless prev_cmd.to_s.strip == d.strip
            print_data d
          end

          prev_data = d
        end

        ch1.request_pty do |ch, success|
          if success
            # do nothing
          else
            ch.close
            (stderr << "Unknown error requesting pty.") 
          end
        end

        ch1.exec(command)

      end

      ssh.loop 0.1
    } # === Net::SSH.start

  rescue Timeout::Error  => e
    raise e.class, server.inspect

  rescue Net::SSH::AuthenticationFailed => e
    raise e.class, "Using: #{server.inspect}"

  ensure
    get_input = false
    t.exit if t
  end

  result.err stderr
  result.out stdout
  
  if !result.err.empty? || result.exit_status != 0
    e = Exit_Error.new("Exit: #{result.exit_status}, COMMAND: #{command}")
    e.exit_status result.exit_status
    e.out         result.out
    e.err         result.err
    raise e
  end

  result.out.strip!
  result
end

#ssh_to_all(command) ⇒ Object



37
38
39
40
41
42
# File 'lib/Chee.rb', line 37

def ssh_to_all command
  @server_array.map { |opts|
    server *opts
    ssh command
  }.flatten
end