18 from typing
import Optional, Tuple, Any, Dict
20 from .
import config_defaults
21 from ..networking
import networkManager, NetworkRequestError
22 from ..utils.docker
import DockerConfigurationException
25 def validateRamField(ram: Optional[Any], ramLimit: int) -> Optional[Tuple[int, str]]:
26 if ramLimit < config_defaults.MINIMUM_RAM:
27 raise DockerConfigurationException(
28 f
"Minimum Node RAM requirement ({config_defaults.MINIMUM_RAM}GB) "
29 f
"is higher than your current Docker desktop RAM limit ({ramLimit}GB). "
30 "Please adjust resource limitations in Docker Desktop settings to match Node requirements."
33 defaultRamValue = int(min(max(config_defaults.MINIMUM_RAM, ramLimit), config_defaults.DEFAULT_RAM))
35 if not isinstance(ram, int):
37 f
"Invalid config \"ram\" field type \"{type(ram)}\". Expected: \"int\""
38 f
"Using default value of {defaultRamValue} GB"
41 return defaultRamValue, message
43 if ram < config_defaults.MINIMUM_RAM:
45 f
"WARNING: Minimum Node RAM requirement ({config_defaults.MINIMUM_RAM}GB) "
46 f
"is higher than the configured value ({ram}GB)"
47 f
"Overriding \"ram\" field to match node RAM requirements."
50 return defaultRamValue, message
54 f
"WARNING: RAM limit in Docker Desktop ({ramLimit}GB) "
55 f
"is lower than the configured value ({ram}GB)"
56 f
"Overriding \"ram\" field to limit in Docker Desktop."
59 return defaultRamValue, message
63 def validateCpuCount(cpuCount: Optional[Any], cpuLimit: int) -> Optional[Tuple[int, str]]:
64 defaultCpuCount = config_defaults.DEFAULT_CPU_COUNT
if config_defaults.DEFAULT_CPU_COUNT <= cpuLimit
else cpuLimit
66 if not isinstance(cpuCount, int):
68 f
"Invalid config \"cpuCount\" field type \"{type(cpuCount)}\". Expected: \"int\""
69 f
"Using default value of {defaultCpuCount} cores"
72 return defaultCpuCount, message
74 if cpuCount > cpuLimit:
76 f
"WARNING: CPU limit in Docker Desktop ({cpuLimit}) "
77 f
"is lower than the configured value ({cpuCount})"
78 f
"Overriding \"cpuCount\" field to limit in Docker Desktop."
81 return defaultCpuCount, message
85 def validateSwapMemory(swap: Optional[Any], swapLimit: int) -> Optional[Tuple[int, str]]:
86 defaultSwapMemory = config_defaults.DEFAULT_SWAP_MEMORY
if config_defaults.DEFAULT_SWAP_MEMORY <= swapLimit
else swapLimit
88 if not isinstance(swap, int):
90 f
"Invalid config \"swap\" field type \"{type(swap)}\". Expected: \"int\""
91 f
"Using default value of {defaultSwapMemory} GB"
94 return defaultSwapMemory, message
98 f
"WARNING: SWAP limit in Docker Desktop ({swapLimit}GB) "
99 f
"is lower than the configured value ({swap}GB)"
100 f
"Overriding \"swap\" field to limit in Docker Desktop."
103 return defaultSwapMemory, message
108 def fetchNodeId(name: str) -> int:
113 response = networkManager.get(
"service/directory", params)
114 if response.hasFailed():
115 raise NetworkRequestError(response,
"Failed to fetch node id.")
117 responseJson = response.getJson(dict)
118 data = responseJson.get(
"data")
120 if not isinstance(data, list):
121 raise TypeError(f
"Invalid \"data\" type {type(data)}. Expected: \"list\"")
124 raise ValueError(f
"Node with name \"{name}\" not found.")
127 if not isinstance(nodeJson, dict):
128 raise TypeError(f
"Invalid \"nodeJson\" type {type(nodeJson)}. Expected: \"dict\"")
130 id = nodeJson.get(
"id")
131 if not isinstance(id, int):
132 raise TypeError(f
"Invalid \"id\" type {type(id)}. Expected: \"int\"")
137 def fetchInitialData() -> Dict[str, Any]:
138 response = networkManager.get(
"user/initial-data")
140 if response.hasFailed():
141 raise NetworkRequestError(response,
"Failed to fetch user's initial data.")
143 return response.getJson(dict)