diff --git a/CHANGELOG b/CHANGELOG index 74e50c2..7a196b0 100644 --- a/CHANGELOG +++ b/CHANGELOG @@ -4,3 +4,5 @@ [#122] Add a das-cli command to print the total number of atoms in the AtomDB (PR: #131) [#133] Changed Redis connection configuration for count-atoms to use the correct port from config file (PR: #134) [#132] das-cli db count-atoms --verbose should details Node and Link counts (PR: #138) +[#141] Validate Redis Cluster port range instead of a single port + diff --git a/src/commands/db/db_cli.py b/src/commands/db/db_cli.py index a3d6470..d96e562 100644 --- a/src/commands/db/db_cli.py +++ b/src/commands/db/db_cli.py @@ -238,7 +238,12 @@ def _redis_node( try: self._redis_container_manager.set_exec_context(node_context) - self._redis_container_manager.start_container(redis_port, redis_cluster) + self._redis_container_manager.start_container( + redis_port, + node_username, + node_ip, + redis_cluster, + ) self._redis_container_manager.unset_exec_context() self.stdout( diff --git a/src/commands/db/redis_container_manager.py b/src/commands/db/redis_container_manager.py index 0fe21ea..24e7ab1 100644 --- a/src/commands/db/redis_container_manager.py +++ b/src/commands/db/redis_container_manager.py @@ -1,6 +1,7 @@ from typing import AnyStr, Dict, List, Union from common import Container, ContainerManager +from common.network import is_server_port_available from config import REDIS_IMAGE_NAME, REDIS_IMAGE_VERSION @@ -25,39 +26,51 @@ def __init__( super().__init__(container, exec_context) self._options = options + @staticmethod + def get_cluster_command_params() -> List[str]: + return [ + "--cluster-enabled", + "yes", + "--cluster-config-file", + "nodes.conf", + "--cluster-node-timeout", + "5000", + ] + def start_container( self, port: int, + username: str, + host: str, cluster: bool = False, ): self.raise_running_container() - command_params = [ - "redis-server", - "--port", - f"{port}", - "--appendonly", - "yes", - "--protected-mode", - "no", - ] + cluster_command_params = self.get_cluster_command_params() if cluster else [] if cluster: - command_params += [ - "--cluster-enabled", - "yes", - "--cluster-config-file", - "nodes.conf", - "--cluster-node-timeout", - "5000", - ] + is_server_port_available( + username, + host, + port, + port + 10000, + ) container_id = self._start_container( restart_policy={ "Name": "on-failure", "MaximumRetryCount": 5, }, - command=command_params, + command=[ + *cluster_command_params, + "redis-server", + "--port", + f"{port}", + "--appendonly", + "yes", + "--protected-mode", + "no", + ], network_mode="host", ) @@ -79,7 +92,7 @@ def start_cluster(self, redis_nodes: List[Dict], redis_port: AnyStr): return container_id def get_count_keys(self) -> dict: - redis_port = self._options.get('redis_port') + redis_port = self._options.get("redis_port") command = f"sh -c \"redis-cli -p {redis_port} KEYS '*' | cut -d ' ' -f2\"" result = self._exec_container(command) diff --git a/src/common/network.py b/src/common/network.py index 4f204c3..f980903 100644 --- a/src/common/network.py +++ b/src/common/network.py @@ -32,26 +32,19 @@ def is_server_port_available( start_port: int, end_port: Union[int, None] = None, ): - def server_up(host, port): + def server_range_up(host, start_port, end_port): + port_range = f"{start_port}:{end_port}" if end_port else str(start_port) + command = f"ssh {username}@{host} \"ufw status | grep '{port_range}.*ALLOW'\"" - command = f"ssh {username}@{host} \"ufw status | grep '{port}.*ALLOW'\"" result = subprocess.call( command, shell=True, stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL, ) - return result == 0 - if end_port is None: - return server_up(host, port=start_port) - - for port in range(start_port, end_port + 1): - if not server_up(host, port): - return False - - return True + return server_range_up(host, start_port, end_port) def is_ssh_server_reachable(server: dict) -> bool: