lib/splash/helpers.rb in prometheus-splash-0.1.1 vs lib/splash/helpers.rb in prometheus-splash-0.2.0

- old
+ new

@@ -1,10 +1,19 @@ # coding: utf-8 module Splash module Helpers + + def user_root + return Etc.getpwuid(0).name + end + + def group_root + return Etc.getgrgid(0).name + end + # facilité pour récupérer un PID depuis une regexp # @param [Hash] options # @option options [String] :pattern une regexp # @return [String] le PID def get_process(options = {}) @@ -15,10 +24,43 @@ else return '' end end + + # facilities to find a file in gem path + # @param [String] _gem a Gem name + # @param [String] _file a file relative path in the gem + # @return [String] the path of the file, if found. + # @return [False] if not found + def search_file_in_gem(_gem,_file) + if Gem::Specification.respond_to?(:find_by_name) + begin + spec = Gem::Specification.find_by_name(_gem) + rescue LoadError + spec = nil + end + else + spec = Gem.searcher.find(_gem) + end + if spec then + if Gem::Specification.respond_to?(:find_by_name) + res = spec.lib_dirs_glob.split('/') + else + res = Gem.searcher.lib_dirs_for(spec).split('/') + end + res.pop + services_path = res.join('/').concat("/#{_file}") + return services_path if File::exist?(services_path) + return false + else + return false + end + end + + + # facilité pour vérifier si le process actif est root # @return [Bool] vrai ou faux def is_root? case (Process.uid) when 0 @@ -33,11 +75,11 @@ # @return [void] le retour de la méthode wrappée def run_as_root(method, options = {}) unless is_root? return {:case => :not_root, :more => "subcommands : #{method.to_s}"} else - return self.send method + return self.send method, options end end # method for daemonize blocks # @param [Hash] options the list of options, keys are symbols @@ -45,11 +87,11 @@ # @option options [String] :pid_file the pid filename # @option options [String] :daemon_user the user to change privileges # @option options [String] :daemon_group the group to change privileges # @option options [String] :stderr_trace the path of the file where to redirect STDERR # @option options [String] :stdout_trace the path of the file where to redirect STDOUT - # @option options [Bool] :debug option to run foreground + # @option options [Bool] :foreground option to run foreground # @yield a process definion or block given # @example usage inline # class Test # include Splash::Helpers # private :daemonize @@ -84,14 +126,14 @@ # end # @return [Fixnum] pid the pid of the forked processus def daemonize(options) #Process.euid = 0 #Process.egid = 0 - return yield if options[:debug] + trap("SIGINT"){ exit! 0 } trap("SIGTERM"){ exit! 0 } trap("SIGHUP"){ exit! 0 } - + return yield if options[:foreground] fork do #Process.daemon File.open(options[:pid_file],"w"){|f| f.puts Process.pid } if options[:pid_file] if options[:daemon_user] and options[:daemon_group] then uid = Etc.getpwnam(options[:daemon_user]).uid