Module: Morpheus::Cli::ProcessesHelper

Included in:
Apps, Clusters, Hosts, Instances, 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


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