Files
tryton/trytond/server.py
T

257 lines
8.8 KiB
Python
Raw Normal View History

2008-09-13 19:41:02 +02:00
#This file is part of Tryton. The COPYRIGHT file at the top level of
#this repository contains the full copyright notices and license terms.
2007-12-10 01:04:13 +01:00
"""
%prog [options]
"""
2008-09-12 19:57:33 +02:00
import logging
format='[%(asctime)s] %(levelname)s:%(name)s:%(message)s'
datefmt='%a %b %d %H:%M:%S %Y'
logging.basicConfig(level=logging.DEBUG, format=format, datefmt=datefmt)
2007-12-10 01:04:13 +01:00
import sys, os, signal
import netsvc
import time
2008-02-21 23:53:42 +01:00
import psycopg2
2007-12-10 01:04:13 +01:00
import pooler
import sql_db
import config
from config import CONFIG
import web_service
2007-12-10 21:17:22 +01:00
import wkf_service
2008-09-13 19:41:02 +02:00
from trytond.modules import register_classes
import mx.DateTime
2008-10-13 16:13:03 +02:00
if not hasattr(mx.DateTime, 'strptime'):
mx.DateTime.strptime = lambda x, y: mx.DateTime.mktime(
time.strptime(x, y))
2008-07-30 11:52:20 +02:00
from getpass import getpass
import sha
2008-09-12 19:57:33 +02:00
import logging
import logging.handlers
import threading
2007-12-10 01:04:13 +01:00
class TrytonServer(object):
def __init__(self):
2008-09-12 19:57:33 +02:00
if CONFIG['logfile']:
logf = CONFIG['logfile']
# test if the directories exist, else create them
try:
handler = logging.handlers.TimedRotatingFileHandler(
logf, 'D', 1, 30)
except Exception, exception:
sys.stderr.write("ERROR: couldn't create the logfile directory:" \
+ str(exception))
else:
formatter = logging.Formatter(format, datefmt)
# tell the handler to use this format
handler.setFormatter(formatter)
# add the handler to the root logger
logging.getLogger().addHandler(handler)
logging.getLogger().setLevel(logging.INFO)
elif os.name != 'nt':
reverse = '\x1b[7m'
reset = '\x1b[0m'
# reverse color for error and critical messages
for level in logging.ERROR, logging.CRITICAL:
msg = reverse + logging.getLevelName(level) + reset
logging.addLevelName(level, msg)
self.logger = logging.getLogger("init")
2007-12-10 01:04:13 +01:00
2008-09-12 19:57:33 +02:00
self.logger.info('using %s as configuration file' % CONFIG.configfile)
self.logger.info('initialising distributed objects services')
2007-12-10 01:04:13 +01:00
self.dispatcher = netsvc.Dispatcher()
self.dispatcher.monitor(signal.SIGINT)
web_service.DB()
web_service.Common()
web_service.Object()
web_service.Wizard()
web_service.Report()
wkf_service.WorkflowService()
def run(self):
"Run the server and never return"
2008-07-30 15:04:21 +02:00
update_module = False
init = {}
for db_name in CONFIG["db_name"]:
try:
cursor = pooler.get_db_only(db_name).cursor()
except psycopg2.OperationalError:
2008-10-29 15:49:54 +01:00
self.logger.error("could not connect to database '%s'!" % db_name,)
continue
init[db_name] = False
if CONFIG['init']:
2008-10-29 15:49:54 +01:00
if not cursor.test():
2008-09-12 19:57:33 +02:00
self.logger.info("init db")
sql_db.init_db(cursor)
init[db_name] = True
cursor.commit()
cursor.close()
2008-10-29 15:49:54 +01:00
pooler.close_db(db_name)
elif not cursor.test():
self.logger.error("'%s' is not a Tryton database!" % db_name)
register_classes()
for db_name in CONFIG["db_name"]:
try:
cursor = pooler.get_db_only(db_name).cursor()
except psycopg2.OperationalError:
2008-10-29 15:49:54 +01:00
self.logger.error("could not connect to database '%s'!" % db_name,)
continue
if not cursor.test():
cursor.close()
pooler.close_db(db_name)
continue
cursor.execute('SELECT code FROM ir_lang ' \
'WHERE translatable = True')
lang = [x[0] for x in cursor.fetchall()]
cursor.close()
2008-10-29 15:49:54 +01:00
pooler.close_db(db_name)
update_module = bool(CONFIG['init'] or CONFIG['update'])
pooler.get_db_and_pool(db_name, update_module=update_module,
lang=lang)
for kind in ('init', 'update'):
CONFIG[kind] = {}
for db_name in CONFIG['db_name']:
if init[db_name]:
while True:
password = getpass('Admin Password for %s:' % db_name)
password2 = getpass('Admin Password Confirmation:')
if password != password2:
sys.stderr.write('Admin Password Confirmation doesn\'t match ' \
'Admin Password!\n')
continue
if not password:
sys.stderr.write('Admin Password is required!\n')
continue
break
cursor = pooler.get_db_only(db_name).cursor()
cursor.execute('UPDATE res_user ' \
'SET password = %s ' \
'WHERE login = \'admin\'',
(sha.new(password).hexdigest(),))
cursor.commit()
2008-02-27 11:34:07 +01:00
cursor.close()
2008-10-29 15:49:54 +01:00
pooler.close_db(db_name)
2008-07-30 11:52:20 +02:00
if update_module:
2008-09-12 19:57:33 +02:00
self.logger.info('Update/Init succeed!')
2007-12-10 01:04:13 +01:00
sys.exit(0)
# Launch Server
if CONFIG['xmlrpc']:
interface = CONFIG["interface"]
try:
2008-01-19 19:53:47 +01:00
port = int(CONFIG["xmlport"])
2007-12-10 01:04:13 +01:00
except:
2008-09-12 19:57:33 +02:00
self.logger.error("invalid port '%s'!" % (CONFIG["xmlport"],))
2007-12-10 01:04:13 +01:00
sys.exit(1)
2008-09-20 12:12:36 +02:00
httpd = netsvc.HttpDaemon(interface, port, CONFIG['secure_xmlrpc'])
2007-12-10 01:04:13 +01:00
2008-08-23 15:32:26 +02:00
xml_gw = netsvc.XmlRpc.RpcGateway('web-service')
2007-12-10 01:04:13 +01:00
httpd.attach("/xmlrpc", xml_gw )
2008-09-12 19:57:33 +02:00
logging.getLogger("web-service").info(
2008-09-20 12:12:36 +02:00
"starting XML-RPC" + \
(CONFIG['secure_xmlrpc'] and ' Secure' or '') + \
" services, port " + str(port))
2007-12-10 01:04:13 +01:00
if CONFIG['netrpc']:
2008-01-19 19:53:47 +01:00
interface = CONFIG["interface"]
2007-12-10 01:04:13 +01:00
try:
2008-01-19 19:53:47 +01:00
port = int(CONFIG["netport"])
2008-02-14 01:43:39 +01:00
except:
2008-09-12 19:57:33 +02:00
self.logger.error("invalid port '%s'!" % (CONFIG["netport"],))
2007-12-10 01:04:13 +01:00
sys.exit(1)
2008-01-19 19:53:47 +01:00
tinysocket = netsvc.TinySocketServerThread(interface, port,
2008-09-20 12:12:36 +02:00
CONFIG['secure_netrpc'])
2008-09-12 19:57:33 +02:00
logging.getLogger("web-service").info(
2008-09-20 12:12:36 +02:00
"starting netrpc" + \
(CONFIG['secure_netrpc'] and ' Secure' or '') + \
" service, port " + str(port))
2007-12-10 01:04:13 +01:00
2008-02-14 01:43:39 +01:00
if CONFIG['webdav']:
interface = CONFIG['interface']
try:
port = int(CONFIG['webdavport'])
except:
2008-09-12 19:57:33 +02:00
self.logger.error("invalid port '%s'!" % (CONFIG['webdavport'],))
2008-02-14 01:43:39 +01:00
sys.exit(1)
2008-09-20 12:12:36 +02:00
webdavd = netsvc.WebDAVServerThread(interface, port,
CONFIG['secure_webdav'])
2008-09-12 19:57:33 +02:00
logging.getLogger("web-service").info(
2008-09-20 12:12:36 +02:00
'starting webdav' + \
(CONFIG['secure_webdav'] and ' Secure' or '') + \
' service, port ' + str(port))
2008-02-14 01:43:39 +01:00
2007-12-10 01:04:13 +01:00
def handler(signum, frame):
2008-09-20 20:46:02 +02:00
if signum == signal.SIGUSR1:
for db_name in pooler.get_db_list():
pooler.restart_pool(db_name)
return
2007-12-10 01:04:13 +01:00
if CONFIG['netrpc']:
tinysocket.stop()
if CONFIG['xmlrpc']:
httpd.stop()
2008-02-14 01:43:39 +01:00
if CONFIG['webdav']:
webdavd.stop()
2007-12-10 01:04:13 +01:00
if CONFIG['pidfile']:
os.unlink(CONFIG['pidfile'])
sys.exit(0)
if CONFIG['pidfile']:
fd_pid = open(CONFIG['pidfile'], 'w')
pidtext = "%d" % (os.getpid())
fd_pid.write(pidtext)
fd_pid.close()
signal.signal(signal.SIGINT, handler)
signal.signal(signal.SIGTERM, handler)
2008-09-20 20:46:02 +02:00
signal.signal(signal.SIGUSR1, handler)
2007-12-10 01:04:13 +01:00
2008-09-12 19:57:33 +02:00
self.logger.info('the server is running, waiting for connections...')
2007-12-10 01:04:13 +01:00
if CONFIG['netrpc']:
tinysocket.start()
if CONFIG['xmlrpc']:
httpd.start()
2008-02-14 01:43:39 +01:00
if CONFIG['webdav']:
webdavd.start()
2007-12-10 01:04:13 +01:00
#DISPATCHER.run()
2008-07-24 23:12:07 +02:00
2008-09-13 19:23:34 +02:00
if CONFIG['psyco']:
import psyco
psyco.full()
2008-07-24 23:12:07 +02:00
2008-10-01 15:48:53 +02:00
now = time.time() - 60
while True:
if time.time() - now >= 60:
2008-10-01 15:59:34 +02:00
now = time.time()
2008-06-06 10:51:10 +02:00
for dbname in pooler.get_db_list():
pool = pooler.get_pool(dbname)
cron_obj = pool.get('ir.cron')
thread = threading.Thread(
target=cron_obj.pool_jobs,
args=(dbname,), kwargs={})
thread.start()
2008-10-01 15:59:34 +02:00
if time.time() - now >= 60:
now = time.time()
2008-10-01 15:48:53 +02:00
time.sleep(1)
2007-12-10 01:04:13 +01:00
if __name__ == "__main__":
SERVER = TrytonServer()
SERVER.run()