-
Notifications
You must be signed in to change notification settings - Fork 45
/
Copy pathutils.py
319 lines (245 loc) · 9.55 KB
/
utils.py
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
import functools
import ipaddress
import json
import logging
import re
import os
import operator
import testinfra
import time
from typing import Optional, Dict
import requests
from requests.adapters import HTTPAdapter
from requests.packages.urllib3.util.retry import Retry
import pytest
LOGGER = logging.getLogger(__name__)
def retry(operation, times=1, wait=1, error_msg=None, name="default"):
last_assert = None
for idx in range(times):
try:
res = operation()
except AssertionError as exc:
last_assert = str(exc)
LOGGER.info("[%s] Attempt %d/%d failed: %s", name, idx, times, str(exc))
time.sleep(wait)
else:
LOGGER.info("[%s] Attempt %d/%d succeeded", name, idx, times)
return res
else:
if error_msg is None:
error_msg = (
"Failed to run operation '{name}' after {attempts} attempts "
"(waited {total}s in total)"
).format(name=name, attempts=times, total=times * wait)
if last_assert:
error_msg = error_msg + ": " + last_assert
pytest.fail(error_msg)
def write_string(host, dest, contents):
return host.run("cat > {} << EOF\n{}\nEOF".format(dest, contents))
def get_ip_from_cidr(host, cidr):
network = ipaddress.IPv4Network(cidr)
with host.sudo():
ip_info = host.check_output("ip a | grep 'inet '")
for line in ip_info.splitlines():
match = re.match(r"inet (?P<ip>[0-9]+(?:\.[0-9]+){3})/[0-9]+ ", line.strip())
assert match is not None, "Unexpected format: {}".format(line.strip())
candidate = match.group("ip")
if ipaddress.IPv4Address(candidate) in network:
return candidate
return None
def get_node_name(nodename, ssh_config=None):
"""Get a node name (from SSH config)."""
if ssh_config is not None:
node = testinfra.get_host(nodename, ssh_config=ssh_config)
return get_grain(node, "id")
return nodename
# Source: https://www.peterbe.com/plog/best-practice-with-retries-with-requests
def requests_retry_session(
retries=3,
backoff_factor=0.3,
status_forcelist=(500, 503),
method_whitelist=frozenset(["GET", "POST"]),
session=None,
):
"""Configure a `requests.session` for retry on error.
By default, this helper performs 3 retries with an exponential sleep
interval between each request and only retries internal server errors(500)
& service unavailable errors(503)
Arguments:
retries: The number of retries to perform before giving up
backoff_factor: The sleep interval between requests computed as
{backoff factor} * (2 ^ ({number retries} - 1))
status_forcelist: HTTP status codes that we should force a retry on
method_whitelist: uppercased HTTP methods that we should retry
session: Used to create a session
Returns:
A `requests.Session` object configured for retry.
"""
session = session or requests.Session()
retry = Retry(
total=retries,
read=retries,
connect=retries,
backoff_factor=backoff_factor,
status_forcelist=status_forcelist,
method_whitelist=method_whitelist,
)
adapter = HTTPAdapter(max_retries=retry)
session.mount("http://", adapter)
session.mount("https://", adapter)
return session
def kubectl_exec(host, command, pod, kubeconfig="/etc/kubernetes/admin.conf", **kwargs):
"""Grab the return code from a `kubectl exec`"""
kube_args = ["--kubeconfig", kubeconfig]
if kwargs.get("container"):
kube_args.extend(["-c", kwargs.get("container")])
if kwargs.get("namespace"):
kube_args.extend(["-n", kwargs.get("namespace")])
kubectl_cmd_tplt = "kubectl exec {} {} -- {}"
with host.sudo():
output = host.run(
kubectl_cmd_tplt.format(pod, " ".join(kube_args), " ".join(command))
)
return output
def run_salt_command(host, command, ssh_config):
"""Run a command inside the salt-master container."""
pod = "salt-master-{}".format(get_node_name("bootstrap", ssh_config))
output = kubectl_exec(
host, command, pod, container="salt-master", namespace="kube-system"
)
assert output.exit_status == 0, "command {} failed with: \nout: {}\nerr: {}".format(
command, output.stdout, output.stderr
)
return output
def get_dict_element(data, path, delimiter="."):
"""
Traverse a dict using a 'delimiter' on a target string.
getitem(a, b) returns the value of a at index b
"""
return functools.reduce(
operator.getitem,
(int(k) if k.isdigit() else k for k in path.split(delimiter)),
data,
)
def set_dict_element(data, path, value, delimiter="."):
"""
Traverse a nested dict using a delimiter on a target string
and replace the value of a key
"""
current = data
elements = [int(k) if k.isdigit() else k for k in path.split(delimiter)]
for element in elements[:-1]:
if isinstance(element, int):
current = current[element] if len(current) > element else []
else:
current = current.setdefault(element, {})
current[elements[-1]] = value
def get_grain(host, key):
with host.sudo():
output = host.check_output(
'salt-call --local --out=json grains.get "{}"'.format(key)
)
grain = json.loads(output)["local"]
return grain
def get_pillar(host, key, local=False):
with host.sudo():
output = host.check_output(
'salt-call {} --out=json pillar.get "{}"'.format(
"--local" if local else "", key
)
)
return json.loads(output)["local"]
def patch_bootstrap_config(context, host, patch):
if "bootstrap_to_restore" not in context:
with host.sudo():
cmd_ret = host.check_output("salt-call --out json --local temp.dir")
tmp_dir = json.loads(cmd_ret)["local"]
with host.sudo():
host.check_output("cp /etc/metalk8s/bootstrap.yaml {}".format(tmp_dir))
context["bootstrap_to_restore"] = os.path.join(tmp_dir, "bootstrap.yaml")
with host.sudo():
host.check_output(
"salt-call --local --retcode-passthrough state.single "
"file.serialize /etc/metalk8s/bootstrap.yaml "
"dataset='{}' "
"merge_if_exists=True".format(json.dumps(patch))
)
def negation(value):
"""Parse an optional negation after a verb (in a Gherkin feature spec)."""
if value == "":
return False
elif value in [" not", "not"]:
return True
else:
raise ValueError("Cannot parse '{}' as an optional negation".format(value))
class BaseAPIError(Exception):
"""Some error occurred when using a `BaseAPI` subclass."""
class BaseAPI:
ERROR_CLS = BaseAPIError
def __init__(self, endpoint):
self.endpoint = endpoint.rstrip("/")
self.session = requests_retry_session()
def request(self, method, route, **kwargs):
kwargs.setdefault("verify", False)
try:
response = self.session.request(
method, f"{self.endpoint}/{route.lstrip('/')}", **kwargs
)
response.raise_for_status()
except requests.exceptions.RequestException as exc:
raise self.ERROR_CLS(exc)
try:
return response.json()
except ValueError as exc:
raise self.ERROR_CLS(exc)
class PrometheusApiError(BaseAPIError):
pass
class PrometheusApi(BaseAPI):
ERROR_CLS = PrometheusApiError
def query(self, metric_name, **query_matchers):
matchers = [
'{}="{}"'.format(key, value) for key, value in query_matchers.items()
]
query_string = metric_name + "{" + ",".join(matchers) + "}"
return self.request("GET", "api/v1/query", params={"query": query_string})
def get_alerts(self, **kwargs):
return self.request("GET", "api/v1/alerts", **kwargs)
def get_rules(self, **kwargs):
return self.request("GET", "api/v1/rules", **kwargs)
def find_rules(self, name=None, group=None, labels=None, **kwargs):
if not labels:
labels = {}
rules = []
response = self.get_rules(**kwargs)
for rule_group in response.get("data", {}).get("groups", []):
group_name = rule_group.get("name")
if group in (group_name, None):
for rule in rule_group.get("rules", []):
if name in (rule.get("name"), None):
if labels.items() <= rule.get("labels", {}).items():
rule["group"] = group_name
rules.append(rule)
return rules
def get_targets(self, **kwargs):
return self.request("GET", "api/v1/targets", **kwargs)
class GrafanaAPIError(BaseAPIError):
pass
class GrafanaAPI(BaseAPI):
ERROR_CLS = GrafanaAPIError
def get_admin_stats(self):
# FIXME: this user should not exist... but it's helpful in tests O:)
return self.request("GET", "api/admin/stats", auth=("admin", "admin"))
def get_dashboards(self):
return self.request(
"GET", "api/search", auth=("admin", "admin"), params={"type": "dash-db"}
)
def get_datasource(self, name):
return self.request(
"GET", f"api/datasources/name/{name}", auth=("admin", "admin")
)
def get_dashboard(self, uid):
return self.request(
"GET",
f"api/dashboards/uid/{uid}",
auth=("admin", "admin"),
)