Module: Morpheus::Cli::ProcessesHelper

Included in:
Apps, Clusters, Hosts, Instances, Migrations, Processes
Defined in:
lib/morpheus/cli/mixins/processes_helper.rb

Overview

Mixin for Morpheus::Cli command classes Provides common methods for viewing process history

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.included(klass) ⇒ Object



7
8
9
# File 'lib/morpheus/cli/mixins/processes_helper.rb', line 7

def self.included(klass)
  klass.send :include, Morpheus::Cli::PrintHelper
end

Instance Method Details

#api_clientObject



11
12
13
14
# File 'lib/morpheus/cli/mixins/processes_helper.rb', line 11

def api_client
  raise "#{self.class} has not defined @api_client" if @api_client.nil?
  @api_client
end

#format_process_duration(process, time_format = "%H:%M:%S") ⇒ Object

format for either ETA/Duration



115
116
117
118
119
120
121
122
123
124
125
126
127
# File 'lib/morpheus/cli/mixins/processes_helper.rb', line 115

def format_process_duration(process, time_format="%H:%M:%S")
  out = ""
  if process['duration'] && process['duration'] > 0
    out = format_duration_milliseconds(process['duration'], time_format)
  elsif process['statusEta'] && process['statusEta'] > 0
    out = format_duration_milliseconds(process['statusEta'], time_format)
  elsif process['startDate'] && process['endDate']
    out = format_duration(process['startDate'], process['endDate'], time_format)
  else
    ""
  end
  out
end

#format_process_error(process, max_length = 20, return_color = cyan) ⇒ Object

decolorize, remove newlines and truncate for table cell



105
106
107
# File 'lib/morpheus/cli/mixins/processes_helper.rb', line 105

def format_process_error(process, max_length=20, return_color=cyan)
  truncate_string(process['error'].to_s.strip.gsub("\n", " "), max_length)
end

#format_process_output(process, max_length = 20, return_color = cyan) ⇒ Object

decolorize, remove newlines and truncate for table cell



110
111
112
# File 'lib/morpheus/cli/mixins/processes_helper.rb', line 110

def format_process_output(process, max_length=20, return_color=cyan)
  truncate_string(process['output'].to_s.strip.gsub("\n", " "), max_length)
end

#format_process_status(process, return_color = cyan) ⇒ Object



89
90
91
92
93
94
95
96
97
98
99
100
101
102
# File 'lib/morpheus/cli/mixins/processes_helper.rb', line 89

def format_process_status(process, return_color=cyan)
  out = ""
  status_string = process['status'].to_s
  if status_string == 'complete'
    out << "#{green}#{status_string.upcase}#{return_color}"
  elsif status_string == 'failed'
    out << "#{red}#{status_string.upcase}#{return_color}"
  elsif status_string == 'expired'
    out << "#{red}#{status_string.upcase}#{return_color}"
  else
    out << "#{cyan}#{status_string.upcase}#{return_color}"
  end
  out
end

#handle_history_command(args, arg_name, label, ref_type, &block) ⇒ Object



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
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
# File 'lib/morpheus/cli/mixins/processes_helper.rb', line 150

def handle_history_command(args, arg_name, label, ref_type, &block)
  raw_args = args.dup
  options = {}
  #options[:show_output] = true
  optparse = Morpheus::Cli::OptionParser.new do |opts|
    opts.banner = "Usage: #{prog_name} #{command_name} history [#{arg_name}]"
    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('--details', "Display more details: memory and storage usage used / max values." ) do
      options[:show_events] = true
      options[:show_output] = true
      options[:details] = true
    end
    # opts.on('--process-id ID', String, "Display details about a specfic process only." ) do |val|
    #   options[:process_id] = val
    # end
    # opts.on('--event-id ID', String, "Display details about a specfic process event only." ) do |val|
    #   options[:event_id] = val
    # end
    build_standard_list_options(opts, options)
    opts.footer = "List historical processes for a specific #{label}.\n" + 
                  "[#{arg_name}] is required. This is the name or id of an #{label}."
  end
  optparse.parse!(args)

  # shortcut to other actions
  # if options[:process_id]
  #   return history_details(raw_args)
  # elsif options[:event_id]
  #   return history_event_details(raw_args)
  # end

  verify_args!(args:args, optparse:optparse, count:1)
  connect(options)
  
  record = block.call(args[0])
  # block should raise_command_error if not found
  if record.nil?
    raise_command_error "#{label} not found for name or id '#{args[0]}'"
  end
  params = {}
  params.merge!(parse_list_options(options))
  # params['query'] = params.delete('phrase') if params['phrase']
  params['refType'] = ref_type
  params['refId'] = record['id']
  @processes_interface.setopts(options)
  if options[:dry_run]
    print_dry_run @processes_interface.dry.list(params)
    return
  end
  json_response = @processes_interface.list(params)
  render_response(json_response, options, "processes") do
    title = "#{label} History: #{record['name'] || record['id']}"
    subtitles = parse_list_subtitles(options)
    print_h1 title, subtitles, options
    processes = json_response['processes']
    if processes.empty?
      print "#{cyan}No process history found.#{reset}\n\n"
    else
      history_records = []
      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)
      print_results_pagination(json_response, {:label => "process", :n_label => "processes"})
      print reset, "\n"
    end
  end
  return 0, nil
