Class: Circus::Agents::SSHConnection

Inherits:
DBusConnection show all
Defined in:
lib/circus/agents/ssh_connection.rb

Instance Method Summary collapse

Methods inherited from DBusConnection

#call, #configure_bg!

Constructor Details

#initialize(target) ⇒ SSHConnection

Returns a new instance of SSHConnection.



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/circus/agents/ssh_connection.rb', line 11

def initialize(target)
  addr = "/tmp/circus-#{UUID.generate}"
  @uri = URI.parse(target)
  @ssh = open_ssh
  # Determine our remote uid so we can use that in authentication requests
  remote_uid = @ssh.exec!('id -u').strip

  @acceptor = Socket.new(Socket::Constants::PF_UNIX, Socket::Constants::SOCK_STREAM, 0)
  @acceptor.bind(Socket.pack_sockaddr_un(addr))
  @acceptor.listen(5)
  
  Thread.new do
    begin
      socket, info = @acceptor.accept
      @ssh.open_channel do |channel|
        channel.exec('nc -U /var/run/dbus/system_bus_socket') do |ch, success|
          abort "Couldn't open DBus connection" unless success

          socket.extend(Net::SSH::BufferedIo)
          @ssh.listen_to(socket)

          ch.on_process do
            if socket.available > 0
              ch.send_data(socket.read_available)
            end
          end

          ch.on_data do |ch, data|
            socket.write(data)
          end

          ch.on_close do
            @ssh.stop_listening_to(socket)
            socket.close
          end
        end
      end
      @ssh.loop
    rescue
      puts "SSH connector thread died:"
      puts $!, $@
    end
  end
  
  bus = RemoteDBusConnection.new("unix:path=#{addr}", remote_uid)
  
  super(bus)
end

Instance Method Details

#send_file(fn) ⇒ Object



60
61
62
63
64
65
66
67
68
69
70
# File 'lib/circus/agents/ssh_connection.rb', line 60

def send_file(fn)
  # Perform an SCP upload of the file into the /tmp of the target
  target_fn = File.join('/tmp', File.basename(fn))
  
  scp_ssh = open_ssh
  scp = Net::SCP.new(scp_ssh)
  scp.upload!(fn, target_fn)
  scp_ssh.exec!("chmod ugo+r #{target_fn}")
  scp_ssh.close
  target_fn
end