-
-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #498 from OpenMined/eelco/fix-deployment
Migrate before running the server
- Loading branch information
Showing
10 changed files
with
116 additions
and
262 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
import yaml | ||
from loguru import logger | ||
|
||
from syftbox import __version__ | ||
from syftbox.lib.constants import PERM_FILE | ||
from syftbox.lib.hash import collect_files, hash_files | ||
from syftbox.lib.permissions import SyftPermission, migrate_permissions | ||
from syftbox.server.db import db | ||
from syftbox.server.db.schema import get_db | ||
from syftbox.server.server import create_folders | ||
from syftbox.server.settings import ServerSettings | ||
|
||
|
||
def run_migrations(settings: ServerSettings): | ||
logger.info("Creating folders") | ||
create_folders(settings.folders) | ||
logger.info("Initializing DB") | ||
init_db(settings) | ||
|
||
|
||
def init_db(settings: ServerSettings) -> None: | ||
# remove this after the upcoming release | ||
if __version__ in ["0.2.11", "0.2.12"]: | ||
# Delete existing DB to avoid conflicts | ||
db_path = settings.file_db_path.absolute() | ||
if db_path.exists(): | ||
db_path.unlink() | ||
migrate_permissions(settings.snapshot_folder) | ||
|
||
# might take very long as snapshot folder grows | ||
logger.info(f"> Collecting Files from {settings.snapshot_folder.absolute()}") | ||
files = collect_files(settings.snapshot_folder.absolute()) | ||
logger.info("> Hashing files") | ||
metadata = hash_files(files, settings.snapshot_folder) | ||
logger.info(f"> Updating file hashes at {settings.file_db_path.absolute()}") | ||
con = get_db(settings.file_db_path.absolute()) | ||
cur = con.cursor() | ||
for m in metadata: | ||
db.save_file_metadata(cur, m) | ||
|
||
# remove files that are not in the snapshot folder | ||
all_metadata = db.get_all_metadata(cur) | ||
for m in all_metadata: | ||
abs_path = settings.snapshot_folder / m.path | ||
if not abs_path.exists(): | ||
logger.info(f"{m.path} not found in {settings.snapshot_folder}, deleting from db") | ||
db.delete_file_metadata(cur, m.path.as_posix()) | ||
|
||
# fill the permission tables | ||
for file in settings.snapshot_folder.rglob(PERM_FILE): | ||
content = file.read_text() | ||
rule_dicts = yaml.safe_load(content) | ||
perm_file = SyftPermission.from_rule_dicts( | ||
permfile_file_path=file.relative_to(settings.snapshot_folder), rule_dicts=rule_dicts | ||
) | ||
db.set_rules_for_permfile(con, perm_file) | ||
db.link_existing_rules_to_file(con, file.relative_to(settings.snapshot_folder)) | ||
|
||
cur.close() | ||
con.commit() | ||
con.close() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.