lib/splash/helpers.rb in prometheus-splash-0.0.2 vs lib/splash/helpers.rb in prometheus-splash-0.0.3
- old
+ new
@@ -5,31 +5,38 @@
# coding: utf-8
module Splash
module Helpers
-
+ # 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 = {})
pattern = options[:pattern]
res = `ps aux|grep '#{pattern}'|grep -v grep`.to_s
unless res.empty? then
return res.split(/\s+/)[1]
else
return ''
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
return true
else
return false
end
end
+ # facilité pour s'assurer qu'on execute une méthode avec les droits root
+ # @param [Symbol] method a method name th wrap
+ # @return [void] le retour de la méthode wrappée
def run_as_root(method)
unless is_root?
$stderr.puts "You need to be root to execute this subcommands : #{method.to_s}"
$stderr.puts "Please execute with sudo, or rvmsudo."
exit 10
@@ -37,17 +44,22 @@
return self.send method
end
end
# method for daemonize blocks
- # @param [Hash] _options the list of options, keys are symbols
- # @option _options [String] :description the description of the process, use for $0
- # @option _options [String] :pid_file the pid filenam
+ # @param [Hash] options the list of options, keys are symbols
+ # @option options [String] :description the description of the process, use for $0
+ # @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
# @yield a process definion or block given
# @example usage inline
# class Test
- # include Splash::Helpers::Application
+ # include Splash::Helpers
# private :daemonize
# def initialize
# @loop = Proc::new do
# loop do
# sleep 1
@@ -60,11 +72,11 @@
# end
# end
#
# @example usage block
# class Test
- # include Splash::Helpers::Application
+ # include Splash::Helpers
# include Dorsal::Privates
# private :daemonize
# def initialize
# end
#
@@ -86,19 +98,21 @@
trap("SIGHUP"){ exit! 0 }
fork do
#Process.daemon
File.open(options[:pid_file],"w"){|f| f.puts Process.pid } if options[:pid_file]
- uid = Etc.getpwnam(options[:daemon_user]).uid
- gid = Etc.getgrnam(options[:daemon_group]).gid
- Process::UID.change_privilege(uid)
- # Process::GID.change_privilege(gid)
- $stdout.reopen(options[:stdout_trace], "w")
- $stderr.reopen(options[:stderr_trace], "w")
+ if options[:daemon_user] and options[:daemon_group] then
+ uid = Etc.getpwnam(options[:daemon_user]).uid
+ gid = Etc.getgrnam(options[:daemon_group]).gid
+ Process::UID.change_privilege(uid)
+ # Process::GID.change_privilege(gid)
+ end
+ $stdout.reopen(options[:stdout_trace], "w") if options[:stdout_trace]
+ $stderr.reopen(options[:stderr_trace], "w") if options[:stderr_trace]
#$0 = options[:description]
- Process.setproctitle options[:description]
+ Process.setproctitle options[:description] if options[:description]
yield
end
return 0
@@ -108,10 +122,13 @@
# facilité d'installation de fichier
# @param [Hash] options
# @option options [String] :source le chemin source du fichier
# @option options [String] :target le chemin cible du fichier
+ # @option options [String] :mode les droits du fichier du type Octal "XXX"
+ # @option options [String] :owner le owner du fichier
+ # @option options [String] :group le groupe du fichier
def install_file(options = {})
#begin
FileUtils::copy options[:source], options[:target] #unless File::exist? options[:target]
FileUtils.chmod options[:mode].to_i(8), options[:target] if options[:mode]
FileUtils.chown options[:owner], options[:group], options[:target] if options[:owner] and options[:group]
@@ -122,9 +139,12 @@
end
# facilité de création de répertoire
# @param [Hash] options
# @option options [String] :path le répertoire à créer (relatif ou absolut)
+ # @option options [String] :mode les droits du fichier du type Octal "XXX"
+ # @option options [String] :owner le owner du fichier
+ # @option options [String] :group le groupe du fichier
def make_folder(options = {})
begin
FileUtils::mkdir_p options[:path] unless File::exist? options[:path]
FileUtils.chmod options[:mode].to_i(8), options[:path] if options[:mode]
FileUtils.chown options[:owner], options[:group], options[:path] if options[:owner] and options[:group]