~ubuntuone-hackers/conn-check/trunk

« back to all changes in this revision

Viewing changes to conn_check/__init__.py

  • Committer: James Westby
  • Date: 2014-07-24 19:30:30 UTC
  • mfrom: (13 conn-check)
  • mto: This revision was merged to the branch mainline in revision 14.
  • Revision ID: james.westby@canonical.com-20140724193030-7dmxpb0rlb7bhztk
Merge trunk.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
1
#!/usr/bin/python -uWignore
2
 
"""Check connectivity to various services."""
 
2
"""Check connectivity to various services.
 
3
"""
3
4
 
4
5
import os
5
6
import re
737
738
    return sequential_check(subchecks)
738
739
 
739
740
 
740
 
def make_redis_check(host, port, **kwargs):
 
741
def make_redis_check(host, port, password=None, **kwargs):
741
742
    """Make a check for the configured redis server."""
742
 
    import redis
 
743
    import txredis
743
744
    subchecks = []
744
745
    subchecks.append(make_tcp_check(host, port))
745
746
 
746
 
    def do_auth():
747
 
        """Connect and authenticate."""
748
 
        client = redis.client.Redis(host=host, port=port)
749
 
        if not client.ping():
750
 
            raise RuntimeError("failed to ping redis")
751
 
 
752
 
    subchecks.append(make_check("auth", do_auth))
 
747
    @inlineCallbacks
 
748
    def do_connect():
 
749
        """Connect and authenticate.
 
750
        """
 
751
        client_creator = ClientCreator(reactor, txredis.client.RedisClient)
 
752
        client = yield client_creator.connectTCP(host=host, port=port,
 
753
                                                 timeout=CONNECT_TIMEOUT)
 
754
 
 
755
        if password is None:
 
756
            ping = yield client.ping()
 
757
            if not ping:
 
758
                raise RuntimeError("failed to ping redis")
 
759
        else:
 
760
            resp = yield client.auth(password)
 
761
            if resp != 'OK':
 
762
                raise RuntimeError("failed to auth to redis")
 
763
 
 
764
    connect_info = "connect with auth" if password is not None else "connect"
 
765
    subchecks.append(make_check(connect_info, do_connect))
753
766
    return add_check_prefix('redis', sequential_check(subchecks))
754
767
 
755
768