Skip to content

Commit a1cbcdd

Browse files
committed
add type hints to pcs.lib.corosync
1 parent efc9e5f commit a1cbcdd

30 files changed

+1572
-1252
lines changed

mypy.ini

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -178,6 +178,14 @@ disallow_untyped_defs = True
178178
[mypy-pcs.lib.commands.tag]
179179
disallow_untyped_defs = True
180180

181+
[mypy-pcs.lib.corosync.*]
182+
disallow_untyped_defs = True
183+
disallow_untyped_calls = True
184+
185+
[mypy-pcs.lib.corosync.qdevice_net]
186+
disallow_untyped_defs = True
187+
disallow_untyped_calls = False
188+
181189
[mypy-pcs.lib.dr.*]
182190
disallow_untyped_defs = True
183191
disallow_untyped_calls = True
@@ -197,6 +205,10 @@ disallow_untyped_calls = True
197205
[mypy-pcs.lib.services]
198206
disallow_untyped_defs = True
199207

208+
[mypy-pcs.lib.tools]
209+
disallow_untyped_defs = True
210+
disallow_untyped_calls = True
211+
200212
[mypy-pcs.lib.validate]
201213
disallow_untyped_defs = True
202214
disallow_untyped_calls = True

pcs/cli/reports/messages.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -189,8 +189,7 @@ class NodeUsedAsTieBreaker(CliReportMessageCustom):
189189
@property
190190
def message(self) -> str:
191191
return (
192-
f"Node '{self._obj.node}' with id '{self._obj.node_id}' is used as "
193-
"a tie breaker for a qdevice, run 'pcs quorum device update model "
192+
self._obj.message + ", run 'pcs quorum device update model "
194193
"tie_breaker=<node id>' to change it"
195194
)
196195

pcs/cluster.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -495,7 +495,7 @@ def stop_cluster_nodes(nodes):
495495
error_list.append(node + ": " + data)
496496
continue
497497
try:
498-
quorum_status = corosync_live.QuorumStatus.from_string(data)
498+
quorum_status = corosync_live.parse_quorum_status(data)
499499
if not quorum_status.is_quorate:
500500
# Get quorum status from a quorate node, non-quorate nodes
501501
# may provide inaccurate info. If no node is quorate, there
@@ -688,7 +688,7 @@ def stop_cluster(argv):
688688
# - retval is 2 on success if a node is not in a partition with quorum
689689
output, dummy_retval = utils.run(["corosync-quorumtool", "-p", "-s"])
690690
try:
691-
if corosync_live.QuorumStatus.from_string(
691+
if corosync_live.parse_quorum_status(
692692
output
693693
).stopping_local_node_cause_quorum_loss():
694694
utils.err(

pcs/common/corosync_conf.py

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,29 @@
11
from dataclasses import dataclass
22
from typing import (
3-
List,
43
Mapping,
54
Optional,
5+
Sequence,
66
)
77

88
from pcs.common.interface.dto import DataTransferObject
9-
from pcs.common.types import CorosyncTransportType
9+
from pcs.common.types import (
10+
CorosyncNodeAddressType,
11+
CorosyncTransportType,
12+
)
1013

1114

1215
@dataclass(frozen=True)
1316
class CorosyncNodeAddressDto(DataTransferObject):
1417
addr: str
1518
link: str
16-
type: str # TODO: create enum of addr types in pcs.lib.corosync.node
19+
type: CorosyncNodeAddressType
1720

1821

1922
@dataclass(frozen=True)
2023
class CorosyncNodeDto(DataTransferObject):
2124
name: str
2225
nodeid: str
23-
addrs: List[CorosyncNodeAddressDto]
26+
addrs: Sequence[CorosyncNodeAddressDto]
2427

2528

2629
@dataclass(frozen=True)
@@ -41,7 +44,7 @@ class CorosyncConfDto(DataTransferObject):
4144
transport_options: Mapping[str, str]
4245
compression_options: Mapping[str, str]
4346
crypto_options: Mapping[str, str]
44-
nodes: List[CorosyncNodeDto]
47+
nodes: Sequence[CorosyncNodeDto]
4548
links_options: Mapping[str, Mapping[str, str]]
4649
quorum_options: Mapping[str, str]
4750
quorum_device: Optional[CorosyncQuorumDeviceSettingsDto]

pcs/common/reports/__init__.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
ReportItem,
1212
ReportItemContext,
1313
ReportItemList,
14+
ReportItemMessage,
1415
ReportItemSeverity,
1516
get_severity,
1617
)

pcs/common/reports/codes.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -170,6 +170,7 @@
170170
"COROSYNC_CONFIG_DISTRIBUTION_NODE_ERROR"
171171
)
172172
COROSYNC_CONFIG_DISTRIBUTION_STARTED = M("COROSYNC_CONFIG_DISTRIBUTION_STARTED")
173+
COROSYNC_CONFIG_MISSING_IDS_OF_NODES = M("COROSYNC_CONFIG_MISSING_IDS_OF_NODES")
173174
COROSYNC_CONFIG_MISSING_NAMES_OF_NODES = M(
174175
"COROSYNC_CONFIG_MISSING_NAMES_OF_NODES"
175176
)

