Skip to content

Commit 05892bf

Browse files
Merge pull request #953 from skalenetwork/fix-regression-update
Add missing locking
2 parents ce61b71 + eddc74a commit 05892bf

30 files changed

+559
-335
lines changed

VERSION

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
2.5.0
1+
2.5.1

core/schains/checks.py

Lines changed: 47 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -17,30 +17,28 @@
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 filecmp
2120
import os
22-
import time
2321
import logging
22+
import time
2423
from abc import ABC, abstractmethod
25-
from typing import Any, Dict
24+
from typing import Any, Dict, Optional
2625

2726
from core.schains.config.directory import (
28-
upstreams_for_rotation_id_version,
27+
config_synced_with_upstream,
2928
get_schain_check_filepath,
3029
get_schain_config,
30+
get_upstream_config_filepath,
3131
schain_config_dir,
32-
schain_config_filepath
32+
schain_config_filepath,
33+
upstreams_for_rotation_id_version,
3334
)
3435
from core.schains.config.helper import (
3536
get_base_port_from_config,
3637
get_node_ips_from_config,
3738
get_own_ip_from_config,
3839
get_local_schain_http_endpoint
3940
)
40-
from core.schains.config.main import (
41-
get_upstream_config_filepath,
42-
get_rotation_ids_from_config_file
43-
)
41+
from core.schains.config.main import get_config_rotations_ids, get_upstream_rotation_ids
4442
from core.schains.dkg.utils import get_secret_key_share_filepath
4543
from core.schains.firewall.types import IRuleController
4644
from core.schains.process_manager_helper import is_monitor_process_alive
@@ -49,6 +47,7 @@
4947
check_endpoint_blocks,
5048
get_endpoint_alive_check_timeout
5149
)
50+
from core.schains.external_config import ExternalConfig, ExternalState
5251
from core.schains.runner import get_container_name
5352
from core.schains.skaled_exit_codes import SkaledExitCodes
5453

@@ -113,13 +112,17 @@ def __init__(
113112
node_id: int,
114113
schain_record: SChainRecord,
115114
rotation_id: int,
116-
stream_version: str
115+
stream_version: str,
116+
estate: ExternalState,
117+
econfig: Optional[ExternalConfig] = None
117118
):
118119
self.name = schain_name
119120
self.node_id = node_id
120121
self.schain_record = schain_record
121122
self.rotation_id = rotation_id
122123
self.stream_version = stream_version
124+
self.estate = estate
125+
self.econfig = econfig or ExternalConfig(schain_name)
123126

124127
def get_all(self, log=True, save=False, checks_filter=None) -> Dict:
125128
if checks_filter:
@@ -161,11 +164,18 @@ def upstream_config(self) -> CheckRes:
161164
self.stream_version
162165
)
163166
logger.debug('Upstream configs for %s: %s', self.name, upstreams)
164-
return len(upstreams) > 0 and self.schain_record.config_version == self.stream_version
167+
return CheckRes(
168+
len(upstreams) > 0 and self.schain_record.config_version == self.stream_version
169+
)
165170

166-
def is_healthy(self) -> bool:
167-
checks = self.get_all()
168-
return False not in checks.values()
171+
@property
172+
def external_state(self) -> CheckRes:
173+
actual_state = self.econfig.get()
174+
logger.debug(
175+
'Checking external config. Current %s. Saved %s',
176+
self.estate, actual_state
177+
)
178+
return CheckRes(self.econfig.synced(self.estate))
169179

170180

171181
class SkaledChecks(IChecks):
@@ -175,14 +185,14 @@ def __init__(
175185
schain_record: SChainRecord,
176186
rule_controller: IRuleController,
177187
*,
178-
ima_linked: bool = True,
188+
econfig: Optional[ExternalConfig] = None,
179189
dutils: DockerUtils = None
180190
):
181191
self.name = schain_name
182192
self.schain_record = schain_record
183193
self.dutils = dutils or DockerUtils()
184194
self.container_name = get_container_name(SCHAIN_CONTAINER, self.name)
185-
self.ima_linked = ima_linked
195+
self.econfig = econfig or ExternalConfig(name=schain_name)
186196
self.rc = rule_controller
187197

