Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add --snapshot-from to skale sync-node init #787

Merged
merged 2 commits into from
Oct 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 13 additions & 3 deletions node_cli/cli/sync_node.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,17 @@
# You should have received a copy of the GNU Affero General Public License
# along with this program. If not, see <https://www.gnu.org/licenses/>.

from typing import Optional

import click

from node_cli.core.node import init_sync, update_sync
from node_cli.utils.helper import (
abort_if_false,
error_exit,
IP_TYPE,
safe_load_texts,
streamed_cmd,
error_exit
)
from node_cli.utils.exit_codes import CLIExitCodes

Expand Down Expand Up @@ -60,14 +63,21 @@ def sync_node():
help=TEXTS['init']['historic_state'],
is_flag=True
)
@click.option(
'--snapshot-from',
type=IP_TYPE,
default=None,
hidden=True,
help='Ip of the node from to download snapshot from'
)
@streamed_cmd
def _init_sync(env_file, archive, catchup, historic_state):
def _init_sync(env_file, archive, catchup, historic_state, snapshot_from: Optional[str]):
if historic_state and not archive:
error_exit(
'--historic-state can be used only is combination with --archive',
exit_code=CLIExitCodes.FAILURE
)
init_sync(env_file, archive, catchup, historic_state)
init_sync(env_file, archive, catchup, historic_state, snapshot_from)


@sync_node.command('update', help='Update sync node from .env file')
Expand Down
9 changes: 4 additions & 5 deletions node_cli/core/node.py
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,8 @@ def init_sync(
env_filepath: str,
archive: bool,
catchup: bool,
historic_state: bool
historic_state: bool,
snapshot_from: str
) -> None:
configure_firewall_rules()
env = get_node_env(env_filepath, sync_node=True)
Expand All @@ -197,7 +198,8 @@ def init_sync(
env,
archive,
catchup,
historic_state
historic_state,
snapshot_from
)
if not inited_ok:
error_exit(
Expand All @@ -217,9 +219,6 @@ def init_sync(
@check_inited
@check_user
def update_sync(env_filepath: str, unsafe_ok: bool = False) -> None:
if not unsafe_ok and not is_update_safe():
error_msg = 'Cannot update safely'
error_exit(error_msg, exit_code=CLIExitCodes.UNSAFE_UPDATE)
logger.info('Node update started')
configure_firewall_rules()
env = get_node_env(env_filepath, sync_node=True)
Expand Down
1 change: 1 addition & 0 deletions node_cli/core/schains.py
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,7 @@ def get_node_cli_schain_status_filepath(schain_name: str) -> str:

def update_node_cli_schain_status(schain_name: str, status: dict) -> None:
path = get_node_cli_schain_status_filepath(schain_name)
os.makedirs(os.path.dirname(path), exist_ok=True)
save_json(path, status)


Expand Down
11 changes: 9 additions & 2 deletions node_cli/operations/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
import distro
import functools
import logging
from typing import Dict
from typing import Dict, Optional

from node_cli.cli.info import VERSION
from node_cli.configs import CONTAINER_CONFIG_PATH, CONTAINER_CONFIG_TMP_PATH
Expand All @@ -47,6 +47,7 @@
from node_cli.operations.skale_node import download_skale_node, sync_skale_node, update_images
from node_cli.core.checks import CheckType, run_checks as run_host_checks
from node_cli.core.iptables import configure_iptables
from node_cli.core.schains import update_node_cli_schain_status
from node_cli.utils.docker_utils import (
compose_rm,
compose_up,
Expand Down Expand Up @@ -184,7 +185,8 @@ def init_sync(
env: dict,
archive: bool,
catchup: bool,
historic_state: bool
historic_state: bool,
snapshot_from: Optional[str]
) -> bool:
cleanup_volume_artifacts(env['DISK_MOUNTPOINT'])
download_skale_node(
Expand Down Expand Up @@ -224,6 +226,11 @@ def init_sync(
distro.version()
)
update_resource_allocation(env_type=env['ENV_TYPE'])

schain_name = env['SCHAIN_NAME']
if snapshot_from:
update_node_cli_schain_status(schain_name, {'snapshot_from': snapshot_from})

update_images(env.get('CONTAINER_CONFIGS_DIR') != '', sync_node=True)

compose_up(env, sync_node=True)
Expand Down
19 changes: 2 additions & 17 deletions tests/cli/sync_node_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,9 @@
from node_cli.configs import SKALE_DIR, NODE_DATA_PATH
from node_cli.core.node_options import NodeOptions
from node_cli.cli.sync_node import _init_sync, _update_sync
from node_cli.utils.exit_codes import CLIExitCodes
from node_cli.utils.helper import init_default_logger

from tests.helper import run_command, safe_update_api_response, subprocess_run_mock
from tests.helper import run_command, subprocess_run_mock
from tests.resources_test import BIG_DISK_SIZE

logger = logging.getLogger(__name__)
Expand Down Expand Up @@ -107,18 +106,4 @@ def test_update_sync(mocked_g_config):
'node_cli.utils.decorators.is_node_inited', return_value=True
):
result = run_command(_update_sync, ['./tests/test-env', '--yes'])
assert result.exit_code == CLIExitCodes.UNSAFE_UPDATE
assert 'Cannot update safely' in result.output

with mock.patch(
'node_cli.utils.helper.requests.get', return_value=safe_update_api_response()
):
result = run_command(_update_sync, ['./tests/test-env', '--yes'])
assert result.exit_code == 0

with mock.patch(
'node_cli.utils.helper.requests.get', return_value=safe_update_api_response(False)
):
result = run_command(_update_sync, ['./tests/test-env', '--yes'])
assert result.exit_code == CLIExitCodes.UNSAFE_UPDATE
assert 'Cannot update safely' in result.output
assert result.exit_code == 0
Loading