def get(args)
exit_code, err = 0, nil
params, options = {}, {}
status_only, time_only, setup_check_only = false, false, false
optparse = Morpheus::Cli::OptionParser.new do |opts|
opts.banner = usage
opts.on( '-s', '--status', "Print only the status." ) do
status_only = true
end
opts.on( '-t', '--time', "Print only the response time." ) do
time_only = true
end
opts.on( '--setup-needed?', "Print only if setup is needed or not, exit 1 if not." ) do
setup_check_only = true
end
build_standard_get_options(opts, options, [:quiet])
opts.footer = <<-EOT
Ping the remote morpheus appliance.
Prints the remote version and status and the time it took to get a response.
EOT
end
optparse.parse!(args)
verify_args!(args:args, count:0, optparse:optparse)
connect(options)
took_sec = nil
begin
params.merge!(parse_query_options(options))
appliance = @remote_appliance
@ping_interface.setopts(options)
@setup_interface.setopts(options)
if options[:dry_run]
print_dry_run @ping_interface.dry.get(params)
return 0
end
api_exception = nil
json_response = nil
start_time = Time.now
begin
json_response = @ping_interface.get(params)
rescue RestClient::Exception => e
if e.response && (e.response.code == 404 || e.response.code == 401)
start_time = Time.now
begin
json_response = @setup_interface.check(params)
rescue RestClient::Exception => e2
begin
json_response = JSON.parse(e2.response.to_s)
rescue TypeError, JSON::ParserError => ex
json_response = nil
end
end
end
rescue => e
api_exception = e
ensure
took_sec = (Time.now - start_time)
end
if json_response
if json_response['buildVersion']
appliance[:build_version] = json_response['buildVersion']
end
if json_response['applianceUrl']
appliance[:appliance_url] = json_response['applianceUrl']
end
if json_response['setupNeeded'] == true
appliance[:status] = 'fresh'
else
appliance[:status] = 'ready'
end
else
end
if options[:remote_url].nil?
Morpheus::Cli::Remote.save_remote_last_check(appliance, json_response, api_exception, took_sec)
appliance = ::Morpheus::Cli::Remote.load_remote(@appliance_name)
end
exit_code, err = 0, nil
if appliance[:status] != 'ready' && appliance[:status] != 'fresh'
exit_code = 1
end
render_response(json_response, options) do
if json_response.nil?
json_response = {}
end
if false && json_response.nil?
print_error yellow, "No ping data returned.",reset,"\n"
else
if status_only
if exit_code == 0
print format_appliance_status(appliance, cyan), reset, "\n"
else
print_error format_appliance_status(appliance, cyan), reset, "\n"
end
return exit_code, err
elsif time_only
status_color = format_appliance_status_color(appliance)
if exit_code == 0
print status_color, format_duration_seconds(took_sec), reset, "\n"
else
print_error status_color, format_duration_seconds(took_sec), reset, "\n"
end
return exit_code, err
elsif setup_check_only
status_color = format_appliance_status_color(appliance)
remote_status_string = format_appliance_status(appliance, cyan)
if appliance[:status] != 'fresh'
exit_code = 1
end
if appliance[:status] == 'fresh'
print cyan, "Yes, remote #{appliance[:name]} status is #{remote_status_string}","\n"
elsif appliance[:status] == 'ready'
print cyan, "No, remote #{appliance[:name]} status is #{remote_status_string} (already setup)","\n"
else
print_error cyan,"Uh oh, remote #{appliance[:name]} status is #{remote_status_string}",reset,"\n"
end
return exit_code, err
else
title = "Morpheus Ping"
subtitles = []
print_h1 title, subtitles, options
error_string = appliance[:last_check] ? appliance[:last_check][:error] : nil
columns = {
"Name" => lambda {|it| appliance[:name] },
"URL" => lambda {|it| appliance[:url] },
"Version" => lambda {|it| appliance[:build_version] },
"Response Time" => lambda {|it| format_duration_seconds(took_sec) },
"Status" => lambda {|it| format_appliance_status(appliance, cyan) },
}
if error_string.to_s.empty?
columns.delete("Error")
end
print_description_list(columns, json_response, options)
if error_string.to_s != ""
print "\n"
print red, error_string, "\n",reset
end
end
print reset, "\n"
end
end
return exit_code, err
rescue RestClient::Exception => e
if options[:remote_url].nil?
Morpheus::Cli::Remote.save_remote_last_check(appliance, json_response, e, took_sec)
appliance = ::Morpheus::Cli::Remote.load_remote(appliance[:name])
end
if status_only
print format_appliance_status(appliance, cyan), reset, "\n"
return exit_code, err
elsif time_only
status_color = format_appliance_status_color(appliance)
print status_color, format_duration_seconds(took_sec), reset, "\n"
return exit_code, err
else
if e.response
print_red_alert "ping failed in #{format_duration_seconds(took_sec)} (HTTP #{e.response.code})"
else
print_red_alert "ping failed in #{format_duration_seconds(took_sec)}"
end
end
print_rest_exception(e, options)
return 1, e.message
end
end