[IMP] set menu sequence to 1000
[odoo/odoo.git] / bin / openerp-server.py
1 #!/usr/bin/env python
2 # -*- coding: utf-8 -*-
3 ##############################################################################
4 #    
5 #    OpenERP, Open Source Management Solution
6 #    Copyright (C) 2004-2009 Tiny SPRL (<http://tiny.be>).
7 #
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.
12 #
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.
17 #
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/>.     
20 #
21 ##############################################################################
22
23 """
24 OpenERP - Server
25 OpenERP is an ERP+CRM program for small and medium businesses.
26
27 The whole source code is distributed under the terms of the
28 GNU Public Licence.
29
30 (c) 2003-TODAY, Fabien Pinckaers - Tiny sprl
31 """
32
33 #----------------------------------------------------------
34 # python imports
35 #----------------------------------------------------------
36 import sys
37 import os
38 import signal
39 import pwd
40
41 import release
42 __author__ = release.author
43 __version__ = release.version
44
45 # We DON't log this using the standard logger, because we might mess
46 # with the logfile's permissions. Just do a quick exit here.
47 if pwd.getpwuid(os.getuid())[0] == 'root' :
48     sys.stderr.write("Attempted to run OpenERP server as root. This is not good, aborting.\n")
49     sys.exit(1)
50
51 #----------------------------------------------------------
52 # get logger
53 #----------------------------------------------------------
54 import netsvc
55 logger = netsvc.Logger()
56
57 #-----------------------------------------------------------------------
58 # import the tools module so that the commandline parameters are parsed
59 #-----------------------------------------------------------------------
60 import tools
61
62 logger.notifyChannel("server", netsvc.LOG_INFO, "version - %s" % release.version )
63 for name, value in [('addons_path', tools.config['addons_path']),
64                     ('database hostname', tools.config['db_host'] or 'localhost'),
65                     ('database port', tools.config['db_port'] or '5432'),
66                     ('database user', tools.config['db_user'])]:
67     logger.notifyChannel("server", netsvc.LOG_INFO, "%s - %s" % ( name, value ))
68
69 # Don't allow if the connection to PostgreSQL done by postgres user
70 if tools.config['db_user'] == 'postgres':
71     logger.notifyChannel("server", netsvc.LOG_ERROR, "%s" % ("Attempted to connect database with postgres user. This is a security flaws, aborting."))
72     sys.exit(1)
73
74 import time
75
76 #----------------------------------------------------------
77 # init net service
78 #----------------------------------------------------------
79 logger.notifyChannel("objects", netsvc.LOG_INFO, 'initialising distributed objects services')
80
81 #---------------------------------------------------------------
82 # connect to the database and initialize it with base if needed
83 #---------------------------------------------------------------
84 import pooler
85
86 #----------------------------------------------------------
87 # import basic modules
88 #----------------------------------------------------------
89 import osv
90 import workflow
91 import report
92 import service
93
94 #----------------------------------------------------------
95 # import addons
96 #----------------------------------------------------------
97
98 import addons
99
100 #----------------------------------------------------------
101 # Load and update databases if requested
102 #----------------------------------------------------------
103
104 import service.http_server
105
106 if not ( tools.config["stop_after_init"] or \
107     tools.config["translate_in"] or \
108     tools.config["translate_out"] ):
109     service.http_server.init_servers()
110     service.http_server.init_xmlrpc()
111
112     import service.netrpc_server
113     service.netrpc_server.init_servers()
114
115 if tools.config['db_name']:
116     for dbname in tools.config['db_name'].split(','):
117         db,pool = pooler.get_db_and_pool(dbname, update_module=tools.config['init'] or tools.config['update'], pooljobs=False)
118         if tools.config["test-file"]:
119             logger.notifyChannel("init", netsvc.LOG_INFO, 
120                  'loading test file %s' % (tools.config["test-file"],))
121             cr = db.cursor()
122             tools.convert_yaml_import(cr, 'base', file(tools.config["test-file"]), {}, 'test', True)
123             cr.rollback()
124         pool.get('ir.cron')._poolJobs(db.dbname)
125
126 #----------------------------------------------------------
127 # translation stuff
128 #----------------------------------------------------------
129 if tools.config["translate_out"]:
130     import csv
131
132     if tools.config["language"]:
133         msg = "language %s" % (tools.config["language"],)
134     else:
135         msg = "new language"
136     logger.notifyChannel("init", netsvc.LOG_INFO, 
137                          'writing translation file for %s to %s' % (msg, 
138                                                                     tools.config["translate_out"]))
139
140     fileformat = os.path.splitext(tools.config["translate_out"])[-1][1:].lower()
141     buf = file(tools.config["translate_out"], "w")
142     tools.trans_export(tools.config["language"], tools.config["translate_modules"], buf, fileformat)
143     buf.close()
144
145     logger.notifyChannel("init", netsvc.LOG_INFO, 'translation file written successfully')
146     sys.exit(0)
147
148 if tools.config["translate_in"]:
149     tools.trans_load(tools.config["db_name"], 
150                      tools.config["translate_in"], 
151                      tools.config["language"])
152     sys.exit(0)
153
154 #----------------------------------------------------------------------------------
155 # if we don't want the server to continue to run after initialization, we quit here
156 #----------------------------------------------------------------------------------
157 if tools.config["stop_after_init"]:
158     sys.exit(0)
159
160
161 #----------------------------------------------------------
162 # Launch Servers
163 #----------------------------------------------------------
164
165 LST_SIGNALS = ['SIGINT', 'SIGTERM']
166 if os.name == 'posix':
167     LST_SIGNALS.extend(['SIGUSR1','SIGQUIT'])
168
169
170 SIGNALS = dict(
171     [(getattr(signal, sign), sign) for sign in LST_SIGNALS]
172 )
173
174 def handler(signum, _):
175     """
176     :param signum: the signal number
177     :param _: 
178     """
179     netsvc.Agent.quit()
180     netsvc.Server.quitAll()
181     if tools.config['pidfile']:
182         os.unlink(tools.config['pidfile'])
183     logger.notifyChannel('shutdown', netsvc.LOG_INFO, 
184                          "Shutdown Server! - %s" % ( SIGNALS[signum], ))
185     logger.shutdown()
186     sys.exit(0)
187
188 for signum in SIGNALS:
189     signal.signal(signum, handler)
190
191 if tools.config['pidfile']:
192     fd = open(tools.config['pidfile'], 'w')
193     pidtext = "%d" % (os.getpid())
194     fd.write(pidtext)
195     fd.close()
196
197
198 netsvc.Server.startAll()
199
200 logger.notifyChannel("web-services", netsvc.LOG_INFO, 
201                      'the server is running, waiting for connections...')
202
203 while True:
204     time.sleep(60)
205
206 # vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4: