2 # -*- coding: utf-8 -*-
3 ##############################################################################
5 # OpenERP, Open Source Management Solution
6 # Copyright (C) 2004-2009 Tiny SPRL (<http://tiny.be>).
8 # This program is free software: you can redistribute it and/or modify
9 # it under the terms of the GNU Affero General Public License as
10 # published by the Free Software Foundation, either version 3 of the
11 # License, or (at your option) any later version.
13 # This program is distributed in the hope that it will be useful,
14 # but WITHOUT ANY WARRANTY; without even the implied warranty of
15 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 # GNU Affero General Public License for more details.
18 # You should have received a copy of the GNU Affero General Public License
19 # along with this program. If not, see <http://www.gnu.org/licenses/>.
21 ##############################################################################
25 OpenERP is an ERP+CRM program for small and medium businesses.
27 The whole source code is distributed under the terms of the
30 (c) 2003-TODAY, Fabien Pinckaers - OpenERP s.a.
42 __author__ = openerp.release.author
43 __version__ = openerp.release.version
45 def check_root_user():
46 """ Exit if the process's user is 'root' (on POSIX system)."""
47 if os.name == 'posix':
49 if pwd.getpwuid(os.getuid())[0] == 'root' :
50 sys.stderr.write("Running as user 'root' is a security risk, aborting.\n")
53 def check_postgres_user():
54 """ Exit if the configured database user is 'postgres'.
56 This function assumes the configuration has been initialized.
58 config = openerp.tools.config
59 if config['db_user'] == 'postgres':
60 sys.stderr.write("Using the database user 'postgres' is a security risk, aborting.")
63 def report_configuration():
64 """ Log the server version and some configuration values.
66 This function assumes the configuration has been initialized.
68 config = openerp.tools.config
69 logger = logging.getLogger('server')
70 logger.info("OpenERP version %s", __version__)
71 for name, value in [('addons paths', config['addons_path']),
72 ('database hostname', config['db_host'] or 'localhost'),
73 ('database port', config['db_port'] or '5432'),
74 ('database user', config['db_user'])]:
75 logger.info("%s: %s", name, value)
78 """ Create a file with the process id written in it.
80 This function assumes the configuration has been initialized.
82 config = openerp.tools.config
84 fd = open(config['pidfile'], 'w')
85 pidtext = "%d" % (os.getpid())
89 def preload_registry(dbname):
90 """ Preload a registry, and start the cron."""
91 db, pool = openerp.pooler.get_db_and_pool(dbname, update_module=config['init'] or config['update'], pooljobs=False)
92 pool.get('ir.cron').restart(db.dbname)
94 def run_test_file(dbname, test_file):
95 """ Preload a registry, possibly run a test file, and start the cron."""
96 db, pool = openerp.pooler.get_db_and_pool(dbname, update_module=config['init'] or config['update'], pooljobs=False)
99 logger = logging.getLogger('server')
100 logger.info('loading test file %s', test_file)
101 openerp.tools.convert_yaml_import(cr, 'base', file(test_file), {}, 'test', True)
105 def export_translation():
106 config = openerp.tools.config
107 dbname = config['db_name']
108 logger = logging.getLogger('server')
110 if config["language"]:
111 msg = "language %s" % (config["language"],)
114 logger.info('writing translation file for %s to %s', msg,
115 config["translate_out"])
117 fileformat = os.path.splitext(config["translate_out"])[-1][1:].lower()
118 buf = file(config["translate_out"], "w")
119 cr = openerp.pooler.get_db(dbname).cursor()
120 openerp.tools.trans_export(config["language"],
121 config["translate_modules"] or ["all"], buf, fileformat, cr)
125 logger.info('translation file written successfully')
127 def import_translation():
128 config = openerp.tools.config
129 context = {'overwrite': config["overwrite_existing_translations"]}
130 dbname = config['db_name']
132 cr = openerp.pooler.get_db(dbname).cursor()
133 openerp.tools.trans_load( cr, config["translate_in"], config["language"],
135 openerp.tools.trans_update_res_ids(cr)
139 def start_services():
140 http_server = openerp.service.http_server
141 netrpc_server = openerp.service.netrpc_server
143 # Instantiate local services (this is a legacy design).
144 openerp.osv.osv.start_object_proxy()
145 # Export (for RPC) services.
146 openerp.service.web_services.start_web_services()
148 # Initialize the HTTP stack.
149 http_server.init_servers()
150 http_server.init_xmlrpc()
151 http_server.init_static_http()
152 netrpc_server.init_servers()
154 # Start the main cron thread.
155 openerp.netsvc.start_agent()
157 # Start the top-level servers threads (normally HTTP, HTTPS, and NETRPC).
158 openerp.netsvc.Server.startAll()
160 # Variable keeping track of the number of calls to the signal handler defined
161 # below. This variable is monitored by ``quit_on_signals()``.
162 quit_signals_received = 0
164 def signal_handler(sig, frame):
165 """ Signal handler: exit ungracefully on the second handled signal.
167 :param sig: the signal number
168 :param frame: the interrupted stack frame or None
170 global quit_signals_received
171 quit_signals_received += 1
172 if quit_signals_received > 1:
173 # logging.shutdown was already called at this point.
174 sys.stderr.write("Forced shutdown.\n")
177 def dumpstacks(sig, frame):
178 """ Signal handler: dump a stack trace for each existing thread."""
179 # code from http://stackoverflow.com/questions/132058/getting-stack-trace-from-a-running-python-application#answer-2569696
180 # modified for python 2.5 compatibility
181 thread_map = dict(threading._active, **threading._limbo)
182 id2name = dict([(threadId, thread.getName()) for threadId, thread in thread_map.items()])
184 for threadId, stack in sys._current_frames().items():
185 code.append("\n# Thread: %s(%d)" % (id2name[threadId], threadId))
186 for filename, lineno, name, line in traceback.extract_stack(stack):
187 code.append('File: "%s", line %d, in %s' % (filename, lineno, name))
189 code.append(" %s" % (line.strip()))
190 logging.getLogger('dumpstacks').info("\n".join(code))
192 def setup_signal_handlers():
193 """ Register the signal handler defined above. """
194 SIGNALS = map(lambda x: getattr(signal, "SIG%s" % x), "INT TERM".split())
195 map(lambda sig: signal.signal(sig, signal_handler), SIGNALS)
196 if os.name == 'posix':
197 signal.signal(signal.SIGQUIT, dumpstacks)
199 def quit_on_signals():
200 """ Wait for one or two signals then shutdown the server.
202 The first SIGINT or SIGTERM signal will initiate a graceful shutdown while
203 a second one if any will force an immediate exit.
206 # Wait for a first signal to be handled. (time.sleep will be interrupted
207 # by the signal handler.)
208 while quit_signals_received == 0:
211 openerp.netsvc.Agent.quit()
212 openerp.netsvc.Server.quitAll()
213 config = openerp.tools.config
214 if config['pidfile']:
215 os.unlink(config['pidfile'])
216 logger = logging.getLogger('server')
217 logger.info("Initiating shutdown")
218 logger.info("Hit CTRL-C again or send a second signal to force the sutdown.")
221 # manually join() all threads before calling sys.exit() to allow a second signal
222 # to trigger _force_quit() in case some non-daemon threads won't exit cleanly.
223 # threading.Thread.join() should not mask signals (at least in python 2.5)
224 for thread in threading.enumerate():
225 if thread != threading.currentThread() and not thread.isDaemon():
226 while thread.isAlive():
227 # need a busyloop here as thread.join() masks signals
228 # and would present the forced shutdown
233 if __name__ == "__main__":
236 openerp.tools.config.parse_config(sys.argv[1:])
237 check_postgres_user()
238 openerp.netsvc.init_logger()
239 report_configuration()
241 config = openerp.tools.config
243 if config["test_file"]:
244 run_test_file(config['db_name'], config['test_file'])
247 if config["translate_out"]:
251 if config["translate_in"]:
255 if not config["stop_after_init"]:
256 # Some module register themselves when they are loaded so we need the
257 # services to be running before loading any registry.
260 if config['db_name']:
261 for dbname in config['db_name'].split(','):
262 preload_registry(dbname)
264 if config["stop_after_init"]:
268 setup_signal_handlers()
269 logger = logging.getLogger('server')
270 logger.info('OpenERP server is running, waiting for connections...')
273 # vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4: