Skip to content

Commit

Permalink
Merge pull request #772 from skalenetwork/fix-repair
Browse files Browse the repository at this point in the history
Fix repair
  • Loading branch information
badrogger authored Sep 24, 2024
2 parents 816dd0f + 2ddd6df commit f8e1fa0
Show file tree
Hide file tree
Showing 10 changed files with 91 additions and 140 deletions.
2 changes: 1 addition & 1 deletion node_cli/cli/__init__.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
__version__ = '2.4.1'
__version__ = '2.5.0'

if __name__ == "__main__":
print(__version__)
1 change: 1 addition & 0 deletions node_cli/configs/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@

NODE_DATA_PATH = os.path.join(SKALE_DIR, 'node_data')
SCHAIN_NODE_DATA_PATH = os.path.join(NODE_DATA_PATH, 'schains')
NODE_CLI_STATUS_FILENAME = 'node_cli.status'
NODE_CONFIG_PATH = os.path.join(NODE_DATA_PATH, 'node_config.json')
CONTAINER_CONFIG_PATH = os.path.join(SKALE_DIR, 'config')
CONTAINER_CONFIG_TMP_PATH = os.path.join(SKALE_TMP_DIR, 'config')
Expand Down
33 changes: 18 additions & 15 deletions node_cli/core/schains.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,22 +2,23 @@
import os
import pprint
import shutil
import time
from pathlib import Path

from typing import Dict, Optional

from node_cli.configs import (
ALLOCATION_FILEPATH,
NODE_CONFIG_PATH,
NODE_CLI_STATUS_FILENAME,
SCHAIN_NODE_DATA_PATH
)
from node_cli.configs.env import get_env_config

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 +28,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 +90,24 @@ 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_FILENAME)


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}
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)
ts = int(time.time())
status = {'schain_name': schain, 'repair_ts': ts}
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: 4 additions & 20 deletions tests/cli/schains_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
import requests

from node_cli.configs import G_CONF_HOME
from tests.helper import response_mock, run_command_mock
from tests.helper import response_mock, run_command, run_command_mock
from node_cli.cli.schains import (get_schain_config, ls, dkg, show_rules,
repair, info_)

Expand Down Expand Up @@ -153,30 +153,14 @@ def test_schain_rules():
assert result.output == ' IP range Port \n-----------------------------\n127.0.0.2 - 127.0.0.2 10000\n127.0.0.2 - 127.0.0.2 10001\nAll IPs 10002\nAll IPs 10003\n127.0.0.2 - 127.0.0.2 10004\n127.0.0.2 - 127.0.0.2 10005\nAll IPs 10007\nAll IPs 10008\nAll IPs 10009\n' # noqa


def test_repair():
def test_repair(tmp_schains_dir):
os.mkdir(os.path.join(tmp_schains_dir, 'test-schain'))
os.environ['TZ'] = 'Europe/London'
time.tzset()
payload = []
resp_mock = response_mock(
requests.codes.ok,
json_data={'payload': payload, 'status': 'ok'}
)
result = run_command_mock('node_cli.utils.helper.requests.post', resp_mock, repair,
['test-schain', '--yes'])
result = run_command(repair, ['test-schain', '--yes'])
assert result.output == 'Schain has been set for repair\n'
assert result.exit_code == 0

payload = ['error']
resp_mock = response_mock(
requests.codes.ok,
json_data={'payload': payload, 'status': 'error'}
)
result = run_command_mock('node_cli.utils.helper.requests.post', resp_mock, repair,
['test-schain', '--yes'])
print(repr(result.output))
assert result.exit_code == 3
assert result.output == f'Command failed with following errors:\n--------------------------------------------------\nerror\n--------------------------------------------------\nYou can find more info in {G_CONF_HOME}.skale/.skale-cli-log/debug-node-cli.log\n' # noqa


def test_info():
payload = {
Expand Down
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.
35 changes: 35 additions & 0 deletions tests/core/core_schains_test.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
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',
'snapshot_from': None,
}

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 f8e1fa0

Please sign in to comment.