#!/usr/bin/ruby require 'pathname' # # Tapicero Daemon # BASE_DIR = Pathname.new(__FILE__).realpath + '../..' begin # # try without rubygems (might be already loaded or not present) # require 'tapicero/version' rescue LoadError # # try with rubygems # require "#{BASE_DIR}/lib/tapicero/version.rb" Tapicero::REQUIRE_PATHS.each do |path| path = BASE_DIR + path $LOAD_PATH.unshift path unless $LOAD_PATH.include?(path) end require 'rubygems' require 'tapicero/version' end # Graceful Ctrl-C Signal.trap("SIGINT") do Tapicero.logger.warn "Received SIGINT - stopping tapicero" puts "\nQuit - leaving tapicero" exit end # this changes later, so save the initial current directory CWD = Dir.pwd # handle --version ourselves if ARGV.grep(/--version/).any? puts "tapicero #{Tapicero::VERSION}, ruby #{RUBY_VERSION}" exit(0) end # --run-once create databases for new users and then exit # --rerun also act upon users that have already been processed # --overwrite-security overwrite existing couch security settings # TODO: not implemented yet: # --overwrite-designs overwrite existing design documents Tapicero::FLAGS.concat ARGV.grep(/--.*/) # if flags have been set but an action is missing we assume # tapicero should run in foreground. if ARGV.first.start_with?('--') ARGV.unshift '--' ARGV.unshift 'run' end # # Start the daemon # require 'tapicero' # so we can use Tapicero.logger below. require 'daemons' if ENV["USER"] == "root" options = {:app_name => 'tapicero', :dir_mode => :system} # this will put the pid file in /var/run else options = {:app_name => 'tapicero', :dir_mode => :normal, :dir => '/tmp'} # this will put the pid file in /tmp end begin Daemons.run("#{BASE_DIR}/lib/tapicero_daemon.rb", options) rescue SystemExit rescue Exception => exc Tapicero.logger.error "Uncaught exception. Daemon will die." Tapicero.logger.error exc.class.name + ": " + exc.to_s Tapicero.logger.error exc.backtrace.join("\n") end