Module: Splash::Helpers
- Included in:
- CLISplash::Config, CLISplash::Transfers, CLISplash::WebAdmin, Backends::File, Commands::CommandWrapper, Config, ConfigUtilities, Daemon::Controller, Daemon::Metrics, Daemon::Orchestrator::Scheduler, Loggers::Cli, Transfers, WebAdmin::Controller, WebAdminApp
- Defined in:
- lib/splash/helpers.rb
Overview
Helpers namespace
facilities for file system commands collapse
-
#install_file(options = {}) ⇒ Object
facility for file installation.
-
#make_folder(options = {}) ⇒ Object
facility for folder creation.
-
#make_link(options = {}) ⇒ Object
facility for Symbolic link.
Verifiers for application : FS and TCP/IP services collapse
-
#check_unicode_term ⇒ Boolean
check if unicode must be used with term ENV.
- #format_by_extensions(extension) ⇒ Object
-
#format_response(data, format) ⇒ Object
!@endgroup.
-
#verify_file(options = {}) ⇒ Array
check file.
-
#verify_folder(options = {}) ⇒ Array
check folder.
-
#verify_link(options = {}) ⇒ Boolean
check symlink.
-
#verify_service(options = {}) ⇒ Bool
TCP/IP service checker.
Instance Method Summary collapse
-
#daemonize(options) { ... } ⇒ Fixnum
method for daemonize blocks.
-
#get_processes(options = {}) ⇒ String|Array
facility for retreiving PID from process query.
-
#group_root ⇒ String
return the ‘root’ group name : root or wheel.
-
#is_root? ⇒ Bool
facility to verifyingif the active process run as root.
-
#run_as_root(method, options = {}) ⇒ void
wrapping execution if process run as root.
-
#search_file_in_gem(_gem, _file) ⇒ String, False
facility to find a file in gem path.
-
#user_root ⇒ String
return the ‘root’ name.
Instance Method Details
#check_unicode_term ⇒ Boolean
check if unicode must be used with term ENV
359 360 361 362 363 364 365 366 |
# File 'lib/splash/helpers.rb', line 359 def check_unicode_term return false unless ENV.include? "TERM" if ENV.values_at("LC_ALL","LC_CTYPE","LANG").compact.include?("UTF-8") and ENV.values_at('TERM').include? "xterm" then return true else return false end end |
#daemonize(options) { ... } ⇒ Fixnum
method for daemonize blocks
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 |
# File 'lib/splash/helpers.rb', line 145 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 Process.setproctitle [:description] if [:description] 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 |
#format_by_extensions(extension) ⇒ Object
348 349 350 351 352 353 354 355 |
# File 'lib/splash/helpers.rb', line 348 def format_by_extensions(extension) result = { 'json' => 'application/json', 'yaml' => 'text/x-yaml', 'yml' => 'text/x-yaml' } return result[extension] end |
#format_response(data, format) ⇒ Object
!@endgroup
339 340 341 342 343 344 345 346 |
# File 'lib/splash/helpers.rb', line 339 def format_response(data, format) response = case format when 'application/json' then JSON.pretty_generate(data) when 'text/x-yaml' then data.to_yaml else data.to_yaml end return response end |
#get_processes(options = {}) ⇒ String|Array
facility for retreiving PID from process query
28 29 30 31 32 33 34 35 36 37 38 39 40 41 |
# File 'lib/splash/helpers.rb', line 28 def get_processes( = {}) patterns = [] patterns = [:patterns] if [:patterns] patterns << [:pattern] if [:pattern] res = PS.get_all_processes patterns.each do |item| res = res.find_processes item end if [:full] then return res else return res.pick_attr('PID') end end |
#group_root ⇒ String
return the ‘root’ group name : root or wheel
18 19 20 |
# File 'lib/splash/helpers.rb', line 18 def group_root return Etc.getgrgid(0).name end |
#install_file(options = {}) ⇒ Object
facility for file installation
206 207 208 209 210 211 212 213 214 215 |
# File 'lib/splash/helpers.rb', line 206 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
facility to verifyingif the active process run as root
78 79 80 81 82 83 84 85 |
# File 'lib/splash/helpers.rb', line 78 def is_root? case (Process.uid) when 0 return true else return false end end |
#make_folder(options = {}) ⇒ Object
facility for folder creation
223 224 225 226 227 228 229 230 231 232 |
# File 'lib/splash/helpers.rb', line 223 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
facility for Symbolic link
238 239 240 241 242 243 244 245 246 |
# File 'lib/splash/helpers.rb', line 238 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.
wrapping execution if process run as root
90 91 92 93 94 95 96 |
# File 'lib/splash/helpers.rb', line 90 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
facility to find a file in gem path
49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 |
# File 'lib/splash/helpers.rb', line 49 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 ⇒ String
return the ‘root’ name
12 13 14 |
# File 'lib/splash/helpers.rb', line 12 def user_root return Etc.getpwuid(0).name end |
#verify_file(options = {}) ⇒ Array
check file
291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 |
# File 'lib/splash/helpers.rb', line 291 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
check folder
259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 |
# File 'lib/splash/helpers.rb', line 259 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 = {}) ⇒ Boolean
check symlink
280 281 282 |
# File 'lib/splash/helpers.rb', line 280 def verify_link( ={}) return File.file?([:name]) end |
#verify_service(options = {}) ⇒ Bool
TCP/IP service checker
314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 |
# File 'lib/splash/helpers.rb', line 314 def verify_service( ={}) begin if [:url] then uri = URI.parse([:url]) host = uri.host port = uri.port else host = [:host] port = [:port] end 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 |