-
Notifications
You must be signed in to change notification settings - Fork 17
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
1,034 additions
and
1,039 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -31,6 +31,7 @@ navani = {git = "git+https://github.com/the-grey-group/[email protected]"} | |
python-dateutil = "*" | ||
pybaselines = "*" | ||
rosettasciio = "*" | ||
fabric = "*" | ||
|
||
[dev-packages] | ||
pytest = "*" | ||
|
Large diffs are not rendered by default.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
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,119 @@ | ||
import subprocess | ||
import tarfile | ||
import tempfile | ||
from pathlib import Path | ||
|
||
from pydatalab.config import CONFIG | ||
from pydatalab.logger import LOGGER | ||
|
||
|
||
def make_snapshot(output_path: Path, encrypt: bool = False) -> None: | ||
"""Make a compressed snapshot of the entire datalab deployment that | ||
can be restored from with sufficient granularity, e.g., including | ||
config files. | ||
Creates a tar file with the following structure: | ||
- `./files/` - contains all files in `CONFIG.FILE_DIRECTORY` | ||
- `./mongodb/` - contains a dump of the mongodb database | ||
- `./config/` - contains a dump of the server config | ||
Arguments: | ||
output_path: Path to the .tar or .tar.gz output file, will raise | ||
a `FileExistsError` error if the file already exists. | ||
encrypt: Whether to encrypt the snapshot on write. | ||
""" | ||
if output_path.exists(): | ||
raise FileExistsError(f"Not overwriting existing file at {output_path}") | ||
|
||
if encrypt: | ||
raise NotImplementedError("Snapshot encryption not yet implemented") | ||
|
||
if "".join(output_path.suffixes) == ".tar.gz": | ||
mode = "w:gz" | ||
elif output_path.suffixes == ".tar": | ||
mode = "w" | ||
else: | ||
raise RuntimeError( | ||
f"Output path should either be a .tar or .tar.gz file, not {output_path} with {output_path.suffix}" | ||
) | ||
|
||
LOGGER.info("Creating snapshot of entire datalab instance.") | ||
LOGGER.debug("Creating snapshot of %s", CONFIG.FILE_DIRECTORY) | ||
# Add contents of `CONFIG.FILE_DIRECTORY` to the tar file | ||
with tarfile.open(output_path, mode=mode) as tar: | ||
for file in Path(CONFIG.FILE_DIRECTORY).iterdir(): | ||
tar.add(file, arcname=Path("files") / file.relative_to(CONFIG.FILE_DIRECTORY)) | ||
|
||
LOGGER.debug("Snapshot of %s created.", CONFIG.FILE_DIRECTORY) | ||
|
||
# Take a database dump and add it to the tar file | ||
LOGGER.debug("Taking dump of database %s", CONFIG.MONGO_URI) | ||
|
||
# Check that mongodump is available | ||
subprocess.check_output(["mongodump", "--version"]) | ||
|
||
with tempfile.TemporaryDirectory() as temp_dir: | ||
command = ["mongodump", CONFIG.MONGO_URI, "--out", str(Path(temp_dir).resolve())] | ||
subprocess.check_output(command) | ||
|
||
for file in Path(temp_dir).iterdir(): | ||
tar.add(file, arcname=Path("mongodb")) | ||
|
||
LOGGER.debug("Dump of database %s created.", CONFIG.MONGO_URI) | ||
|
||
LOGGER.debug("Dumping server config.") | ||
with tempfile.TemporaryDirectory() as temp_dir: | ||
with open(tmp_config := Path(temp_dir) / "config.json", "w") as f: | ||
data = CONFIG.json(indent=2, skip_defaults=True) | ||
f.write(data) | ||
|
||
tar.add( | ||
tmp_config, | ||
arcname=Path("config") / "config.json", | ||
) | ||
LOGGER.debug("Config dump created.") | ||
LOGGER.info("Snapshot saved at %s", output_path) | ||
|
||
|
||
def restore_snapshot(snapshot_path: Path, decrypt: bool = False): | ||
"""Restore a snapshot created with `make_snapshot` to the current | ||
datalab instance, using the current configuration. | ||
This will overwrite the contents of any existing MongoDB of the same | ||
name. | ||
Arguments: | ||
snapshot_path: Path to the .tar or .tar.gz snapshot file. | ||
""" | ||
LOGGER.info("Attempting to restore snapshot from %s", snapshot_path) | ||
if decrypt: | ||
raise NotImplementedError("Snapshot decryption not yet implemented") | ||
if not snapshot_path.exists(): | ||
raise FileNotFoundError(f"Snapshot file not found at {snapshot_path}") | ||
|
||
if "".join(snapshot_path.suffixes) == ".tar.gz": | ||
mode = "r:gz" | ||
elif snapshot_path.suffixes == ".tar": | ||
mode = "r" | ||
else: | ||
raise RuntimeError( | ||
f"Snapshot path should either be a .tar or .tar.gz file, not {snapshot_path} with {snapshot_path.suffix}" | ||
) | ||
|
||
with tarfile.open(snapshot_path, mode=mode) as tar: | ||
LOGGER.debug("Restoring files from %s", snapshot_path) | ||
files = [m for m in tar.getmembers() if m.name.startswith("files/")] | ||
tar.extractall(path=CONFIG.FILE_DIRECTORY, members=files) | ||
LOGGER.debug("Files restored from %s", snapshot_path) | ||
|
||
LOGGER.debug("Restoring database from %s", snapshot_path) | ||
with tempfile.TemporaryDirectory() as temp_dir: | ||
database = [m for m in tar.getmembers() if m.name.startswith("mongodb/")] | ||
tar.extractall(path=temp_dir, members=database) | ||
command = ["mongorestore", CONFIG.MONGO_URI, "--drop", str(Path(temp_dir) / "mongodb")] | ||
subprocess.check_output(command) | ||
LOGGER.debug("Database restored from %s", snapshot_path) | ||
|
||
LOGGER.info("Snapshot restored from %s", snapshot_path) |
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