end


21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/morpheus/cli/mixins/processes_helper.rb', line 21

def print_process_details(process, options={})
  description_cols = {
    "Process ID" => lambda {|it| it['id'] },
    "Name" => lambda {|it| it['displayName'] },
    "Description" => lambda {|it| it['description'] },
    "Process Type" => lambda {|it| it['processType'] ? (it['processType']['name'] || it['processType']['code']) : it['processTypeName'] },
    "Created By" => lambda {|it| it['createdBy'] ? (it['createdBy']['displayName'] || it['createdBy']['username']) : '' },
    "Start Date" => lambda {|it| format_local_dt(it['startDate']) },
    "End Date" => lambda {|it| format_local_dt(it['endDate']) },
    "Duration" => lambda {|it| format_process_duration(it) },
  }
  if process['message'].to_s.strip != ''
    description_cols.merge!({ "Message" => lambda {|it| it['message']}
    })
  end
  description_cols.merge!({
    "Status" => lambda {|it| format_process_status(it) },
    # "# Events" => lambda {|it| (it['events'] || []).size() },
  })
  print_description_list(description_cols, process, options)

  if process['error']
    print_h2 "Error"
    print reset
    #puts format_process_error(process_event)
    puts process['error'].to_s.strip
  end

  if process['output']
    print_h2 "Output"
    print reset
    #puts format_process_error(process_event)
    puts process['output'].to_s.strip
  end
end


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
# File 'lib/morpheus/cli/mixins/processes_helper.rb', line 57

def print_process_event_details(process_event, options={})
  # process_event =~ process
  description_cols = {
    "Process ID" => lambda {|it| it['processId'] },
    "Event ID" => lambda {|it| it['id'] },
    "Name" => lambda {|it| it['displayName'] },
    "Description" => lambda {|it| it['description'] },
    "Process Type" => lambda {|it| it['processType'] ? (it['processType']['name'] || it['processType']['code']) : it['processTypeName'] },
    "Created By" => lambda {|it| it['createdBy'] ? (it['createdBy']['displayName'] || it['createdBy']['username']) : '' },
    "Start Date" => lambda {|it| format_local_dt(it['startDate']) },
    "End Date" => lambda {|it| format_local_dt(it['endDate']) },
    "Duration" => lambda {|it| format_process_duration(it) },
    "Status" => lambda {|it| format_process_status(it) },
  }
  print_description_list(description_cols, process_event, options)

  if process_event['error']
    print_h2 "Error"
    print reset
    #puts format_process_error(process_event)
    puts process_event['error'].to_s.strip
  end

  if process_event['output']
    print_h2 "Output"
    print reset
    #puts format_process_error(process_event)
    puts process_event['output'].to_s.strip
  end
end

#processes_interfaceObject



16
17
18
19
# File 'lib/morpheus/cli/mixins/processes_helper.rb', line 16

def processes_interface
  # get_interface('processes')
  api_client.processes
end

#wait_for_process_execution(process_id, options = {}, print_output = true) ⇒ Object



129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
# File 'lib/morpheus/cli/mixins/processes_helper.rb', line 129

def wait_for_process_execution(process_id, options={}, print_output = true)
  refresh_interval = 10
  if options[:refresh_interval].to_i > 0
    refresh_interval = options[:refresh_interval]
  end
  refresh_display_seconds = refresh_interval % 1.0 == 0 ? refresh_interval.to_i : refresh_interval
  unless options[:quiet]
    print cyan, "Refreshing every #{refresh_display_seconds} seconds until process is complete...", "\n", reset
  end
  process = processes_interface.get(process_id)['process']
  while ['new','queued','pending','running'].include?(process['status']) do
    sleep(refresh_interval)
    process = processes_interface.get(process_id)['process']
  end
  if print_output && options[:quiet] != true
    print_h1 "Process Details", [], options
    print_process_details(process, options)
  end
  return process
end