pcs/common/reports/messages.py

Lines changed: 26 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1723,7 +1723,26 @@ def message(self) -> str:
17231723
note = (
17241724
"unable to continue" if self.fatal else "those nodes were omitted"
17251725
)
1726-
return f"Some nodes are missing names in corosync.conf, {note}"
1726+
return (
1727+
f"Some nodes are missing names in corosync.conf, {note}. "
1728+
"Edit corosync.conf and make sure all nodes have their name set."
1729+
)
1730+
1731+
1732+
@dataclass(frozen=True)
1733+
class CorosyncConfigMissingIdsOfNodes(ReportItemMessage):
1734+
"""
1735+
Some nodes in corosync.conf do not have their id set
1736+
"""
1737+
1738+
_code = codes.COROSYNC_CONFIG_MISSING_IDS_OF_NODES
1739+
1740+
@property
1741+
def message(self) -> str:
1742+
return (
1743+
"Some nodes are missing IDs in corosync.conf. "
1744+
"Edit corosync.conf and make sure all nodes have their nodeid set."
1745+
)
17271746

17281747

17291748
@dataclass(frozen=True)
@@ -1799,7 +1818,7 @@ class CorosyncBadNodeAddressesCount(ReportItemMessage):
17991818
actual_count: int
18001819
min_count: int
18011820
max_count: int
1802-
node_name: str = ""
1821+
node_name: Optional[str] = None
18031822
node_index: Optional[int] = None
18041823
_code = codes.COROSYNC_BAD_NODE_ADDRESSES_COUNT
18051824

@@ -1865,7 +1884,7 @@ class CorosyncAddressIpVersionWrongForLink(ReportItemMessage):
18651884

18661885
address: str
18671886
expected_address_type: str
1868-
link_number: Optional[int] = None
1887+
link_number: Optional[Union[int, str]] = None
18691888
_code = codes.COROSYNC_ADDRESS_IP_VERSION_WRONG_FOR_LINK
18701889

18711890
@property
@@ -2193,7 +2212,7 @@ class CorosyncLinkDoesNotExistCannotUpdate(ReportItemMessage):
21932212
existing_link_list -- linknumbers of existing links
21942213
"""
21952214

2196-
link_number: int
2215+
link_number: Union[int, str]
21972216
existing_link_list: List[str]
21982217
_code = codes.COROSYNC_LINK_DOES_NOT_EXIST_CANNOT_UPDATE
21992218

@@ -5463,15 +5482,15 @@ class NodeUsedAsTieBreaker(ReportItemMessage):
54635482
node_id -- node id
54645483
"""
54655484

5466-
node: str
5467-
node_id: int
5485+
node: Optional[str]
5486+
node_id: Union[None, str, int]
54685487
_code = codes.NODE_USED_AS_TIE_BREAKER
54695488

54705489
@property
54715490
def message(self) -> str:
54725491
return (
54735492
f"Node '{self.node}' with id '{self.node_id}' is used as a tie "
5474-
"breaker for a qdevice"
5493+
"breaker for a qdevice and therefore cannot be removed"
54755494
)
54765495

54775496

pcs/common/types.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,3 +67,10 @@ def from_str(cls, transport: str) -> "CorosyncTransportType":
6767
return cls(transport.upper())
6868
except ValueError:
6969
raise UnknownCorosyncTransportTypeException(transport) from None
70+
71+
72+
class CorosyncNodeAddressType(AutoNameEnum):
73+
IPV4 = "IPv4"
74+
IPV6 = "IPv6"
75+
FQDN = "FQDN"
76+
UNRESOLVABLE = "unresolvable"

pcs/lib/commands/cluster.py

