Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 12 additions & 1 deletion das-cli/src/commands/config/config_cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
SHORT_HELP_CONFIG_SET,
)
from .config_provider import InteractiveConfigProvider, NonInteractiveConfigProvider
from .config_sections.normalize_file import verify_normalize_missing_values

from settings.config import CONFIGFILE_PATH

Expand Down Expand Up @@ -72,6 +73,14 @@ def __init__(

def _set_file_path(self, save_path) -> None:
self._settings.set_path(save_path)
verify_normalize_missing_values(self._settings, save_path)

self.stdout(
"Formatting file and setting up incorrect/incomplete values.",
severity=StdoutSeverity.WARNING
)

self._settings.save_path()
self.stdout(
f"Configuration file set to -> {self._settings.get_dir_path()}",
severity=StdoutSeverity.SUCCESS,
Expand All @@ -81,6 +90,7 @@ def _save(self, save_path: str) -> None:
self._remote_context_manager.commit()
self._settings.set_path(save_path)
self._settings.save()
self._settings.save_path()
self.stdout(
f"Configuration file saved -> {self._settings.get_dir_path()}",
severity=StdoutSeverity.SUCCESS,
Expand All @@ -89,8 +99,9 @@ def _save(self, save_path: str) -> None:
def interactive_mode(self) -> None:
config_mappings = self._interactive_config_provider.setup_settings()
save_path = config_mappings.pop("file_path")

self._interactive_config_provider.apply_values_to_settings(config_mappings)
self._save(save_path=save_path)
self._save(save_path)

def non_interactive_mode(self, config_key_value: tuple, config_path=str) -> None:
key, value = config_key_value
Expand Down
82 changes: 82 additions & 0 deletions das-cli/src/commands/config/config_sections/normalize_file.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
import getpass
from typing import Dict, Any, List

from common.docker import RemoteContextManager
from common.settings import Settings
from common.utils import get_rand_token


def normalize_servers(
nodes: List[Dict[str, Any]],
current_user: str,
context_manager: RemoteContextManager
) -> List[Dict[str, Any]]:
updated_nodes = []
servers_to_create_context: List[Dict[str, str]] = []

for node in nodes:
username = node.get("username")
context = node.get("context")
ip = node.get("ip", "localhost")

if context == "default":
if not username or username in ["root", "default"]:
node["username"] = current_user

updated_nodes.append(node)

elif context in [None, "", "None"]:
servers_to_create_context.append(
{
"ip": ip,
"username": username or current_user,
}
)

else:
updated_nodes.append(node)

if servers_to_create_context:
new_contexts = context_manager.create_servers_context(servers_to_create_context)
updated_nodes.extend(new_contexts)

return updated_nodes


def verify_populate_missing_values(settings: Settings, path: str) -> None:
content: Dict[str, Any] = settings.get_content()
current_user = getpass.getuser()

context_manager = RemoteContextManager()

mongodb = content.get("atomdb", {}).get("mongodb", {})

if mongodb:
mongodb_nodes = mongodb.get("nodes", [])

if mongodb_nodes:
mongodb["nodes"] = normalize_servers(
mongodb_nodes, current_user, context_manager
)

if mongodb.get("cluster"):
secret = mongodb.get("cluster_secret_key")

if not secret or secret in ["None", ""]:
mongodb["cluster_secret_key"] = get_rand_token(num_bytes=15)

redis = content.get("atomdb", {}).get("redis", {})

if redis:
redis_nodes = redis.get("nodes", [])

if redis_nodes:
redis["nodes"] = normalize_servers(
redis_nodes, current_user, context_manager
)

settings.set_content(content)

context_manager.commit()

settings.save()
2 changes: 0 additions & 2 deletions das-cli/src/commands/config/config_sections/savefile.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,4 @@ def savefile_path_section() -> tuple[str, bool]:
"This file already exists, would you like to reset/overwrite it's existing values?"
)

print(reset_file)

return (save_path, reset_file)
2 changes: 1 addition & 1 deletion das-cli/src/commands/config/config_sections/setup_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,4 +23,4 @@ def get_default_value(settings: Settings, path: str) -> str | Dict[str, Any] | N

return value
else:
return existing_value
return existing_value
14 changes: 10 additions & 4 deletions das-cli/src/common/config/store.py
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,10 @@ def set_path(self, new_file_path) -> None:
"""Set the new configuration file path or identifier."""
pass

def save_path(self, file_path: str) -> None:
"""Save the new configuration file path of identifier."""
pass

@abstractmethod
def get_dir_path(self) -> str:
"""Get the directory path where the configuration is stored."""
Expand All @@ -79,7 +83,7 @@ def get_dir_path(self) -> str:
class JsonConfigStore(ConfigStore):
def __init__(self, env_file_path: str):
self._file_path = CONFIGFILE_PATH
self._env_file_path = env_file_path
self._env_path = env_file_path
self._content: Dict[str, Any] = {}
self._new_content: Dict[str, Any] = {}
self._overwrite_mode = False
Expand All @@ -92,11 +96,13 @@ def set_content(self, content: Dict[str, Any]) -> None:
self._new_content = content

def get_path(self) -> str:
return self._env_file_path
return self._env_path

def set_path(self, new_file_path) -> None:
def set_path(self, new_file_path: str) -> None:
self._file_path = new_file_path
env_file = open(self._env_file_path, "w")

def save_path(self) -> None:
env_file = open(self._env_path, "w")
env_file.write(f"configpath={self._file_path}\n")

def get_dir_path(self) -> str:
Expand Down
5 changes: 4 additions & 1 deletion das-cli/src/common/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,9 @@ def get_path(self):
def set_path(self, new_file_path):
self._store.set_path(new_file_path)

def save_path(self):
self._store.save_path()

def get_dir_path(self):
return self._store.get_dir_path()

Expand Down Expand Up @@ -84,7 +87,7 @@ def raise_on_version_mismatch(self):
def raise_on_missing_file(self):
if not self.exists():
raise FileNotFoundError(
"Configuration file not found. You can run the command 'config set' to create a configuration file or point to an existing file."
"No existing configuration path was found. You can run the command `config set` to create a configuration file or point to an existing file."
)

def validate_configuration_file(self):
Expand Down
Loading