Class: Morpheus::Cli::Processes

Inherits:
Object
  • Object
show all
Includes:
CliCommand, InfrastructureHelper, OptionSourceHelper, ProcessesHelper, ProvisioningHelper
Defined in:
lib/morpheus/cli/processes_command.rb

Instance Attribute Summary

Attributes included from CliCommand

#no_prompt

Instance Method Summary collapse

Methods included from OptionSourceHelper

#find_available_user_option, #find_cloud_option, #find_group_option, #find_tenant_option, #get_available_user_options, #get_cloud_options, #get_group_options, #get_tenant_options, included, #load_option_source_data, #options_interface, #parse_cloud_id_list, #parse_group_id_list, #parse_option_source_id_list, #parse_project_id_list, #parse_tenant_id_list, #parse_user_id_list

Methods included from InfrastructureHelper

#cloud_type_for_id, #cloud_type_for_name, #cloud_type_for_name_or_id, #clouds_interface, #find_cloud_by_id, #find_cloud_by_name, #find_cloud_by_name_or_id, #find_group_by_id, #find_group_by_name, #find_group_by_name_or_id, #find_network_by_id, #find_network_by_name, #find_network_by_name_or_id, #find_network_group_by_id, #find_network_group_by_name, #find_network_group_by_name_or_id, #find_network_type_by_id, #find_network_type_by_name, #find_network_type_by_name_or_id, #find_subnet_by_id, #find_subnet_by_name, #find_subnet_by_name_or_id, #find_subnet_type_by_id, #find_subnet_type_by_name, #find_subnet_type_by_name_or_id, #get_available_cloud_types, #groups_interface, included, #network_groups_interface, #network_types_interface, #networks_interface, #prompt_for_network, #prompt_for_networks, #prompt_for_subnets, #subnet_types_interface, #subnets_interface

Methods included from ProvisioningHelper

#accounts_interface, #add_perms_options, #api_client, #apps_interface, #cloud_datastores_interface, #clouds_interface, #datastores_interface, #find_app_by_id, #find_app_by_name, #find_app_by_name_or_id, #find_cloud_by_id_for_provisioning, #find_cloud_by_name_for_provisioning, #find_cloud_by_name_or_id_for_provisioning, #find_cloud_resource_pool_by_name_or_id, #find_group_by_id_for_provisioning, #find_group_by_name_for_provisioning, #find_group_by_name_or_id_for_provisioning, #find_host_by_id, #find_host_by_name, #find_host_by_name_or_id, #find_instance_by_id, #find_instance_by_name, #find_instance_by_name_or_id, #find_instance_type_by_code, #find_instance_type_by_id, #find_instance_type_by_name, #find_instance_type_by_name_or_id, #find_instance_type_layout_by_id, #find_server_by_id, #find_server_by_name, #find_server_by_name_or_id, #find_workflow_by_id, #find_workflow_by_name, #find_workflow_by_name_or_id, #format_app_status, #format_blueprint_type, #format_container_connection_string, #format_container_status, #format_instance_connection_string, #format_instance_container_display_name, #format_instance_status, #format_metadata, #format_snapshot_status, #get_available_accounts, #get_available_clouds, #get_available_environments, #get_available_groups, #get_available_plans, #get_provision_type_for_zone_type, #get_static_environments, included, #instance_type_layouts_interface, #instance_types_interface, #instances_interface, #load_balance_protocols_dropdown, #options_interface, #parse_blueprint_type, #parse_host_id_list, #parse_instance_id_list, #parse_metadata, #parse_resource_id_list, #parse_server_id_list, #print_permissions, #prompt_evars, #prompt_exposed_ports, #prompt_instance_load_balancer, #prompt_metadata, #prompt_network_interfaces, #prompt_new_instance, #prompt_permissions, #prompt_resize_volumes, #prompt_security_groups, #prompt_volumes, #provision_types_interface, #reject_service_plan_option_types, #servers_interface

Methods included from ProcessesHelper

#format_process_duration, #format_process_error, #format_process_output, #format_process_status, included, #print_process_details, #print_process_event_details

Methods included from CliCommand

#apply_options, #build_common_options, #build_option_type_options, #build_standard_add_options, #build_standard_delete_options, #build_standard_get_options, #build_standard_list_options, #build_standard_post_options, #build_standard_put_options, #build_standard_remove_options, #build_standard_update_options, #command_description, #command_name, #default_refresh_interval, #default_sigdig, #default_subcommand, #establish_remote_appliance_connection, #full_command_usage, #get_subcommand_description, #handle_subcommand, included, #interactive?, #my_help_command, #my_terminal, #my_terminal=, #parse_bytes_param, #parse_id_list, #parse_list_options, #parse_list_subtitles, #parse_passed_options, #parse_payload, #parse_query_options, #print, #print_error, #println, #prog_name, #puts, #puts_error, #raise_args_error, #raise_command_error, #render_response, #run_command_for_each_arg, #subcommand_aliases, #subcommand_description, #subcommand_usage, #subcommands, #usage, #validate_outfile, #verify_args!, #visible_subcommands

