#!/usr/bin/env ruby $LOAD_PATH.unshift File.join(File.dirname(__FILE__), '..', 'lib') require 'optparse' require 'bluepill' # defaults options = { :log_file => "/var/log/bluepill.log", :base_dir => "/var/bluepill" } OptionParser.new do |opts| opts.banner = "Usage: bluepill [app] cmd [options]" opts.on("--logfile LOGFILE") do |file| options[:log_file] = file end opts.on("--base-dir DIR") do |base_dir| options[:base_dir] = base_dir end end.parse! APPLICATION_COMMANDS = %w(status start stop restart unmonitor quit log) controller = Bluepill::Controller.new(options.slice(:base_dir)) if controller.running_applications.include?(ARGV.first) # the first arg is the application name options[:application] = ARGV.shift elsif APPLICATION_COMMANDS.include?(ARGV.first) if controller.running_applications.length == 1 options[:application] = controller.running_applications.first elsif controller.running_applications.length > 1 $stderr.puts "You must specify an application name. Here's the list of running applications:" controller.running_applications.each_with_index do |app, index| $stderr.puts " #{index + 1}. #{app}" end $stderr.puts "Usage: bluepill [app] cmd [options]" exit(1) else $stderr.puts "Error: There are no running bluepill daemons.\nTo start a bluepill daemon, use: bluepill load " exit(2) end end options[:command] = ARGV.shift case options[:command] when "load" file = ARGV.shift if File.exists?(file) eval(File.read(file)) else $stderr.puts "Can't find file: #{file}" end when "log" orig_pattern = pattern = ARGV.shift pattern = controller.send_cmd(options[:application], :grep_pattern, pattern) cmd = "tail -n 100 -f #{options[:log_file]} | grep -E '#{pattern}'" puts "Tailing log for #{orig_pattern}..." Kernel.exec(cmd) when *APPLICATION_COMMANDS process_or_group_name = ARGV.shift puts controller.send_cmd(options[:application], options[:command], process_or_group_name) else puts "Unknown command `%s` (or application `%s` has not been loaded yet)" % [options[:command], options[:command]] end