#!/usr/bin/env ruby # # backlog Startup script for the backlog application # # chkconfig: 345 88 12 # description: Backlog $VERBOSE=true require 'rubygems' require 'fileutils' require 'ftools' require 'yaml' require 'rbconfig' APPLICATION = 'backlog' STATUS_STOPPED = 0 STATUS_RUNNING = 1 STATUS_STALE = 2 STATUS_NOT_ROOT = 3 if gem = Gem::searcher.find(APPLICATION) INSTALL_DIR = "#{Gem.dir}/gems/#{APPLICATION}-#{gem.version}" else INSTALL_DIR = "/usr/local/backlog/current" end config_file = Config::CONFIG["sysconfdir"] + '/backlog.conf' config = {} config = File.open(config_file) {|f| YAML::load(f)} if File.exists? config_file LOG_DIR = "#{INSTALL_DIR}/log" PID_FILE = '/var/run/backlog.pid' def start(config) File.makedirs LOG_DIR unless File.exists? LOG_DIR File.makedirs File.dirname(PID_FILE) unless File.exists? File.dirname(PID_FILE) status = get_status case status when STATUS_RUNNING: puts "Process already running. Stopping." exit 1 when STATUS_STALE: puts "Deleting stale PID file." File.delete(PID_FILE) when STATUS_STOPPED: when STATUS_NOT_ROOT: puts "Permission denied. Please run as root/administrator." exit 6 else puts "Unknown status: #{status}. Stopping" exit 2 end `mongrel_rails start -p #{config['port'] || 3000} -e production -c #{INSTALL_DIR} -d -m #{INSTALL_DIR}/config/mime_types.yaml -P #{PID_FILE} 1>#{LOG_DIR}/stdout.log 2>#{LOG_DIR}/stderr.log` end def stop(config) `mongrel_rails stop -c #{INSTALL_DIR} -P #{PID_FILE}` end def get_pid if File.exists? PID_FILE return File.readlines(PID_FILE)[0].to_i else return nil end end def get_status pid = get_pid if pid begin Process.kill(0, pid) return STATUS_RUNNING rescue Errno::ESRCH return STATUS_STALE rescue Errno::EPERM return STATUS_NOT_ROOT end else return STATUS_STOPPED end end # See how we were called. case ARGV[0] when 'start': start(config) when 'stop': stop(config) when 'restart': stop(config) start(config) when 'reload': puts `mongrel_rails restart -c #{INSTALL_DIR} -P #{PID_FILE}` when 'status' case get_status when STATUS_RUNNING: puts "#{APPLICATION} is running as pid #{get_pid}." exit 0 when STATUS_STOPPED: puts "#{APPLICATION} is stopped." exit 5 when STATUS_STALE: puts "#{APPLICATION} is stopped but PID file exists with pid #{get_pid}." exit 4 when STATUS_NOT_ROOT: puts "Permission denied. Please run as root/administrator." exit 6 else puts "Unknown status #{get_status}." exit 3 end when 'setup_unix' current_user = `whoami` users = `su - postgres -c "echo '\\du' | psql template1"` unless users =~ /root/ puts users createuser_command = `which createuser`.chomp puts `su - postgres -c "#{createuser_command} -dAR #{current_user}"` end dbs = `su - postgres -c "echo '\\l' | psql template1"` unless dbs =~ /#{APPLICATION}_production/ puts dbs createdb_command = `which createdb`.chomp puts `su - postgres -c "#{createdb_command} #{APPLICATION}_production"` end Dir.chdir INSTALL_DIR Dir.mkdir LOG_DIR unless File.exists? LOG_DIR puts `rake db:migrate RAILS_ENV=production` # TODO: provide startup information based on launchd in OS X versions >= 10.4 if File.directory? '/etc/init.d' startup_app = "/etc/init.d/#{APPLICATION}" File.delete startup_app if File.exists? startup_app `cp -p #{INSTALL_DIR}/bin/backlog_init.d #{startup_app}` unless File.exists? startup_app `chmod a+x #{startup_app}` if File.exists? startup_app end FileUtils.cp "#{INSTALL_DIR}/etc/#{APPLICATION}.conf", config_file unless File.exists? config_file `su - -c "chkconfig --add #{APPLICATION}"` `su - -c "chkconfig #{APPLICATION} on"` puts else puts "Usage: #$0 {start|stop|restart|status|setup_unix}" exit 1 end exit 0