Class: Aptible::CLI::Agent

Constant Summary

Constants included from Helpers::Token

Helpers::Token::TOKEN_ENV_VAR

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Subcommands::Backup

included

Methods included from Subcommands::SSH

included

Methods included from Subcommands::Restart

included

Methods included from Subcommands::Rebuild

included

Methods included from Subcommands::Ps

included

Methods included from Subcommands::Logs

included

Methods included from Subcommands::Domains

included

Methods included from Subcommands::DB

included

Methods included from Subcommands::Config

included

Methods included from Subcommands::Apps

included

Methods included from Helpers::Token

#current_token_hash, #fetch_token, #save_token, #token_file

Class Method Details

.exit_on_failure?Boolean

Forward return codes on failures.

Returns:

  • (Boolean)


43
44
45
# File 'lib/aptible/cli/agent.rb', line 43

def self.exit_on_failure?
  true
end

Instance Method Details

#loginObject



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
88
89
90
91
92
93
94
95
96
97
98
99
# File 'lib/aptible/cli/agent.rb', line 58

def 
  email = options[:email] || ask('Email: ')
  password = options[:password] || ask('Password: ', echo: false)
  puts ''

  token_options = { email: email, password: password }

  otp_token = options[:otp_token]
  token_options[:otp_token] = otp_token if otp_token

  begin
    lifetime = '1w'
    lifetime = '12h' if token_options[:otp_token]
    lifetime = options[:lifetime] if options[:lifetime]

    duration = ChronicDuration.parse(lifetime)
    if duration.nil?
      fail Thor::Error, "Invalid token lifetime requested: #{lifetime}"
    end

    token_options[:expires_in] = duration
    token = Aptible::Auth::Token.create(token_options)
  rescue OAuth2::Error => e
    if e.code == 'otp_token_required'
      token_options[:otp_token] = options[:otp_token] ||
                                  ask('2FA Token: ')
      retry
    end

    raise Thor::Error, 'Could not authenticate with given credentials: ' \
                       "#{e.code}"
  end

  save_token(token.access_token)
  puts "Token written to #{token_file}"

  lifetime_format = { units: 2, joiner: ', ' }
  token_lifetime = (token.expires_at - token.created_at).round
  expires_in = ChronicDuration.output(token_lifetime, lifetime_format)
  puts "This token will expire after #{expires_in} " \
       '(use --lifetime to customize)'
end

#versionObject



48
49
50
# File 'lib/aptible/cli/agent.rb', line 48

def version
  puts "aptible-cli v#{Aptible::CLI::VERSION}"
end