Class: CLISplash::Commands

Inherits:
Thor
  • Object
show all
Includes:
Splash::Backends, Splash::Commands, Splash::Config, Splash::Exiter, Splash::Loggers, Splash::Templates, Splash::Transports
Defined in:
lib/splash/cli/commands.rb

Overview

Thor inherited class for commands management

Constant Summary

Constants included from Splash::Loggers

Splash::Loggers::ALIAS, Splash::Loggers::LEVELS

Constants included from Splash::Constants

Splash::Constants::AUTHOR, Splash::Constants::BACKENDS_STRUCT, Splash::Constants::CONFIG_FILE, Splash::Constants::COPYRIGHT, Splash::Constants::DAEMON_LOGMON_SCHEDULING, Splash::Constants::DAEMON_METRICS_SCHEDULING, Splash::Constants::DAEMON_PID_FILE, Splash::Constants::DAEMON_PROCESS_NAME, Splash::Constants::DAEMON_PROCMON_SCHEDULING, Splash::Constants::DAEMON_STDERR_TRACE, Splash::Constants::DAEMON_STDOUT_TRACE, Splash::Constants::DEFAULT_RETENTION, Splash::Constants::EMAIL, Splash::Constants::EXECUTION_TEMPLATE, Splash::Constants::EXECUTION_TEMPLATE_TOKENS_LIST, Splash::Constants::LICENSE, Splash::Constants::LOGGERS_STRUCT, Splash::Constants::PID_PATH, Splash::Constants::PROMETHEUS_ALERTMANAGER_URL, Splash::Constants::PROMETHEUS_PUSHGATEWAY_URL, Splash::Constants::PROMETHEUS_URL, Splash::Constants::TRACE_PATH, Splash::Constants::TRANSPORTS_STRUCT, Splash::Constants::VERSION, Splash::Constants::WEBADMIN_IP, Splash::Constants::WEBADMIN_PID_FILE, Splash::Constants::WEBADMIN_PID_PATH, Splash::Constants::WEBADMIN_PORT, Splash::Constants::WEBADMIN_PROCESS_NAME, Splash::Constants::WEBADMIN_PROXY, Splash::Constants::WEBADMIN_STDERR_TRACE, Splash::Constants::WEBADMIN_STDOUT_TRACE

Constants included from Splash::Exiter

Splash::Exiter::EXIT_MAP

Instance Method Summary collapse

Methods included from Splash::Loggers

#change_logger, #get_logger, #get_session

Methods included from Splash::Config

#get_config, #rehash_config

Methods included from Splash::ConfigUtilities

#addservice, #checkconfig, #flush_backend, #setupsplash

Methods included from Splash::Helpers

#check_unicode_term, #daemonize, #format_by_extensions, #format_response, #get_processes, #group_root, #install_file, #is_root?, #make_folder, #make_link, #run_as_root, #search_file_in_gem, #user_root, #verify_file, #verify_folder, #verify_link, #verify_service

Methods included from Splash::Transports

#get_default_client, #get_default_subscriber

Methods included from Splash::Exiter

#splash_exit, #splash_return

Methods included from Splash::Backends

#get_backend, #list_backends

Instance Method Details

#execute(name) ⇒ 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
# File 'lib/splash/cli/commands.rb', line 33

