Add logging to vagrant-login
This commit is contained in:
parent
42b7f13790
commit
e828719c2f
|
@ -7,11 +7,13 @@ module VagrantPlugins
|
||||||
#
|
#
|
||||||
# @param [Vagrant::Environment] env
|
# @param [Vagrant::Environment] env
|
||||||
def initialize(env)
|
def initialize(env)
|
||||||
@env = env
|
@logger = Log4r::Logger.new("vagrant::login::client")
|
||||||
|
@env = env
|
||||||
end
|
end
|
||||||
|
|
||||||
# Removes the token, effectively logging the user out.
|
# Removes the token, effectively logging the user out.
|
||||||
def clear_token
|
def clear_token
|
||||||
|
@logger.info("Clearing token")
|
||||||
token_path.delete if token_path.file?
|
token_path.delete if token_path.file?
|
||||||
end
|
end
|
||||||
|
|
||||||
|
@ -38,6 +40,8 @@ module VagrantPlugins
|
||||||
# @param [String] pass
|
# @param [String] pass
|
||||||
# @return [String] token The access token, or nil if auth failed.
|
# @return [String] token The access token, or nil if auth failed.
|
||||||
def login(user, pass)
|
def login(user, pass)
|
||||||
|
@logger.info("Logging in '#{user}'")
|
||||||
|
|
||||||
with_error_handling do
|
with_error_handling do
|
||||||
url = "#{Vagrant.server_url}/api/v1/authenticate"
|
url = "#{Vagrant.server_url}/api/v1/authenticate"
|
||||||
request = { "user" => { "login" => user, "password" => pass } }
|
request = { "user" => { "login" => user, "password" => pass } }
|
||||||
|
@ -52,9 +56,12 @@ module VagrantPlugins
|
||||||
#
|
#
|
||||||
# @param [String] token
|
# @param [String] token
|
||||||
def store_token(token)
|
def store_token(token)
|
||||||
|
@logger.info("Storing token in #{token_path}")
|
||||||
|
|
||||||
token_path.open("w") do |f|
|
token_path.open("w") do |f|
|
||||||
f.write(token)
|
f.write(token)
|
||||||
end
|
end
|
||||||
|
|
||||||
nil
|
nil
|
||||||
end
|
end
|
||||||
|
|
||||||
|
@ -65,13 +72,17 @@ module VagrantPlugins
|
||||||
# @return [String]
|
# @return [String]
|
||||||
def token
|
def token
|
||||||
if ENV["ATLAS_TOKEN"] && !ENV["ATLAS_TOKEN"].empty?
|
if ENV["ATLAS_TOKEN"] && !ENV["ATLAS_TOKEN"].empty?
|
||||||
|
@logger.debug("Using authentication token from environment variable")
|
||||||
return ENV["ATLAS_TOKEN"]
|
return ENV["ATLAS_TOKEN"]
|
||||||
end
|
end
|
||||||
|
|
||||||
if token_path.exist?
|
if token_path.exist?
|
||||||
|
@logger.debug("Using authentication token from disk at #{token_path}")
|
||||||
return token_path.read.strip
|
return token_path.read.strip
|
||||||
end
|
end
|
||||||
|
|
||||||
|
@logger.debug("No authentication token in environment or #{token_path}")
|
||||||
|
|
||||||
nil
|
nil
|
||||||
end
|
end
|
||||||
|
|
||||||
|
@ -80,8 +91,13 @@ module VagrantPlugins
|
||||||
def with_error_handling(&block)
|
def with_error_handling(&block)
|
||||||
yield
|
yield
|
||||||
rescue RestClient::Unauthorized
|
rescue RestClient::Unauthorized
|
||||||
|
@logger.debug("Unauthorized!")
|
||||||
false
|
false
|
||||||
rescue RestClient::NotAcceptable => e
|
rescue RestClient::NotAcceptable => e
|
||||||
|
@logger.debug("Got unacceptable response:")
|
||||||
|
@logger.debug(e.message)
|
||||||
|
@logger.debug(e.backtrace.join("\n"))
|
||||||
|
|
||||||
begin
|
begin
|
||||||
errors = JSON.parse(e.response)["errors"].join("\n")
|
errors = JSON.parse(e.response)["errors"].join("\n")
|
||||||
raise Errors::ServerError, errors: errors
|
raise Errors::ServerError, errors: errors
|
||||||
|
@ -89,6 +105,7 @@ module VagrantPlugins
|
||||||
|
|
||||||
raise "An unexpected error occurred: #{e.inspect}"
|
raise "An unexpected error occurred: #{e.inspect}"
|
||||||
rescue SocketError
|
rescue SocketError
|
||||||
|
@logger.info("Socket error")
|
||||||
raise Errors::ServerUnreachable, url: Vagrant.server_url.to_s
|
raise Errors::ServerUnreachable, url: Vagrant.server_url.to_s
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue