Skip to content

Commit

Permalink
[#141] Verify port range for Redis Cluster instead of a single port (#…
Browse files Browse the repository at this point in the history
…142)

* das-toolbox-141: Verify port range

* das-toolbox-141: Refactor function is_server_port_available

* das-toolbox-141: Change start container

* das-toolbox-141: Validate port range for redis cluster

* das-toolbox-141: Fix server range up

* das-toolbox-141: Update changelog file
  • Loading branch information
levisingularity authored Dec 9, 2024
1 parent b243cf4 commit d9990f8
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 31 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG
Original file line number Diff line number Diff line change
Expand Up @@ -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

7 changes: 6 additions & 1 deletion src/commands/db/db_cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
51 changes: 32 additions & 19 deletions src/commands/db/redis_container_manager.py
Original file line number Diff line number Diff line change
@@ -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


Expand All @@ -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",
)

Expand All @@ -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)
Expand Down
15 changes: 4 additions & 11 deletions src/common/network.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down

0 comments on commit d9990f8

Please sign in to comment.