Method: Morpheus::Cli::Processes#list

Defined in:
lib/morpheus/cli/commands/processes_command.rb

#list(args) ⇒ Object



32
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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
# File 'lib/morpheus/cli/commands/processes_command.rb', line 32

def list(args)
  params = {}
  options = {}
  #options[:show_output] = true
  optparse = Morpheus::Cli::OptionParser.new do |opts|
    opts.banner = subcommand_usage()
    opts.on( nil, '--events', "Display sub processes (events)." ) do
      options[:show_events] = true
    end
    opts.on( nil, '--output', "Display process output." ) do
      options[:show_output] = true
    end
    opts.on(nil, '--details', "Display all details. Includes sub processes, output and error data is not truncated." ) do
      options[:show_events] = true
      options[:show_output] = true
      options[:details] = true
    end
    opts.on('--app APP', String, "Limit results to specific app(s).") do |val|
      params['appIds'] = val.split(',').collect {|it| it.to_s.strip }.reject { |it| it.empty? }
    end
    opts.on('--instance INSTANCE', String, "Limit results to specific instance(s).") do |val|
      params['instanceIds'] = val.split(',').collect {|it| it.to_s.strip }.reject { |it| it.empty? }
    end
    opts.on('--container CONTAINER', String, "Limit results to specific container(s).") do |val|
      params['containerIds'] = val.split(',').collect {|it| it.to_s.strip }.reject { |it| it.empty? }
    end
    opts.on('--host HOST', String, "Limit results to specific host(s).") do |val|
      params['serverIds'] = val.split(',').collect {|it| it.to_s.strip }.reject { |it| it.empty? }
    end
    opts.on('--server HOST', String, "Limit results to specific servers(s).") do |val|
      params['serverIds'] = val.split(',').collect {|it| it.to_s.strip }.reject { |it| it.empty? }
    end
    opts.add_hidden_option('--server')
    opts.on('--cloud CLOUD', String, "Limit results to specific cloud(s).") do |val|
      params['zoneIds'] = val.split(',').collect {|it| it.to_s.strip }.reject { |it| it.empty? }
    end
    opts.on('--user USER', String, "Limit results to user(s).") do |val|
      #params['userId'] = val.split(',').collect {|it| it.to_s.strip }.reject { |it| it.empty? }
      options[:user] = val
    end
    build_common_options(opts, options, [:list, :query, :json, :yaml, :csv, :fields, :dry_run, :remote])
    opts.footer = "List historical processes."
  end
  optparse.parse!(args)

  if args.count != 0
    puts optparse
    return 1
  end
  connect(options)
  begin
    params.merge!(parse_list_options(options))
    if params['instanceIds']
      params['instanceIds'] = params['instanceIds'].collect do |instance_id|
        if instance_id.to_s =~ /\A\d{1,}\Z/
          # just allow instance IDs
          instance_id.to_i
        else
          instance = find_instance_by_name_or_id(instance_id)
          if instance.nil?
            return 1, "instance not found for '#{instance_id}'" # never happens because find exits
          end
          instance['id']
        end
      end
    end
    if params['serverIds']
      params['serverIds'] = params['serverIds'].collect do |server_id|
        if server_id.to_s =~ /\A\d{1,}\Z/
          # just allow server IDs
          server_id.to_i
        else
          server = find_server_by_name_or_id(server_id)
          if server.nil?
            return 1, "server not found for '#{server_id}'" # never happens because find exits
          end
          server['id']
        end
      end
    end
    if params['appIds']
      params['appIds'] = params['appIds'].collect do |app_id|
        if app_id.to_s =~ /\A\d{1,}\Z/
          # just allow app IDs
          app_id.to_i
        else
          app = find_app_by_name_or_id(app_id)
          if app.nil?
            return 1, "app not found for '#{app_id}'" # never happens because find exits
          end
          app['id']
        end
      end
    end
    if params['zoneIds']
      params['zoneIds'] = params['zoneIds'].collect do |zone_id|
        if zone_id.to_s =~ /\A\d{1,}\Z/
          # just allow zone IDs
          zone_id.to_i
        else
          zone = find_cloud_by_name_or_id(zone_id)
          if zone.nil?
            return 1, "cloud not found for '#{zone_id}'" # never happens because find exits
          end
          zone['id']
        end
      end
    end
    if options[:user]
      user = find_available_user_option(options[:user])
      return 1, "user not found by '#{options[:user]}'" if user.nil?
      params['userId'] = user['id']
    end
    @processes_interface.setopts(options)
    if options[:dry_run]
      print_dry_run @processes_interface.dry.list(params)
      return
    end
    json_response = @processes_interface.list(params)
    if options[:json]
      puts as_json(json_response, options, "processes")
      return 0
    elsif options[:yaml]
      puts as_yaml(json_response, options, "processes")
      return 0
    elsif options[:csv]
      puts records_as_csv(json_response['processes'], options)
      return 0
    else

      title = "Process List"
      subtitles = []
      if params[:query]
        subtitles << "Search: #{params[:query]}".strip
      end
      subtitles += parse_list_subtitles(options)
      print_h1 title, subtitles
      if json_response['processes'].empty?
        print "#{cyan}No process history found.#{reset}\n\n"
        return 0
      else
        history_records = []
        json_response["processes"].each do |process|
          row = {
            id: process['id'],
            eventId: nil,
            uniqueId: process['uniqueId'],
            name: process['displayName'],
            description: process['description'],
            processType: process['processType'] ? (process['processType']['name'] || process['processType']['code']) : process['processTypeName'],
            createdBy: process['createdBy'] ? (process['createdBy']['displayName'] || process['createdBy']['username']) : '',
            startDate: format_local_dt(process['startDate']),
            duration: format_process_duration(process),
            status: format_process_status(process),
            error: format_process_error(process, options[:details] ? nil : 20),
            output: format_process_output(process, options[:details] ? nil : 20)
          }
          history_records << row
          process_events = process['events'] || process['processEvents']
          if options[:show_events]
            if process_events
              process_events.each do |process_event|
                event_row = {
                  id: process['id'],
                  eventId: process_event['id'],
                  uniqueId: process_event['uniqueId'],
                  name: process_event['displayName'], # blank like the UI
                  description: process_event['description'],
                  processType: process_event['processType'] ? (process_event['processType']['name'] || process_event['processType']['code']) : process['processTypeName'],
                  createdBy: process_event['createdBy'] ? (process_event['createdBy']['displayName'] || process_event['createdBy']['username']) : '',
                  startDate: format_local_dt(process_event['startDate']),
                  duration: format_process_duration(process_event),
                  status: format_process_status(process_event),
                  error: format_process_error(process_event, options[:details] ? nil : 20),
                  output: format_process_output(process_event, options[:details] ? nil : 20)
                }
                history_records << event_row
              end
            else
              
            end
          end
        end
        columns = [
          {:id => {:display_name => "PROCESS ID"} },
          :name, 
          :description, 
          {:processType => {:display_name => "PROCESS TYPE"} },
          {:createdBy => {:display_name => "CREATED BY"} },
          {:startDate => {:display_name => "START DATE"} },
          {:duration => {:display_name => "ETA/DURATION"} },
          :status, 
          :error
        ]
        if options[:show_events]
          columns.insert(1, {:eventId => {:display_name => "EVENT ID"} })
        end
        if options[:show_output]
          columns << :output
        end
        # custom pretty table columns ...
        if options[:include_fields]
          columns = options[:include_fields]
        end
        print cyan
        print as_pretty_table(history_records, columns, options)
        #print_results_pagination(json_response)
        if options[:show_events]
          print_results_pagination({size: history_records.size, total: history_records.size}, {:label => "process", :n_label => "processes"})
        else
          print_results_pagination(json_response, {:label => "process", :n_label => "processes"})
        end
        print reset, "\n"
        return 0
      end
    end
  rescue RestClient::Exception => e
    print_rest_exception(e, options)
    exit 1
  end
end