module Morpheus::Cli::JobsHelper

Mixin for Morpheus::Cli command classes Provides refreshing job execution records by id

Public Class Methods

included(klass) click to toggle source
# File lib/morpheus/cli/mixins/jobs_helper.rb, line 6
def self.included(klass)
  klass.send :include, Morpheus::Cli::PrintHelper
  # klass.send :include, Morpheus::Cli::ProcessesHelper
end

Public Instance Methods

api_client() click to toggle source
# File lib/morpheus/cli/mixins/jobs_helper.rb, line 11
def api_client
  raise "#{self.class} has not defined @api_client" if @api_client.nil?
  @api_client
end
format_job_status(status_string, return_color=cyan) click to toggle source
# File lib/morpheus/cli/mixins/jobs_helper.rb, line 126
def format_job_status(status_string, return_color=cyan)
  out = ""
  if status_string
    if ['complete','success', 'successful', 'ok'].include?(status_string)
      out << "#{green}#{status_string.upcase}"
    elsif ['error', 'offline', 'failed', 'failure'].include?(status_string)
      out << "#{red}#{status_string.upcase}"
    elsif ['running'].include?(status_string)
      out << "#{cyan}#{status_string.upcase}"
    else
      out << "#{yellow}#{status_string.upcase}"
    end
  end
  out + return_color
end
get_available_contexts_for_task(task) click to toggle source
# File lib/morpheus/cli/mixins/jobs_helper.rb, line 174
def get_available_contexts_for_task(task)
  #If task has target of resource, then CAN NOT run it local
  targets = []
  has_resource = task['executeTarget'] == 'resource'
  if !has_resource
    targets << {'name' => 'None', 'value' => 'appliance'}
  end
  targets << {'name' => 'Instance', 'value' => 'instance'}
  targets << {'name' => 'Instance Label', 'value' => 'instance-label'}
  targets << {'name' => 'Server', 'value' => 'server'}
  targets << {'name' => 'Server Label', 'value' => 'server-label'}
  return targets
end
get_available_contexts_for_workflow(workflow) click to toggle source
# File lib/morpheus/cli/mixins/jobs_helper.rb, line 188
def get_available_contexts_for_workflow(workflow)
  #If any task has target of resource, then CAN NOT run it local
  targets = []
  has_resource = workflow['taskSetTasks'].find {|task| task['executeTarget'] == 'resource' }
  if !has_resource
    targets << {'name' => 'None', 'value' => 'appliance'}
  end
  targets << {'name' => 'Instance', 'value' => 'instance'}
  targets << {'name' => 'Instance Label', 'value' => 'instance-label'}
  targets << {'name' => 'Server', 'value' => 'server'}
  targets << {'name' => 'Server Label', 'value' => 'server-label'}
  return targets
end
get_process_event_data(process_or_event) click to toggle source
# File lib/morpheus/cli/mixins/jobs_helper.rb, line 21
def get_process_event_data(process_or_event)
  {
      id: process_or_event['id'],
      description: process_or_event['description'] || (process_or_event['refType'] == 'instance' ? process_or_event['displayName'] : (process_or_event['processTypeName'] || '').capitalize),
      start_date: format_local_dt(process_or_event['startDate']),
      created_by: process_or_event['createdBy'] ? process_or_event['createdBy']['displayName'] : '',
      duration: format_human_duration((process_or_event['duration'] || process_or_event['statusEta'] || 0) / 1000.0),
      status: format_job_status(process_or_event['status']),
      error: process_or_event['message'] || process_or_event['error'],
      output: process_or_event['output'],
  }
end
jobs_interface() click to toggle source
# File lib/morpheus/cli/mixins/jobs_helper.rb, line 16
def jobs_interface
  # get_interface('jobs')
  api_client.jobs
end
print_job_execution(job_execution, options) click to toggle source
print_job_executions(execs, options={}) click to toggle source
print_process_events(events, options={}) click to toggle source

both process and process events

wait_for_job_execution(job_execution_id, options={}, print_output = true) click to toggle source

refresh execution request until it is finished returns json response data of the last execution request when status reached ‘completed’ or ‘failed’

# File lib/morpheus/cli/mixins/jobs_helper.rb, line 152
def wait_for_job_execution(job_execution_id, options={}, print_output = true)
  refresh_interval = 5
  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 execution is complete...", "\n", reset
  end
  job_execution = jobs_interface.get_execution(job_execution_id)['jobExecution']
  while ['new','queued','pending','running'].include?(job_execution['status']) do
    sleep(refresh_interval)
    job_execution = jobs_interface.get_execution(job_execution_id)['jobExecution']
  end
  if print_output && options[:quiet] != true
    print_h1 "Morpheus Job Execution", [], options
    print_job_execution(job_execution, options)
  end
  return job_execution
end