From d1559884b9fe1415fabee25472a0bb382b1d594b Mon Sep 17 00:00:00 2001 From: pedro Date: Wed, 25 Sep 2024 11:51:07 -0300 Subject: [PATCH] workbench-script: attempt to write to snapshot dir --- .gitignore | 2 +- snapshots/README.md | 3 +++ workbench-script.py | 31 +++++++++++++++++++++++-------- 3 files changed, 27 insertions(+), 9 deletions(-) create mode 100644 snapshots/README.md diff --git a/.gitignore b/.gitignore index b3b940f..ee8601d 100644 --- a/.gitignore +++ b/.gitignore @@ -1,5 +1,5 @@ iso settings.ini -# ignore all possible snapshots in this dir +# ignore all possible snapshots in this repo *.json pxe/.env diff --git a/snapshots/README.md b/snapshots/README.md new file mode 100644 index 0000000..7b90349 --- /dev/null +++ b/snapshots/README.md @@ -0,0 +1,3 @@ +This is the path by default used by workbench-script + +You can change it in the configuration diff --git a/workbench-script.py b/workbench-script.py index df5c744..628b805 100644 --- a/workbench-script.py +++ b/workbench-script.py @@ -249,15 +249,30 @@ def gen_snapshot(all_disks): def save_snapshot_in_disk(snapshot, path): - filename = "{}/{}_{}.json".format( - path, - datetime.now().strftime("%Y%m%d-%H_%M_%S"), - snapshot['uuid'] - ) - print(f"workbench: INFO: Snapshot written in path '{filename}'") - with open(filename, "w") as f: - f.write(json.dumps(snapshot)) + snapshot_path = os.path.join(path, 'snapshots') + filename = "{}/{}_{}.json".format( + snapshot_path, + datetime.now().strftime("%Y%m%d-%H_%M_%S"), + snapshot['uuid']) + + try: + if not os.path.exists(snapshot_path): + os.makedirs(snapshot_path) + print(f"workbench: INFO: Created snapshots directory at '{snapshot_path}'") + + with open(filename, "w") as f: + f.write(json.dumps(snapshot)) + print(f"workbench: INFO: Snapshot written in path '{filename}'") + except Exception as e: + print(f"workbench: WARNING: Failed to write in snapshots directory: {e}. Attempting to save in actual path.") + fallback_filename = "{}/{}_{}.json".format( + path, + datetime.now().strftime("%Y%m%d-%H_%M_%S"), + snapshot['uuid']) + with open(fallback_filename, "w") as f: + f.write(json.dumps(snapshot)) + print(f"workbench: INFO: Snapshot written in fallback path '{fallback_filename}'") # TODO sanitize url, if url is like this, it fails # url = 'http://127.0.0.1:8000/api/snapshot/'