1
0
mirror of https://github.com/OpenNebula/one.git synced 2024-12-24 21:34:01 +03:00

F #3380: add log subcommand to onehook (#3744)

This commit is contained in:
Alejandro Huertas Herrero 2019-09-23 15:12:42 +02:00 committed by Ruben S. Montero
parent 0ba47ba5a0
commit c48a5799ab
5 changed files with 151 additions and 19 deletions

View File

@ -175,8 +175,8 @@ module CLIHelper
ANSI_YELLOW = "\33[33m"
# CLI states
OK_STATES = %w[runn rdy on configured]
BAD_STATES = %w[fail err err error]
OK_STATES = %w[runn rdy on configured SUCCESS]
BAD_STATES = %w[fail err error ERROR]
REGULAR_STATES = %w[pending]
# Set state color
@ -233,6 +233,17 @@ module CLIHelper
exit(-1)
end
# Check if value is in base64
#
# @param value [String] Value to check
#
# @return [Boolean] True if it's base64
def self.base64?(value)
re = %r(^([A-Za-z0-9+\/]{4})*([A-Za-z0-9+\/]{3}=|[A-Za-z0-9+\/]{2}==)?$)
!value.match(re)[2].nil?
end
# Hash with search
module HashWithSearch

View File

@ -28,6 +28,54 @@ class OneHookHelper < OpenNebulaHelper::OneHelper
'onehook.yaml'
end
# Get hook logs
#
# @param options [Object] CLI user options
def hook_logs(options)
options[:hook_id].nil? ? hook_id = -1 : hook_id = options[:hook_id]
if options.key? :success
rc = 1
elsif options.key? :error
rc = -1
else
rc = 0
end
if options[:since]
since_date = DateTime.parse(options[:since]).to_time.to_i
else
since_date = -1
end
if options[:until]
until_date = DateTime.parse(options[:until]).to_time.to_i
else
until_date = -1
end
hook_log = OpenNebula::HookLog.new(@client)
log_info = hook_log.info(since_date, until_date, hook_id, rc)
if OpenNebula.is_error?(log_info)
STDERR.puts(log_info.message)
exit(-1)
end
begin
exerc = [hook_log.to_hash['HOOKLOG']['HOOK_EXECUTION_RECORD']]
exerc = exerc.flatten.compact
rescue StandardError
exerc = nil
end
if !exerc.nil? && !exerc.empty? && (!options.key? :xml)
print_execution(exerc, false)
elsif options.key? :xml
puts hook_log.to_xml
end
end
private
def factory(id = nil)
@ -80,12 +128,20 @@ class OneHookHelper < OpenNebulaHelper::OneHelper
# </EXECUTION_RESULT>
# </HOOK_EXECUTION_RECORD>
#
def print_execution(execs)
puts
CLIHelper.print_header('EXECUTION LOG', false)
def print_execution(execs, header = true)
if header
puts
CLIHelper.print_header('EXECUTION LOG', false)
end
table = CLIHelper::ShowTable.new(nil, self) do
column :ID, 'Execution ID', :size => 6, :left => false do |d|
unless header
column :HOOK, 'Hook ID', :adjust => true do |d|
d['HOOK_ID']
end
end
column :ID, 'Execution ID', :adjust => true do |d|
d['EXECUTION_ID']
end
@ -93,20 +149,24 @@ class OneHookHelper < OpenNebulaHelper::OneHelper
OpenNebulaHelper.time_to_str(d['TIMESTAMP'], false, true, false)
end
column :EXECUTION, 'Return code', :size => 12 do |d|
column :RC, 'Return code', :adjust => true do |d|
d['EXECUTION_RESULT']['CODE']
end
column :EXECUTION, 'Return code', :adjust => true do |d|
rc = d['EXECUTION_RESULT']['CODE'].to_i
if rc.zero?
'SUCESS'
'SUCCESS'
else
"ERROR (#{rc})"
'ERROR'
end
end
default :ID, :TIMESTAMP, :EXECUTION
default :HOOK, :ID, :TIMESTAMP, :RC, :EXECUTION
end
table.show(execs)
table.show(execs, :stat_column => :EXECUTION)
end
def format_resource(hook, options = {})
@ -138,7 +198,17 @@ class OneHookHelper < OpenNebulaHelper::OneHelper
arguments = ''
arguments = er['ARGUMENTS'] if er['ARGUMENTS'].class == String
if er['ARGUMENTS'].is_a? String
er['ARGUMENTS'].split.each do |arg|
if CLIHelper.base64?(arg)
arguments += Base64.decode64(arg)
else
arguments += arg
end
arguments += ' '
end
end
timestamp = OpenNebulaHelper.time_to_str(er['TIMESTAMP'])
puts format str, 'EXECUTION ID', er['EXECUTION_ID']

View File

@ -79,6 +79,45 @@ CommandParser::CmdParser.new(ARGV) do
:description => 'execution ID'
}
########################################################################
# Hook log Options
########################################################################
ERROR = {
:name => 'error',
:large => '--error',
:description => 'Show only fail executions'
}
SUCCESS = {
:name => 'success',
:large => '--success',
:description => 'Show only success executions'
}
HOOK_ID = {
:name => 'hook_id',
:large => '--hook-id id',
:description => 'Hook ID to check logs',
:format => Integer
}
SINCE = {
:name => 'since',
:large => '--since date',
:description => 'First date to take logs',
:format => String
}
UNTIL = {
:name => 'until',
:large => '--until date',
:description => 'Last date to take logs',
:format => String
}
LOG_OPTIONS = [ERROR, SUCCESS, HOOK_ID, SINCE, UNTIL]
########################################################################
# Global Options
########################################################################
@ -240,4 +279,21 @@ CommandParser::CmdParser.new(ARGV) do
t.retry args[1].to_i
end
end
log_desc = <<-EOT.unindent
Get logs about hook executions
Examples:
~ $ onehook log --since 09/19/19 # returns all logs since that date
~ $ onehook log --error # returns all failing execs logs
~ $ onehook log --hook-id 0 # returns all logs from hook 0
EOT
command :log, log_desc, :options => [LOG_OPTIONS, OpenNebulaHelper::XML] do
helper.hook_logs(options)
0
end
end

View File

@ -97,7 +97,7 @@ module OpenNebula
# @return [nil, OpenNebula::Error] nil in case of success, Error
# otherwise
def delete
call(HOOK_METHODS[:delete], @pe_id, false)
call(HOOK_METHODS[:delete], @pe_id)
end
# Replaces the Hook contents

View File

@ -19,7 +19,7 @@ require 'opennebula/xml_utils'
module OpenNebula
# Class representing the HookLog
class HookLog < XMLPool
class HookLog < XMLElement
#######################################################################
# Constants and Class attribute accessors
@ -42,11 +42,6 @@ module OpenNebula
@client = client
end
# Factory method to create HookLog objects
def factory(element_xml)
OpenNebula::HookLog.new(element_xml, @client)
end
#######################################################################
# XML-RPC Methods for the HookLog object
#######################################################################