class Freud::Launcher
Public Instance Methods
default_state()
click to toggle source
# File lib/freud/launcher.rb, line 15 def default_state { process: Process } end
run(command, args = [])
click to toggle source
# File lib/freud/launcher.rb, line 19 def run(command, args = []) @args = args case(command) when "help" then show_help when "start" then daemonize(fetch_executable(command, true)) else execute(fetch_executable(command)) end end
Private Instance Methods
apply_sudo(background) { || ... }
click to toggle source
# File lib/freud/launcher.rb, line 39 def apply_sudo(background) command = yield return(command) unless (sudo_user.to_s != "") bash = sprintf('bash -c "%s"', command.gsub(/"/, '\\"')) sudo_env = env.map { |key, value| sprintf('%s="%s"', key, value.gsub(/"/, '\\"')) }.join(" ") maybe_background = background ? "-b" : "" sudo_options = "-n #{maybe_background} -u #{sudo_user}" "sudo #{sudo_options} #{sudo_env} -- #{bash}" end
create_logfile()
click to toggle source
# File lib/freud/launcher.rb, line 89 def create_logfile return unless logfile begin file = File.open(logfile, "a") file.close rescue logger.fatal("Unable to open logfile: #{logfile}") end self end
daemonize(command)
click to toggle source
# File lib/freud/launcher.rb, line 63 def daemonize(command) return(self) if running? options = spawn_default_options options[:err] = [ logfile, "a" ] if logfile create_logfile if background options.merge!(pgroup: true) log_runtime_environment(command, options) pid = process.spawn(env, command, options) maybe_create_pidfile(pid) else $PROGRAM_NAME = command maybe_create_pidfile(process.pid) execute(command, options) end end
execute(command, options = nil)
click to toggle source
# File lib/freud/launcher.rb, line 56 def execute(command, options = nil) log_runtime_environment(command) $PROGRAM_NAME = command process.exec(env, command, options || spawn_default_options) self end
fetch_executable(command, background = false)
click to toggle source
# File lib/freud/launcher.rb, line 30 def fetch_executable(command, background = false) apply_sudo(background) do commands.fetch(command) do show_help(false) logger.fatal("Unknown command: #{command}") end end end
log_runtime_environment(command, options = nil)
click to toggle source
# File lib/freud/launcher.rb, line 80 def log_runtime_environment(command, options = nil) options ||= spawn_default_options logger.debug("running #{command}") logger.debug("env #{ENV.inspect}") logger.debug("env #{env.inspect}") logger.debug("spawn_default_options #{options.inspect}") self end
maybe_create_pidfile(pid)
click to toggle source
# File lib/freud/launcher.rb, line 110 def maybe_create_pidfile(pid) return(self) unless (create_pidfile == true) pidfile.write(pid) self end
running?()
click to toggle source
FIXME Kill stale pidfile?
# File lib/freud/launcher.rb, line 117 def running? pidfile.running? end
show_help(terminate = true)
click to toggle source
# File lib/freud/launcher.rb, line 50 def show_help(terminate = true) logger.info("Valid commands: #{commands.keys.join(", ")}") exit(0) if terminate self end
spawn_default_options()
click to toggle source
# File lib/freud/launcher.rb, line 100 def spawn_default_options output = {} output[:unsetenv_others] = (reset_env == true) output[:chdir] = root output[:close_others] = true output[:in] = "/dev/null" output[:out] = :err output end