188198
def get_all(self, log=True, save=False, checks_filter=None) -> Dict:
@@ -210,27 +220,20 @@ def upstream_exists(self) -> CheckRes:
210220
def rotation_id_updated(self) -> int:
211221
if not self.config:
212222
return CheckRes(False)
213-
upstream_path = get_upstream_config_filepath(self.name)
214-
config_path = schain_config_filepath(self.name)
215-
upstream_rotations = get_rotation_ids_from_config_file(upstream_path)
216-
config_rotations = get_rotation_ids_from_config_file(config_path)
223+
upstream_rotations = get_upstream_rotation_ids(self.name)
224+
config_rotations = get_config_rotations_ids(self.name)
217225
logger.debug(
218-
'Comparing rotation_ids between upstream %s and %s',
219-
upstream_path,
220-
config_path
226+
'Comparing rotation_ids. Upstream: %s. Config: %s',
227+
upstream_rotations,
228+
config_rotations
221229
)
222230
return CheckRes(upstream_rotations == config_rotations)
223231

224232
@property
225233
def config_updated(self) -> CheckRes:
226234
if not self.config:
227235
return CheckRes(False)
228-
upstream_path = get_upstream_config_filepath(self.name)
229-
config_path = schain_config_filepath(self.name)
230-
logger.debug('Checking if %s updated according to %s', config_path, upstream_path)
231-
if not upstream_path:
232-
return CheckRes(True)
233-
return CheckRes(filecmp.cmp(upstream_path, config_path))
236+
return CheckRes(config_synced_with_upstream(self.name))
234237

235238
@property
236239
def config(self) -> CheckRes:
@@ -251,12 +254,14 @@ def firewall_rules(self) -> CheckRes:
251254
base_port = get_base_port_from_config(conf)
252255
node_ips = get_node_ips_from_config(conf)
253256
own_ip = get_own_ip_from_config(conf)
257+
ranges = self.econfig.ranges
254258
self.rc.configure(
255259
base_port=base_port,
256260
own_ip=own_ip,
257-
node_ips=node_ips
261+
node_ips=node_ips,
262+
sync_ip_ranges=ranges
258263
)
259-
logger.info(f'Rule controller {self.rc.expected_rules()}')
264+
logger.debug(f'Rule controller {self.rc.expected_rules()}')
260265
return CheckRes(self.rc.is_rules_synced())
261266
return CheckRes(False)
262267

@@ -277,6 +282,8 @@ def exit_code_ok(self) -> CheckRes:
277282
@property
278283
def ima_container(self) -> CheckRes:
279284
"""Checks that IMA container is running"""
285+
if not self.econfig.ima_linked:
286+
return CheckRes(True)
280287
name = get_container_name(IMA_CONTAINER, self.name)
281288
return CheckRes(self.dutils.is_container_running(name))
282289

@@ -314,9 +321,10 @@ def __init__(
314321
schain_record: SChainRecord,
315322
rule_controller: IRuleController,
316323
stream_version: str,
324+
estate: ExternalState,
317325
rotation_id: int = 0,
318326
*,
319-
ima_linked: bool = True,
327+
econfig: Optional[ExternalConfig] = None,
320328
dutils: DockerUtils = None
321329
):
322330
self._subjects = [
@@ -325,13 +333,15 @@ def __init__(
325333
node_id=node_id,
326334
schain_record=schain_record,
327335
rotation_id=rotation_id,
328-
stream_version=stream_version
336+
stream_version=stream_version,
337+
estate=estate,
338+
econfig=econfig
329339
),
330340
SkaledChecks(
331341
schain_name=schain_name,
332342
schain_record=schain_record,
333343
rule_controller=rule_controller,
334-
ima_linked=ima_linked,
344+
econfig=econfig,
335345
dutils=dutils
336346
)
337347
]
@@ -354,17 +364,16 @@ def get_all(self, log=True, save=False, checks_filter=None):
354364
checks_filter=checks_filter
355365
)
356366
plain_checks.update(subj_checks)
367+
if not self.estate.ima_linked:
368+
if 'ima_container' in plain_checks:
369+
del plain_checks['ima_container']
357370