def execute(name)
  log = get_logger
  log.level = :fatal if options[:quiet]
  if is_root? then
    if options[:hostname] then
      options[:hostname] = Socket.gethostname if options[:hostname] == 'hostname'
      splash_exit({ :case => :options_incompatibility, :more => '--hostname forbidden with delagate commands'}) if get_config.commands[name.to_sym][:delegate_to]
      log.info "Remote Splash configured commands on #{options[:hostname]}:"
      log.info "ctrl+c for interrupt"
      begin
        transport = get_default_client
        if transport.class == Hash  and transport.include? :case then
          splash_exit transport
        else
          if options[:ack] then
            res = transport.execute({ :verb => :ack_command,
                                  payload: {:name => name},
                                  :return_to => "splash.#{Socket.gethostname}.returncli",
                                  :queue => "splash.#{options[:hostname]}.input" })
            res[:more] = "Remote command : :ack_command OK"
            splash_exit res
          else
            res = transport.execute({ :verb => :execute_command,
                                payload: {:name => name},
                                :return_to => "splash.#{Socket.gethostname}.returncli",
                                :queue => "splash.#{options[:hostname]}.input" })
          end
        end
      rescue Interrupt
        splash_exit case: :interrupt, more: "Remote command exection"
      end
      log.receive "Command execute confirmation"
      res[:more] = "Remote command : :execute_command Scheduled"
      splash_exit res
    else
      command =  CommandWrapper::new(name)
      if options[:ack] then
        splash_exit command.ack
      end
      acase = command.call_and_notify trace: options[:trace], notify: options[:notify], callback: options[:callback]
      splash_exit acase
    end
  else
    splash_exit case: :not_root, :more => "Command execution"
  end
end

#get_result(command) ⇒ Object



341
342
343
344
345
346
347
348
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
# File 'lib/splash/cli/commands.rb', line 341

def get_result(command)
  unless is_root? then
    splash_exit case: :not_root, :more => "Command specific execution report"
  end
  log = get_logger
  backend = get_backend :execution_trace
  redis = (backend.class == Splash::Backends::Redis)? true : false
  if not redis and options[:hostname] then
    splash_exit case: :specific_config_required, :more => "Redis backend is requiered for Remote execution report request"
  end
  splash_exit case: :not_root if not is_root? and not redis
  list = get_config.commands.keys
  if options[:hostname] then
    options[:hostname] = Socket.gethostname if options[:hostname] == 'hostname'
    list = backend.list("*", options[:hostname]).map(&:to_sym)
  end
  if list.include? command.to_sym then
    log.info "Splash command #{command} previous execution report:\n"
    req  = { :key => command}
    req[:hostname] = options[:hostname] if options[:hostname]
    if backend.exist? req then
      res = backend.get req
      tp = Template::new(
          list_token: get_config.execution_template_tokens,
          template_file: get_config.execution_template_path)
      if options[:date] then
        prov =  YAML::load(res).select{|key,value| key.keys.first == options[:date]}.first
      else
        prov = YAML::load(res).last
      end
      if prov.nil? then
        log.ko "Command not runned one this date or date misformatted."
      else
        tp.map prov.values.first
        log.flat tp.output
      end
    else
      log.ko "Command not already runned."
    end
    splash_exit case: :quiet_exit
  else
    if options[:hostname]
        splash_exit case: :not_found, :more => "Command never runned remotly"
      else
        splash_exit case: :not_found, :more => "Command never runned"
      end
  end
end

#history(command) ⇒ Object



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
# File 'lib/splash/cli/commands.rb', line 288

def history(command)
  if is_root? then
    log = get_logger
    log.info "Command : #{command}"
    config = get_config
    if options[:table] then
      table = TTY::Table.new do |t|
        t << ["Start Date","Status", "end_date", "Execution time","STDOUT empty ? ", "STDERR empty ? "]
        t << ['','','','','','']
        CmdRecords::new(command).get_all_records.each do |item|
          record =item.keys.first
          value=item[record]
          t << [record, value[:status].to_s,
                        value[:end_date],
                        value[:exec_time],
                        value[:stdout].empty?,
                        value[:stdout].empty?]
        end
      end
      if check_unicode_term  then
        puts table.render(:unicode)
      else
        puts table.render(:ascii)
      end

    else
      CmdRecords::new(command).get_all_records.each do |item|
        record =item.keys.first
        value=item[record]
        log.item record
        log.arrow "Status : #{value[:status].to_s}"
        log.arrow "End date : #{value[:end_date]}"
        log.arrow "Execution time : #{value[:exec_time]}"
        log.arrow "STDOUT empty ?  : #{value[:stdout].empty?}"
        log.arrow "STDERR empty ?  : #{value[:stderr].empty?}"
      end
    end
    splash_exit case: :quiet_exit
  else
    splash_exit case: :not_root, :more => "Command execution history"
  end