Constructor Details

#initializeProcesses

alias_subcommand :details, :get set_default_subcommand :list



18
19
20
# File 'lib/morpheus/cli/processes_command.rb', line 18

def initialize()
  #@appliance_name, @appliance_url = Morpheus::Cli::Remote.active_appliance
end

Instance Method Details

#connect(options) ⇒ Object



22
23
24
25
26
27
# File 'lib/morpheus/cli/processes_command.rb', line 22

def connect(options)
  @api_client = establish_remote_appliance_connection(options)
  @processes_interface = @api_client.processes
  #@instances_interface = @api_client.instances
  @clouds_interface = @api_client.clouds
end

#event_details(args) ⇒ Object



349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
# File 'lib/morpheus/cli/processes_command.rb', line 349

def event_details(args)
  options = {}
  params = {}
  process_event_id = nil
  optparse = Morpheus::Cli::OptionParser.new do |opts|
    opts.banner = subcommand_usage("[event-id]")
    build_common_options(opts, options, [:query, :json, :yaml, :csv, :fields, :dry_run, :remote])
    opts.footer = "Display details for a specific process event.\n" +
                  "[event-id] is required. This is the id of the process event."
  end
  optparse.parse!(args)
  if args.count != 1
    puts_error optparse
    return 1
  end
  connect(options)
  begin
    process_event_id = args[0]
    params.merge!(parse_list_options(options))
    @processes_interface.setopts(options)
    if options[:dry_run]
      print_dry_run @processes_interface.dry.get_event(process_event_id, params)
      return
    end
    json_response = @processes_interface.get_event(process_event_id, params)
    if options[:json]
      puts as_json(json_response, options, "processEvent")
      return 0
    elsif options[:yaml]
      puts as_yaml(json_response, options, "processEvent")
      return 0
    elsif options[:csv]
      puts records_as_csv(json_response['processEvent'], options)
      return 0
    else
      process_event = json_response['processEvent'] || json_response['event']
      title = "Process Event Details"
      subtitles = []
      subtitles += parse_list_subtitles(options)
      print_h1 title, subtitles
      print_process_event_details(process_event)
      print reset, "\n"
      return 0
    end
  rescue RestClient::Exception => e
    print_rest_exception(e, options)
    exit 1
  end
end

#get(args) ⇒ Object



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
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
# File 'lib/morpheus/cli/processes_command.rb', line 255

def get(args)
  options = {}
  params = {}
  process_id = nil
  optparse = Morpheus::Cli::OptionParser.new do |opts|
    opts.banner = subcommand_usage("[id]")
    opts.on(nil, '--details', "Display more details. Shows everything, untruncated." ) do
      options[:details] = true
    end
    build_common_options(opts, options, [:query, :json, :yaml, :csv, :fields, :dry_run, :remote])
    opts.footer = "Display details for a specific process.\n"
                  "[id] is required. This is the id of the process."
  end
  optparse.parse!(args)
  if args.count != 1
    puts_error optparse
    return 1
  end
  connect(options)
  begin
    process_id = args[0]
    params.merge!(parse_list_options(options))
    @processes_interface.setopts(options)
    if options[:dry_run]
      print_dry_run @processes_interface.dry.get(process_id, params)
      return
    end
    json_response = @processes_interface.get(process_id, params)
    if options[:json]
      puts as_json(json_response, options, "process")
      return 0
    elsif options[:yaml]
      puts as_yaml(json_response, options, "process")
      return 0
    elsif options[:csv]
      puts records_as_csv(json_response['process'], options)
      return 0
    else
      process = json_response["process"]
      title = "Process Details"
      subtitles = []
      subtitles << " Process ID: #{process_id}"
      subtitles += parse_list_subtitles(options)
      print_h1 title, subtitles
      print_process_details(process)

      print_h2 "Process Events"
      process_events = process['events'] || process['processEvents'] || []
      history_records = []
      if process_events.empty?
        puts "#{cyan}No events found.#{reset}"
      else      
        process_events.each do |process_event|
          event_row = {
                  id: process_event['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
        columns = [
          {:id => {:display_name => "EVENT 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,
          :output
        ]
        print cyan
        print as_pretty_table(history_records, columns, options)
        print_results_pagination({size: process_events.size, total: process_events.size})
        print reset, "\n"
        return 0
      end
    end
  rescue RestClient::Exception => e
    print_rest_exception(e, options)
    exit 1
  end
end

#handle(args) ⇒ Object



29
30
31
# File 'lib/morpheus/cli/processes_command.rb', line 29

def handle(args)
  handle_subcommand(args)
end

#list(args) ⇒ Object



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
253
# File 'lib/morpheus/cli/processes_command.rb', line 33

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