def init_db(store, use_single_lang=False): db_create_tables(store) appdata_dict = db_update_appdata(store) log.debug("Performing database initialization...") config.system_cfg_init(store) if GLSettings.skip_wizard: NodeFactory(store).set_val('wizard_done', True) log.debug("Inserting internationalized strings...") if not use_single_lang: EnabledLanguage.add_all_supported_langs(store, appdata_dict) else: EnabledLanguage.add_new_lang(store, u'en', appdata_dict) with open(os.path.join(GLSettings.client_path, 'logo.png'), 'r') as logo_file: data = logo_file.read() files.db_add_file(store, data, u'logo') with open(os.path.join(GLSettings.client_path, 'favicon.ico'), 'r') as favicon_file: data = favicon_file.read() files.db_add_file(store, data, u'favicon')
def init_db(store): db_create_tables(store) appdata_dict = db_update_appdata(store) log.debug("Performing database initialization...") node = models.Node() node.wizard_done = GLSettings.skip_wizard node.receipt_salt = generateRandomSalt() for k in appdata_dict['node']: setattr(node, k, appdata_dict['node'][k]) notification = models.Notification() for k in appdata_dict['templates']: setattr(notification, k, appdata_dict['templates'][k]) logo = '' with open(os.path.join(GLSettings.client_path, 'logo.png'), 'r') as logo_file: logo = logo_file.read() node.logo = models.File() node.logo.data = base64.b64encode(logo) store.add(node) store.add(notification) admin_dict = { 'username': u'admin', 'password': u'globaleaks', 'deeletable': False, 'role': u'admin', 'state': u'enabled', 'deletable': False, 'name': u'Admin', 'description': u'', 'mail_address': u'', 'language': node.default_language, 'timezone': node.default_timezone, 'password_change_needed': False, 'pgp_key_remove': False, 'pgp_key_status': 'disabled', 'pgp_key_info': '', 'pgp_key_fingerprint': '', 'pgp_key_public': '', 'pgp_key_expiration': datetime_null() } admin = db_create_admin_user(store, admin_dict, node.default_language) admin.password_change_needed = False
def db_perform_data_update(store): prv = PrivateFactory(store) stored_ver = prv.get_val('version') t = (stored_ver, __version__) # Catch all failures if stored_ver != __version__: prv.set_val('version', __version__) appdata = db_update_appdata(store) config.update_defaults(store) l10n.update_defaults(store, appdata) db_fix_fields_attrs(store) ok = config.is_cfg_valid(store) if not ok: m = 'Error: the system is not stable, update failed from %s to %s' % t raise DatabaseIntegrityError(m)
def db_perform_data_update(store): prv = PrivateFactory(store) stored_ver = prv.get_val('version') t = (stored_ver, __version__) if stored_ver != __version__: prv.set_val('version', __version__) # The below commands can change the current store based on the what is # currently stored in the DB. appdata = db_update_appdata(store) l10n.update_defaults(store, appdata) config.update_defaults(store) db_fix_fields_attrs(store) ok = config.is_cfg_valid(store) if not ok: m = 'Error: the system is not stable, update failed from %s to %s' % t raise Exception(m)
def init_db(store, use_single_lang=False): db_create_tables(store) appdata_dict = db_update_appdata(store) log.debug("Performing database initialization...") config.system_cfg_init(store) if not use_single_lang: EnabledLanguage.add_all_supported_langs(store, appdata_dict) else: EnabledLanguage.add_new_lang(store, u'en', appdata_dict) with open(os.path.join(GLSettings.client_path, 'data/logo.png'), 'r') as logo_file: data = logo_file.read() files.db_add_file(store, data, u'logo') with open(os.path.join(GLSettings.client_path, 'data/favicon.ico'), 'r') as favicon_file: data = favicon_file.read() files.db_add_file(store, data, u'favicon')
def perform_version_update(version): """ @param version: @return: """ to_delete_on_fail = [] to_delete_on_success = [] if version < FIRST_DATABASE_VERSION_SUPPORTED: GLSettings.print_msg("Migrations from DB version lower than %d are no more supported!" % FIRST_DATABASE_VERSION_SUPPORTED) GLSettings.print_msg("If you can't create your Node from scratch, contact us asking for support.") quit() tmpdir = os.path.abspath(os.path.join(GLSettings.db_path, 'tmp')) orig_db_file = os.path.abspath(os.path.join(GLSettings.db_path, 'glbackend-%d.db' % version)) final_db_file = os.path.abspath(os.path.join(GLSettings.db_path, 'glbackend-%d.db' % DATABASE_VERSION)) shutil.rmtree(tmpdir, True) os.mkdir(tmpdir) shutil.copy2(orig_db_file, tmpdir) old_db_file = None new_db_file = None try: while version < DATABASE_VERSION: old_db_file = os.path.abspath(os.path.join(tmpdir, 'glbackend-%d.db' % version)) new_db_file = os.path.abspath(os.path.join(tmpdir, 'glbackend-%d.db' % (version + 1))) GLSettings.db_file = new_db_file GLSettings.enable_input_length_checks = False to_delete_on_fail.append(new_db_file) to_delete_on_success.append(old_db_file) GLSettings.print_msg("Updating DB from version %d to version %d" % (version, version + 1)) store_old = Store(create_database('sqlite:' + old_db_file)) store_new = Store(create_database('sqlite:' + new_db_file)) # Here is instanced the migration script MigrationModule = importlib.import_module("globaleaks.db.migrations.update_%d" % (version + 1)) migration_script = MigrationModule.MigrationScript(migration_mapping, version, store_old, store_new) GLSettings.print_msg("Migrating table:") try: try: migration_script.prologue() except Exception as exception: GLSettings.print_msg("Failure while executing migration prologue: %s" % exception) raise exception for model_name, _ in migration_mapping.iteritems(): if migration_script.model_from[model_name] is not None and migration_script.model_to[model_name] is not None: try: migration_script.migrate_model(model_name) # Commit at every table migration in order to be able to detect # the precise migration that may fail. migration_script.commit() except Exception as exception: GLSettings.print_msg("Failure while migrating table %s: %s " % (model_name, exception)) raise exception try: migration_script.epilogue() migration_script.commit() except Exception as exception: GLSettings.print_msg("Failure while executing migration epilogue: %s " % exception) raise exception finally: # the database should bee always closed before leaving the application # in order to not keep leaking journal files. migration_script.close() GLSettings.print_msg("Migration stats:") # we open a new db in order to verify integrity of the generated file store_verify = Store(create_database('sqlite:' + new_db_file)) for model_name, _ in migration_mapping.iteritems(): if model_name == 'ApplicationData': continue if migration_script.model_from[model_name] is not None and migration_script.model_to[model_name] is not None: count = store_verify.find(migration_script.model_to[model_name]).count() if migration_script.entries_count[model_name] != count: if migration_script.fail_on_count_mismatch[model_name]: raise AssertionError("Integrity check failed on count equality for table %s: %d != %d" % \ (model_name, count, migration_script.entries_count[model_name])) else: GLSettings.print_msg(" * %s table migrated (entries count changed from %d to %d)" % \ (model_name, migration_script.entries_count[model_name], count)) else: GLSettings.print_msg(" * %s table migrated (%d entry(s))" % \ (model_name, migration_script.entries_count[model_name])) version += 1 store_verify.close() store_appdata = Store(create_database('sqlite:' + new_db_file)) db_update_appdata(store_appdata) db_fix_fields_attrs(store_appdata) store_appdata.commit() store_appdata.close() except Exception as exception: print exception # simply propagage the exception raise exception else: # in case of success first copy the new migrated db, then as last action delete the original db file shutil.copy(new_db_file, final_db_file) os.remove(orig_db_file) finally: # always cleanup the temporary directory used for the migration shutil.rmtree(tmpdir, True)