def initialize_system(password=None, testsuite=False, force=False): """Call all the necessary methods to startup Stoq applications for every purpose: production usage, testing or demonstration """ log.info("Initialize_system") try: db_settings.clean_database(db_settings.dbname, force=force) create_base_schema() create_log("INIT START") store = new_store() populate_initial_data(store) register_accounts(store) register_payment_methods(store) from stoqlib.domain.uiform import create_default_forms create_default_forms(store) store.commit(close=True) ensure_sellable_constants() ensure_system_parameters() _ensure_card_providers() create_default_profiles() _install_invoice_templates() if not testsuite: create_default_profile_settings() ensure_admin_user(password) except Exception as e: raise if not testsuite: collect_traceback(sys.exc_info(), submit=True) raise SystemExit("Could not initialize system: %r" % (e, )) create_log("INIT DONE")
def initialize_system(password=None, testsuite=False, force=False): """Call all the necessary methods to startup Stoq applications for every purpose: production usage, testing or demonstration """ log.info("Initialize_system") try: db_settings.clean_database(db_settings.dbname, force=force) create_base_schema() create_log("INIT START") store = new_store() populate_initial_data(store) register_accounts(store) register_payment_methods(store) from stoqlib.domain.uiform import create_default_forms create_default_forms(store) store.commit(close=True) ensure_sellable_constants() ensure_system_parameters() _ensure_card_providers() create_default_profiles() _install_invoice_templates() if not testsuite: create_default_profile_settings() ensure_admin_user(password) except Exception, e: raise if not testsuite: collect_traceback(sys.exc_info(), submit=True) raise SystemExit("Could not initialize system: %r" % (e, ))
def restore_database(user_hash, time=None): assert user_hash # If the database doesn't exist, get_default_store will fail try: default_store = get_default_store() except Exception: default_store = None if default_store is not None and db_settings.has_database(): try: default_store.lock_database() except DatabaseError: raise TaskException( "Could not lock database. This means that there are other " "clients connected. Make sure to close every Stoq client " "before updating the database") except Exception: raise TaskException( "Database is empty or in a corrupted state. Fix or drop it " "before trying to proceed with the restore") else: default_store.unlock_database() # FIXME: Windows will not liberate resource for other process to # write to the file. We should write our own TemporaryFile on Stoq # that handles all those cases for us and use here with tempfile.NamedTemporaryFile(delete=False) as f: pass try: if not db_settings.dump_database(f.name): raise TaskException("Failed to dump the database") backup_name = db_settings.restore_database(f.name) logger.info("Created a backup of the current database state on %s", backup_name) finally: os.unlink(f.name) tmp_path = tempfile.mkdtemp() try: restore_path = os.path.join(tmp_path, 'stoq') backup.restore(restore_path, user_hash, time=time) # None will make the default store be closed, which we need # to sucessfully restore the database set_default_store(None) db_settings.clean_database(db_settings.dbname, force=True) db_settings.execute_sql(os.path.join(restore_path, 'stoq.dump'), lock_database=True) logger.info("Backup restore finished sucessfully") finally: # get_default_store will recreate it (since we closed it above) get_default_store() shutil.rmtree(tmp_path, ignore_errors=True)
def restore_database(user_hash, time=None): assert user_hash # If the database doesn't exist, get_default_store will fail try: default_store = get_default_store() except Exception: default_store = None if default_store is not None and db_settings.has_database(): try: default_store.lock_database() except DatabaseError: raise TaskException( "Could not lock database. This means that there are other " "clients connected. Make sure to close every Stoq client " "before updating the database") except Exception: raise TaskException( "Database is empty or in a corrupted state. Fix or drop it " "before trying to proceed with the restore") else: default_store.unlock_database() # FIXME: Windows will not liberate resource for other process to # write to the file. We should write our own TemporaryFile on Stoq # that handles all those cases for us and use here with tempfile.NamedTemporaryFile(delete=False) as f: pass try: if not db_settings.dump_database(f.name): raise TaskException("Failed to dump the database") backup_name = db_settings.restore_database(f.name) logger.info("Created a backup of the current database state on %s", backup_name) finally: os.unlink(f.name) tmp_path = tempfile.mkdtemp() try: restore_path = os.path.join(tmp_path, 'stoq') logger.info("restoring database to %s", restore_path) backup.restore(restore_path, user_hash, time=time) # None will make the default store be closed, which we need # to sucessfully restore the database set_default_store(None) db_settings.clean_database(db_settings.dbname, force=True) db_settings.execute_sql(os.path.join(restore_path, 'stoq.dump'), lock_database=True) logger.info("Backup restore finished sucessfully") finally: # get_default_store will recreate it (since we closed it above) get_default_store()
def restore_database(user_hash, time=None): assert user_hash # If the database doesn't exist, get_default_store will fail try: default_store = get_default_store() except Exception: default_store = None if default_store is not None and db_settings.has_database(): try: default_store.lock_database() except DatabaseError: raise TaskException( "Could not lock database. This means that there are other " "clients connected. Make sure to close every Stoq client " "before updating the database") except Exception: raise TaskException( "Database is empty or in a corrupted state. Fix or drop it " "before trying to proceed with the restore") else: default_store.unlock_database() with tempfile.NamedTemporaryFile() as f: if not db_settings.dump_database(f.name): raise TaskException("Failed to dump the database") backup_name = db_settings.restore_database(f.name) logger.info("Created a backup of the current database state on %s", backup_name) tmp_path = tempfile.mkdtemp() try: # None will make the default store be closed, which we need # to sucessfully restore the database set_default_store(None) restore_path = os.path.join(tmp_path, 'stoq') backup.restore(restore_path, user_hash, time=time) db_settings.clean_database(db_settings.dbname, force=True) db_settings.execute_sql(os.path.join(restore_path, 'stoq.dump'), lock_database=True) logger.info("Backup restore finished sucessfully") finally: # get_default_store will recreate it (since we closed it above) get_default_store() shutil.rmtree(tmp_path, ignore_errors=True)
def provide_database_settings(dbname=None, address=None, port=None, username=None, password=None, createdb=True): """ Provide database settings. :param dbname: :param address: :param port: :param username: :param password: :param create: Create a new empty database if one is missing """ if not username: username = get_username() if not dbname: dbname = username + u'_test' if not address: address = os.environ.get(u'PGHOST', u'') if not port: port = os.environ.get(u'PGPORT', u'5432') if not password: password = u"" # Remove all old utilities pointing to the previous database. utilities.clean() provide_utility(ISystemNotifier, test_system_notifier, replace=True) _provide_application_descriptions() _provide_domain_slave_mapper() _provide_app_info() db_settings.address = address db_settings.port = port db_settings.dbname = dbname db_settings.username = username db_settings.password = password rv = False if createdb or not db_settings.database_exists(dbname): db_settings.clean_database(dbname, force=True) rv = True return rv
def initialize_system(password=None, testsuite=False, force=False, empty=False): """Call all the necessary methods to startup Stoq applications for every purpose: production usage, testing or demonstration :param force: When False, we will ask the user if he really wants to replace the existing database. :param empty: If we should create the database without any data. When we do this the database will not be really usable by stoq. This should be used to create a database for the syncronization server. """ log.info("Initialize_system") try: db_settings.clean_database(db_settings.dbname, force=force) create_base_schema() create_log.info("INIT START") store = new_store() if not empty: populate_initial_data(store) register_accounts(store) register_payment_methods(store) from stoqlib.domain.uiform import create_default_forms create_default_forms(store) ensure_sellable_constants(store) sysparam.ensure_system_parameters(store) store.commit(close=True) _ensure_card_providers() create_default_profiles() _install_invoice_templates() if not testsuite: create_default_profile_settings() ensure_admin_user(password) except Exception: # if not testsuite: # collect_traceback(sys.exc_info(), submit=True) # raise SystemExit("Could not initialize system: %r" % (e, )) raise create_log.info("INIT DONE")
def provide_database_settings(dbname=None, address=None, port=None, username=None, password=None, create=True): """ Provide database settings. :param dbname: :param address: :param port: :param username: :param password: :param create: Create a new empty database if one is missing """ if not username: username = get_username() if not dbname: dbname = username + u'_test' if not address: address = os.environ.get(u'PGHOST', u'') if not port: port = os.environ.get(u'PGPORT', u'5432') if not password: password = u"" # Remove all old utilities pointing to the previous database. utilities.clean() provide_utility(ISystemNotifier, test_system_notifier, replace=True) _provide_application_descriptions() _provide_domain_slave_mapper() _provide_app_info() db_settings.address = address db_settings.port = port db_settings.dbname = dbname db_settings.username = username db_settings.password = password rv = False if create or not db_settings.database_exists(dbname): db_settings.clean_database(dbname, force=True) rv = True return rv
def initialize_system(password=None, testsuite=False, force=False, empty=False): """Call all the necessary methods to startup Stoq applications for every purpose: production usage, testing or demonstration :param force: When False, we will ask the user if he really wants to replace the existing database. :param empty: If we should create the database without any data. When we do this the database will not be really usable by stoq. This should be used to create a database for the syncronization server. """ log.info("Initialize_system") try: db_settings.clean_database(db_settings.dbname, force=force) create_base_schema() create_log.info("INIT START") store = new_store() if not empty: populate_initial_data(store) register_accounts(store) register_payment_methods(store) from stoqlib.domain.uiform import create_default_forms create_default_forms(store) ensure_sellable_constants(store) sysparam.ensure_system_parameters(store) store.commit(close=True) _ensure_card_providers() create_default_profiles() _install_invoice_templates() if not testsuite: create_default_profile_settings() ensure_admin_user(password) except Exception: # if not testsuite: # collect_traceback(sys.exc_info(), submit=True) # raise SystemExit("Could not initialize system: %r" % (e, )) raise create_log.info("INIT DONE")