#!/usr/bin/env ruby # # Jerbil # # Author:: Robert Sharp # Copyright:: Copyright (c) 2010 Robert Sharp # License:: Open Software Licence v3.0 # # This software is licensed for use under the Open Software Licence v. 3.0 # The terms of this licence can be found at http://www.opensource.org/licenses/osl-3.0.php # and in the file copyright.txt. Under the terms of this licence, all derivative works # must themselves be licensed under the Open Software Licence v. 3.0 # # # stop the jerbil server on this host # require 'rubygems' require 'jerbil' require 'jerbil/servers' require 'jerbil/version' require 'jerbil/config' require 'jerbil/support' require 'drb' require 'optparse' verbose = false env = :null conf_file = nil OptionParser.new do |opts| opts.banner = "Usage: jerbild [opts]" opts.separator "" opts.separator " Stop the Jerbil Server" opts.separator "" opts.on("-n", "--no-daemon", "ignored") do daemonize = false end opts.on("-S", "--no-syslog", "ignored") do log_to_syslog = false end opts.on("-c", "--config [file]", String, "use this config file to find Jerbil" ) do |cfile| if FileTest.readable?(cfile) then conf_file = cfile else puts "Config file cannot be read." exit 1 end end opts.on("-V", "--verbose", "output more information about what is going on ") do verbose = true end opts.on("-e", "--environment [TYPE]", [:prod, :test, :dev], "Select env (prod, test, dev), default is prod") do |e| env = e end opts.on("-h", "--help", "Provide Help") do |h| opts.separator "" puts opts exit 0 end end.parse! puts "Stopping the local Jerbil Server" if verbose puts "Version: jerbil-" + Jerbil::Version + " [#{Jerbil::Version_Date}]" if verbose options = Jerbil.get_config(conf_file) #servers = options[:servers] if env != :null then server_env = env else server_env = options[:environment] || :prod end puts "Running with environment: #{server_env}" if verbose local = Jerbil::Servers.get_local_server(server_env) if local.nil? then puts "Cannot find record for a local Jerbil Server" if verbose exit 1 end puts "Found local Jerbil Server record" if verbose pkey = Jerbil::Support.get_key_and_delete_file(:jerbil, server_env, options[:key_dir]) puts "Obtained a private key: #{pkey} from #{options[:key_dir]}" if verbose pid = Jerbil::Support.get_pid_and_delete_file(:jerbil, server_env, options[:pid_dir]) puts "Obtained a pid: #{pid}" if verbose puts "Stopping Jerbil Test Server" if verbose begin jerbild = local.connect # see if you can verify? jerbild.verify rescue DRb::DRbConnError, Jerbil::ServerConnectError #fall back to pid killing if pid > 0 then Process.kill("SIGKILL", pid.to_i) puts "Manually killed the process" if verbose else puts "No pid, assume it is already dead" if verbose end exit 0 end # did, so kill it properly begin jerbild.stop(pkey) rescue Jerbil::InvalidPrivateKey # process must exist but something went wrong with the key puts "Invalid or missing private key: #{pkey}" if verbose if pid > 0 then puts "Manually killing the process by pid: #{pid}" if verbose Process.kill("SIGKILL", pid.to_i) else # no key, no pid, but the process still exists. puts "Jerbil Server appears to be running but there is no pid file or key file!" if verbose exit 1 end rescue DRb::DRbConnError # always raised when remote end exits, so just ignore it end puts "Stopped Jerbil Server" if verbose # puts "Stopping Monitor process" if verbose # # mpid = Jerbil::Support.get_pid_and_delete_file(:jmonitor, server_env, options[:pid_dir]) # if mpid then # puts "Obtained a pid for monitor: #{mpid}" if verbose # Process.kill "SIGKILL", mpid.to_i # else # puts "No mpid was found!" if verbose # end exit 0