end

#listObject



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
# File 'lib/splash/cli/commands.rb', line 187

def list
  unless is_root? then
    splash_exit case: :not_root, :more => "Command list"
  end
  log = get_logger
  list = {}
  if options[:hostname] then
    options[:hostname] = Socket.gethostname if options[:hostname] == 'hostname'
    log.info "Remote Splash configured commands on #{options[:hostname]}:"
    log.info  "ctrl+c for interrupt"
    begin
      transport = get_default_client
      if transport.class == Hash  and transport.include? :case then
        splash_exit transport
      else
        list = transport.execute({ :verb => :list_commands,
                              :return_to => "splash.#{Socket.gethostname}.returncli",
                              :queue => "splash.#{options[:hostname]}.input" })
      end
    rescue Interrupt
      splash_exit case: :interrupt, more: "remote list Command"
    end
    log.receive "Receving list of commands from #{options[:hostname]}"
  else
    list = get_config.commands
  end
  log.info "Splash configured commands :"
  log.ko 'No configured commands found' if list.keys.empty?
  list.keys.each do |command|
    log.item "#{command.to_s}"
    if options[:detail] then
      log.arrow "command line : '#{list[command][:command]}'"
      log.arrow "command description : '#{list[command][:desc]}'"
      log.arrow "command failure callback : '#{list[command.to_sym][:on_failure]}'" if list[command.to_sym][:on_failure]
      log.arrow "command success callback : '#{list[command.to_sym][:on_success]}'" if list[command.to_sym][:on_success]
      if list[command.to_sym][:schedule]
        sched,val = list[command.to_sym][:schedule].flatten
        log.arrow "command scheduled : #{sched} #{val}."
      end
    end
  end
  splash_exit case: :quiet_exit
end

#reportlistObject



404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
# File 'lib/splash/cli/commands.rb', line 404

def reportlist
  unless is_root? then
    splash_exit case: :not_root, :more => "Command execution report list"
  end
  log = get_logger
  options[:hostname] = Socket.gethostname if options[:hostname] == 'hostname'
  if options[:hostname] and options[:all] then
    splash_exit case: :options_incompatibility, more: "--all, --hostname"
  end
  backend = get_backend :execution_trace
  redis = (backend.class == Splash::Backends::Redis)? true : false
  if not redis and (options[:hostname] or options[:all]) then
    splash_exit case: :specific_config_required, more: "Redis Backend requiered for Remote execution report Request"
  end
  splash_exit case: :not_root if not is_root? and not redis
  pattern = (options[:pattern])? options[:pattern] : '*'
  if options[:all] then
    res = backend.listall pattern
  elsif options[:hostname]
    res = backend.list pattern, options[:hostname]
  else
    res = backend.list pattern
  end
  log.info "List of Executions reports :\n"
  log.ko "Not reports found" if res.empty?
  res.each do |item|
    host = ""
    command = ""
    if options[:all]
      host,command = item.split('#')
      log.item "Command : #{command} @ host : #{host}"
    else
      command = item
      log.item "Command : #{command}"
    end
    if options[:detail] then
      req = { :key => command }
      req[:hostname] = host if options[:all]
      res = YAML::load(backend.get(req))
      res.each do |record|
        log.arrow "#{record.keys.first} : #{record[record.keys.first][:status]}"
      end
    end
  end
  splash_exit case: :quiet_exit
end

#schedule(name) ⇒ Object



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
# File 'lib/splash/cli/commands.rb', line 95