Lines changed: 46 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -800,13 +800,12 @@ def get_corosync_conf_struct(env: LibraryEnvironment) -> CorosyncConfDto:
800800
quorum_device_dto: Optional[CorosyncQuorumDeviceSettingsDto] = None
801801
if corosync_conf.has_quorum_device():
802802
(
803-
qd_model,
804803
qd_model_options,
805804
qd_generic_options,
806805
qd_heuristics_options,
807806
) = corosync_conf.get_quorum_device_settings()
808807
quorum_device_dto = CorosyncQuorumDeviceSettingsDto(
809-
model=qd_model,
808+
model=corosync_conf.get_quorum_device_model(),
810809
model_options=qd_model_options,
811810
generic_options=qd_generic_options,
812811
heuristics_options=qd_heuristics_options,
@@ -911,26 +910,28 @@ def add_nodes(
911910
skip_non_existing=skip_offline_nodes,
912911
)
913912
report_processor.report_list(target_report_list)
914-
# get a target for qnetd if needed
915-
(
916-
qdevice_model,
917-
qdevice_model_options,
918-
_,
919-
_,
920-
) = corosync_conf.get_quorum_device_settings()
921-
if qdevice_model == "net":
922-
try:
923-
qnetd_target = target_factory.get_target(
924-
qdevice_model_options["host"]
925-
)
926-
except HostNotFound:
927-
report_processor.report(
928-
ReportItem.error(
929-
reports.messages.HostNotFound(
930-
[qdevice_model_options["host"]]
913+
914+
if corosync_conf.has_quorum_device():
915+
# get a target for qnetd if needed
916+
qdevice_model = corosync_conf.get_quorum_device_model()
917+
if qdevice_model == "net":
918+
(
919+
qdevice_model_options,
920+
_,
921+
_,
922+
) = corosync_conf.get_quorum_device_settings()
923+
try:
924+
qnetd_target = target_factory.get_target(
925+
qdevice_model_options["host"]
926+
)
927+
except HostNotFound:
928+
report_processor.report(
929+
ReportItem.error(
930+
reports.messages.HostNotFound(
931+
[qdevice_model_options["host"]]
932+
)
931933
)
932934
)
933-
)
934935

935936
# Get targets for new nodes and report unknown (== not-authorized) nodes.
936937
# If a node doesn't contain the 'name' key, validation of inputs reports it.
@@ -1156,19 +1157,21 @@ def add_nodes(
11561157
run_and_raise(env.get_node_communicator(), com_cmd)
11571158

11581159
# qdevice setup
1159-
if qdevice_model == "net":
1160-
qdevice_net.set_up_client_certificates(
1161-
env.cmd_runner(),
1162-
env.report_processor,
1163-
env.communicator_factory,
1164-
qnetd_target,
1165-
corosync_conf.get_cluster_name(),
1166-
new_nodes_target_list,
1167-
# we don't want to allow skipping offline nodes which are being
1168-
# added, otherwise qdevice will not work properly
1169-
skip_offline_nodes=False,
1170-
allow_skip_offline=False,
1171-
)
1160+
if corosync_conf.has_quorum_device():
1161+
qdevice_model = corosync_conf.get_quorum_device_model()
1162+
if qdevice_model == "net":
1163+
qdevice_net.set_up_client_certificates(
1164+
env.cmd_runner(),
1165+
env.report_processor,
1166+
env.communicator_factory,
1167+
qnetd_target,
1168+
corosync_conf.get_cluster_name(),
1169+
new_nodes_target_list,
1170+
# we don't want to allow skipping offline nodes which are being
1171+
# added, otherwise qdevice will not work properly
1172+
skip_offline_nodes=False,
1173+
allow_skip_offline=False,
1174+
)
11721175

11731176
# sbd setup
11741177
if is_sbd_enabled:
@@ -1744,7 +1747,16 @@ def remove_nodes(
17441747
config_validators.remove_nodes(
17451748
node_list,
17461749
corosync_conf.get_nodes(),
1747-
corosync_conf.get_quorum_device_settings(),
1750+
(
1751+
corosync_conf.get_quorum_device_model()
1752+
if corosync_conf.has_quorum_device()
1753+
else None
1754+
),
1755+
(
1756+
corosync_conf.get_quorum_device_settings()
1757+
if corosync_conf.has_quorum_device()
1758+
else ({}, {}, {})
1759+
),
17481760
)
17491761
)
17501762
if report_processor.has_errors:

pcs/lib/commands/quorum.py

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
from pcs.lib.node import get_existing_nodes_names
1919

2020

21-
def get_config(lib_env):
21+
def get_config(lib_env: LibraryEnvironment):
2222
"""
2323
Extract and return quorum configuration from corosync.conf
2424
lib_env LibraryEnvironment
@@ -27,13 +27,12 @@ def get_config(lib_env):
2727
device = None
2828
if cfg.has_quorum_device():
2929
(
30-
model,
3130
model_options,
3231
generic_options,
3332
heuristics_options,
3433
) = cfg.get_quorum_device_settings()
3534
device = {
36-
"model": model,
35+
"model": cfg.get_quorum_device_model(),
3736
"model_options": model_options,
3837
"generic_options": generic_options,
3938
"heuristics_options": heuristics_options,
@@ -171,7 +170,7 @@ def add_device(
171170
model_options,
172171
generic_options,
173172
heuristics_options,
174-
[node.nodeid for node in cfg.get_nodes()],
173+
[node.nodeid for node in cfg.get_nodes() if node.nodeid],
175174
force_model=force_model,
176175
force_options=force_options,
177176
)
@@ -289,7 +288,7 @@ def update_device(
289288
model_options,
290289
generic_options,
291290
heuristics_options,
292-
[node.nodeid for node in cfg.get_nodes()],
291+
[node.nodeid for node in cfg.get_nodes() if node.nodeid],
293292
force_options=force_options,
294293
)
295294
).has_errors:

pcs/lib/communication/cluster.py

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -103,9 +103,7 @@ def _process_response(self, response):
103103
# corosync is not running on the node, this is OK
104104
return self._get_next_list()
105105
try:
106-
quorum_status = corosync_live.QuorumStatus.from_string(
107-
response.data
108-
)
106+
quorum_status = corosync_live.parse_quorum_status(response.data)
109107
if not quorum_status.is_quorate:
110108
return self._get_next_list()
111109
self._quorum_status = quorum_status

0 commit comments

Comments
 (0)