~ubuntuone-hackers/conn-check/trunk

« back to all changes in this revision

Viewing changes to conn_check/checks.py

[r=wesmason] Add firewall rule yaml output options

Show diffs side-by-side

added added

removed removed

Lines of Context:
46
46
def load_tls_certs(path):
47
47
    cert_map = {}
48
48
    for filepath in glob.glob("{}/*.pem".format(os.path.abspath(path))):
49
 
        # There might be some dead symlinks in there, so let's make sure it's real.
50
 
        if os.path.exists(filepath):
 
49
        # There might be some dead symlinks in there,
 
50
        # so let's make sure it's real.
 
51
        if os.path.isfile(filepath):
51
52
            data = open(filepath).read()
52
53
            x509 = load_certificate(FILETYPE_PEM, data)
53
54
            # Now, de-duplicate in case the same cert has multiple names.
102
103
        if ip == host:
103
104
            raise ValueError("timed out")
104
105
        else:
105
 
            raise ValueError("timed out connecting to %s" % ip)
 
106
            raise ValueError("timed out connecting to {}".format(ip))
106
107
 
107
108
 
108
109
def make_tcp_check(host, port, timeout=None, **kwargs):
109
110
    """Return a check for TCP connectivity."""
110
111
    return make_check("tcp:{}:{}".format(host, port),
111
112
                      lambda: do_tcp_check(host, port, timeout=timeout),
112
 
                      info="%s:%s" % (host, port))
 
113
                      info="{}:{}".format(host, port))
113
114
 
114
115
 
115
116
def make_tls_check(host, port, disable_tls_verification=False, timeout=None,
116
117
                   **kwargs):
117
118
    """Return a check for TLS setup."""
118
 
    return make_check("tls:{}:{}".format(host, port),
119
 
                      lambda: do_tcp_check(host, port, tls=True,
120
 
                          tls_verify=(not disable_tls_verification),
121
 
                          timeout=timeout),
122
 
                      info="%s:%s" % (host, port))
 
119
 
 
120
    verify = not disable_tls_verification
 
121
    check = make_check("tls:{}:{}".format(host, port),
 
122
                       lambda: do_tcp_check(host, port, tls=True,
 
123
                                            tls_verify=verify,
 
124
                                            timeout=timeout),
 
125
                       info="{}:{}".format(host, port))
 
126
 
 
127
    return check
123
128
 
124
129
 
125
130
class UDPCheckProtocol(DatagramProtocol):
175
180
        if ip == host:
176
181
            raise ValueError("timed out")
177
182
        else:
178
 
            raise ValueError("timed out waiting for %s" % ip)
 
183
            raise ValueError("timed out waiting for {}".format(ip))
179
184
 
180
185
 
181
186
def make_udp_check(host, port, send, expect, timeout=None,
182
187
                   **kwargs):
183
188
    """Return a check for UDP connectivity."""
184
189
    return make_check("udp:{}:{}".format(host, port),
185
 
            lambda: do_udp_check(host, port, send, expect, timeout),
186
 
                      info="%s:%s" % (host, port))
 
190
                      lambda: do_udp_check(host, port, send, expect, timeout),
 
191
                      info="{}:{}".format(host, port))
187
192
 
188
193
 
189
194
def extract_host_port(url):
219
224
    def do_request():
220
225
        proxies = {}
221
226
        if proxy_url:
222
 
            proxies['http'] = proxies['https']= proxy_url
 
227
            proxies['http'] = proxies['https'] = proxy_url
223
228
        elif proxy_host:
224
 
            proxies['http'] = proxies['https']= '{}:{}'.format(
225
 
                                                 proxy_host, proxy_port)
 
229
            proxies['http'] = proxies['https'] = '{}:{}'.format(
 
230
                proxy_host, proxy_port)
226
231
 
227
232
        headers = kwargs.get('headers')
228
233
        body = kwargs.get('body')
266
271
            if response.status_code != expected_code:
267
272
                raise RuntimeError(
268
273
                    "Unexpected response code: {}".format(
269
 
                                               response.status_code))
 
274
                        response.status_code))
270
275
 
271
276
    subchecks.append(make_check('', do_request,
272
277
                     info='{} {}'.format(method, url)))
 
278
 
273
279
    return add_check_prefix('http:{}'.format(url),
274
280
                            sequential_check(subchecks))
275
281
 
299
305
        yield client.authenticate(username, password)
300
306
 
301
307
    subchecks.append(make_check("amqp:{}:{}".format(host, port),
302
 
                                do_auth, info="user %s" % (username,),))
 
308
                                do_auth, info="user {}".format(username),))
303
309
    return sequential_check(subchecks)
304
310
 
305
311
 
329
335
        conn.close()
330
336
 
331
337
    subchecks.append(make_check("postgres:{}:{}".format(host, port),
332
 
                                check_auth, info="user %s" % (username,),
 
338
                                check_auth, info="user {}".format(username),
333
339
                                blocking=True))
 
340
 
334
341
    return sequential_check(subchecks)
335
342
 
336
343
 
358
365
            if resp != 'OK':
359
366
                raise RuntimeError("failed to auth to redis")
360
367
 
361
 
    connect_info = "connect with auth" if password is not None else "connect"
 
368
    if password is not None:
 
369
        connect_info = "connect with auth"
 
370
    else:
 
371
        connect_info = "connect"
362
372
    subchecks.append(make_check(connect_info, do_connect))
 
373
 
363
374
    return add_check_prefix('redis:{}:{}'.format(host, port),
364
375
                            sequential_check(subchecks))
365
376
 
378
389
                                                 timeout=timeout)
379
390
 
380
391
        version = yield client.version()
 
392
        if version is None:
 
393
            raise RuntimeError('Failed retrieve memcached server version')
381
394
 
382
395
    subchecks.append(make_check('connect', do_connect))
 
396
 
383
397
    return add_check_prefix('memcache:{}:{}'.format(host, port),
384
398
                            sequential_check(subchecks))
385
399
 
411
425
 
412
426
        mongo = yield conn
413
427
        names = yield mongo[database].collection_names()
 
428
        if names is None:
 
429
            raise RuntimeError('Failed to retrieve collection names')
414
430
 
415
431
    def timeout_handler():
416
 
        """Manual timeout handler as txmongo timeout args above don't work in
417
 
        many situations."""
 
432
        """Manual timeout handler as txmongo timeout args don't always work."""
418
433
        if 'deferred' in do_connect.func_dict:
419
434
            err = ValueError("timeout connecting to mongodb")
420
435
            do_connect.func_dict['deferred'].errback(err)
421
436
 
422
 
    connect_info = "connect with auth" if any((username, password)) else "connect"
 
437
    if any((username, password)):
 
438
        connect_info = "connect with auth"
 
439
    else:
 
440
        connect_info = "connect"
423
441
    subchecks.append(make_check(connect_info, do_connect))
 
442
 
424
443
    return add_check_prefix('mongodb:{}:{}'.format(host, port),
425
444
                            sequential_check(subchecks))
426
445