Skip to content

Commit

Permalink
fix: validate environment for move state and handle errors
Browse files Browse the repository at this point in the history
  • Loading branch information
Jon Hagg committed Aug 18, 2021
1 parent a1f1516 commit 2dd0c9b
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 8 deletions.
7 changes: 3 additions & 4 deletions powersimdata/data_access/context.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
from powersimdata.data_access.data_access import LocalDataAccess, SSHDataAccess
from powersimdata.data_access.launcher import HttpLauncher, NativeLauncher, SSHLauncher
from powersimdata.utility import server_setup
from powersimdata.utility.config import DeploymentMode, get_deployment_mode
from powersimdata.utility.config import DeploymentMode


class Context:
Expand All @@ -22,8 +22,7 @@ def get_data_access(data_loc=None):
else:
root = server_setup.DATA_ROOT_DIR

mode = get_deployment_mode()
if mode == DeploymentMode.Server:
if server_setup.DEPLOYMENT_MODE == DeploymentMode.Server:
return SSHDataAccess(root)
return LocalDataAccess(root)

Expand All @@ -34,7 +33,7 @@ def get_launcher(scenario):
:param powersimdata.scenario.scenario.Scenario scenario: a scenario object
:return: (:class:`powersimdata.data_access.launcher.Launcher`) -- a launcher instance
"""
mode = get_deployment_mode()
mode = server_setup.DEPLOYMENT_MODE
if mode == DeploymentMode.Server:
return SSHLauncher(scenario)
elif mode == DeploymentMode.Container:
Expand Down
13 changes: 11 additions & 2 deletions powersimdata/scenario/move.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
from fs.copy import copy_dir
from fs.errors import FSError
from fs.walk import Walker

from powersimdata.data_access.data_access import get_ssh_fs
from powersimdata.scenario.ready import Ready
from powersimdata.utility import server_setup
from powersimdata.utility.config import DeploymentMode


class Move(Ready):
Expand All @@ -22,14 +24,18 @@ def move_scenario(self, target="disk", confirm=True):
:param str target: optional argument specifying the backup system.
:param bool confirm: prompt before deleting each batch of files
:raises TypeError: if target is not a str
:raises ValueError: if target is unknown (only "disk" is supported)
:raises ValueError: if target is unknown (only "disk" is supported) or
data not on server
"""
if not isinstance(target, str):
raise TypeError("string is expected for optional argument target")

if target != "disk":
raise ValueError("scenario data can only be backed up to disk now")

if server_setup.DEPLOYMENT_MODE != DeploymentMode.Server:
raise ValueError("move state only supported for scenario data on server.")

scenario_id = self._scenario_info["id"]
backup = BackUpDisk(self._data_access, scenario_id)
backup.backup_scenario(confirm=confirm)
Expand Down Expand Up @@ -67,6 +73,9 @@ def backup_scenario(self, confirm=True):
src_path = self._join(server_setup.DATA_ROOT_DIR, folder)
dst_path = self._join(server_setup.BACKUP_DATA_ROOT_DIR, folder)
walker = Walker(filter=[pattern])
copy_dir(src_fs, src_path, dst_fs, dst_path, walker=walker)
try:
copy_dir(src_fs, src_path, dst_fs, dst_path, walker=walker)
except FSError as e:
print(f"Operation failed: {e}")

self._data_access.remove(self._join(folder, pattern), confirm=confirm)
4 changes: 2 additions & 2 deletions powersimdata/utility/server_setup.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import os

from powersimdata.utility.config import get_config
from powersimdata.utility.config import get_config, get_deployment_mode

config = get_config()
SERVER_ADDRESS = config.SERVER_ADDRESS
Expand All @@ -13,7 +13,7 @@
LOCAL_DIR = config.LOCAL_DIR
MODEL_DIR = config.MODEL_DIR
ENGINE_DIR = config.ENGINE_DIR

DEPLOYMENT_MODE = get_deployment_mode()

os.makedirs(LOCAL_DIR, exist_ok=True)

Expand Down

0 comments on commit 2dd0c9b

Please sign in to comment.