lib/splash/commands.rb in prometheus-splash-0.0.3 vs lib/splash/commands.rb in prometheus-splash-0.1.0

- old
+ new

@@ -45,10 +45,11 @@ def call_and_notify(options) puts "Executing command : '#{@name}' " start = Time.now + start_date = DateTime.now.to_s unless options[:trace] then puts " * Traceless execution" if @config.commands[@name.to_sym][:user] then puts " * Execute with user : #{@config.commands[@name.to_sym][:user]}." system("sudo -u #{@config.commands[@name.to_sym][:user]} #{@config.commands[@name.to_sym][:command]} > /dev/null 2>&1") @@ -68,20 +69,21 @@ time = Time.now - start tp = Template::new( list_token: @config.execution_template_tokens, template_file: @config.execution_template_path) - tp.date = DateTime.now.to_s + tp.start_date = start_date + tp.end_date = DateTime.now.to_s tp.cmd_name = @name tp.cmd_line = @config.commands[@name.to_sym][:command] tp.desc = @config.commands[@name.to_sym][:desc] tp.status = status.to_s tp.stdout = stdout tp.stderr = stderr tp.exec_time = time.to_s backend = get_backend :execution_trace - key = "#{@name}_trace.last" + key = @name backend.put key: key, value: tp.output exit_code = status.exitstatus end @@ -89,31 +91,34 @@ if options[:notify] then notify(exit_code,time.to_i) else puts " * Without Prometheus notification" end - on_failure = (@config.commands[@name.to_sym][:on_failure])? @config.commands[@name.to_sym][:on_failure] : false - on_success = (@config.commands[@name.to_sym][:on_success])? @config.commands[@name.to_sym][:on_success] : false + if options[:callback] then + on_failure = (@config.commands[@name.to_sym][:on_failure])? @config.commands[@name.to_sym][:on_failure] : false + on_success = (@config.commands[@name.to_sym][:on_success])? @config.commands[@name.to_sym][:on_success] : false - if on_failure and exit_code > 0 then - puts " * On failure callback : #{on_failure}" - if @config.commands.keys.include? on_failure then - @name = on_failure.to_s - call_and_notify options - else - $stderr.puts "on_failure call error : configuration mistake : #{on_failure} command inexistant." + if on_failure and exit_code > 0 then + puts " * On failure callback : #{on_failure}" + if @config.commands.keys.include? on_failure then + @name = on_failure.to_s + call_and_notify options + else + $stderr.puts "on_failure call error : configuration mistake : #{on_failure} command inexistant." + end end - end - if on_success and exit_code == 0 then - puts " * On success callback : #{on_success}" - if @config.commands.keys.include? on_success then - @name = on_success.to_s - call_and_notify options - else - $stderr.puts "on_success call error : configuration mistake : #{on_success} command inexistant." + if on_success and exit_code == 0 then + puts " * On success callback : #{on_success}" + if @config.commands.keys.include? on_success then + @name = on_success.to_s + call_and_notify options + else + $stderr.puts "on_success call error : configuration mistake : #{on_success} command inexistant." + end end + else + puts " * Without callbacks sequences" end - exit exit_code end end end