module NixAdm::Util
Optional mixin for general utility functions
Public Instance Methods
Run shell command in bash with pipefail option for full error detection on pipelines.
# File src/lib/nixadm/util.rb, line 69 def bash(cmd) cmd = "set -o pipefail && #{cmd}" system("bash -c #{cmd.shellescape}") cmd_status = $? if cmd_status != 0 raise "'#{cmd}' execution failed (exit code: #{cmd_status})" end end
Connect to a database. Uses configuration file. Config file is YAML and has following format for databases:
databases:
logger: host: db db: nixadm user: bob password: bobspassword
@param db_name The name of the entry in the configuration file for the
database connection paramaters.
@return Database connection if successful, nil otherwise
# File src/lib/nixadm/util.rb, line 130 def connectDb(db_name, config_file = nil) if config_file.nil? config_file = $nixadm_config_file # Account for BSD systems that user /usr/local/etc prefix = RbConfig::CONFIG['prefix'] if prefix == '/usr/local' config_file = prefix + $nixadm_config_file end end @config = YAML::load_file(config_file) params = @config['databases'][db_name] login = params['user'] password = params['password'] db = params['db'] host = params['host'] port = params['port'] || '5432' return PG::Connection.new(host, port, '', '', db, login, password) end
Call when job has completed. Logs a completed message and clears the service function name.
# File src/lib/nixadm/util.rb, line 174 def jobFinish() log('Completed', 0) @logfields[:function] = '' end
Called before starting a backup job. Sets service function name based on called method and logs a start message.
# File src/lib/nixadm/util.rb, line 158 def jobStart(host=nil) if not host.nil? @logfields[:function] = "#{caller_locations()[0].label} #{host} " else @logfields[:function] = "#{caller_locations()[0].label}" end @job = SecureRandom.uuid() log('Started', 0) end
Logs a message. Uses service name defined in jobStart().
@param msg A log message describing a completed operation @param code The status code of the operation in the log message
# File src/lib/nixadm/util.rb, line 184 def log(message, code = 0) fields = {}.merge(@logfields) fields['job'] = @job fields['code'] = code if not message.nil? fields['message'] = message.sqlEscape() end keys = [] values = [] fields.each do |k,v| keys << k.to_s values << v end sql = %Q{insert into sys.log (#{keys.join(",")}) values ('#{values.join("','")}')} @logdb.exec(sql) $stderr.puts message end
Logging to PostgreSQL
Connect to a database. Uses configuration file. Config file is YAML and logger uses the following format for database connection:
databases:
logger: host: db db: nixadm user: bob password: bobspassword
@param db_name The name of the entry in the configuration file for the
database connection paramaters.
@return Database connection if successful, nil otherwise
# File src/lib/nixadm/util.rb, line 96 def logSystemInit(config_file = nil) @logdb = connectDb('logger', config_file) @logfields = { :host => Socket.gethostname, :system => 'backup', :module => self.class.name, :function => 'run' } end
Call when complete backup is done. Disconnects from database
# File src/lib/nixadm/util.rb, line 110 def logSystemfinalize() @logdb.disconnect() end