Class: Aptible::CLI::Agent

Constant Summary

Constants included from Subcommands::Deploy

Subcommands::Deploy::DOCKER_IMAGE_DEPLOY_ARGS, Subcommands::Deploy::NULL_SHA1

Constants included from Helpers::Token

Helpers::Token::TOKEN_ENV_VAR

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Subcommands::Endpoints

included

Methods included from Subcommands::Inspect

included, #inspect_resource

Methods included from Subcommands::Operation

included

Methods included from Subcommands::Backup

included

Methods included from Subcommands::SSH

included

Methods included from Subcommands::Services

included

Methods included from Subcommands::Restart

included

Methods included from Subcommands::Deploy

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::Ssh

#connect_to_ssh_portal, #exit_with_ssh_portal, #with_ssh_cmd

Methods included from Helpers::Token

#current_token_hash, #fetch_token, #save_token, #token_file

Constructor Details

#initialize ⇒ Agent

Returns a new instance of Agent.



63
64
65
66
67
# File 'lib/aptible/cli/agent.rb', line 63

def initialize(*)
  nag_toolbelt unless toolbelt?
  Aptible::Resource.configure { |conf| conf.user_agent = version_string }
  super
end

Class Method Details

.exit_on_failure? ⇒ Boolean

Forward return codes on failures.

Returns:

  • (Boolean)


59
60
61
# File 'lib/aptible/cli/agent.rb', line 59

def self.exit_on_failure?
  true
end

Instance Method Details

#login ⇒ Object



84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
# File 'lib/aptible/cli/agent.rb', line 84

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?
      raise 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)
  CLI.logger.info "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)
  CLI.logger.info "This token will expire after #{expires_in} " \
                  '(use --lifetime to customize)'
end

#version ⇒ Object



70
71
72
73
74
75
76
# File 'lib/aptible/cli/agent.rb', line 70

def version
  Formatter.render(Renderer.current) do |root|
    root.keyed_object('version') do |node|
      node.value('version', version_string)
    end
  end
end