358371
if log:
359372
log_checks_dict(self.name, plain_checks)
360373
if save:
361374
save_checks_dict(self.name, plain_checks)
362375
return plain_checks
363376

364-
def is_healthy(self):
365-
checks = self.get_all()
366-
return False not in checks.values()
367-
368377

369378
def save_checks_dict(schain_name, checks_dict):
370379
schain_check_path = get_schain_check_filepath(schain_name)

core/schains/cleaner.py

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@
3737
)
3838
from core.schains.process_manager_helper import terminate_schain_process
3939
from core.schains.runner import get_container_name, is_exited
40+
from core.schains.external_config import ExternalConfig
4041
from core.schains.types import ContainerType
4142
from core.schains.firewall.utils import get_sync_agent_ranges
4243

@@ -45,7 +46,6 @@
4546
from tools.configs.containers import (
4647
SCHAIN_CONTAINER, IMA_CONTAINER, SCHAIN_STOP_TIMEOUT
4748
)
48-
from tools.configs.ima import DISABLE_IMA
4949
from tools.docker_utils import DockerUtils
5050
from tools.helper import merged_unique, read_json, is_node_part_of_chain
5151
from tools.sgx_utils import SGX_SERVER_URL
@@ -205,16 +205,25 @@ def remove_schain(skale, node_id, schain_name, msg, dutils=None) -> None:
205205
sync_agent_ranges = get_sync_agent_ranges(skale)
206206
rotation_data = skale.node_rotation.get_rotation(schain_name)
207207
rotation_id = rotation_data['rotation_id']
208+
estate = ExternalConfig(name=schain_name).get()
208209
cleanup_schain(
209210
node_id,
210211
schain_name,
211212
sync_agent_ranges,
212213
rotation_id=rotation_id,
214+
estate=estate,
213215
dutils=dutils
214216
)
215217

216218

217-
def cleanup_schain(node_id, schain_name, sync_agent_ranges, rotation_id, dutils=None) -> None:
219+
def cleanup_schain(
220+
node_id,
221+
schain_name,
222+
sync_agent_ranges,
223+
rotation_id,
224+
estate,
225+
dutils=None
226+
) -> None:
218227
dutils = dutils or DockerUtils()
219228
schain_record = upsert_schain_record(schain_name)
220229

@@ -229,7 +238,8 @@ def cleanup_schain(node_id, schain_name, sync_agent_ranges, rotation_id, dutils=
229238
rule_controller=rc,
230239
stream_version=stream_version,
231240
schain_record=schain_record,
232-
rotation_id=rotation_id
241+
rotation_id=rotation_id,
242+
estate=estate
233243
)
234244
if checks.skaled_container.status or is_exited(
235245
schain_name,
@@ -246,7 +256,7 @@ def cleanup_schain(node_id, schain_name, sync_agent_ranges, rotation_id, dutils=
246256
node_ips = get_node_ips_from_config(conf)
247257
rc.configure(base_port=base_port, own_ip=own_ip, node_ips=node_ips)
248258
rc.cleanup()
249-
if not DISABLE_IMA:
259+
if estate.ima_linked:
250260
if checks.ima_container.status or is_exited(
251261
schain_name,
252262
container_type=ContainerType.ima,

core/schains/cmd.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,16 +28,16 @@
2828

2929
def get_schain_container_cmd(
3030
schain_name: str,
31-
public_key: str = None,
3231
start_ts: int = None,
32+
download_snapshot: bool = False,
3333
enable_ssl: bool = True,
3434
snapshot_from: str = ''
3535
) -> str:
3636
"""Returns parameters that will be passed to skaled binary in the sChain container"""
3737
opts = get_schain_container_base_opts(schain_name, enable_ssl=enable_ssl)
3838
if snapshot_from:
3939
opts.extend(['--no-snapshot-majority', snapshot_from])
40-
if public_key:
40+
if download_snapshot:
4141
sync_opts = get_schain_container_sync_opts(start_ts)
4242
opts.extend(sync_opts)
4343
return ' '.join(opts)

0 commit comments

Comments
 (0)