Class: Central::Cli::LoginCommand

Inherits:
Clamp::Command
  • Object
show all
Includes:
Common
Defined in:
lib/central/cli/login_command.rb

Instance Method Summary collapse

Methods included from Common

#access_token=, #add_master, #api_url, #api_url=, #clear_current_stack, #client, #current_master, #current_master=, #current_master_index, #current_stack, #current_stack=, #ensure_custom_ssl_ca, #require_api_url, #require_current_stack, #require_token, #reset_client, #save_settings, #settings, #settings_filename

Instance Method Details

#display_logoObject



106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
# File 'lib/central/cli/login_command.rb', line 106

def 
   = <<LOGO

8""""8
8    " eeee eeeee eeeee eeeee  eeeee e
8e     8    8   8   8   8   8  8   8 8
88     8eee 8e  8   8e  8eee8e 8eee8 8e
88   e 88   88  8   88  88   8 88  8 88
88eee8 88ee 88  8   88  88   8 88  8 88eee
------------------------------------------
Copyright 2016 Stefano Harding

LOGO
  puts 
end

#do_login(email, password) ⇒ Object



61
62
63
64
65
66
67
68
69
# File 'lib/central/cli/login_command.rb', line 61

def (email, password)
  params = {
    username: email,
    password: password,
    grant_type: 'password',
    scope: 'user'
  }
  .post('auth', params)
end

#executeObject



8
9
10
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
# File 'lib/central/cli/login_command.rb', line 8

def execute
  require 'highline/import'

  api_url = ask('Central Machine Node URL: ') until !url.nil? && !url.empty?

  @api_url = url

  unless request_server_info
    puts 'Could not connect to server'.colorize(:red)
    return false
  end

  email = ask('Email: ')
  password = ask('Password: ') { |q| q.echo = '*' }
  response = (email, password)

  if response
    update_master_info(name, url, response['access_token'])
    
    puts ''
    puts "Logged in as #{response['user']['name'].green}"
    reset_client
    stacks = client(require_token).get('stacks')['stacks']
    stack = stacks[0]
    if stack
      self.current_stack = stack
      puts "Using stack #{stack['name'].cyan}"
      puts ''
      if stacks.size > 1
        puts "You have access to following stacks and can switch between them using 'cm stack use <name>'"
        puts ''
        stacks.each do |stack|
          puts "  * #{stack['name']}"
        end
        puts ''
      end
    else
      clear_current_stack
    end

    puts "Welcome! See 'cm --help' to get started."
    true
  else
    puts 'Login Failed'.colorize(:red)
    false
  end
end

#login_clientObject



56
57
58
59
# File 'lib/central/cli/login_command.rb', line 56

def 
  @login_client = Central::Client.new(@api_url) if @login_client.nil?
  @login_client
end

#request_server_infoObject



71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
# File 'lib/central/cli/login_command.rb', line 71

def request_server_info
  valid = true
  begin
    .get('ping') # test server connection
  rescue Excon::Errors::SocketError => exec
    if exec.message.include?('Unable to verify certificate')
      puts 'The server uses a certificate signed by an unknown authority.'.colorize(:red)
      puts "Protip: you can bypass the certificate check by setting #{'SSL_IGNORE_ERRORS=true'.colorize(:yellow)} env variable, but any data you send to the server could be intercepted by others."
      exit(1)
    else
      valid = false
    end
  rescue => exec
    valid = false
  end
  valid
end

#update_master_info(name, url, token) ⇒ Object

Parameters:

  • name (String)
  • url (String)
  • token (String)


95
96
97
98
99
100
101
102
103
104
# File 'lib/central/cli/login_command.rb', line 95

def update_master_info(name, url, token)
  name ||= 'default'
  master = {
    'name' => name,
    'url' => url,
    'token' => token
  }

  add_master(name, master)
end