Class: RobotArmy::Connection

Inherits:
Object
  • Object
show all
Defined in:
lib/robot-army/connection.rb

Direct Known Subclasses

OfficerConnection

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(host, user = nil, password = nil) ⇒ Connection

Returns a new instance of Connection.



4
5
6
7
8
9
# File 'lib/robot-army/connection.rb', line 4

def initialize(host, user=nil, password=nil)
  @host = host
  @user = user
  @password = password
  @closed = true
end

Instance Attribute Details

#hostObject (readonly)

Returns the value of attribute host.



2
3
4
# File 'lib/robot-army/connection.rb', line 2

def host
  @host
end

#messengerObject (readonly)

Returns the value of attribute messenger.



2
3
4
# File 'lib/robot-army/connection.rb', line 2

def messenger
  @messenger
end

#passwordObject (readonly)

Returns the value of attribute password.



2
3
4
# File 'lib/robot-army/connection.rb', line 2

def password
  @password
end

#userObject (readonly)

Returns the value of attribute user.



2
3
4
# File 'lib/robot-army/connection.rb', line 2

def user
  @user
end

Class Method Details

.handle_response(response) ⇒ Object



156
157
158
159
160
161
162
163
164
165
166
167
168
# File 'lib/robot-army/connection.rb', line 156

def self.handle_response(response)
  debug "handling response=#{response.inspect}"
  case response[:status]
  when 'ok'
    return response[:data]
  when 'error'
    raise response[:data]
  when 'warning'
    raise RobotArmy::Warning, response[:data]
  else
    raise RuntimeError, "Unknown response status from remote process: #{response[:status].inspect}"
  end
end

.localhost(user = nil, password = nil, &block) ⇒ Object



170
171
172
173
# File 'lib/robot-army/connection.rb', line 170

def self.localhost(user=nil, password=nil, &block)
  conn = new(:localhost, user, password)
  block ? conn.open(&block) : conn
end

Instance Method Details

#answer_sudo_prompt(stdin, stderr) ⇒ Object



41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/robot-army/connection.rb', line 41

def answer_sudo_prompt(stdin, stderr)
  tries = password.is_a?(Proc) ? 3 : 1
  
  tries.times do
    if asking_for_password?(stderr)
      # ask, and you shall receive
      stdin.puts(password.is_a?(Proc) ?
        password.call : password.to_s)
    end
  end
  
  if asking_for_password?(stderr)
    # ack, that didn't work, bail
    stdin.puts
    stderr.readpartial(1024)
    raise RobotArmy::InvalidPassword
  end
end

#asking_for_password?(stream) ⇒ Boolean

Returns:

  • (Boolean)


33
34
35
36
37
38
39
# File 'lib/robot-army/connection.rb', line 33

def asking_for_password?(stream)
  if RobotArmy::IO.has_data?(stream)
    data = RobotArmy::IO.read_data(stream)
    debug "read #{data.inspect}"
    return data && data =~ /#{password_prompt}\n*$/
  end
end

#closeObject



146
147
148
149
150
# File 'lib/robot-army/connection.rb', line 146

def close
  raise RobotArmy::ConnectionNotOpen if closed?
  messenger.post(:command => :exit)
  @closed = true
end

#closed?Boolean

Returns:

  • (Boolean)


142
143
144
# File 'lib/robot-army/connection.rb', line 142

def closed?
  @closed
end

#getObject



134
135
136
# File 'lib/robot-army/connection.rb', line 134

def get
  messenger.get
end

#handle_response(response) ⇒ Object



152
153
154
# File 'lib/robot-army/connection.rb', line 152

def handle_response(response)
  self.class.handle_response(response)
end

#infoObject



129
130
131
132
# File 'lib/robot-army/connection.rb', line 129

def info
  post(:command => :info)
  handle_response(get)
end

#loaderObject



11
12
13
# File 'lib/robot-army/connection.rb', line 11

def loader
  @loader ||= RobotArmy::Loader.new
end

#open(&block) ⇒ Object



15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/robot-army/connection.rb', line 15

def open(&block)
  start_child if closed?
  @closed = false
  unless block_given?
    return self
  else
    begin
      return yield(self)
    ensure
      close unless closed?
    end
  end
end

#password_promptObject



29
30
31
# File 'lib/robot-army/connection.rb', line 29

def password_prompt
  @password_prompt ||= RobotArmy.random_string
end

#post(*args) ⇒ Object



138
139
140
# File 'lib/robot-army/connection.rb', line 138

def post(*args)
  messenger.post(*args)
end

#start_childObject



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
# File 'lib/robot-army/connection.rb', line 60

def start_child
  begin
    ##
    ## bootstrap the child process
    ##
    
    # small hack to retain control of stdin
    cmd = %{ruby -rbase64 -e "eval(Base64.decode64(STDIN.gets(%(|))))"}
    if user
      # use sudo with user's home dir, custom prompt, reading password from stdin
      cmd = %{sudo -H -u #{user} -p #{password_prompt} -S #{cmd}}
    end
    cmd = "ssh #{host} '#{cmd}'" unless host == :localhost
    debug "running #{cmd}"
    
    loader.libraries.replace $TESTING ? 
      [File.join(File.dirname(__FILE__), '..', 'robot-army')] : %w[rubygems robot-army]
    
    stdin, stdout, stderr = Open3.popen3 cmd
    stdin.sync = stdout.sync = stderr.sync = true
    
    # look for the prompt
    answer_sudo_prompt(stdin, stderr) if user && password
    
    ruby = loader.render
    code = Base64.encode64(ruby)
    stdin << code << '|'
    
    
    ##
    ## make sure it was loaded okay
    ##
    
    @messenger = RobotArmy::Messenger.new(stdout, stdin)
    response = messenger.get
    
    if response
      case response[:status]
      when 'error'
        $stderr.puts "Error trying to execute: #{ruby.gsub(/^/, '  ')}\n"
        raise response[:data]
      when 'ok'
        # yay! established connection
      end
    else
      # try to get stderr
      begin
        require 'timeout'
        err = timeout(1){ "process stderr: #{stderr.read}" }
      rescue Timeout::Error
        err = 'additionally, failed to get stderr'
      end
    
      raise "Failed to start remote ruby process. #{err}"
    end
  
    ##
    ## finish up
    ##
  
    @closed = false
  rescue Object => e
    $stderr.puts "Failed to establish connection to #{host}: #{e.message}"
    raise e
  ensure
    @closed = true
  end
end