Skip to content

Commit

Permalink
Update lvmpy. Fix is update safe
Browse files Browse the repository at this point in the history
  • Loading branch information
badrogger committed Dec 30, 2024
1 parent ab876ff commit 151e571
Show file tree
Hide file tree
Showing 5 changed files with 65 additions and 22 deletions.
2 changes: 1 addition & 1 deletion lvmpy
7 changes: 6 additions & 1 deletion node_cli/core/node.py
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@
from node_cli.utils.texts import Texts
from node_cli.utils.exit_codes import CLIExitCodes
from node_cli.utils.decorators import check_not_inited, check_inited, check_user
from node_cli.utils.docker_utils import is_admin_running, is_api_running, is_sync_admin_running
from node_cli.migrations.focal_to_jammy import migrate as migrate_2_6


Expand All @@ -96,7 +97,11 @@ class NodeStatuses(Enum):
NOT_CREATED = 5


def is_update_safe() -> bool:
def is_update_safe(sync_node: bool = False) -> bool:
if not sync_node and (not is_admin_running() or not is_api_running()):
return True
if sync_node and not is_sync_admin_running():
return True
status, payload = get_request(BLUEPRINT_NAME, 'update-safe')
if status == 'error':
return False
Expand Down
21 changes: 21 additions & 0 deletions node_cli/utils/docker_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -335,6 +335,27 @@ def cleanup_unused_images(dclient=None, ignore=None):
)


def is_container_running(name: str, dclient: Optional[DockerClient] = None) -> bool:
dc = dclient or docker_client()
try:
container = dc.containers.get(name)
return container.status == 'running'
except docker.errors.NotFound:
return False


def is_admin_running(dclient: Optional[DockerClient] = None) -> bool:
return is_container_running(name='skale_admin', dclient=dclient)


def is_api_running(dclient: Optional[DockerClient] = None) -> bool:
return is_container_running(name='skale_api', dclient=dclient)


def is_sync_admin_running(dclient: Optional[DockerClient] = None) -> bool:
return is_container_running(name='skale_sync_admin', dclient=dclient)


def system_prune():
logger.info('Removing dangling docker artifacts')
cmd = ['docker', 'system', 'prune', '-f']
Expand Down
29 changes: 14 additions & 15 deletions tests/cli/node_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,6 @@
response_mock,
run_command,
run_command_mock,
safe_update_api_response,
subprocess_run_mock,
)
from tests.resources_test import BIG_DISK_SIZE
Expand Down Expand Up @@ -368,28 +367,28 @@ def test_turn_off_maintenance_on(mocked_g_config):
with mock.patch('subprocess.run', new=subprocess_run_mock), mock.patch(
'node_cli.core.node.turn_off_op'
), mock.patch('node_cli.utils.decorators.is_node_inited', return_value=True):
result = run_command_mock(
'node_cli.utils.helper.requests.post',
resp_mock,
_turn_off,
['--maintenance-on', '--yes'],
)
assert (
result.output
== 'Setting maintenance mode on...\nNode is successfully set in maintenance mode\n'
) # noqa
assert result.exit_code == 0
with mock.patch(
'node_cli.utils.helper.requests.get', return_value=safe_update_api_response()
'node_cli.utils.docker_utils.is_container_running', return_value=True
):
result = run_command_mock(
'node_cli.utils.helper.requests.post',
resp_mock,
_turn_off,
['--maintenance-on', '--yes'],
)
assert (
result.output
== 'Setting maintenance mode on...\nNode is successfully set in maintenance mode\n'
) # noqa
assert result.exit_code == 0
result = run_command_mock(
'node_cli.utils.helper.requests.post',
resp_mock,
_turn_off,
['--maintenance-on', '--yes'],
)
assert 'Cannot turn off safely' in result.output
assert result.exit_code == CLIExitCodes.UNSAFE_UPDATE
assert 'Cannot turn off safely' in result.output
assert result.exit_code == CLIExitCodes.UNSAFE_UPDATE


def test_turn_on_maintenance_off(mocked_g_config):
Expand Down
28 changes: 23 additions & 5 deletions tests/core/core_node_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -181,14 +181,32 @@ def test_update_node(mocked_g_config, resource_file):


def test_is_update_safe():
assert not is_update_safe()
assert is_update_safe()
assert is_update_safe(sync_node=True)

with mock.patch('node_cli.core.node.is_admin_running', return_value=True):
with mock.patch('node_cli.core.node.is_api_running', return_value=True):
assert not is_update_safe()
assert is_update_safe(sync_node=True)

with mock.patch('node_cli.core.node.is_sync_admin_running', return_value=True):
assert is_update_safe()
assert not is_update_safe(sync_node=True)

with mock.patch('node_cli.utils.docker_utils.is_container_running', return_value=True):
with mock.patch(
'node_cli.utils.helper.requests.get', return_value=safe_update_api_response()
):
assert is_update_safe()

with mock.patch('node_cli.utils.helper.requests.get', return_value=safe_update_api_response()):
assert is_update_safe()

with mock.patch(
'node_cli.utils.helper.requests.get', return_value=safe_update_api_response(safe=False)
):
assert not is_update_safe()
with mock.patch('node_cli.utils.docker_utils.is_container_running', return_value=True):
with mock.patch(
'node_cli.utils.helper.requests.get', return_value=safe_update_api_response(safe=False)
):
assert not is_update_safe()


def test_repair_sync(tmp_sync_datadir, mocked_g_config, resource_file):
Expand Down

0 comments on commit 151e571

Please sign in to comment.