Module: VMC::Cli::ConsoleHelper

Included in:
VMC::Cli::Command::Apps
Defined in:
lib/cli/console_helper.rb

Instance Method Summary collapse

Instance Method Details

#close_consoleObject



99
100
101
# File 'lib/cli/console_helper.rb', line 99

def close_console
  @telnet_client.close
end

#console_connection_info(appname) ⇒ Object



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/cli/console_helper.rb', line 7

def console_connection_info(appname)
  app = client.app_info(appname)
  fw = VMC::Cli::Framework.lookup_by_framework(app[:staging][:model])
  if !fw.console
    err "'#{appname}' is a #{fw.name} application.  " +
      "Console access is not supported for #{fw.name} applications."
  end
  instances_info_envelope = client.app_instances(appname)
  instances_info_envelope = {} if instances_info_envelope.is_a?(Array)

  instances_info = instances_info_envelope[:instances] || []
  err "No running instances for [#{appname}]" if instances_info.empty?

  entry = instances_info[0]
  if !entry[:console_port]
    begin
      client.app_files(appname, '/app/cf-rails-console')
      err "Console port not provided for [#{appname}].  Try restarting the app."
    rescue VMC::Client::TargetError, VMC::Client::NotFound
      err "Console access not supported for [#{appname}]. " +
        "Please redeploy your app to enable support."
    end
  end
  conn_info = {'hostname' => entry[:console_ip], 'port' => entry[:console_port]}
end

#console_credentials(appname) ⇒ Object



94
95
96
97
# File 'lib/cli/console_helper.rb', line 94

def console_credentials(appname)
  content = client.app_files(appname, '/app/cf-rails-console/.consoleaccess', '0')
  YAML.load(content)
end

#console_login(auth_info, port) ⇒ Object



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
# File 'lib/cli/console_helper.rb', line 54

def (auth_info, port)
  if !auth_info["username"] || !auth_info["password"]
    err "Unable to verify console credentials."
  end
  @telnet_client = telnet_client(port)
  prompt = nil
  err_msg = "Login attempt timed out."
  3.times do
    begin
      results = @telnet_client.("Name"=>auth_info["username"],
        "Password"=>auth_info["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_client = telnet_client(port)
    end
  end
  unless prompt
    close_console
    err err_msg
  end
  prompt
end

#console_tab_completion_data(cmd) ⇒ Object



103
104
105
106
107
108
109
110
# File 'lib/cli/console_helper.rb', line 103

def console_tab_completion_data(cmd)
  begin
    results = @telnet_client.cmd("String"=> cmd + "\t", "Match"=>/\S*\n$/, "Timeout"=>10)
    results.chomp.split(",")
  rescue TimeoutError
    [] #Just return empty results if timeout occurred on tab completion
  end
end

#send_console_command(cmd) ⇒ Object



89
90
91
92
# File 'lib/cli/console_helper.rb', line 89

def send_console_command(cmd)
  results = @telnet_client.cmd(cmd)
  results.split("\n")
end

#start_local_console(port, appname) ⇒ Object



33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/cli/console_helper.rb', line 33

def start_local_console(port, appname)
  auth_info = console_credentials(appname)
  banner = "Connecting to '#{appname}' console: "
  display banner, false
  t = Thread.new do
    count = 0
    while count < 90 do
      display '.', false
      sleep 1
      count += 1
    end
  end
  prompt = (auth_info, port)
  Thread.kill(t)
  clear(80)
  display "#{banner}#{'OK'.green}"
  display "\n"
  initialize_readline
  run_console prompt
end