1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343
|
import json
from pcs import settings
from pcs_test.tools.command_env.mock_node_communicator import (
place_communication,
place_multinode_call,
)
from pcs_test.tools.misc import outdent
class HostShortcuts:
def __init__(self, calls):
self.__calls = calls
def check_reachability(
self,
node_labels=None,
communication_list=None,
name="http.host.check_reachability",
):
"""
Create a call for checking if pcsd is reachable on hosts
node_labels list -- create success responses from these nodes
communication_list list -- create custom responses
name string -- the key of this call
"""
place_multinode_call(
self.__calls,
name,
node_labels,
communication_list,
action="remote/check_auth",
output='{"success":true}',
param_list=[("check_auth_only", 1)],
)
def check_auth(
self,
node_labels=None,
communication_list=None,
name="http.host.check_auth",
):
"""
Create a call for checking authentication on hosts
node_labels list -- create success responses from these nodes
communication_list list -- create custom responses
name string -- the key of this call
"""
place_multinode_call(
self.__calls,
name,
node_labels,
communication_list,
action="remote/check_auth",
output='{"success":true}',
param_list=[("check_auth_only", 1)],
)
def get_host_info(
self,
node_labels=None,
output_data=None,
communication_list=None,
name="http.host.get_host_info",
):
"""
Create a call for getting overall info about a host
node_labels list -- create success responses from these nodes
output_data dict -- default output data which will be converted to JSON
communication_list list -- create custom responses
name string -- the key of this call
"""
place_multinode_call(
self.__calls,
name,
node_labels,
communication_list,
action="remote/check_host",
output=json.dumps(output_data) if output_data else "",
)
def cluster_destroy(
self,
node_labels=None,
communication_list=None,
name="http.host.cluster_destroy",
):
"""
Create a call for destroying a cluster on the hosts
node_labels list -- create success responses from these nodes
communication_list list -- create custom responses
name string -- the key of this call
"""
place_multinode_call(
self.__calls,
name,
node_labels,
communication_list,
action="remote/cluster_destroy",
)
def update_known_hosts(
self,
node_labels=None,
to_add=None,
to_add_hosts=None,
communication_list=None,
name="http.host.update_known_hosts",
):
"""
Create a call for updating known hosts on the hosts.
node_labels list -- create success responses from these nodes
dict to_add -- records to add:
{host_name: {dest_list: [{"addr": , "port": ,}]}}
list to_add_hosts -- constructs to_add from host names
communication_list list -- create custom responses
name string -- the key of this call
"""
if to_add_hosts and to_add:
raise AssertionError(
"Cannot specify both 'to_add_hosts' and 'to_add'"
)
if to_add_hosts:
to_add = {
name: {
"dest_list": [
{"addr": name, "port": settings.pcsd_default_port}
]
}
for name in to_add_hosts
}
add_with_token = {
name: dict(data, token=None) for name, data in to_add.items()
}
place_multinode_call(
self.__calls,
name,
node_labels,
communication_list,
action="remote/known_hosts_change",
param_list=[
(
"data_json",
json.dumps(
dict(
known_hosts_add=add_with_token,
known_hosts_remove={},
)
),
)
],
)
def send_pcsd_cert(
self,
cert,
key,
node_labels=None,
communication_list=None,
name="http.host.send_pcsd_cert",
before=None,
):
"""
Create a call for sending pcsd SSL cert and key
string cert -- pcsd SSL certificate
string key -- pcsd SSL key
node_labels list -- create success responses from these nodes
communication_list list -- create custom responses
name string -- the key of this call
"""
place_multinode_call(
self.__calls,
name,
node_labels,
communication_list,
action="remote/set_certs",
param_list=[("ssl_cert", cert), ("ssl_key", key)],
before=before,
)
def enable_cluster(
self,
node_labels=None,
communication_list=None,
name="http.host.enable_cluster",
):
"""
Create a call for enabling cluster on the nodes.
node_labels list -- create success responses from these nodes
communication_list list -- create custom responses
name string -- the key of this call
"""
place_multinode_call(
self.__calls,
name,
node_labels,
communication_list,
action="remote/cluster_enable",
)
def start_cluster(
self,
node_labels=None,
communication_list=None,
name="http.host.start_cluster",
):
"""
Create a call for starting cluster on the nodes.
node_labels list -- create success responses from these nodes
communication_list list -- create custom responses
name string -- the key of this call
"""
place_multinode_call(
self.__calls,
name,
node_labels,
communication_list,
action="remote/cluster_start",
)
def check_pacemaker_started(
self,
pacemaker_started_node_list=(),
pacemaker_not_started_node_list=(),
communication_list=None,
name="http.host.check_pacemaker_started",
):
"""
Create a call for checking pacemaker status on nodes.
pacemaker_started_node_list list -- list of node names on which
pacemaker is fully running
pacemaker_not_started_node_list list -- listof node names on which
pacemaker is not fully started yet
communication_list list -- create custom responses
name string -- the key of this call
"""
if bool(
pacemaker_started_node_list or pacemaker_not_started_node_list
) == bool(communication_list):
raise AssertionError(
"Exactly one of 'pacemaker_started_node_list and/or "
"pacemaker_not_started_node_list', 'communication_list' must "
"be specified"
)
if not communication_list:
communication_list = [
dict(
label=node,
output=json.dumps(
dict(
pending=False,
online=True,
)
),
)
for node in pacemaker_started_node_list
] + [
dict(
label=node,
output=json.dumps(
dict(
pending=True,
online=False,
)
),
)
for node in pacemaker_not_started_node_list
]
place_communication(
self.__calls,
name,
communication_list,
action="remote/pacemaker_node_status",
)
def get_quorum_status(
self,
node_list=None,
node_labels=None,
communication_list=None,
name="http.host.get_quorum_status",
):
output = ""
if node_list:
output = outdent(
"""\
Quorum information
------------------
Date: Fri Jan 16 13:03:28 2015
Quorum provider: corosync_votequorum
Nodes: {nodes_num}
Node ID: 1
Ring ID: 19860
Quorate: Yes\n
Votequorum information
----------------------
Expected votes: {nodes_num}
Highest expected: {nodes_num}
Total votes: {nodes_num}
Quorum: {quorum_num}
Flags: Quorate\n
Membership information
----------------------
Nodeid Votes Qdevice Name
{nodes}\
"""
).format(
nodes_num=len(node_list),
quorum_num=(len(node_list) // 2) + 1,
nodes="".join(
[
_quorum_status_node_fixture(node_id, node)
for node_id, node in enumerate(node_list, 1)
]
),
)
place_multinode_call(
self.__calls,
name,
node_labels,
communication_list,
action="remote/get_quorum_info",
output=output,
)
def _quorum_status_node_fixture(node_id, node_name, votes=1, is_local=False):
_local = " (local)" if is_local else ""
return (
f" {node_id} {votes} NR {node_name}{_local}\n"
)
|