Module: NexClient::Commands::ExecTasks

Extended by:
Helpers
Defined in:
lib/nex_client/commands/exec_tasks.rb

Constant Summary collapse

EXEC_TASKS_TITLE =
"Execution Tasks".colorize(:magenta)
EXEC_TASKS_HEADERS =
['id','name','active','crontab','last_run','next_run','executor'].map(&:upcase)
SCRIPT_TITLE =
"Script".colorize(:magenta)

Constants included from Helpers

Helpers::LOG_COLORS, Helpers::VARS_HEADERS, Helpers::VARS_TITLE

Class Method Summary collapse

Methods included from Helpers

display_events, display_logs, display_raw_vars, display_record_errors, display_vars, display_yaml_vars, error, events, format_cmd, hash_from_file, perform_ssh_cmd, success, vars_from_file, vars_from_plain_file, vars_from_yaml_file, with_cert_identity

Class Method Details

.create(args, opts) ⇒ Object

Create a new execution task



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
# 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) ⇒ Object



98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
# 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) ⇒ Object



119
120
121
122
123
124
125
126
127
# 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) ⇒ Object



150
151
152
153
154
# 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) ⇒ Object



129
130
131
132
133
134
135
136
137
138
139
140
# 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) ⇒ Object



142
143
144
145
146
147
148
# 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) ⇒ Object



27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# 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) ⇒ Object



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
# 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