2009-12-02 10:06:58 +00:00
|
|
|
#!/usr/bin/env python
|
2009-10-20 10:52:23 +00:00
|
|
|
# -*- coding: utf-8 -*-
|
2006-12-07 13:41:40 +00:00
|
|
|
##############################################################################
|
2010-09-22 11:32:56 +00:00
|
|
|
#
|
2009-10-14 12:32:15 +00:00
|
|
|
# OpenERP, Open Source Management Solution
|
|
|
|
# Copyright (C) 2004-2009 Tiny SPRL (<http://tiny.be>).
|
2006-12-07 13:41:40 +00:00
|
|
|
#
|
2008-11-03 18:27:16 +00:00
|
|
|
# This program is free software: you can redistribute it and/or modify
|
2009-10-14 12:32:15 +00:00
|
|
|
# it under the terms of the GNU Affero General Public License as
|
|
|
|
# published by the Free Software Foundation, either version 3 of the
|
|
|
|
# License, or (at your option) any later version.
|
2006-12-07 13:41:40 +00:00
|
|
|
#
|
2008-11-03 18:27:16 +00:00
|
|
|
# This program is distributed in the hope that it will be useful,
|
|
|
|
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
2009-10-14 12:32:15 +00:00
|
|
|
# GNU Affero General Public License for more details.
|
2006-12-07 13:41:40 +00:00
|
|
|
#
|
2009-10-14 12:32:15 +00:00
|
|
|
# You should have received a copy of the GNU Affero General Public License
|
2010-09-22 11:32:56 +00:00
|
|
|
# along with this program. If not, see <http://www.gnu.org/licenses/>.
|
2006-12-07 13:41:40 +00:00
|
|
|
#
|
2008-11-03 18:27:16 +00:00
|
|
|
##############################################################################
|
2006-12-07 13:41:40 +00:00
|
|
|
|
|
|
|
"""
|
2008-09-10 08:46:40 +00:00
|
|
|
OpenERP - Server
|
|
|
|
OpenERP is an ERP+CRM program for small and medium businesses.
|
2006-12-07 13:41:40 +00:00
|
|
|
|
|
|
|
The whole source code is distributed under the terms of the
|
|
|
|
GNU Public Licence.
|
|
|
|
|
2010-09-22 11:32:56 +00:00
|
|
|
(c) 2003-TODAY, Fabien Pinckaers - OpenERP s.a.
|
2006-12-07 13:41:40 +00:00
|
|
|
"""
|
|
|
|
|
|
|
|
#----------------------------------------------------------
|
|
|
|
# python imports
|
|
|
|
#----------------------------------------------------------
|
2010-09-22 11:32:56 +00:00
|
|
|
import logging
|
2008-12-29 08:56:31 +00:00
|
|
|
import os
|
2008-12-30 13:29:11 +00:00
|
|
|
import pwd
|
2010-09-22 11:32:56 +00:00
|
|
|
import signal
|
|
|
|
import sys
|
|
|
|
import threading
|
|
|
|
import traceback
|
2008-05-15 05:23:30 +00:00
|
|
|
|
2008-09-12 22:31:12 +00:00
|
|
|
import release
|
|
|
|
__author__ = release.author
|
|
|
|
__version__ = release.version
|
|
|
|
|
2008-12-23 08:18:33 +00:00
|
|
|
# We DON't log this using the standard logger, because we might mess
|
|
|
|
# with the logfile's permissions. Just do a quick exit here.
|
|
|
|
if pwd.getpwuid(os.getuid())[0] == 'root' :
|
2009-11-24 14:44:05 +00:00
|
|
|
sys.stderr.write("Attempted to run OpenERP server as root. This is not good, aborting.\n")
|
|
|
|
sys.exit(1)
|
2008-12-23 08:18:33 +00:00
|
|
|
|
2006-12-07 13:41:40 +00:00
|
|
|
#----------------------------------------------------------
|
|
|
|
# get logger
|
|
|
|
#----------------------------------------------------------
|
|
|
|
import netsvc
|
2010-09-22 11:32:56 +00:00
|
|
|
logger = logging.getLogger('server')
|
2006-12-07 13:41:40 +00:00
|
|
|
|
|
|
|
#-----------------------------------------------------------------------
|
|
|
|
# import the tools module so that the commandline parameters are parsed
|
|
|
|
#-----------------------------------------------------------------------
|
|
|
|
import tools
|
2010-09-22 11:32:56 +00:00
|
|
|
logger.info("OpenERP version - %s", release.version)
|
2008-12-11 21:20:56 +00:00
|
|
|
for name, value in [('addons_path', tools.config['addons_path']),
|
2008-12-11 20:57:07 +00:00
|
|
|
('database hostname', tools.config['db_host'] or 'localhost'),
|
|
|
|
('database port', tools.config['db_port'] or '5432'),
|
2008-12-11 21:12:18 +00:00
|
|
|
('database user', tools.config['db_user'])]:
|
2010-09-22 11:32:56 +00:00
|
|
|
logger.info("%s - %s", name, value)
|
2008-12-11 21:20:56 +00:00
|
|
|
|
2010-03-11 09:22:28 +00:00
|
|
|
# Don't allow if the connection to PostgreSQL done by postgres user
|
|
|
|
if tools.config['db_user'] == 'postgres':
|
2010-09-22 11:32:56 +00:00
|
|
|
logger.error("Connecting to the database as 'postgres' user is forbidden, as it present major security issues. Shutting down.")
|
2010-03-11 09:22:28 +00:00
|
|
|
sys.exit(1)
|
2006-12-07 13:41:40 +00:00
|
|
|
|
2007-02-03 22:58:57 +00:00
|
|
|
import time
|
2006-12-07 13:41:40 +00:00
|
|
|
|
|
|
|
#----------------------------------------------------------
|
|
|
|
# init net service
|
|
|
|
#----------------------------------------------------------
|
2010-09-22 11:32:56 +00:00
|
|
|
logger.info('initialising distributed objects services')
|
2006-12-07 13:41:40 +00:00
|
|
|
|
|
|
|
#---------------------------------------------------------------
|
|
|
|
# connect to the database and initialize it with base if needed
|
|
|
|
#---------------------------------------------------------------
|
|
|
|
import pooler
|
|
|
|
|
|
|
|
#----------------------------------------------------------
|
2008-08-12 14:44:56 +00:00
|
|
|
# import basic modules
|
2006-12-07 13:41:40 +00:00
|
|
|
#----------------------------------------------------------
|
2008-12-11 21:20:56 +00:00
|
|
|
import osv
|
|
|
|
import workflow
|
|
|
|
import report
|
|
|
|
import service
|
2006-12-07 13:41:40 +00:00
|
|
|
|
|
|
|
#----------------------------------------------------------
|
|
|
|
# import addons
|
|
|
|
#----------------------------------------------------------
|
2008-12-14 16:46:47 +00:00
|
|
|
|
2006-12-07 13:41:40 +00:00
|
|
|
import addons
|
|
|
|
|
2008-12-14 16:46:47 +00:00
|
|
|
#----------------------------------------------------------
|
|
|
|
# Load and update databases if requested
|
|
|
|
#----------------------------------------------------------
|
|
|
|
|
2009-09-03 21:06:47 +00:00
|
|
|
import service.http_server
|
|
|
|
|
2009-09-05 08:11:57 +00:00
|
|
|
if not ( tools.config["stop_after_init"] or \
|
2009-11-24 14:44:05 +00:00
|
|
|
tools.config["translate_in"] or \
|
|
|
|
tools.config["translate_out"] ):
|
|
|
|
service.http_server.init_servers()
|
|
|
|
service.http_server.init_xmlrpc()
|
2010-07-26 09:33:34 +00:00
|
|
|
service.http_server.init_static_http()
|
2009-09-05 08:11:57 +00:00
|
|
|
|
2009-11-24 14:44:05 +00:00
|
|
|
import service.netrpc_server
|
|
|
|
service.netrpc_server.init_servers()
|
2009-09-03 21:06:47 +00:00
|
|
|
|
2008-12-14 16:46:47 +00:00
|
|
|
if tools.config['db_name']:
|
2010-07-04 13:49:37 +00:00
|
|
|
for dbname in tools.config['db_name'].split(','):
|
|
|
|
db,pool = pooler.get_db_and_pool(dbname, update_module=tools.config['init'] or tools.config['update'], pooljobs=False)
|
2010-08-10 13:17:55 +00:00
|
|
|
if tools.config["test_file"]:
|
2010-09-22 11:32:56 +00:00
|
|
|
logger.info('loading test file %s', tools.config["test_file"])
|
2010-07-04 13:49:37 +00:00
|
|
|
cr = db.cursor()
|
2010-08-10 13:17:55 +00:00
|
|
|
tools.convert_yaml_import(cr, 'base', file(tools.config["test_file"]), {}, 'test', True)
|
2010-07-04 13:49:37 +00:00
|
|
|
cr.rollback()
|
|
|
|
pool.get('ir.cron')._poolJobs(db.dbname)
|
2006-12-07 13:41:40 +00:00
|
|
|
|
|
|
|
#----------------------------------------------------------
|
|
|
|
# translation stuff
|
|
|
|
#----------------------------------------------------------
|
|
|
|
if tools.config["translate_out"]:
|
2008-07-22 14:24:36 +00:00
|
|
|
import csv
|
2006-12-07 13:41:40 +00:00
|
|
|
|
2008-08-19 13:10:16 +00:00
|
|
|
if tools.config["language"]:
|
|
|
|
msg = "language %s" % (tools.config["language"],)
|
|
|
|
else:
|
|
|
|
msg = "new language"
|
2010-09-22 11:32:56 +00:00
|
|
|
logger.info('writing translation file for %s to %s', msg, tools.config["translate_out"])
|
2008-07-22 07:38:43 +00:00
|
|
|
|
2008-07-22 14:24:36 +00:00
|
|
|
fileformat = os.path.splitext(tools.config["translate_out"])[-1][1:].lower()
|
|
|
|
buf = file(tools.config["translate_out"], "w")
|
|
|
|
tools.trans_export(tools.config["language"], tools.config["translate_modules"], buf, fileformat)
|
|
|
|
buf.close()
|
2008-08-12 14:44:56 +00:00
|
|
|
|
2010-09-22 11:32:56 +00:00
|
|
|
logger.info('translation file written successfully')
|
2008-07-22 14:24:36 +00:00
|
|
|
sys.exit(0)
|
2006-12-07 13:41:40 +00:00
|
|
|
|
|
|
|
if tools.config["translate_in"]:
|
2008-12-23 23:38:42 +00:00
|
|
|
tools.trans_load(tools.config["db_name"],
|
|
|
|
tools.config["translate_in"],
|
|
|
|
tools.config["language"])
|
2008-07-22 14:24:36 +00:00
|
|
|
sys.exit(0)
|
2006-12-07 13:41:40 +00:00
|
|
|
|
|
|
|
#----------------------------------------------------------------------------------
|
|
|
|
# if we don't want the server to continue to run after initialization, we quit here
|
|
|
|
#----------------------------------------------------------------------------------
|
|
|
|
if tools.config["stop_after_init"]:
|
|
|
|
sys.exit(0)
|
|
|
|
|
2007-01-25 12:30:41 +00:00
|
|
|
|
2006-12-07 13:41:40 +00:00
|
|
|
#----------------------------------------------------------
|
2009-08-29 15:23:46 +00:00
|
|
|
# Launch Servers
|
2006-12-07 13:41:40 +00:00
|
|
|
#----------------------------------------------------------
|
|
|
|
|
2009-01-03 01:37:18 +00:00
|
|
|
LST_SIGNALS = ['SIGINT', 'SIGTERM']
|
|
|
|
|
2008-12-23 23:38:42 +00:00
|
|
|
SIGNALS = dict(
|
2009-01-03 01:37:18 +00:00
|
|
|
[(getattr(signal, sign), sign) for sign in LST_SIGNALS]
|
2008-12-23 23:38:42 +00:00
|
|
|
)
|
|
|
|
|
2010-09-22 11:32:56 +00:00
|
|
|
netsvc.quit_signals_received = 0
|
|
|
|
|
|
|
|
def handler(signum, frame):
|
2008-12-23 23:38:42 +00:00
|
|
|
"""
|
|
|
|
:param signum: the signal number
|
2010-09-22 11:32:56 +00:00
|
|
|
:param frame: the interrupted stack frame or None
|
2008-12-23 23:38:42 +00:00
|
|
|
"""
|
2010-09-22 11:32:56 +00:00
|
|
|
netsvc.quit_signals_received += 1
|
|
|
|
if netsvc.quit_signals_received > 1:
|
|
|
|
sys.stderr.write("Forced shutdown.\n")
|
|
|
|
os._exit(0)
|
2007-01-25 12:30:41 +00:00
|
|
|
|
2010-09-22 11:32:56 +00:00
|
|
|
def dumpstacks(signum, frame):
|
2010-09-20 16:54:56 +00:00
|
|
|
# code from http://stackoverflow.com/questions/132058/getting-stack-trace-from-a-running-python-application#answer-2569696
|
2010-09-24 12:56:15 +00:00
|
|
|
# modified for python 2.5 compatibility
|
|
|
|
thread_map = dict(threading._active, **threading._limbo)
|
|
|
|
id2name = dict([(threadId, thread.getName()) for threadId, thread in thread_map.items()])
|
2010-09-20 16:54:56 +00:00
|
|
|
code = []
|
|
|
|
for threadId, stack in sys._current_frames().items():
|
|
|
|
code.append("\n# Thread: %s(%d)" % (id2name[threadId], threadId))
|
|
|
|
for filename, lineno, name, line in traceback.extract_stack(stack):
|
|
|
|
code.append('File: "%s", line %d, in %s' % (filename, lineno, name))
|
|
|
|
if line:
|
|
|
|
code.append(" %s" % (line.strip()))
|
2010-09-22 11:32:56 +00:00
|
|
|
logging.getLogger('dumpstacks').info("\n".join(code))
|
|
|
|
|
|
|
|
for signum in SIGNALS:
|
|
|
|
signal.signal(signum, handler)
|
2010-09-20 16:54:56 +00:00
|
|
|
|
|
|
|
if os.name == 'posix':
|
|
|
|
signal.signal(signal.SIGQUIT, dumpstacks)
|
|
|
|
|
2010-09-22 11:32:56 +00:00
|
|
|
def quit():
|
|
|
|
netsvc.Agent.quit()
|
|
|
|
netsvc.Server.quitAll()
|
|
|
|
if tools.config['pidfile']:
|
|
|
|
os.unlink(tools.config['pidfile'])
|
|
|
|
logger = logging.getLogger('shutdown')
|
|
|
|
logger.info("Initiating OpenERP Server shutdown")
|
|
|
|
logger.info("Hit CTRL-C again or send a second signal to immediately terminate the server...")
|
|
|
|
logging.shutdown()
|
|
|
|
|
|
|
|
# manually join() all threads before calling sys.exit() to allow a second signal
|
|
|
|
# to trigger _force_quit() in case some non-daemon threads won't exit cleanly.
|
|
|
|
# threading.Thread.join() should not mask signals (at least in python 2.5)
|
|
|
|
for thread in threading.enumerate():
|
2010-09-24 10:13:54 +00:00
|
|
|
if thread != threading.currentThread() and not thread.isDaemon():
|
2010-09-22 11:32:56 +00:00
|
|
|
while thread.isAlive():
|
|
|
|
# need a busyloop here as thread.join() masks signals
|
|
|
|
# and would present the forced shutdown
|
|
|
|
thread.join(0.05)
|
|
|
|
time.sleep(0.05)
|
|
|
|
sys.exit(0)
|
|
|
|
|
2008-12-23 23:47:52 +00:00
|
|
|
if tools.config['pidfile']:
|
|
|
|
fd = open(tools.config['pidfile'], 'w')
|
2008-08-12 14:44:56 +00:00
|
|
|
pidtext = "%d" % (os.getpid())
|
2008-07-22 14:24:36 +00:00
|
|
|
fd.write(pidtext)
|
|
|
|
fd.close()
|
2007-02-03 22:58:57 +00:00
|
|
|
|
2009-08-29 15:23:46 +00:00
|
|
|
netsvc.Server.startAll()
|
|
|
|
|
2010-09-22 11:32:56 +00:00
|
|
|
logger.info('OpenERP server is running, waiting for connections...')
|
2006-12-07 13:41:40 +00:00
|
|
|
|
2010-09-22 11:32:56 +00:00
|
|
|
while netsvc.quit_signals_received == 0:
|
2009-08-29 15:23:46 +00:00
|
|
|
time.sleep(60)
|
2008-07-23 15:01:27 +00:00
|
|
|
|
2010-09-22 11:32:56 +00:00
|
|
|
quit()
|
|
|
|
|
2008-07-23 15:01:27 +00:00
|
|
|
# vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4:
|