Class: Console

Inherits:
Object
  • Object
show all
Defined in:
lib/sty/console.rb

Constant Summary collapse

CONSOLE_URL =
'https://ap-southeast-2.console.aws.amazon.com/'
SIGN_IN_URL =
'https://signin.aws.amazon.com/federation'
SIGN_OUT_URL =
'https://ap-southeast-2.console.aws.amazon.com/console/logout!doLogout'
SESSION_DURATION_SECONDS =
43200
BROWSERS =
%w(chrome firefox vivaldi safari)

Instance Method Summary collapse

Instance Method Details

#action(browser, incognito, logout) ⇒ Object



73
74
75
76
77
78
79
# File 'lib/sty/console.rb', line 73

def action(browser, incognito, logout)
  if logout
    logout(browser, incognito)
  else
    (browser, incognito)
  end
end

#go(url, browser = '', incognito = false) ⇒ Object



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/sty/console.rb', line 18

def go(url, browser = '', incognito = false)
  url = "'#{url}'"
  run = %w(open)
  run << '-n' if incognito
  case browser.downcase
  when 'safari'
    run << "-a 'Safari'"
    run << url
  when 'chrome'
    run << "-a 'Google Chrome'"
    incognito ? run << "--args --incognito #{url}" : run << url
  when 'firefox'
    run << '-a Firefox'
    incognito ? run << "--args -private-window #{url}" : run << url
  when 'vivaldi'
    run << '-a Vivaldi'
    incognito ? run << "--args --incognito #{url}" : run << url
  else
    run << url
  end

  puts run.join(' ')
  system(run.join(' '))
end

#login(browser, incognito) ⇒ Object



85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
# File 'lib/sty/console.rb', line 85

def (browser, incognito)
  act_acc

  if remained_minutes <= 0
    puts red("Session expired")
    exit 1
  end

  ['Action'] = 'login'
  ['Destination'] = CONSOLE_URL

  params_str = .map do |k, v|
    "#{k}=#{CGI.escape(v.to_s)}"
  end.join('&')

  console_url = "#{SIGN_IN_URL}?#{params_str}"

  go(console_url, browser || '', incognito)
end

#logout(browser, incognito) ⇒ Object



81
82
83
# File 'lib/sty/console.rb', line 81

def logout(browser, incognito)
  go(SIGN_OUT_URL, browser || '', incognito)
end

#signin_paramsObject



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
# File 'lib/sty/console.rb', line 43

def 
  return @signin_params if @signin_params

  creds_string = {'sessionId' => ENV['AWS_ACCESS_KEY_ID'],
                  'sessionKey' => ENV['AWS_SECRET_ACCESS_KEY'],
                  'sessionToken' => ENV['AWS_SESSION_TOKEN']}.to_json

  uri = URI(SIGN_IN_URL)
  params = {'Action' => 'getSigninToken',
            'Session' => creds_string}
  uri.query = URI.encode_www_form(params)

  begin
    res = Net::HTTP.get_response(uri)
  rescue Exception => e
    puts red("ERROR! Unable to obtain signin token.")
    puts white(e.message)
    exit 1
  end

  unless res.is_a?(Net::HTTPSuccess)
    puts red("ERROR! Unable to obtain signin token.")
    puts white("#{SIGN_IN_URL} returns #{res.code}")
    puts res.body
    exit 1
  end

  @signin_params = JSON.parse(res.body)
end