~stephane-openerp/openobject-server/python_lib

« back to all changes in this revision

Viewing changes to bin/service/web_services.py

  • Committer: Stephane Wirtel
  • Date: 2010-01-31 21:49:48 UTC
  • Revision ID: stephane@openerp.com-20100131214948-a3p93d5ml7tir1f0
[REF] Extract the Logger class from the netsvc module

Show diffs side-by-side

added added

removed removed

Lines of Context:
33
33
import addons
34
34
import ir
35
35
import netsvc
 
36
import logger
36
37
import pooler
37
38
import release
38
39
import sql_db
128
129
                    traceback.print_exc(file=e_str)
129
130
                    traceback_str = e_str.getvalue()
130
131
                    e_str.close()
131
 
                    netsvc.Logger().notifyChannel('web-services', netsvc.LOG_ERROR, 'CREATE DATABASE\n%s' % (traceback_str))
 
132
                    logger.Logger().notifyChannel('web-services', logger.LOG_ERROR, 'CREATE DATABASE\n%s' % (traceback_str))
132
133
                    serv.actions[id]['traceback'] = traceback_str
133
134
                    if cr:
134
135
                        cr.close()
135
 
        logger = netsvc.Logger()
136
 
        logger.notifyChannel("web-services", netsvc.LOG_INFO, 'CREATE DATABASE: %s' % (db_name.lower()))
 
136
        logger.Logger().notifyChannel("web-services", logger.LOG_INFO, 'CREATE DATABASE: %s' % (db_name.lower()))
137
137
        dbi = DBInitialize()
138
138
        create_thread = threading.Thread(target=dbi,
139
139
                args=(self, id, db_name, demo, lang, user_password))
158
158
 
159
159
    def exp_drop(self, db_name):
160
160
        sql_db.close_db(db_name)
161
 
        logger = netsvc.Logger()
 
161
        log = logger.Logger()
162
162
 
163
163
        db = sql_db.db_connect('template1')
164
164
        db.lock()
169
169
                try:
170
170
                    cr.execute('DROP DATABASE "%s"' % db_name)
171
171
                except Exception, e:
172
 
                    logger.notifyChannel("web-services", netsvc.LOG_ERROR,
173
 
                            'DROP DB: %s failed:\n%s' % (db_name, e))
 
172
                    log.notifyChannel("web-services", logger.LOG_ERROR, 'DROP DB: %s failed:\n%s' % (db_name, e))
174
173
                    raise Exception("Couldn't drop database %s: %s" % (db_name, e))
175
174
                else:
176
 
                    logger.notifyChannel("web-services", netsvc.LOG_INFO,
177
 
                        'DROP DB: %s' % (db_name))
 
175
                    log.notifyChannel("web-services", logger.LOG_INFO, 'DROP DB: %s' % (db_name))
178
176
            finally:
179
177
                cr.close()
180
178
        finally:
191
189
            os.environ['PGPASSWORD'] = ''
192
190
 
193
191
    def exp_dump(self, db_name):
194
 
        logger = netsvc.Logger()
 
192
        log = logger.Logger()
195
193
 
196
194
        self._set_pg_psw_env_var()
197
195
 
209
207
        data = stdout.read()
210
208
        res = stdout.close()
211
209
        if res:
212
 
            logger.notifyChannel("web-services", netsvc.LOG_ERROR,
213
 
                    'DUMP DB: %s failed\n%s' % (db_name, data))
 
210
            log.notifyChannel("web-services", logger.LOG_ERROR, 'DUMP DB: %s failed\n%s' % (db_name, data))
214
211
            raise Exception, "Couldn't dump database"
215
 
        logger.notifyChannel("web-services", netsvc.LOG_INFO,
216
 
                'DUMP DB: %s' % (db_name))
 
212
        log.notifyChannel("web-services", logger.LOG_INFO, 'DUMP DB: %s' % (db_name))
217
213
 
218
214
        self._unset_pg_psw_env_var()
219
215
 
220
216
        return base64.encodestring(data)
221
217
 
222
218
    def exp_restore(self, db_name, data):
223
 
        logger = netsvc.Logger()
 
219
        log = logger.Logger()
224
220
 
225
221
        self._set_pg_psw_env_var()
226
222
 
227
223
        if self.db_exist(db_name):
228
 
            logger.notifyChannel("web-services", netsvc.LOG_WARNING,
229
 
                    'RESTORE DB: %s already exists' % (db_name,))
 
