33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
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
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
126
127
128
129
130
131
132
133
134
135
136
137
138
|
# File 'lib/morpheus/cli/logs_command.rb', line 33
def list(args)
options = {}
params = {}
optparse = Morpheus::Cli::OptionParser.new do |opts|
opts.banner = subcommand_usage("[id]")
opts.on('--hosts HOSTS', String, "Filter logs to specific Host ID(s)") do |val|
params['servers'] = val.to_s.split(",").collect {|it| it.to_s.strip }.select {|it| it }.compact
end
opts.on('--servers HOSTS', String, "alias for --hosts") do |val|
params['servers'] = val.to_s.split(",").collect {|it| it.to_s.strip }.select {|it| it }.compact
end
opts.on('--vms HOSTS', String, "alias for --hosts") do |val|
params['servers'] = val.to_s.split(",").collect {|it| it.to_s.strip }.select {|it| it }.compact
end
opts.on('--container CONTAINER', String, "Filter logs to specific Container ID(s)") do |val|
params['containers'] = val.to_s.split(",").collect {|it| it.to_s.strip }.select {|it| it }.compact
end
opts.on('--cluster ID', String, "Filter logs to specific Cluster ID") do |val|
params['clusters'] = val.to_s.split(",").collect {|it| it.to_s.strip }.select {|it| it }.compact
end
opts.on('--start TIMESTAMP','--start TIMESTAMP', "Start timestamp. Default is 30 days ago.") do |val|
options[:start] = parse_time(val) end
opts.on('--end TIMESTAMP','--end TIMESTAMP', "End timestamp. Default is now.") do |val|
options[:end] = parse_time(val) end
opts.on('--level VALUE', String, "Log Level. DEBUG,INFO,WARN,ERROR") do |val|
params['level'] = params['level'] ? [params['level'], val].flatten : [val]
end
opts.on('-t', '--table', "Format ouput as a table.") do
options[:table] = true
end
opts.on('-a', '--all', "Display all details: entire message." ) do
options[:details] = true
end
build_common_options(opts, options, [:list, :query, :json, :yaml, :csv, :fields, :dry_run, :remote])
opts. = "List logs for a container.\n" +
"[id] is required. This is the id of a container."
end
optparse.parse!(args)
if args.count != 0
raise_command_error "wrong number of arguments, expected 0 and got (#{args.count}) #{args.join(' ')}\n#{optparse}"
end
connect(options)
begin
params['level'] = params['level'].collect {|it| it.to_s.upcase }.join('|') if params['level'] params.merge!(parse_list_options(options))
params['query'] = params.delete('phrase') if params['phrase']
params['order'] = params['direction'] unless params['direction'].nil? params['startMs'] = (options[:start].to_i * 1000) if options[:start]
params['endMs'] = (options[:end].to_i * 1000) if options[:end]
params['interval'] = options[:interval].to_s if options[:interval]
@logs_interface.setopts(options)
if options[:dry_run]
print_dry_run @logs_interface.dry.list(params)
return
end
json_response = @logs_interface.list(params)
render_result = json_response['logs'] ? render_with_format(json_response, options, 'logs') : render_with_format(json_response, options, 'data')
return 0 if render_result
title = "Morpheus Logs"
subtitles = parse_list_subtitles(options)
if options[:start]
subtitles << "Start: #{options[:start]}".strip
end
if options[:end]
subtitles << "End: #{options[:end]}".strip
end
if params['query']
subtitles << "Search: #{params['query']}".strip
end
if params['servers']
subtitles << "Servers: #{params['servers']}".strip
end
if params['containers']
subtitles << "Containers: #{params['containers']}".strip
end
if params['clusters']
subtitles << "Clusters: #{params['clusters']}".strip
end
if params['level']
subtitles << "Level: #{params['level']}"
end
print_h1 title, subtitles, options
logs = json_response['data'] || json_response['logs']
if logs.empty?
print "#{cyan}No logs found.#{reset}\n"
else
print format_log_records(logs, options)
({'meta'=>{'total'=>(json_response['total']['value'] rescue json_response['total']),'size'=>logs.size,'max'=>(json_response['max'] || options[:max]),'offset'=>(json_response['offset'] || options[:offset] || 0)}})
end
print reset,"\n"
return 0
rescue RestClient::Exception => e
print_rest_exception(e, options)
exit 1
end
end
|