Skip to content

Commit 27e5e48

Browse files
committed
Add redis service startup during mirage node update
1 parent 036d884 commit 27e5e48

2 files changed

Lines changed: 40 additions & 8 deletions

File tree

node_cli/operations/mirage.py

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
# You should have received a copy of the GNU Affero General Public License
1818
# along with this program. If not, see <https://www.gnu.org/licenses/>.
1919

20+
import time
2021
import logging
2122
from enum import Enum
2223

@@ -39,11 +40,14 @@
3940
update_images,
4041
)
4142
from node_cli.utils.docker_utils import (
43+
REDIS_SERVICE_DICT,
44+
REDIS_START_TIMEOUT,
4245
NodeType,
4346
compose_rm,
4447
compose_up,
4548
docker_cleanup,
4649
remove_dynamic_containers,
50+
wait_for_container,
4751
)
4852
from node_cli.utils.helper import str_to_bool
4953
from node_cli.utils.meta import MirageCliMetaManager
@@ -94,9 +98,15 @@ def update_mirage(env_filepath: str, env: dict, update_type: MirageUpdateType) -
9498

9599
if update_type == MirageUpdateType.FROM_BOOT:
96100
migrate_nftables_from_boot()
97-
migrate_chain_record(env)
98101

99102
update_images(env=env, node_type=NodeType.MIRAGE)
103+
104+
compose_up(env=env, node_type=NodeType.MIRAGE, services=list(REDIS_SERVICE_DICT))
105+
wait_for_container(REDIS_SERVICE_DICT['redis'])
106+
time.sleep(REDIS_START_TIMEOUT)
107+
if update_type == MirageUpdateType.FROM_BOOT:
108+
migrate_chain_record(env)
109+
100110
compose_up(env=env, node_type=NodeType.MIRAGE)
101111
return True
102112

node_cli/utils/docker_utils.py

Lines changed: 29 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -17,15 +17,17 @@
1717
# You should have received a copy of the GNU Affero General Public License
1818
# along with this program. If not, see <https://www.gnu.org/licenses/>.
1919

20+
import os
2021
import io
22+
import time
2123
import itertools
22-
import os
2324
import logging
2425
from typing import Optional
2526

2627
import docker
2728
from docker.client import DockerClient
2829
from docker.models.containers import Container
30+
from docker.errors import NotFound
2931

3032
from node_cli.utils.helper import run_cmd, str_to_bool
3133
from node_cli.configs import (
@@ -44,8 +46,10 @@
4446
SCHAIN_REMOVE_TIMEOUT = 300
4547
IMA_REMOVE_TIMEOUT = 20
4648
TELEGRAF_REMOVE_TIMEOUT = 20
49+
REDIS_START_TIMEOUT = 10
50+
51+
REDIS_SERVICE_DICT = {'redis': 'skale_redis'}
4752

48-
# Services have format <service_name>: <container_name>
4953
CORE_COMMON_COMPOSE_SERVICES = {
5054
'transaction-manager': 'skale_transaction-manager',
5155
'redis': 'skale_redis',
@@ -245,7 +249,7 @@ def is_volume_exists(name: str, dutils=None):
245249
dutils = dutils or docker_client()
246250
try:
247251
dutils.volumes.get(name)
248-
except docker.errors.NotFound:
252+
except NotFound:
249253
return False
250254
return True
251255

@@ -300,7 +304,7 @@ def get_compose_services(node_type: NodeType) -> list[str]:
300304
return result
301305

302306

303-
def get_up_compose_cmd(node_type: NodeType, services: Optional[list[str]] = None) -> tuple:
307+
def get_up_compose_cmd(node_type: NodeType, services: list[str] | None = None) -> tuple:
304308
compose_path = get_compose_path(node_type)
305309

306310
if services is None:
@@ -309,7 +313,9 @@ def get_up_compose_cmd(node_type: NodeType, services: Optional[list[str]] = None
309313
return ('docker', 'compose', '-f', compose_path, 'up', '-d', *services)
310314

311315

312-
def compose_up(env, node_type: NodeType, is_mirage_boot: bool = False):
316+
def compose_up(
317+
env, node_type: NodeType, is_mirage_boot: bool = False, services: list[str] | None = None
318+
):
313319
if node_type == NodeType.SYNC:
314320
logger.info('Running containers for sync node')
315321
run_cmd(cmd=get_up_compose_cmd(node_type=NodeType.SYNC), env=env)
@@ -322,7 +328,7 @@ def compose_up(env, node_type: NodeType, is_mirage_boot: bool = False):
322328
logger.info('Running mirage base set of containers')
323329
if not is_mirage_boot:
324330
logger.debug('Launching mirage containers with env %s', env)
325-
run_cmd(cmd=get_up_compose_cmd(node_type=NodeType.MIRAGE), env=env)
331+
run_cmd(cmd=get_up_compose_cmd(node_type=NodeType.MIRAGE, services=services), env=env)
326332
else:
327333
logger.debug('Launching mirage boot containers with env %s', env)
328334
run_cmd(
@@ -385,7 +391,7 @@ def is_container_running(name: str, dclient: Optional[DockerClient] = None) -> b
385391
try:
386392
container = dc.containers.get(name)
387393
return container.status == 'running'
388-
except docker.errors.NotFound:
394+
except NotFound:
389395
return False
390396

391397

@@ -421,3 +427,19 @@ def docker_cleanup(dclient=None, ignore=None):
421427
system_prune()
422428
except Exception as e:
423429
logger.warning('Image cleanup errored with %s', e)
430+
431+
432+
def wait_for_container(container_name: str, attempts: int = 10, interval: int = 3) -> bool:
433+
logger.info('Waiting for container %s to be up', container_name)
434+
dc = docker_client()
435+
436+
for i in range(attempts):
437+
try:
438+
container = dc.containers.get(container_name)
439+
if container.status == 'running':
440+
logger.info('Container %s is up', container_name)
441+
return True
442+
except NotFound:
443+
logger.warning('Container %s not found, retrying...', container_name)
444+
time.sleep(interval)
445+
return False

0 commit comments

Comments
 (0)