module NexClient::Commands::ExecTasks

Constants

EXEC_TASKS_HEADERS
EXEC_TASKS_TITLE
SCRIPT_TITLE

Public Class Methods

create(args,opts) click to toggle source

Create a new execution task

# File lib/nex_client/commands/exec_tasks.rb, line 45
def self.create(args,opts)
  case
  when opts.cube
    executor = NexClient::CubeInstance.find(uuid: opts.cube).first
  when opts.app
    executor = NexClient::App.find(name: opts.app).first
  when opts.addon
    executor ||= NexClient::Addon.find(name: opts.addon).first
  when opts.rack
    [
      NexClient::ComputeRack,
      NexClient::GatewayRack,
      NexClient::RoutingRack,
      NexClient::StorageRack
    ].each do |server_type|
      executor ||= server_type.find(private_ip_address: opts.rack).first
    end
  else
    error("You need to specify an executor. For help: nex-cli exec-tasks:create -h")
    return false
  end

  # Display error
  unless executor
    error("Error! Could not find executor")
    return false
  end

  # Ask for additional details
  script = opts.script.present? ? File.read(opts.script) : ask("Copy/paste your script below:") { |q| q.gather = "" }
  name = opts.name.present? ? opts.name : ask("Enter the name of this task: ")
  crontab = opts.crontab.present? ? opts.crontab : ask("Enter a cron expression (m h d M w): ")

  exec_task = NexClient::ExecTask.new(
    name: name,
    script: script.join("\n"),
    crontab: crontab
  )
  exec_task.relationships.attributes = { executor: { data: { type: executor.type, id: executor.id } } }
  exec_task.save

  # Display errors if any
  if exec_task.errors.any?
    display_record_errors(exec_task)
    return false
  end

  # Display task
  e = NexClient::ExecTask.includes(:executor).find(exec_task.id).first
  self.display_exec_tasks(e)
  self.format_script(e)
end
destroy(args,opts) click to toggle source
# File lib/nex_client/commands/exec_tasks.rb, line 98
def self.destroy(args,opts)
  id = args.first
  e = NexClient::ExecTask.includes(:executor).find(id).first

  # Display error
  unless e
    error("Error! Could not find exec_task: #{id}")
    return false
  end

  # Ask confirmation
  answer = ask("Enter the id of this task to confirm: ")
  unless answer == e.id
    error("Aborting deletion...")
    return false
  end

  e.destroy
  success("Successfully destroyed task: #{id}")
end
display_exec_tasks(list) click to toggle source
# File lib/nex_client/commands/exec_tasks.rb, line 119
def self.display_exec_tasks(list)
  table = Terminal::Table.new title: EXEC_TASKS_TITLE, headings: EXEC_TASKS_HEADERS do |t|
    [list].flatten.compact.each do |e|
      t.add_row(self.format_record(e))
    end
  end
  puts table
  puts "\n"
end
format_executor(record) click to toggle source
# File lib/nex_client/commands/exec_tasks.rb, line 150
def self.format_executor(record)
  return '-' unless o = record.executor
  type = o.type.singularize.gsub('_instance','')
  "#{type}:#{o.name}"
end
format_record(record) click to toggle source
# File lib/nex_client/commands/exec_tasks.rb, line 129
def self.format_record(record)
  executor = self.format_executor(record)
  [
    record.id,
    record.name,
    record.active,
    record.crontab,
    record.last_run_at,
    record.next_run_at,
    executor
  ]
end
format_script(record) click to toggle source
# File lib/nex_client/commands/exec_tasks.rb, line 142
def self.format_script(record)
  table = Terminal::Table.new title: SCRIPT_TITLE do |t|
    t.add_row([record.script])
  end
  puts table
  puts "\n"
end
info(args,opts) click to toggle source
# File lib/nex_client/commands/exec_tasks.rb, line 27
def self.info(args,opts)
  id = args.first
  e = NexClient::ExecTask.includes(:executor).find(id).first

  # Display error
  unless e
    error("Error! Could not find exec_task: #{id}")
    return false
  end

  # Display task
  self.display_exec_tasks(e)

  # Display script
  self.format_script(e)
end
list(args,opts) click to toggle source
# File lib/nex_client/commands/exec_tasks.rb, line 11
def self.list(args,opts)
  filters = {}
  filters[:'executor.name'] = args.first if args.first.present?

  # Create table
  list = NexClient::ExecTask.includes(:executor).where(filters).order('name')
  self.display_exec_tasks(list)

  # Loop through results
  while (list.pages.links||{})['next']
    return true if ask("Press enter for next page ('q' to quit)") =~ /q/
    list = list.pages.next
    self.display_exec_tasks(list)
  end
end