#!/usr/bin/env ruby # # Little helper script for starting up a complete dsadmin system in "development" mode: # all admind instances (using config/admind.xml) and the web frontend (using config/webfe.xml). # # All processes are started on the local machine. Make sure the +admind.xml+ reflects that! # # After starting everything, the script waits until it is killed (SIGTERM / SIGINT), and then # shuts down all started processes. # # Author:: Christian Reiniger # License:: GNU GPL v2 or later, as described in the accompanying LICENSE file # Homepage: http://dsadmin.dotsrc.org/ # # ---- # Last change: # # CVS User:: $Author: creinig $ # Date:: $Date: 2006-10-24 10:29:33 +0200 (Tue, 24 Oct 2006) $ # Revision:: $Revision: 110 $ # require 'rbconfig.rb' require 'pp' require 'pathname' unless defined?(RAILS_ROOT) root_path = File.join(File.dirname(__FILE__), '..') unless RUBY_PLATFORM =~ /mswin32/ require 'pathname' root_path = Pathname.new(root_path).cleanpath(true).to_s end RAILS_ROOT = root_path end INSTALL_PATH = Pathname.new(RAILS_ROOT).realpath.to_s $: << File.join(INSTALL_PATH, 'lib') require 'dsadmin/base_config_manager' require 'dsadmin/system' def start_instance(instanceid) rubycmd = File.join(::Config::CONFIG['bindir'], "ruby") argv = ['--instanceid', instanceid, '--environment', 'development', '--cfgfile', 'config/admind.xml'] args = ["#{INSTALL_PATH}/script/admind.rb"] + argv puts "Starting admind as '#{rubycmd} '#{args.join(' ')}'" pid = fork do Dir.chdir(INSTALL_PATH) exec(rubycmd, *args) end puts " Started as PID #{pid}" return pid end def start_webfe rubycmd = File.join(::Config::CONFIG['bindir'], "ruby") argv = ['--environment', 'development', '--cfgfile', 'config/webfe.xml'] args = ["#{INSTALL_PATH}/script/webfe.rb"] + argv puts "Starting webfe as '#{rubycmd} '#{args.join(' ')}'" pid = fork do Dir.chdir(INSTALL_PATH) Dsadmin::System.instance.runAs(ENV['SUDO_UID'].to_i, ENV['SUDO_GID'].to_i) { exec(rubycmd, *args) } end puts " Started webfe as PID #{pid}" return pid end def shutdown for pid in $pids do begin Process.kill("TERM", pid) puts "Process ##{pid} got TERM signal" # Process.waitpid(pid) # puts "Process ##{pid} exited" rescue Errno::ESRCH => e puts "Process ##{pid} was already dead" end end end unless((Process.uid == 0) && ENV['SUDO_UID'] && ENV['SUDO_GID']) puts "This has to be run as root, and only via sudo." puts "We need the info on the original user (SUDO_UID & SUDO_GID) !!" exit(-1) end # * Check for admind.xml and webfe.xml unless(File.readable?(File.join(INSTALL_PATH, 'config', 'admind.xml'))) puts "admind.xml does not exist or is not readable. Please fix." exit(-1) end unless(File.readable?(File.join(INSTALL_PATH, 'config', 'webfe.xml'))) puts "webfe.xml does not exist or is not readable. Please fix." exit(-1) end # * read admind.xml cfg = Dsadmin::BaseConfigManager.new('config/admind.xml') # * setup signal handler for killing the instances and exiting $pids = [] $threads = [] Kernel.trap("TERM") { shutdown } Kernel.trap("INT") { shutdown } # * start all instances listed in admind.xml, remembering the PIDs csects = cfg.get('admind', Hash) for instanceid in csects.keys do $pids << start_instance(instanceid) end # * drop privileges to SUDO_UID / SUDO_GID # * start webfe, remembering the PID $pids << start_webfe # * wait forever puts "Waiting. Shut down the system via SIGTERM or SIGINT" #for thread in $threads do # thread.join #end while(not $pids.empty?) pid = Process.wait puts "Process ##{pid} exited" $pids.delete(pid) end