# -*- encoding: utf-8 -*- require 'optparse' require 'evented_bluepill/version' module EventedBluepill module Options extend self APPLICATION_COMMANDS = %w(status start stop restart unmonitor quit log) # Default options @options = { :log_file => "/var/log/evented_bluepill.log", :base_dir => "/var/evented_bluepill", :privileged => true } def parse! parse_options! parse_application! # Check for root unless privileged? $stderr.puts "You must run evented_bluepill as root or use --no-privileged option." exit(3) end end def [](key) @options[key] end def running_applications Dir[File.join(EventedBluepill::Options.sockets_dir, "*.sock")].map {|x| File.basename(x, ".sock")} end def sockets_dir File.join(@options[:base_dir], 'socks') end def pids_dir File.join(@options[:base_dir], 'pids') end def privileged? !@options[:privileged] || ::Process.euid == 0 end private def parse_options! OptionParser.new do |opts| opts.banner = "Usage: evented_bluepill [app] cmd [options]" opts.on('-l', "--logfile LOGFILE", "Path to logfile, defaults to #{@options[:log_file]}") do |file| @options[:log_file] = file end opts.on('-c', "--base-dir DIR", "Directory to store evented_bluepill socket and pid files, defaults to #{@options[:base_dir]}") do |base_dir| @options[:base_dir] = base_dir end opts.on("-v", "--version") do puts "evented_bluepill, version #{EventedBluepill::VERSION::STRING}" exit end opts.on("--[no-]privileged", "Allow/disallow to run #{$0} as non-privileged process. disallowed by default") do |v| @options[:privileged] = v end help = proc do puts opts puts puts "Commands:" puts " load CONFIG_FILE\t\tLoads new instance of evented_bluepill using the specified config file" puts " status\t\t\tLists the status of the proceses for the specified app" puts " start [TARGET]\t\tIssues the start command for the target process or group, defaults to all processes" puts " stop [TARGET]\t\tIssues the stop command for the target process or group, defaults to all processes" puts " restart [TARGET]\t\tIssues the restart command for the target process or group, defaults to all processes" puts " unmonitor [TARGET]\t\tStop monitoring target process or group, defaults to all processes" puts " log [TARGET]\t\tShow the log for the specified process or group, defaults to all for app" puts " quit\t\t\tStop evented_bluepill" puts puts "See http://github.com/msnexploder/evented_bluepill for README" exit end opts.on_tail('-h','--help', 'Show this message', &help) help.call if ARGV.empty? end.parse! end def parse_application! if running_applications.include?(File.basename($0)) && File.symlink?($0) # evented_bluepill was called as a symlink with the name of the target application options[:application] = File.basename($0) elsif running_applications.include?(ARGV.first) # the first arg is the application name options[:application] = ARGV.shift elsif APPLICATION_COMMANDS.include?(ARGV.first) if running_applications.length == 1 # there is only one, let's just use that options[:application] = running_applications.first elsif running_applications.length > 1 # There is more than one, tell them the list and exit $stderr.puts "You must specify an application name to run that command. Here's the list of running applications:" running_applications.each_with_index do |app, index| $stderr.puts " #{index + 1}. #{app}" end $stderr.puts "Usage: evented_bluepill [app] cmd [options]" exit(1) else # There are none running AND they aren't trying to start one $stderr.puts "Error: There are no running evented_bluepill daemons.\nTo start a evented_bluepill daemon, use: evented_bluepill load " exit(2) end end end end end