Module: Splash::Helpers
- Included in:
- CLISplash::Config, Backends::File, CommandWrapper, Config, ConfigUtilities, LogsMonitor::DaemonController, Orchestrator::Scheduler
- Defined in:
- lib/splash/helpers.rb
facilités sur le système de fichier collapse
-
#install_file(options = {}) ⇒ Object
facilité d’installation de fichier.
-
#make_folder(options = {}) ⇒ Object
facilité de création de répertoire.
-
#make_link(options = {}) ⇒ Object
facilité de liaison symbolique de fichier.
Vérifiers de l'application collapse
-
#verify_file(options = {}) ⇒ Array
verifier d’existence d’un fichier.
-
#verify_folder(options = {}) ⇒ Array
verifier d’existence d’un repertoire.
-
#verify_link(options = {}) ⇒ Bool
verifier d’existence d’un lien.
-
#verify_service(options = {}) ⇒ Bool
verifier de l’ecoute d’un service sur un host et port donné en TCP.
Instance Method Summary collapse
-
#daemonize(options) { ... } ⇒ Fixnum
method for daemonize blocks.
-
#get_process(options = {}) ⇒ String
facilité pour récupérer un PID depuis une regexp.
- #group_root ⇒ Object
-
#is_root? ⇒ Bool
facilité pour vérifier si le process actif est root.
-
#run_as_root(method, options = {}) ⇒ void
facilité pour s’assurer qu’on execute une méthode avec les droits root.
-
#search_file_in_gem(_gem, _file) ⇒ String, False
facilities to find a file in gem path.
- #user_root ⇒ Object
Instance Method Details
#daemonize(options) { ... } ⇒ Fixnum
method for daemonize blocks
132 133 134 135 136 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 |
# File 'lib/splash/helpers.rb', line 132 def daemonize() #Process.euid = 0 #Process.egid = 0 trap("SIGINT"){ if [:sigint_handler] then [:sigint_handler].call else exit! 0 end } trap("SIGTERM"){ if [:sigterm_handler] then [:sigterm_handler].call else exit! 0 end } trap("SIGHUP"){ if [:sighup_handler] then [:sighup_handler].call else exit! 0 end } if [:foreground] change_logger logger: :dual return yield end fork do change_logger logger: :daemon #Process.daemon File.open([:pid_file],"w"){|f| f.puts Process.pid } if [:pid_file] if [:daemon_user] and [:daemon_group] then uid = Etc.getpwnam([:daemon_user]).uid gid = Etc.getgrnam([:daemon_group]).gid Process::UID.change_privilege(uid) # Process::GID.change_privilege(gid) end $stdout.reopen([:stdout_trace], "w") if [:stdout_trace] $stderr.reopen([:stderr_trace], "w") if [:stderr_trace] #$0 = options[:description] Process.setproctitle [:description] if [:description] yield end return 0 end |
#get_process(options = {}) ⇒ String
facilité pour récupérer un PID depuis une regexp
20 21 22 23 24 25 26 27 28 |
# File 'lib/splash/helpers.rb', line 20 def get_process( = {}) pattern = [:pattern] res = `ps aux|grep '#{pattern}'|grep -v grep`.to_s unless res.empty? then return res.split(/\s+/)[1] else return '' end end |
#group_root ⇒ Object
12 13 14 |
# File 'lib/splash/helpers.rb', line 12 def group_root return Etc.getgrgid(0).name end |
#install_file(options = {}) ⇒ Object
facilité d’installation de fichier
192 193 194 195 196 197 198 199 200 201 |
# File 'lib/splash/helpers.rb', line 192 def install_file( = {}) #begin FileUtils::copy [:source], [:target] #unless File::exist? options[:target] FileUtils.chmod [:mode].to_i(8), [:target] if [:mode] FileUtils.chown [:owner], [:group], [:target] if [:owner] and [:group] return true #rescue StandardError # return false #end end |
#is_root? ⇒ Bool
facilité pour vérifier si le process actif est root
65 66 67 68 69 70 71 72 |
# File 'lib/splash/helpers.rb', line 65 def is_root? case (Process.uid) when 0 return true else return false end end |
#make_folder(options = {}) ⇒ Object
facilité de création de répertoire
209 210 211 212 213 214 215 216 217 218 |
# File 'lib/splash/helpers.rb', line 209 def make_folder( = {}) begin FileUtils::mkdir_p [:path] unless File::exist? [:path] FileUtils.chmod [:mode].to_i(8), [:path] if [:mode] FileUtils.chown [:owner], [:group], [:path] if [:owner] and [:group] return true rescue StandardError return false end end |
#make_link(options = {}) ⇒ Object
facilité de liaison symbolique de fichier
224 225 226 227 228 229 230 231 232 |
# File 'lib/splash/helpers.rb', line 224 def make_link( = {}) begin FileUtils::rm [:link] if (File::symlink? [:link] and not File::exist? [:link]) FileUtils::ln_s [:source], [:link] unless File::exist? [:link] return true rescue StandardError return false end end |
#run_as_root(method, options = {}) ⇒ void
This method returns an undefined value.
facilité pour s’assurer qu’on execute une méthode avec les droits root
77 78 79 80 81 82 83 |
# File 'lib/splash/helpers.rb', line 77 def run_as_root(method, = {}) unless is_root? return {:case => :not_root, :more => "subcommands : #{method.to_s}"} else return self.send method, end end |
#search_file_in_gem(_gem, _file) ⇒ String, False
facilities to find a file in gem path
36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 |
# File 'lib/splash/helpers.rb', line 36 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 |
#user_root ⇒ Object
8 9 10 |
# File 'lib/splash/helpers.rb', line 8 def user_root return Etc.getpwuid(0).name end |
#verify_file(options = {}) ⇒ Array
verifier d’existence d’un fichier
277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 |
# File 'lib/splash/helpers.rb', line 277 def verify_file( ={}) res = Array::new return [:inexistant] unless File.file?([:name]) stat = File.stat([:name]) if [:mode] then mode = "%o" % stat.mode res << :mode if mode[-3..-1] != [:mode] end if [:owner] then res << :owner if Etc.getpwuid(stat.uid).name != [:owner] end if [:group] then res << :group if Etc.getgrgid(stat.gid).name != [:group] end return res end |
#verify_folder(options = {}) ⇒ Array
verifier d’existence d’un repertoire
245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 |
# File 'lib/splash/helpers.rb', line 245 def verify_folder( ={}) res = Array::new return [:inexistant] unless File.directory?([:name]) stat = File.stat([:name]) if [:mode] then mode = "%o" % stat.mode res << :mode if mode[-3..-1] != [:mode] end if [:owner] then res << :owner if Etc.getpwuid(stat.uid).name != [:owner] end if [:group] then res << :group if Etc.getgrgid(stat.gid).name != [:group] end return res end |
#verify_link(options = {}) ⇒ Bool
verifier d’existence d’un lien
266 267 268 |
# File 'lib/splash/helpers.rb', line 266 def verify_link( ={}) return File.file?([:name]) end |
#verify_service(options = {}) ⇒ Bool
verifier de l’ecoute d’un service sur un host et port donné en TCP
299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 |
# File 'lib/splash/helpers.rb', line 299 def verify_service( ={}) begin Timeout::timeout(1) do begin s = TCPSocket.new([:host], [:port]) s.close return true rescue Errno::ECONNREFUSED, Errno::EHOSTUNREACH return false end end rescue Timeout::Error return false end end |