This repository has been archived by the owner on May 3, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathnew-app-and-wait
executable file
·332 lines (248 loc) · 9.91 KB
/
new-app-and-wait
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
#!/usr/bin/python3
import sys
import argparse
import functools
import logging
import re
import socket
import subprocess
import tempfile
import time
import urllib3
from vshn_npo import constants
from vshn_npo import oc_client
from vshn_npo import retry
from vshn_npo import utils
from vshn_npo import iputils
def capture_curl(url, options=None, curl_binary=constants.DEFAULT_CURL_BINARY):
cmd = [
curl_binary,
"--verbose",
"--location",
"--silent",
"--show-error",
"--fail",
"--retry", "3",
"--max-time", "300",
]
if options:
cmd.extend(options)
cmd.append(url)
logging.debug(cmd)
return subprocess.check_output(cmd).decode("UTF-8")
def check_url_anyip(url, pattern, timeout):
"""Check :param:`url` using all IP addresses returned for host.
Resolve the host for the given URL and attempt to retrieve the URL via all
returned IP addresses (IPv4 and IPv6). All requests must succeed and contain
the given pattern.
:param url: URL string
:param pattern: Compiled regular expression
"""
logging.info("Checking access to \"%s\" via all load balancers", url)
tcalc = utils.Timeout(timeout)
parsed_url = urllib3.util.parse_url(url)
if parsed_url.port:
port = parsed_url.port
else:
port = urllib3.connection.port_by_scheme.get(parsed_url.scheme)
if not port:
raise Exception("Can't determine port for URL {!r}".format(url))
checks = []
logging.info("Resolving host \"%s\"", parsed_url.host)
addrinfo = socket.getaddrinfo(parsed_url.host, port, type=socket.SOCK_STREAM)
host_has_global_ip6 = iputils.host_has_global_ip6()
for (af, socktype, proto, _, sa) in addrinfo:
if af == socket.AF_INET6 and not host_has_global_ip6:
# Skip check over IPv6 if the host running the check doesn't have any
# global IPv6 addresses
continue
if af == socket.AF_INET:
(ipaddr, _) = sa
elif af == socket.AF_INET6:
(ipaddr, _, _, _) = sa
else:
raise Exception("Unsupported address family: {!r}".format(af))
opts = [
"--resolve", "{}:{}:{}".format(parsed_url.host, port, ipaddr),
"--max-time", "{}".format(int(max(10, min(300, tcalc.remaining)))),
]
checks.append((ipaddr, url, opts))
delay = utils.Delayer(1.0, 30.0)
while checks:
failed = []
while checks:
(ipaddr, url, opts) = checks.pop()
logging.info("Retrieving %r from %r", url, ipaddr)
try:
text = capture_curl(url, options=opts)
except subprocess.CalledProcessError as err:
logging.error("Request error: %s", err)
failed.append((ipaddr, url, opts))
else:
if not pattern.search(text):
raise Exception("Expected pattern \"{}\" not found".format(pattern.pattern))
checks.extend(failed)
if tcalc.expired:
raise Exception("Not available via all IP addresses after {:0.0f} seconds".
format(tcalc.elapsed))
delay.sleep(tcalc.remaining)
class ExpectedPatternContext(retry.UrlWaitContext):
def __init__(self, pattern):
super(ExpectedPatternContext, self).__init__()
self._pattern = pattern
def check_response(self, resp):
"""Fail unless response contains pattern caller is looking for.
"""
super(ExpectedPatternContext, self).check_response(resp)
if not self._pattern.search(resp.text):
raise Exception("Expected pattern \"{}\" not found".format(self._pattern.pattern))
logging.info("Pattern found in response")
def Check(client, args, timeout):
appname = "testapp"
with tempfile.TemporaryDirectory() as tmpdir:
create_cmd = [
"new-app",
"--name={}".format(appname),
args.source,
]
if args.strategy is not None:
create_cmd.append("--strategy={}".format(args.strategy))
logging.info("Creating application from %s", args.source)
client.run(create_cmd, env_override={
"TMPDIR": tmpdir,
})
client.run(["expose", "svc", appname])
# This is required for some cases where a CDN is running in
# front of the cluster and the CDN only makes HTTPS connections to the
# backends. We always patch the route to be available over HTTP and HTTPS.
client.run(["patch", "route", appname, "-p",
'{"spec":{"tls":{"termination":"Edge","insecureEdgeTerminationPolicy":"Allow"}}}'])
# Construct application URL
route = client.capture_json(["get", "--output=json", "route", appname])
host = route["spec"]["host"]
utils.validate_fqdn(host)
url = "http://{}{}".format(host, args.path)
# Wait until application becomes available
ctx = ExpectedPatternContext(args.pattern)
logging.info("Waiting for %s to match pattern %r", url, args.pattern)
return (url, retry.wait_for_url(url, timeout.remaining, ctx=ctx))
def get_names(client, kind):
return client.capture_output([
"get", "--no-headers", "--show-kind", "--output=name",
"--ignore-not-found", kind,
]).splitlines()
def collect_logs_inner(client, kind, has_logs):
names = sorted(get_names(client, kind))
logging.debug("Objects of type %r: %r", kind, names)
for i in names:
client.run(["describe", "--show-events", i], ignore_errors=True)
if has_logs:
logging.debug("Logs for %r", i)
client.run(["logs", "--timestamps", i], ignore_errors=True)
def collect_logs(client):
client.run(["status", "-v"], ignore_errors=True)
for kind in [
"build",
"deployment",
"deploymentconfig",
"replicationcontroller",
"pod",
]:
collect_logs_inner(client, kind, True)
for kind in [
"endpoints",
"events",
"imagestreamimages",
"imagestreams",
"imagestreamtags",
"limitranges",
"quota",
"rolebindings",
"routes",
"secrets",
"serviceaccounts",
"services",
]:
collect_logs_inner(client, kind, False)
def main():
arg_formatter = argparse.ArgumentDefaultsHelpFormatter
parser = argparse.ArgumentParser(formatter_class=arg_formatter)
parser.add_argument("--nagios-output", type=str, metavar="FILE", default=None,
help=("Write Nagios-compatile output to FILE rather than"
" standard output"))
parser.add_argument("--oc", type=str, default=constants.DEFAULT_OC_BINARY,
help="Path to OpenShift client binary")
parser.add_argument("--strategy", choices=["docker", "source"], default=None,
help=("Specify the build strategy to use if"
" detection should not be used"))
parser.add_argument("-w", "--warning", type=int, metavar="SEC", default=5 * 60,
help=("Warn if application takes more given number of"
" seconds to become available"))
parser.add_argument("-c", "--critical", type=int, metavar="SEC", default=15 * 60,
help=("Fail if application takes more given number of"
" seconds to become available"))
parser.add_argument("--project-prefix", type=str, default="e2e",
help="Prefix for project name")
parser.add_argument("--path", type=str, default="/",
help="Resource to request from application")
parser.add_argument("--pattern", type=re.compile, default="<body>",
help=("Regular expression pattern to expect in body;"
" use \"(?i)\" for a case-insensitive pattern"))
parser.add_argument("--pattern-from", type=argparse.FileType("r"),
default=None, metavar="FILE",
help="Load expected pattern from file")
parser.add_argument("--project-initial-wait", type=int, metavar="SEC", default=3,
help=("Wait for given number of seconds after creating"
" the project until starting the build"))
parser.add_argument("config", type=str,
help="Configuration file with login credentials")
parser.add_argument("source", type=str,
help=("Source code URL, template name or image name;"
" see \"oc new-app --help\""))
args = parser.parse_args()
if args.critical < args.warning:
parser.error("--warning must be less than --critical")
if args.pattern_from is not None:
args.pattern = re.compile(args.pattern_from.read().rstrip())
args.pattern_from = None
logging.basicConfig(level=logging.NOTSET,
format="%(asctime)s %(message)s")
with utils.NagiosOutputFile(args.nagios_output) as exit_fn, \
oc_client.TemporaryConfig(args.config) as cfgfile:
cl = oc_client.Client(args.oc, cfgfile)
cl.run(["whoami"])
cl.run(["version"])
namer = oc_client.ProjectNamer(args.project_prefix)
# Get rid of old projects
oc_client.cleanup_projects(cl, namer, 2 * args.critical)
timeout = utils.Timeout(max(args.warning, args.critical))
project_name = oc_client.create_project(cl, namer)
cl.namespace = project_name
# Workaround for service account token generation is delayed
# https://bugzilla.redhat.com/show_bug.cgi?id=1719792
time.sleep(args.project_initial_wait)
try:
(url, duration) = Check(cl, args, timeout)
check_url_anyip(url, args.pattern, timeout.remaining)
finally:
collect_logs(cl)
logging.info("Deleting project \"%s\"", project_name)
oc_client.delete_project(cl, project_name, ignore_errors=True)
if duration >= args.warning:
code = constants.STATE_WARNING
else:
code = constants.STATE_OK
noneempty_fn = lambda value: "" if value is None else value
# http://docs.icinga.org/latest/en/perfdata.html#perfdata-format
metrics = [
("duration={}s;{};{};0".
format(int(duration), noneempty_fn(args.warning),
noneempty_fn(args.critical))),
]
exit_fn(code,
("Available after {:0.0f} seconds at {}".
format(duration, time.asctime())),
metrics=metrics)
if __name__ == "__main__":
main()
# vim: set sw=2 sts=2 et :