224
            log.notifyChannel("web-services", logger.LOG_WARNING, 'RESTORE DB: %s already exists' % (db_name,))
230
225
            raise Exception, "Database already exists"
231
226
 
232
227
        db = sql_db.db_connect('template1')
265
260
        res = stdout.close()
266
261
        if res:
267
262
            raise Exception, "Couldn't restore database"
268
 
        logger.notifyChannel("web-services", netsvc.LOG_INFO,
 
263
        log.notifyChannel("web-services", logger.LOG_INFO,
269
264
                'RESTORE DB: %s' % (db_name))
270
265
 
271
266
        self._unset_pg_psw_env_var()
274
269
 
275
270
    def exp_rename(self, old_name, new_name):
276
271
        sql_db.close_db(old_name)
277
 
        logger = netsvc.Logger()
 
272
        log = logger.Logger()
278
273
 
279
274
        db = sql_db.db_connect('template1')
280
275
        db.lock()
284
279
                try:
285
280
                    cr.execute('ALTER DATABASE "%s" RENAME TO "%s"' % (old_name, new_name))
286
281
                except Exception, e:
287
 
                    logger.notifyChannel("web-services", netsvc.LOG_ERROR,
 
282
                    log.notifyChannel("web-services", logger.LOG_ERROR,
288
283
                            'RENAME DB: %s -> %s failed:\n%s' % (old_name, new_name, e))
289
284
                    raise Exception("Couldn't rename database %s to %s: %s" % (old_name, new_name, e))
290
285
                else:
292
287
                    if os.path.exists(os.path.join(fs, old_name)):
293
288
                        os.rename(os.path.join(fs, old_name), os.path.join(fs, new_name))
294
289
 
295
 
                    logger.notifyChannel("web-services", netsvc.LOG_INFO,
 
290
                    log.notifyChannel("web-services", logger.LOG_INFO,
296
291
                        'RENAME DB: %s -> %s' % (old_name, new_name))
297
292
            finally:
298
293
                cr.close()
355
350
        from osv.orm import except_orm
356
351
        from osv.osv import except_osv
357
352
 
358
 
        l = netsvc.Logger()
 
353
        l = logger.Logger()
359
354
        for db in databases:
360
355
            try:
361
 
                l.notifyChannel('migration', netsvc.LOG_INFO, 'migrate database %s' % (db,))
 
356
                l.notifyChannel('migration', logger.LOG_INFO, 'migrate database %s' % (db,))
362
357
                tools.config['update']['base'] = True
363
358
                pooler.restart_pool(db, force_demo=False, update_module=True)
364
359
            except except_orm, inst:
368
363
            except Exception, e:
369
364
                import traceback
370
365
                tb_s = reduce(lambda x, y: x+y, traceback.format_exception( sys.exc_type, sys.exc_value, sys.exc_traceback))
371
 
                l.notifyChannel('web-services', netsvc.LOG_ERROR, tb_s)
 
366
                l.notifyChannel('web-services', logger.LOG_ERROR, tb_s)
372
367
                raise
373
368
        return True
374
369
db()
393
388
        self.joinGroup("web-services")
394
389
 
395
390
    def dispatch(self, method, auth, params):
396
 
        logger = netsvc.Logger()
 
391
        log = logger.Logger()
397
392
        if method in [ 'ir_set','ir_del', 'ir_get' ]:
398
393
            return self.common_dispatch(method,auth,params)
399
394
        if method == 'login':
401
396
            res = security.login(params[0], params[1], params[2])
402
397
            msg = res and 'successful login' or 'bad login or password'
403
398
            # TODO log the client ip address..
404
 
            logger.notifyChannel("web-service", netsvc.LOG_INFO, "%s from '%s' using database '%s'" % (msg, params[1], params[0].lower()))
 
399
            log.notifyChannel("web-service", logger.LOG_INFO, "%s from '%s' using database '%s'" % (msg, params[1], params[0].lower()))
405
400
            return res or False
406
401
        elif method == 'logout':
407
402
            if auth:
408
403
                auth.logout(params[1])
409
 
            logger.notifyChannel("web-service", netsvc.LOG_INFO,'Logout %s from database %s'%(login,db))
 
404
            log.notifyChannel("web-service", logger.LOG_INFO,'Logout %s from database %s'%(login,db))
410
405
            return True
411
406
        elif method in ['about', 'timezone_get', 'get_server_environment', 'login_message', 'get_stats' ]:
412
407
            pass
481
476
 
482
477
 
483
478
    def exp_get_migration_scripts(self, contract_id, contract_password):
484
 
        l = netsvc.Logger()
 
479
        l = logger.Logger()
485
480
        import tools.maintenance as tm
486
481
        try:
487
482
            rc = tm.remote_contract(contract_id, contract_password)
490
485
            if rc.status != 'full':
491
486
                raise tm.RemoteContractException('Can not get updates for a partial contract')
492
487
 
493
 
            l.notifyChannel('migration', netsvc.LOG_INFO, 'starting migration with contract %s' % (rc.name,))
 
488
            l.notifyChannel('migration', logger.LOG_INFO, 'starting migration with contract %s' % (rc.name,))
494
489
 
495
490
            zips = rc.retrieve_updates(rc.id, addons.get_modules_with_version())
496
491
 
498
493
 
499
494
            backup_directory = os.path.join(tools.config['root_path'], 'backup', time.strftime('%Y-%m-%d-%H-%M'))
500
495
            if zips and not os.path.isdir(backup_directory):
501
 
                l.notifyChannel('migration', netsvc.LOG_INFO, 'create a new backup directory to \
 
496
                l.notifyChannel('migration', logger.LOG_INFO, 'create a new backup directory to \
502
497
                                store the old modules: %s' % (backup_directory,))
503
498
                os.makedirs(backup_directory)
504
499
 
505
500
            for module in zips:
506
 
                l.notifyChannel('migration', netsvc.LOG_INFO, 'upgrade module %s' % (module,))
 
501
                l.notifyChannel('migration', logger.LOG_INFO, 'upgrade module %s' % (module,))
507
502
                mp = addons.get_module_path(module)
508
503
                if mp:
509
504
                    if os.path.isdir(mp):
520
515
                    try:
521
516
                        base64_decoded = base64.decodestring(zips[module])
522
517
                    except:
523
 
                        l.notifyChannel('migration', netsvc.LOG_ERROR, 'unable to read the module %s' % (module,))
 
518
                        l.notifyChannel('migration', logger.LOG_ERROR, 'unable to read the module %s' % (module,))
524
519
                        raise
525
520
 
526
521
                    zip_contents = cStringIO.StringIO(base64_decoded)
529
524
                        try:
530
525
                            tools.extract_zip_file(zip_contents, tools.config['addons_path'] )
531
526
                        except:
532
 
                            l.notifyChannel('migration', netsvc.LOG_ERROR, 'unable to extract the module %s' % (module, ))
 
527
                            l.notifyChannel('migration', logger.LOG_ERROR, 'unable to extract the module %s' % (module, ))
533
528
                            rmtree(module)
534
529
                            raise
535
530
                    finally:
536
531
                        zip_contents.close()
537
532
                except:
538
 
                    l.notifyChannel('migration', netsvc.LOG_ERROR, 'restore the previous version of the module %s' % (module, ))
 
533
                    l.notifyChannel('migration', logger.LOG_ERROR, 'restore the previous version of the module %s' % (module, ))
539
534
                    nmp = os.path.join(backup_directory, module)
540
535
                    if os.path.isdir(nmp):
541
536
                        copytree(nmp, tools.config['addons_path'])
549
544
        except Exception, e:
550
545
            import traceback
551
546
            tb_s = reduce(lambda x, y: x+y, traceback.format_exception( sys.exc_type, sys.exc_value, sys.exc_traceback))
552
 
            l.notifyChannel('migration', netsvc.LOG_ERROR, tb_s)
 
547
            l.notifyChannel('migration', logger.LOG_ERROR, tb_s)
553
548
            raise
554
549
 
555
550
    def exp_get_server_environment(self):
581
576
        return tools.config.get('login_message', False)
582
577
 
583
578
    def exp_set_loglevel(self,loglevel):
584
 
        l = netsvc.Logger()
 
579
        l = logger.Logger()
585
580
        l.set_loglevel(int(loglevel))
586
581
        return True
587
582
 
742
737
                
743
738
                tb = sys.exc_info()
744
739
                tb_s = "".join(traceback.format_exception(*tb))
745
 
                logger = netsvc.Logger()
746
 
                logger.notifyChannel('web-services', netsvc.LOG_ERROR,
 
740
                logger = logger.Logger()
 
741
                logger.notifyChannel('web-services', logger.LOG_ERROR,
747
742
                        'Exception: %s\n%s' % (str(exception), tb_s))
748
743
                self._reports[id]['exception'] = ExceptionWithTraceback(tools.exception_to_unicode(exception), tb)
749
744
                self._reports[id]['state'] = True