def restore(database_name, password, data, update=False): logger = logging.getLogger('database') security.check_super(password) try: database = Database().connect() cursor = database.cursor() cursor.close(close=True) raise Exception("Database already exists!") except Exception: pass Database.restore(database_name, data) logger.info('RESTORE DB: %s' % (database_name)) if update: cursor = Database(database_name).connect().cursor() cursor.execute('SELECT code FROM ir_lang ' \ 'WHERE translatable') lang = [x[0] for x in cursor.fetchall()] cursor.execute('UPDATE ir_module_module SET ' \ "state = 'to upgrade' " \ "WHERE state = 'installed'") cursor.commit() cursor.close() Pool(database_name).init(update=update, lang=lang) logger.info('Update/Init succeed!') return True
def run(self): "Run the server and never return" from trytond.backend import Database from trytond.pool import Pool from trytond.monitor import monitor update = bool(CONFIG['init'] or CONFIG['update']) init = {} signal.signal(signal.SIGINT, lambda *a: self.stop()) signal.signal(signal.SIGTERM, lambda *a: self.stop()) if hasattr(signal, 'SIGQUIT'): signal.signal(signal.SIGQUIT, lambda *a: self.stop()) if hasattr(signal, 'SIGUSR1'): signal.signal(signal.SIGUSR1, lambda *a: self.restart()) if CONFIG['pidfile']: with open(CONFIG['pidfile'], 'w') as fd_pid: fd_pid.write("%d" % (os.getpid())) if CONFIG['psyco']: import psyco psyco.full() if not CONFIG["db_name"] \ and bool(CONFIG['init'] or CONFIG['update']): raise Exception('Missing database option!') if not update: self.start_servers() for db_name in CONFIG["db_name"]: init[db_name] = False database = Database(db_name).connect() cursor = database.cursor() try: if CONFIG['init']: if not cursor.test(): self.logger.info("init db") Database.init(cursor) init[db_name] = True cursor.commit() elif not cursor.test(): raise Exception("'%s' is not a Tryton database!" % db_name) finally: cursor.close() for db_name in CONFIG["db_name"]: if update: cursor = Database(db_name).connect().cursor() try: if not cursor.test(): raise Exception("'%s' is not a Tryton database!" % db_name) cursor.execute('SELECT code FROM ir_lang ' \ 'WHERE translatable') lang = [x[0] for x in cursor.fetchall()] finally: cursor.close() else: lang = None Pool(db_name).init(update=update, lang=lang) for kind in ('init', 'update'): CONFIG[kind] = {} for db_name in CONFIG['db_name']: if init[db_name]: while True: password = getpass('Admin Password for %s: ' % db_name) password2 = getpass('Admin Password Confirmation: ') if password != password2: sys.stderr.write('Admin Password Confirmation ' \ 'doesn\'t match Admin Password!\n') continue if not password: sys.stderr.write('Admin Password is required!\n') continue break database = Database(db_name).connect() cursor = database.cursor() try: salt = ''.join(random.sample( string.letters + string.digits, 8)) password += salt if hashlib: password = hashlib.sha1(password).hexdigest() else: password = sha.new(password).hexdigest() cursor.execute('UPDATE res_user ' \ 'SET password = %s, salt = %s ' \ 'WHERE login = \'admin\'', (password, salt)) cursor.commit() finally: cursor.close() if update: self.logger.info('Update/Init succeed!') logging.shutdown() sys.exit(0) threads = {} while True: if CONFIG['cron']: for dbname in Pool.database_list(): thread = threads.get(dbname) if thread and thread.is_alive(): continue pool = Pool(dbname) if not pool.lock.acquire(0): continue try: if 'ir.cron' not in pool.object_name_list(): continue cron_obj = pool.get('ir.cron') finally: pool.lock.release() thread = threading.Thread( target=cron_obj.run, args=(dbname,), kwargs={}) thread.start() threads[dbname] = thread if CONFIG['auto_reload']: for _ in range(60): if monitor(): self.restart() time.sleep(1) else: time.sleep(60)
def run(self): "Run the server and never return" from trytond.backend import Database from trytond.pool import Pool from trytond.monitor import monitor update = bool(CONFIG['init'] or CONFIG['update']) init = {} signal.signal(signal.SIGINT, lambda *a: self.stop()) signal.signal(signal.SIGTERM, lambda *a: self.stop()) if hasattr(signal, 'SIGQUIT'): signal.signal(signal.SIGQUIT, lambda *a: self.stop()) if hasattr(signal, 'SIGUSR1'): signal.signal(signal.SIGUSR1, lambda *a: self.restart()) if CONFIG['pidfile']: with open(CONFIG['pidfile'], 'w') as fd_pid: fd_pid.write("%d" % (os.getpid())) if not CONFIG["db_name"] \ and bool(CONFIG['init'] or CONFIG['update']): raise Exception('Missing database option!') if not update: self.start_servers() for db_name in CONFIG["db_name"]: init[db_name] = False database = Database(db_name).connect() cursor = database.cursor() try: if CONFIG['init']: if not cursor.test(): self.logger.info("init db") Database.init(cursor) init[db_name] = True cursor.commit() elif not cursor.test(): raise Exception("'%s' is not a Tryton database!" % db_name) finally: cursor.close() for db_name in CONFIG["db_name"]: if update: cursor = Database(db_name).connect().cursor() try: if not cursor.test(): raise Exception("'%s' is not a Tryton database!" % db_name) cursor.execute('SELECT code FROM ir_lang ' 'WHERE translatable') lang = [x[0] for x in cursor.fetchall()] finally: cursor.close() else: lang = None Pool(db_name).init(update=update, lang=lang) for kind in ('init', 'update'): CONFIG[kind] = {} for db_name in CONFIG['db_name']: if init[db_name]: # try to read password from environment variable # TRYTONPASSFILE, empty TRYTONPASSFILE ignored passpath = os.getenv('TRYTONPASSFILE') password = '' if passpath: try: with open(passpath) as passfile: password = passfile.readline()[:-1] except Exception, err: sys.stderr.write('Can not read password ' 'from "%s": "%s"\n' % (passpath, err)) if not password: while True: password = getpass('Admin Password for %s: ' % db_name) password2 = getpass('Admin Password Confirmation: ') if password != password2: sys.stderr.write('Admin Password Confirmation ' 'doesn\'t match Admin Password!\n') continue if not password: sys.stderr.write('Admin Password is required!\n') continue break database = Database(db_name).connect() cursor = database.cursor() try: salt = ''.join(random.sample( string.letters + string.digits, 8)) password += salt password = hashlib.sha1(password).hexdigest() cursor.execute('UPDATE res_user ' 'SET password = %s, salt = %s ' 'WHERE login = \'admin\'', (password, salt)) cursor.commit() finally: cursor.close()