def schedule(name)
  unless is_root? then
    splash_exit case: :not_root, :more => "Command scheduling"
  end
  log = get_logger
  log.level = :fatal if options[:quiet]
  hostname = (options[:hostname])? options[:hostname] : Socket.gethostname
  splash_exit({ :case => :options_incompatibility, :more => '--at or --in is required'}) unless options[:at] or options[:in]
  splash_exit({ :case => :options_incompatibility, :more => '--at an --in'}) if options[:at] and options[:in]
  log.info "Remote Splash scheduling command on #{hostname}:"
  log.info "ctrl+c for interrupt"
  begin
    transport = get_default_client
    if transport.class == Hash  and transport.include? :case then
      splash_exit transport
    else
      schedule = { :in => options[:in]} if options[:in]
      schedule = { :at => options[:at]} if options[:at]
      res = transport.execute({ :verb => :execute_command,
                              payload: {:name => name, :schedule => schedule},
                              :return_to => "splash.#{Socket.gethostname}.returncli",
                              :queue => "splash.#{hostname}.input" })
    end
  rescue Interrupt
    splash_exit case: :interrupt, more: "Remote command exection"
  end
  log.receive "Execute command sheduled confirmed"
  res[:more] = "Remote command : :execute_command with schedule"
  splash_exit res

end

#show(command) ⇒ Object



238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
# File 'lib/splash/cli/commands.rb', line 238

def show(command)
  unless is_root? then
    splash_exit case: :not_root, :more => "Command show specifications"
  end
  log = get_logger
  list = {}
  if options[:hostname] then
    options[:hostname] = Socket.gethostname if options[:hostname] == 'hostname'
    log.info "Remote Splash configured commands on #{options[:hostname]}:"
    log.info "ctrl+c for interrupt"
    begin
      transport = get_default_client
      if transport.class == Hash  and transport.include? :case then
        splash_exit transport
      else
        list = transport.execute({ :verb => :list_commands,
                              :return_to => "splash.#{Socket.gethostname}.returncli",
                              :queue => "splash.#{options[:hostname]}.input" })
      end
    rescue Interrupt
      splash_exit case: :interrupt, more: "remote list Command"
    end
    log.receive "Receving list of commands from #{options[:hostname]}"
  else
    list = get_config.commands
  end
  if list.keys.include? command.to_sym then
    log.info "Splash command : #{command}"
    log.item "command line : '#{list[command.to_sym][:command]}'"
    log.item "command description : '#{list[command.to_sym][:desc]}'"
    log.item "command failure callback : '#{list[command.to_sym][:on_failure]}'" if list[command.to_sym][:on_failure]
    log.item "command success callback : '#{list[command.to_sym][:on_success]}'" if list[command.to_sym][:on_success]
    if list[command.to_sym][:schedule]
      sched,val = list[command.to_sym][:schedule].flatten
      log.item "command scheduled : #{sched} #{val}."
    end
    splash_exit case: :quiet_exit
  else
    splash_exit case: :not_found, :more => 'Command not configured'
  end
end

#treeview(command) ⇒ Object



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
# File 'lib/splash/cli/commands.rb', line 134

def treeview(command)
  unless is_root? then
    splash_exit case: :not_root, :more => "Command treeview"
  end
  depht = 0
  log  = get_logger
  if options[:hostname] then
    options[:hostname] = Socket.gethostname if options[:hostname] == 'hostname'
    log.info "Remote Splash treeview command on #{options[:hostname]}:"
    log.info "ctrl+c for interrupt"
    begin
      transport = get_default_client
      if transport.class == Hash  and transport.include? :case then
        splash_exit transport
      else
        commands = transport.execute({ :verb => :list_commands,
                              :return_to => "splash.#{Socket.gethostname}.returncli",
                              :queue => "splash.#{options[:hostname]}.input" })
      end
    rescue Interrupt
      splash_exit case: :interrupt, more: "Remote command exection"
    end
    log.receive "Receving list of commands from #{options[:hostname]}"
  else
    commands  = get_config.commands
  end
  log.info "Command : #{command.to_s}" if depht == 0
  aproc = Proc::new do |command,depht|
    cmd  = commands[command.to_sym]
    if cmd[:on_failure] then
      spacer=  " " * depht + " "
      log.flat "#{spacer}* on failure => #{cmd[:on_failure]}"
      aproc.call(cmd[:on_failure], depht+2)
    end
    if cmd[:on_success] then
      spacer = " " * depht + " "
      log.flat "#{spacer}* on success => #{cmd[:on_success]}"
      aproc.call(cmd[:on_success],depht+2)
    end
  end
  aproc.call(command,depht)
end