1 # -*- encoding: utf-8 -*-
2 ##############################################################################
4 # OpenERP, Open Source Management Solution
5 # Copyright (C) 2004-2009 Tiny SPRL (<http://tiny.be>). All Rights Reserved
8 # This program is free software: you can redistribute it and/or modify
9 # it under the terms of the GNU General Public License as published by
10 # the Free Software Foundation, either version 3 of the License, or
11 # (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 General Public License for more details.
18 # You should have received a copy of the GNU General Public License
19 # along with this program. If not, see <http://www.gnu.org/licenses/>.
21 ##############################################################################
33 from tools.translate import _
44 class db(netsvc.Service):
45 def __init__(self, name="db"):
46 netsvc.Service.__init__(self, name)
47 self.joinGroup("web-services")
48 self.exportMethod(self.create)
49 self.exportMethod(self.get_progress)
50 self.exportMethod(self.drop)
51 self.exportMethod(self.dump)
52 self.exportMethod(self.restore)
53 self.exportMethod(self.rename)
54 self.exportMethod(self.list)
55 self.exportMethod(self.list_lang)
56 self.exportMethod(self.change_admin_password)
57 self.exportMethod(self.server_version)
58 self.exportMethod(self.migrate_databases)
61 self.id_protect = threading.Semaphore()
63 self._pg_psw_env_var_is_set = False # on win32, pg_dump need the PGPASSWORD env var
65 def create(self, password, db_name, demo, lang, user_password='admin'):
66 security.check_super(password)
67 self.id_protect.acquire()
70 self.id_protect.release()
72 self.actions[id] = {'clean': False}
74 db = sql_db.db_connect('template1')
75 cr = db.serialized_cursor()
77 cr.autocommit(True) # XXX inhibit the effect of a serialized cursor. is it what we want ?
78 cr.execute('CREATE DATABASE "%s" ENCODING \'unicode\'' % db_name)
81 sql_db.close_db('template1')
82 class DBInitialize(object):
83 def __call__(self, serv, id, db_name, demo, lang, user_password='admin'):
86 serv.actions[id]['progress'] = 0
88 cr = sql_db.db_connect(db_name).cursor()
93 pool = pooler.restart_pool(db_name, demo, serv.actions[id],
94 update_module=True)[1]
96 cr = sql_db.db_connect(db_name).cursor()
99 modobj = pool.get('ir.module.module')
100 mids = modobj.search(cr, 1, [('state', '=', 'installed')])
101 modobj.update_translations(cr, 1, mids, lang)
103 cr.execute('UPDATE res_users SET password=%s, context_lang=%s, active=True WHERE login=%s', (
104 user_password, lang, 'admin'))
105 cr.execute('SELECT login, password, name ' \
108 serv.actions[id]['users'] = cr.dictfetchall()
109 serv.actions[id]['clean'] = True
113 serv.actions[id]['clean'] = False
114 serv.actions[id]['exception'] = e
115 from cStringIO import StringIO
118 traceback.print_exc(file=e_str)
119 traceback_str = e_str.getvalue()
121 netsvc.Logger().notifyChannel('web-services', netsvc.LOG_ERROR, 'CREATE DATABASE\n%s' % (traceback_str))
122 serv.actions[id]['traceback'] = traceback_str
125 logger = netsvc.Logger()
126 logger.notifyChannel("web-services", netsvc.LOG_INFO, 'CREATE DATABASE: %s' % (db_name.lower()))
128 create_thread = threading.Thread(target=dbi,
129 args=(self, id, db_name, demo, lang, user_password))
130 create_thread.start()
131 self.actions[id]['thread'] = create_thread
134 def get_progress(self, password, id):
135 security.check_super(password)
136 if self.actions[id]['thread'].isAlive():
137 # return addons.init_progress[db_name]
138 return (min(self.actions[id].get('progress', 0),0.95), [])
140 clean = self.actions[id]['clean']
142 users = self.actions[id]['users']
146 e = self.actions[id]['exception']
150 def drop(self, password, db_name):
151 security.check_super(password)
152 sql_db.close_db(db_name)
153 logger = netsvc.Logger()
155 db = sql_db.db_connect('template1')
156 cr = db.serialized_cursor()
157 cr.autocommit(True) # XXX inhibit the effect of a serialized cursor. is it what we want ?
160 cr.execute('DROP DATABASE "%s"' % db_name)
162 logger.notifyChannel("web-services", netsvc.LOG_ERROR,
163 'DROP DB: %s failed:\n%s' % (db_name, e))
164 raise Exception("Couldn't drop database %s: %s" % (db_name, e))
166 logger.notifyChannel("web-services", netsvc.LOG_INFO,
167 'DROP DB: %s' % (db_name))
171 sql_db.close_db('template1')
174 def _set_pg_psw_env_var(self):
175 if os.name == 'nt' and not os.environ.get('PGPASSWORD', ''):
176 os.environ['PGPASSWORD'] = tools.config['db_password']
177 self._pg_psw_env_var_is_set = True
179 def _unset_pg_psw_env_var(self):
180 if os.name == 'nt' and self._pg_psw_env_var_is_set:
181 os.environ['PGPASSWORD'] = ''
183 def dump(self, password, db_name):
184 security.check_super(password)
185 logger = netsvc.Logger()
187 self._set_pg_psw_env_var()
189 cmd = ['pg_dump', '--format=c', '--no-owner']
190 if tools.config['db_user']:
191 cmd.append('--username=' + tools.config['db_user'])
192 if tools.config['db_host']:
193 cmd.append('--host=' + tools.config['db_host'])
194 if tools.config['db_port']:
195 cmd.append('--port=' + tools.config['db_port'])
198 stdin, stdout = tools.exec_pg_command_pipe(*tuple(cmd))
203 logger.notifyChannel("web-services", netsvc.LOG_ERROR,
204 'DUMP DB: %s failed\n%s' % (db_name, data))
205 raise Exception, "Couldn't dump database"
206 logger.notifyChannel("web-services", netsvc.LOG_INFO,
207 'DUMP DB: %s' % (db_name))
209 self._unset_pg_psw_env_var()
211 return base64.encodestring(data)
213 def restore(self, password, db_name, data):
214 security.check_super(password)
215 logger = netsvc.Logger()
217 self._set_pg_psw_env_var()
219 if self.db_exist(db_name):
220 logger.notifyChannel("web-services", netsvc.LOG_WARNING,
221 'RESTORE DB: %s already exists' % (db_name,))
222 raise Exception, "Database already exists"
224 db = sql_db.db_connect('template1')
225 cr = db.serialized_cursor()
226 cr.autocommit(True) # XXX inhibit the effect of a serialized cursor. is it what we want ?
228 cr.execute("""CREATE DATABASE "%s" ENCODING 'unicode' TEMPLATE "template0" """ % db_name)
231 sql_db.close_db('template1')
233 cmd = ['pg_restore', '--no-owner']
234 if tools.config['db_user']:
235 cmd.append('--username=' + tools.config['db_user'])
236 if tools.config['db_host']:
237 cmd.append('--host=' + tools.config['db_host'])
238 if tools.config['db_port']:
239 cmd.append('--port=' + tools.config['db_port'])
240 cmd.append('--dbname=' + db_name)
243 buf=base64.decodestring(data)
245 tmpfile = (os.environ['TMP'] or 'C:\\') + os.tmpnam()
246 file(tmpfile, 'wb').write(buf)
248 args2.append(' ' + tmpfile)
250 stdin, stdout = tools.exec_pg_command_pipe(*args2)
251 if not os.name == "nt":
252 stdin.write(base64.decodestring(data))
256 raise Exception, "Couldn't restore database"
257 logger.notifyChannel("web-services", netsvc.LOG_INFO,
258 'RESTORE DB: %s' % (db_name))
260 self._unset_pg_psw_env_var()
264 def rename(self, password, old_name, new_name):
265 security.check_super(password)
266 sql_db.close_db(old_name)
267 logger = netsvc.Logger()
269 db = sql_db.db_connect('template1')
270 cr = db.serialized_cursor()
273 cr.execute('ALTER DATABASE "%s" RENAME TO "%s"' % (old_name, new_name))
275 logger.notifyChannel("web-services", netsvc.LOG_ERROR,
276 'RENAME DB: %s -> %s failed:\n%s' % (old_name, new_name, e))
277 raise Exception("Couldn't rename database %s to %s: %s" % (old_name, new_name, e))
279 fs = os.path.join(tools.config['root_path'], 'filestore')
280 if os.path.exists(os.path.join(fs, old_name)):
281 os.rename(os.path.join(fs, old_name), os.path.join(fs, new_name))
283 logger.notifyChannel("web-services", netsvc.LOG_INFO,
284 'RENAME DB: %s -> %s' % (old_name, new_name))
288 sql_db.close_db('template1')
291 def db_exist(self, db_name):
293 db = sql_db.db_connect(db_name)
299 db = sql_db.db_connect('template1')
303 db_user = tools.config["db_user"]
304 if not db_user and os.name == 'posix':
306 db_user = pwd.getpwuid(os.getuid())[0]
308 cr.execute("select decode(usename, 'escape') from pg_user where usesysid=(select datdba from pg_database where datname=%s)", (tools.config["db_name"],))
310 db_user = res and str(res[0])
312 cr.execute("select decode(datname, 'escape') from pg_database where datdba=(select usesysid from pg_user where usename=%s) and datname not in ('template0', 'template1', 'postgres') order by datname", (db_user,))
314 cr.execute("select decode(datname, 'escape') from pg_database where datname not in('template0', 'template1','postgres') order by datname")
315 res = [str(name) for (name,) in cr.fetchall()]
320 sql_db.close_db('template1')
324 def change_admin_password(self, old_password, new_password):
325 security.check_super(old_password)
326 tools.config['admin_passwd'] = new_password
331 return tools.scan_languages()
333 def server_version(self):
334 """ Return the version of the server
335 Used by the client to verify the compatibility with its own version
337 return release.version
339 def migrate_databases(self, password, databases):
341 from osv.orm import except_orm
342 from osv.osv import except_osv
344 security.check_super(password)
348 l.notifyChannel('migration', netsvc.LOG_INFO, 'migrate database %s' % (db,))
349 tools.config['update']['base'] = True
350 pooler.restart_pool(db, force_demo=False, update_module=True)
351 except except_orm, inst:
352 self.abortResponse(1, inst.name, 'warning', inst.value)
353 except except_osv, inst:
354 self.abortResponse(1, inst.name, inst.exc_type, inst.value)
357 tb_s = reduce(lambda x, y: x+y, traceback.format_exception( sys.exc_type, sys.exc_value, sys.exc_traceback))
358 l.notifyChannel('web-services', netsvc.LOG_ERROR, tb_s)
363 class common(netsvc.Service):
364 def __init__(self,name="common"):
365 netsvc.Service.__init__(self,name)
366 self.joinGroup("web-services")
367 self.exportMethod(self.ir_get)
368 self.exportMethod(self.ir_set)
369 self.exportMethod(self.ir_del)
370 self.exportMethod(self.about)
371 self.exportMethod(self.login)
372 self.exportMethod(self.logout)
373 self.exportMethod(self.timezone_get)
374 self.exportMethod(self.get_available_updates)
375 self.exportMethod(self.get_migration_scripts)
376 self.exportMethod(self.get_server_environment)
378 def ir_set(self, db, uid, password, keys, args, name, value, replace=True, isobject=False):
379 security.check(db, uid, password)
380 cr = pooler.get_db(db).cursor()
381 res = ir.ir_set(cr,uid, keys, args, name, value, replace, isobject)
386 def ir_del(self, db, uid, password, id):
387 security.check(db, uid, password)
388 cr = pooler.get_db(db).cursor()
389 res = ir.ir_del(cr,uid, id)
394 def ir_get(self, db, uid, password, keys, args=None, meta=None, context=None):
399 security.check(db, uid, password)
400 cr = pooler.get_db(db).cursor()
401 res = ir.ir_get(cr,uid, keys, args, meta, context)
406 def login(self, db, login, password):
407 res = security.login(db, login, password)
408 logger = netsvc.Logger()
409 msg = res and 'successful login' or 'bad login or password'
410 logger.notifyChannel("web-service", netsvc.LOG_INFO, "%s from '%s' using database '%s'" % (msg, login, db.lower()))
413 def logout(self, db, login, password):
414 logger = netsvc.Logger()
415 logger.notifyChannel("web-service", netsvc.LOG_INFO,'Logout %s from database %s'%(login,db))
418 def about(self, extended=False):
419 """Return information about the OpenERP Server.
421 @param extended: if True then return version info
422 @return string if extended is False else tuple
427 OpenERP is an ERP+CRM program for small and medium businesses.
429 The whole source code is distributed under the terms of the
432 (c) 2003-TODAY, Fabien Pinckaers - Tiny sprl''')
435 return info, release.version
438 def timezone_get(self, db, login, password):
439 return time.tzname[0]
442 def get_available_updates(self, password, contract_id, contract_password):
443 security.check_super(password)
444 import tools.maintenance as tm
446 rc = tm.remote_contract(contract_id, contract_password)
448 raise tm.RemoteContractException('This contract does not exist or is not active')
450 return rc.get_available_updates(rc.id, addons.get_modules_with_version())
452 except tm.RemoteContractException, e:
453 self.abortResponse(1, 'Migration Error', 'warning', str(e))
456 def get_migration_scripts(self, password, contract_id, contract_password):
457 security.check_super(password)
459 import tools.maintenance as tm
461 rc = tm.remote_contract(contract_id, contract_password)
463 raise tm.RemoteContractException('This contract does not exist or is not active')
464 if rc.status != 'full':
465 raise tm.RemoteContractException('Can not get updates for a partial contract')
467 l.notifyChannel('migration', netsvc.LOG_INFO, 'starting migration with contract %s' % (rc.name,))
469 zips = rc.retrieve_updates(rc.id, addons.get_modules_with_version())
471 from shutil import rmtree, copytree, copy
473 backup_directory = os.path.join(tools.config['root_path'], 'backup', time.strftime('%Y-%m-%d-%H-%M'))
474 if zips and not os.path.isdir(backup_directory):
475 l.notifyChannel('migration', netsvc.LOG_INFO, 'create a new backup directory to \
476 store the old modules: %s' % (backup_directory,))
477 os.makedirs(backup_directory)
480 l.notifyChannel('migration', netsvc.LOG_INFO, 'upgrade module %s' % (module,))
481 mp = addons.get_module_path(module)
483 if os.path.isdir(mp):
484 copytree(mp, os.path.join(backup_directory, module))
485 if os.path.islink(mp):
490 copy(mp + 'zip', backup_directory)
491 os.unlink(mp + '.zip')
495 base64_decoded = base64.decodestring(zips[module])
497 l.notifyChannel('migration', netsvc.LOG_ERROR, 'unable to read the module %s' % (module,))
500 zip_contents = cStringIO.StringIO(base64_decoded)
504 tools.extract_zip_file(zip_contents, tools.config['addons_path'] )
506 l.notifyChannel('migration', netsvc.LOG_ERROR, 'unable to extract the module %s' % (module, ))
512 l.notifyChannel('migration', netsvc.LOG_ERROR, 'restore the previous version of the module %s' % (module, ))
513 nmp = os.path.join(backup_directory, module)
514 if os.path.isdir(nmp):
515 copytree(nmp, tools.config['addons_path'])
517 copy(nmp+'.zip', tools.config['addons_path'])
521 except tm.RemoteContractException, e:
522 self.abortResponse(1, 'Migration Error', 'warning', str(e))
525 tb_s = reduce(lambda x, y: x+y, traceback.format_exception( sys.exc_type, sys.exc_value, sys.exc_traceback))
526 l.notifyChannel('migration', netsvc.LOG_ERROR, tb_s)
529 def get_server_environment(self):
531 revno = os.popen('bzr revno').read()
534 for line in os.popen('bzr log -r %s'%(int(revno))).readlines():
535 if line.find(':')!=-1:
537 rev_log += '\t' + line
542 rev_log = 'Exception: %s\n' % (str(e))
544 os_lang = os.environ.get('LANG', '').split('.')[0]
545 environment = '\nEnvironment_Information : \n' \
547 'Operating System : %s\n' \
548 'Operating System Version : %s\n' \
549 'Operating System Locale : %s\n'\
550 'Python Version : %s\n'\
551 'OpenERP-Server Version : %s\n'\
552 'Last revision Details: \n%s' \
553 %(sys.platform,os.name,str(sys.version.split('\n')[1]),os_lang,str(sys.version[0:5]),release.version,rev_log)
557 class objects_proxy(netsvc.Service):
558 def __init__(self, name="object"):
559 netsvc.Service.__init__(self,name)
560 self.joinGroup('web-services')
561 self.exportMethod(self.execute)
562 self.exportMethod(self.exec_workflow)
563 self.exportMethod(self.obj_list)
565 def exec_workflow(self, db, uid, passwd, object, method, id):
566 security.check(db, uid, passwd)
567 service = netsvc.LocalService("object_proxy")
568 res = service.exec_workflow(db, uid, object, method, id)
571 def execute(self, db, uid, passwd, object, method, *args):
572 security.check(db, uid, passwd)
573 service = netsvc.LocalService("object_proxy")
574 res = service.execute(db, uid, object, method, *args)
577 def obj_list(self, db, uid, passwd):
578 security.check(db, uid, passwd)
579 service = netsvc.LocalService("object_proxy")
580 res = service.obj_list()
587 # - None = end of wizard
589 # Wizard Type: 'form'
594 # TODO: change local request to OSE request/reply pattern
596 class wizard(netsvc.Service):
597 def __init__(self, name='wizard'):
598 netsvc.Service.__init__(self,name)
599 self.joinGroup('web-services')
600 self.exportMethod(self.execute)
601 self.exportMethod(self.create)
607 def _execute(self, db, uid, wiz_id, datas, action, context):
608 self.wiz_datas[wiz_id].update(datas)
609 wiz = netsvc.LocalService('wizard.'+self.wiz_name[wiz_id])
610 return wiz.execute(db, uid, self.wiz_datas[wiz_id], action, context)
612 def create(self, db, uid, passwd, wiz_name, datas=None):
615 security.check(db, uid, passwd)
616 #FIXME: this is not thread-safe
618 self.wiz_datas[self.id] = {}
619 self.wiz_name[self.id] = wiz_name
620 self.wiz_uid[self.id] = uid
623 def execute(self, db, uid, passwd, wiz_id, datas, action='init', context=None):
626 security.check(db, uid, passwd)
628 if wiz_id in self.wiz_uid:
629 if self.wiz_uid[wiz_id] == uid:
630 return self._execute(db, uid, wiz_id, datas, action, context)
632 raise Exception, 'AccessDenied'
634 raise Exception, 'WizardNotFound'
638 # TODO: set a maximum report number per user to avoid DOS attacks
644 class ExceptionWithTraceback(Exception):
645 def __init__(self, msg, tb):
648 self.args = (msg, tb)
650 class report_spool(netsvc.Service):
651 def __init__(self, name='report'):
652 netsvc.Service.__init__(self, name)
653 self.joinGroup('web-services')
654 self.exportMethod(self.report)
655 self.exportMethod(self.report_get)
658 self.id_protect = threading.Semaphore()
660 def report(self, db, uid, passwd, object, ids, datas=None, context=None):
665 security.check(db, uid, passwd)
667 self.id_protect.acquire()
670 self.id_protect.release()
672 self._reports[id] = {'uid': uid, 'result': False, 'state': False, 'exception': None}
674 def go(id, uid, ids, datas, context):
675 cr = pooler.get_db(db).cursor()
677 obj = netsvc.LocalService('report.'+object)
678 (result, format) = obj.create(cr, uid, ids, datas, context)
679 self._reports[id]['result'] = result
680 self._reports[id]['format'] = format
681 self._reports[id]['state'] = True
682 except Exception, exception:
686 tb_s = "".join(traceback.format_exception(*tb))
687 logger = netsvc.Logger()
688 logger.notifyChannel('web-services', netsvc.LOG_ERROR,
689 'Exception: %s\n%s' % (str(exception), tb_s))
690 self._reports[id]['exception'] = ExceptionWithTraceback(tools.exception_to_unicode(exception), tb)
691 self._reports[id]['state'] = True
696 thread.start_new_thread(go, (id, uid, ids, datas, context))
699 def _check_report(self, report_id):
700 result = self._reports[report_id]
701 if result['exception']:
702 raise result['exception']
703 res = {'state': result['state']}
705 if tools.config['reportgz']:
707 res2 = zlib.compress(result['result'])
710 #CHECKME: why is this needed???
711 if isinstance(result['result'], unicode):
712 res2 = result['result'].encode('latin1', 'replace')
714 res2 = result['result']
716 res['result'] = base64.encodestring(res2)
717 res['format'] = result['format']
718 del self._reports[report_id]
721 def report_get(self, db, uid, passwd, report_id):
722 security.check(db, uid, passwd)
724 if report_id in self._reports:
725 if self._reports[report_id]['uid'] == uid:
726 return self._check_report(report_id)
728 raise Exception, 'AccessDenied'
730 raise Exception, 'ReportNotFound'
735 # vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4: