~ubuntu-branches/ubuntu/natty/landscape-client/natty-updates

« back to all changes in this revision

Viewing changes to landscape/lib/dns.py

  • Committer: Package Import Robot
  • Author(s): Andreas Hasenack
  • Date: 2012-03-19 09:33:34 UTC
  • mto: (1.2.10)
  • mto: This revision was merged to the branch mainline in revision 39.
  • Revision ID: package-import@ubuntu.com-20120319093334-oxjttz163vvfgq8s
ImportĀ upstreamĀ versionĀ 12.04

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
"""DNS lookups for server autodiscovery."""
 
2
 
 
3
import logging
 
4
from twisted.names import dns
 
5
from twisted.names.client import Resolver
 
6
 
 
7
 
 
8
def discover_server(autodiscover_srv_query_string="",
 
9
                    autodiscover_a_query_string="", resolver=None):
 
10
    """
 
11
    Look up the dns location of the landscape server.
 
12
 
 
13
    @param autodiscover_srv_query_string: The query string to send to the DNS
 
14
        server when making a SRV query.
 
15
    @param autodiscover_a_query_string: The query string to send to the DNS
 
16
        server when making a A query.
 
17
    @type resolver: The resolver to use.  If none is specified a resolver that
 
18
        uses settings from /etc/resolv.conf will be created. (Testing only)
 
19
    """
 
20
    if not resolver:
 
21
        resolver = Resolver("/etc/resolv.conf")
 
22
    d = _lookup_server_record(resolver, autodiscover_srv_query_string)
 
23
    d.addErrback(_lookup_hostname, resolver, autodiscover_a_query_string)
 
24
    return d
 
25
 
 
26
 
 
27
def _lookup_server_record(resolver, service_name):
 
28
    """
 
29
    Do a DNS SRV record lookup for the location of the landscape server.
 
30
 
 
31
    @type resolver: A resolver to use for DNS lookups
 
32
        L{twisted.names.client.Resolver}.
 
33
    @param service_name: The query string to send to the DNS server when
 
34
        making a SRV query.
 
35
    @return: A deferred containing either the hostname of the landscape server
 
36
        if found or an empty string if not found.
 
37
    """
 
38
    def lookup_done(result):
 
39
        name = ""
 
40
        for item in result:
 
41
            for row in item:
 
42
                if row.type == dns.SRV:
 
43
                    name = row.payload.target.name
 
44
                    break
 
45
        return name
 
46
 
 
47
    def lookup_failed(result):
 
48
        logging.info("SRV lookup of %s failed." % service_name)
 
49
        return result
 
50
 
 
51
    d = resolver.lookupService(service_name)
 
52
    d.addCallback(lookup_done)
 
53
    d.addErrback(lookup_failed)
 
54
    return d
 
55
 
 
56
 
 
57
def _lookup_hostname(result, resolver, hostname):
 
58
    """
 
59
    Do a DNS name lookup for the location of the landscape server.
 
60
 
 
61
    @param result: The result from a call to lookup_server_record.
 
62
    @param resolver: The resolver to use for DNS lookups.
 
63
    @param hostname: The query string to send to the DNS server when making
 
64
        a A query.
 
65
    @param return: A deferred containing the ip address of the landscape
 
66
        server if found or None if not found.
 
67
    """
 
68
    def lookup_done(result):
 
69
        return result
 
70
 
 
71
    def lookup_failed(result):
 
72
        logging.info("Name lookup of %s failed." % hostname)
 
73
        return result
 
74
 
 
75
    d = resolver.getHostByName(hostname)
 
76
    d.addCallback(lookup_done)
 
77
    d.addErrback(lookup_failed)
 
78
    return d