Class: JDCConsole

Inherits:
JDCTunnel show all
Defined in:
lib/console/console.rb

Constant Summary

Constants inherited from JDCTunnel

JDCTunnel::HELPER_APP, JDCTunnel::HELPER_NAME, JDCTunnel::HELPER_VERSION, JDCTunnel::PORT_RANGE

Instance Method Summary collapse

Methods inherited from JDCTunnel

#open!, #pick_port!, #wait_for_end, #wait_for_start

Constructor Details

#initialize(client, app, port = 10000) ⇒ JDCConsole

Returns a new instance of JDCConsole.



7
8
9
10
11
# File 'lib/console/console.rb', line 7

def initialize(client, app, port = 10000)
  @client = client
  @app = app
  @port = port
end

Instance Method Details

#get_connection_info(auth) ⇒ Object



13
14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/console/console.rb', line 13

def get_connection_info(auth)
  instances = @app.instances
  if instances.empty?
    raise "App has no running instances; try starting it."
  end

  unless console = instances[0].console
    raise "App does not have console access; try restarting it."
  end

  { "hostname" => console[:ip],
    "port" => console[:port]
  }
end

#get_credentialsObject



28
29
30
# File 'lib/console/console.rb', line 28

def get_credentials
  YAML.load(@app.file("app", "jdc-rails-console", ".consoleaccess"))
end

#login(auth = get_credentials) ⇒ Object



40
41
42
43
44
45
46
47
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
# File 'lib/console/console.rb', line 40

def (auth = get_credentials)
  if !auth["username"] || !auth["password"]
    raise "Unable to verify console credentials."
  end

  @telnet = telnet_client

  prompt = nil
  err_msg = "Login attempt timed out."

  5.times do
    begin
      results = @telnet.(
        "Name" => auth["username"],
        "Password" => auth["password"])

      lines = results.sub("Login: Password: ", "").split("\n")

      last_line = lines.pop

      if last_line =~ /[$%#>] \z/n
        prompt = last_line
      elsif last_line =~ /Login failed/
        err_msg = last_line
      end

      break

    rescue TimeoutError
      sleep 1

    rescue EOFError
      # This may happen if we login right after app starts
      close_console
      sleep 5
      @telnet = telnet_client
    end
  end

  unless prompt
    close_console
    raise err_msg
  end

  prompt
end

#start_consoleObject



32
33
34
35
36
37
38
# File 'lib/console/console.rb', line 32

def start_console
  prompt = 

  init_readline

  run_console prompt
end