Class: Splash::Commands::CommandWrapper
- Inherits:
-
Object
- Object
- Splash::Commands::CommandWrapper
- Includes:
- Backends, Splash::Config, Exiter, Helpers, Templates, Transports
- Defined in:
- lib/splash/commands.rb
Overview
command execution wrapper
Constant Summary
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 Exiter
Constants included from Loggers
Loggers::ALIAS, Loggers::LEVELS
Instance Method Summary collapse
-
#ack ⇒ Object
wrapper for ack command ( return 0 to prometheus via notify).
-
#call_and_notify(options) ⇒ Hash
execute commands or sequence via callbacks, remote or not, notify prometheus, templatize report to backends the big cheese.
-
#initialize(name) ⇒ CommandWrapper
constructor
Constructor.
-
#notify(value, time, session) ⇒ Hash
send metrics to Prometheus PushGateway.
Methods included from Transports
#get_default_client, #get_default_subscriber
Methods included from Splash::Config
Methods included from Splash::ConfigUtilities
#addservice, #checkconfig, #flush_backend, #setupsplash
Methods included from 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 Exiter
Methods included from Loggers
#change_logger, #get_logger, #get_session
Methods included from Backends
Constructor Details
#initialize(name) ⇒ CommandWrapper
Constructor
95 96 97 98 99 100 101 102 |
# File 'lib/splash/commands.rb', line 95 def initialize(name) @config = get_config @url = @config.prometheus_pushgateway_url @name = name unless @config.commands.keys.include? @name.to_sym then splash_exit case: :not_found, more: "command #{@name} is not defined in configuration" end end |
Instance Method Details
#ack ⇒ Object
wrapper for ack command ( return 0 to prometheus via notify)
105 106 107 108 |
# File 'lib/splash/commands.rb', line 105 def ack get_logger.info "Sending ack for command : '#{@name}'" notify(0,0) end |
#call_and_notify(options) ⇒ Hash
execute commands or sequence via callbacks, remote or not, notify prometheus, templatize report to backends the big cheese
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 176 177 178 179 180 181 182 183 184 185 186 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 230 231 232 233 |
# File 'lib/splash/commands.rb', line 137 def call_and_notify() log = get_logger session = ([:session])? [:session] : get_session acase = { :case => :quiet_exit } exit_code = 0 if @config.commands[@name.to_sym][:delegate_to] then return { :case => :options_incompatibility, :more => '--hostname forbidden with delagate commands'} if [:hostname] log.send "Remote command : #{@name} execution delegate to : #{@config.commands[@name.to_sym][:delegate_to][:host]} as : #{@config.commands[@name.to_sym][:delegate_to][:remote_command]}", session begin transport = get_default_client if transport.class == Hash and transport.include? :case then return transport else res = transport.execute({ :verb => :execute_command, payload: {:name => @config.commands[@name.to_sym][:delegate_to][:remote_command].to_s}, :return_to => "splash.#{Socket.gethostname}.return", :queue => "splash.#{@config.commands[@name.to_sym][:delegate_to][:host]}.input" }) exit_code = res[:exit_code] log.receive "return with exitcode #{exit_code}", session end rescue Interrupt splash_exit case: :interrupt, more: "Remote command exection" end else log.info "Executing command : '#{@name}' ", session start = Time.now start_date = DateTime.now.to_s unless [:trace] then log.item "Traceless execution", session if @config.commands[@name.to_sym][:user] then log.item "Execute with user : #{@config.commands[@name.to_sym][:user]}.", session system("sudo -u #{@config.commands[@name.to_sym][:user]} #{@config.commands[@name.to_sym][:command]} > /dev/null 2>&1") else system("#{@config.commands[@name.to_sym][:command]} > /dev/null 2>&1") end time = Time.now - start exit_code = $?.exitstatus else log.item "Tracefull execution", session if @config.commands[@name.to_sym][:user] then log.item "Execute with user : #{@config.commands[@name.to_sym][:user]}.", session stdout, stderr, status = Open3.capture3("sudo -u #{@config.commands[@name.to_sym][:user]} #{@config.commands[@name.to_sym][:command]}") else stdout, stderr, status = Open3.capture3(@config.commands[@name.to_sym][:command]) end time = Time.now - start data = Hash::new data[:start_date] = start_date data[:end_date] = DateTime.now.to_s data[:cmd_name] = @name data[:cmd_line] = @config.commands[@name.to_sym][:command] data[:desc] = @config.commands[@name.to_sym][:desc] data[:status] = status.to_s data[:stdout] = stdout data[:stderr] = stderr data[:exec_time] = time.to_s cmdrec = CmdRecords::new @name cmdrec.purge(@config.commands[@name.to_sym][:retention]) cmdrec.add_record data exit_code = status.exitstatus end log.ok "Command executed", session log.arrow "exitcode #{exit_code}", session if [:notify] then acase = notify(exit_code,time.to_i,session) else log.item "Without Prometheus notification", session end end if [: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 log.item "On failure callback : #{on_failure}", session if @config.commands.keys.include? on_failure then @name = on_failure.to_s call_and_notify else acase = { :case => :configuration_error , :more => "on_failure call error : #{on_failure} command inexistant."} end end if on_success and exit_code == 0 then log.item "On success callback : #{on_success}", session if @config.commands.keys.include? on_success then @name = on_success.to_s call_and_notify else acase = { :case => :configuration_error , :more => "on_success call error : #{on_failure} command inexistant."} end end else log.item "Without callbacks sequences", session end acase[:exit_code] = exit_code return acase end |
#notify(value, time, session) ⇒ Hash
send metrics to Prometheus PushGateway
114 115 116 117 118 119 120 121 122 123 124 125 126 |
# File 'lib/splash/commands.rb', line 114 def notify(value,time, session) log = get_logger unless verify_service url: @config.prometheus_pushgateway_url then return { :case => :service_dependence_missing, :more => "Prometheus Notification not send."} end cmdmonitor = CmdNotifier::new({name: @name, exitcode: value, time: time}) if cmdmonitor.notify then log.ok "Sending metrics to Prometheus Pushgateway",session else log.ko "Failed to send metrics to Prometheus Pushgateway",session end return { :case => :quiet_exit} end |