#!/www/python/bin/python

"""$URL: svn+ssh://svn.mems-exchange.org/repos/trunk/dulcinea/bin/start-durus.py $
$Id: start-durus.py 24725 2004-07-19 20:46:33Z dbinger $

Script invoked by the site command to start and stop Durus storage server.
"""

import sys, os
from dulcinea import site_util
from durus import run_durus, storage_server
site_util.ensure_uid_gid_not_root()
config = site_util.get_config()
site = sys.argv[1]
action = sys.argv[2]
pid_file_name = site_util.get_pid_file_name ('durus', site)
ip, port = site_util.parse_address(config.get(site, 'durus-address'))
assert site_util.is_local(config.get(site, 'durus-address'))
db = os.path.join(config.get(site, 'var-directory'), '%s.durus' % site)

site_log_dir = os.path.join(config.get(site, 'log-directory'), site)
durus_log = os.path.join(site_log_dir, 'durus.log')

durus = config.defaults().get('durus', run_durus.__file__)
logginglevel = config.get(site, 'logginglevel', 20)

if durus.endswith('c'):
    durus = durus[:-1]
import sys
args =  [sys.executable,
         durus,
         '--host', ip,
         '--port', str(port),
         '--file', db,
         '--logginglevel', str(logginglevel),
         '--logfile', durus_log]
if action == 'stop':
    args.append('--stop')
elif action != 'start':
    raise SystemExit, 'action must be "start" or "stop"'

pid = os.fork()
if pid == 0:
    os.execve(args[0], args, {})
else:
    if action == 'start':
        pid_file = open(pid_file_name, 'w')
        pid_file.write("%s" % pid)
        pid_file.close()
        storage_server.wait_for_server(ip, port)
