Skip to content

Commit

Permalink
Toggle repair through node_cli schain status file
Browse files Browse the repository at this point in the history
  • Loading branch information
badrogger committed Jun 13, 2024
1 parent 2fe74ca commit 6908f68
Show file tree
Hide file tree
Showing 7 changed files with 83 additions and 118 deletions.
31 changes: 17 additions & 14 deletions node_cli/core/schains.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
import os
import pprint
import shutil
import time
from pathlib import Path

from typing import Dict, Optional
Expand All @@ -16,8 +17,7 @@
from node_cli.utils.helper import (
get_request,
error_exit,
safe_load_yml,
post_request
safe_load_yml
)
from node_cli.utils.exit_codes import CLIExitCodes
from node_cli.utils.print_formatters import (
Expand All @@ -27,7 +27,7 @@
print_schains
)
from node_cli.utils.docker_utils import ensure_volume, is_volume_exists
from node_cli.utils.helper import read_json, run_cmd
from node_cli.utils.helper import read_json, run_cmd, save_json
from lvmpy.src.core import mount, volume_mountpoint


Expand Down Expand Up @@ -89,22 +89,25 @@ def show_config(name: str) -> None:
error_exit(payload, exit_code=CLIExitCodes.BAD_API_RESPONSE)


def get_node_cli_schain_status_filepath(schain_name: str) -> str:
return os.path.join(SCHAIN_NODE_DATA_PATH, schain_name, 'node-cli.status')


def update_node_cli_schain_status(schain_name: str, status: dict) -> None:
path = get_node_cli_schain_status_filepath(schain_name)
save_json(path, status)


def toggle_schain_repair_mode(
schain: str,
snapshot_from: Optional[str] = None
) -> None:
json_params = {'schain_name': schain}
ts = int(time.time())
status = {'schain_name': schain, 'repair_ts': ts}
if snapshot_from:
json_params.update({'snapshot_from': snapshot_from})
status, payload = post_request(
blueprint=BLUEPRINT_NAME,
method='repair',
json=json_params
)
if status == 'ok':
print('Schain has been set for repair')
else:
error_exit(payload, exit_code=CLIExitCodes.BAD_API_RESPONSE)
status.update({'snapshot_from': snapshot_from})
update_node_cli_schain_status(schain, status)
print('Schain has been set for repair')


def describe(schain: str, raw=False) -> None:
Expand Down
17 changes: 15 additions & 2 deletions node_cli/utils/helper.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import os
import re
import sys
import uuid
from urllib.parse import urlparse

import yaml
Expand Down Expand Up @@ -77,16 +78,22 @@ class InvalidEnvFileError(Exception):
pass


def read_json(path):
def read_json(path: str) -> dict:
with open(path, encoding='utf-8') as data_file:
return json.loads(data_file.read())


def write_json(path, content):
def write_json(path: str, content: dict) -> None:
with open(path, 'w') as outfile:
json.dump(content, outfile, indent=4)


def save_json(path: str, content: dict) -> None:
tmp_path = get_tmp_path(path)
write_json(tmp_path, content)
shutil.move(tmp_path, path)


def init_file(path, content=None):
if not os.path.exists(path):
write_json(path, content)
Expand Down Expand Up @@ -400,3 +407,9 @@ def convert(self, value, param, ctx):

URL_TYPE = UrlType()
IP_TYPE = IpType()


def get_tmp_path(path: str) -> str:
base, ext = os.path.splitext(path)
salt = uuid.uuid4().hex[:5]
return base + salt + '.tmp' + ext
24 changes: 17 additions & 7 deletions tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,13 +29,14 @@
import yaml

from node_cli.configs import (
CONTAINER_CONFIG_TMP_PATH,
GLOBAL_SKALE_CONF_FILEPATH,
GLOBAL_SKALE_DIR,
META_FILEPATH,
NGINX_CONTAINER_NAME,
REMOVED_CONTAINERS_FOLDER_PATH,
STATIC_PARAMS_FILEPATH
CONTAINER_CONFIG_TMP_PATH,
GLOBAL_SKALE_CONF_FILEPATH,
GLOBAL_SKALE_DIR,
META_FILEPATH,
NGINX_CONTAINER_NAME,
REMOVED_CONTAINERS_FOLDER_PATH,
STATIC_PARAMS_FILEPATH,
SCHAIN_NODE_DATA_PATH
)
from node_cli.configs.node_options import NODE_OPTIONS_FILEPATH
from node_cli.configs.ssl import SSL_FOLDER_PATH
Expand Down Expand Up @@ -302,3 +303,12 @@ def tmp_config_dir():
yield CONTAINER_CONFIG_TMP_PATH
finally:
shutil.rmtree(CONTAINER_CONFIG_TMP_PATH)


@pytest.fixture
def tmp_schains_dir():
os.makedirs(SCHAIN_NODE_DATA_PATH)
try:
yield SCHAIN_NODE_DATA_PATH
finally:
shutil.rmtree(SCHAIN_NODE_DATA_PATH)
File renamed without changes.
File renamed without changes.
34 changes: 34 additions & 0 deletions tests/core/core_schains_test.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import os
import datetime

import freezegun

from node_cli.core.schains import toggle_schain_repair_mode
from node_cli.utils.helper import read_json


CURRENT_TIMESTAMP = 1594903080
CURRENT_DATETIME = datetime.datetime.utcfromtimestamp(CURRENT_TIMESTAMP)


@freezegun.freeze_time(CURRENT_DATETIME)
def test_toggle_repair_mode(tmp_schains_dir):
schain_name = "test_schain"
schain_folder = os.path.join(tmp_schains_dir, schain_name)
os.mkdir(schain_folder)
toggle_schain_repair_mode(schain_name)
schain_status_path = os.path.join(schain_folder, "node-cli.status")
assert os.path.isfile(schain_status_path)

assert read_json(schain_status_path) == {
'repair_ts': CURRENT_TIMESTAMP,
'schain_name': 'test_schain',
}

toggle_schain_repair_mode(schain_name, snapshot_from='127.0.0.1')

assert read_json(schain_status_path) == {
'repair_ts': CURRENT_TIMESTAMP,
'schain_name': 'test_schain',
'snapshot_from': '127.0.0.1',
}
95 changes: 0 additions & 95 deletions tests/core_ssl_test.py

This file was deleted.

0 comments on commit 6908f68

Please sign in to comment.