Merge lp:~therve/landscape-client/localhost-hostname into lp:~landscape/landscape-client/trunk

Proposed by Thomas Herve
Status: Merged
Approved by: Björn Tillenius
Approved revision: 301
Merged at revision: 300
Proposed branch: lp:~therve/landscape-client/localhost-hostname
Merge into: lp:~landscape/landscape-client/trunk
Diff against target: 137 lines (+48/-7)
4 files modified
landscape/broker/registration.py (+4/-3)
landscape/lib/network.py (+19/-1)
landscape/lib/tests/test_network.py (+23/-1)
landscape/monitor/computerinfo.py (+2/-2)
To merge this branch: bzr merge lp:~therve/landscape-client/localhost-hostname
Reviewer Review Type Date Requested Status
Björn Tillenius (community) Approve
Kevin McDermott (community) Approve
Review via email: mp+42951@code.launchpad.net

Description of the change

The branch defines a new get_fqnd function which does some magic if the returned value of socket.getfqdn() is not sensible. I've found the getaddrinfo invocation in hostname source, and the intertubes. It "worked" on my machine which was broken since the upgrade to maverick, and also my lucid server.

To post a comment you must log in.
Revision history for this message
Kevin McDermott (bigkevmcd) wrote :

+ C{get_fqdn} fallbacks to C{socket.getaddrinfo} with C{AI_CANONNAME}

"falls back"...

+ Return the current fqdn of the machine, trying hard to return a meaninful

meaningful

Other than a couple of typos, this is cool, shame we have to work round an Ubuntu bug :-(

+1 for merge

review: Approve
301. By Thomas Herve

English iz hard

Revision history for this message
Björn Tillenius (bjornt) wrote :

+1! It seems like we could get a spurious test failure, if the machine that runs the test has the string 'localhost' in its name, no? Probably not worth worrying about.

review: Approve

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1=== modified file 'landscape/broker/registration.py'
2--- landscape/broker/registration.py 2010-09-09 16:10:27 +0000
3+++ landscape/broker/registration.py 2010-12-07 14:52:20 +0000
4@@ -8,6 +8,7 @@
5 from landscape.lib.log import log_failure
6 from landscape.lib.fetch import fetch, FetchError
7 from landscape.lib.tag import is_valid_tag_list
8+from landscape.lib.network import get_fqdn
9
10
11 EC2_HOST = "169.254.169.254"
12@@ -242,7 +243,7 @@
13 logging.info("Queueing message to register with OTP")
14 message = {"type": "register-cloud-vm",
15 "otp": self._otp,
16- "hostname": socket.getfqdn(),
17+ "hostname": get_fqdn(),
18 "account_name": None,
19 "registration_password": None,
20 "tags": tags}
21@@ -255,7 +256,7 @@
22 u"as an EC2 instance." % (id.account_name, with_tags))
23 message = {"type": "register-cloud-vm",
24 "otp": None,
25- "hostname": socket.getfqdn(),
26+ "hostname": get_fqdn(),
27 "account_name": id.account_name,
28 "registration_password": \
29 id.registration_password,
30@@ -274,7 +275,7 @@
31 "computer_title": id.computer_title,
32 "account_name": id.account_name,
33 "registration_password": id.registration_password,
34- "hostname": socket.getfqdn(),
35+ "hostname": get_fqdn(),
36 "tags": tags}
37 self._exchange.send(message)
38 else:
39
40=== modified file 'landscape/lib/network.py'
41--- landscape/lib/network.py 2010-08-16 09:31:24 +0000
42+++ landscape/lib/network.py 2010-12-07 14:52:20 +0000
43@@ -62,7 +62,7 @@
44 # Generator over the interface names
45 already_found = set()
46 for index in range(0, byte_length, IF_STRUCT_SIZE):
47- ifreq_struct = result[index:index+IF_STRUCT_SIZE]
48+ ifreq_struct = result[index:index + IF_STRUCT_SIZE]
49 interface_name = ifreq_struct[:ifreq_struct.index("\0")]
50 if interface_name not in already_found:
51 already_found.add(interface_name)
52@@ -177,6 +177,24 @@
53 return devices
54
55
56+def get_fqdn():
57+ """
58+ Return the current fqdn of the machine, trying hard to return a meaningful
59+ name.
60+
61+ In particular, it means working against a NetworkManager bug which seems to
62+ make C{getfqdn} return localhost6.localdomain6 for machine without a domain
63+ since Maverick.
64+ """
65+ fqdn = socket.getfqdn()
66+ if "localhost" in fqdn:
67+ # Try the heavy artillery
68+ fqdn = socket.getaddrinfo(socket.gethostname(), None, socket.AF_INET,
69+ socket.SOCK_DGRAM, socket.IPPROTO_IP,
70+ socket.AI_CANONNAME)[0][3]
71+ return fqdn
72+
73+
74 if __name__ == "__main__":
75 import pprint
76 pprint.pprint(get_active_device_info())
77
78=== modified file 'landscape/lib/tests/test_network.py'
79--- landscape/lib/tests/test_network.py 2010-07-23 08:08:41 +0000
80+++ landscape/lib/tests/test_network.py 2010-12-07 14:52:20 +0000
81@@ -5,7 +5,8 @@
82 from landscape.tests.helpers import LandscapeTest
83
84 from landscape.lib.network import (
85- get_network_traffic, get_active_device_info, get_active_interfaces)
86+ get_network_traffic, get_active_device_info, get_active_interfaces,
87+ get_fqdn)
88 from landscape.tests.mocker import ANY
89
90
91@@ -156,3 +157,24 @@
92 "send_colls": 19,
93 "send_carrier": 0,
94 "send_compressed": 0}}
95+
96+
97+class FQDNTest(LandscapeTest):
98+
99+ def test_default_fqdn(self):
100+ """
101+ C{get_fqdn} returns the output of C{socket.getfqdn} if it returns
102+ something sensible.
103+ """
104+ self.addCleanup(setattr, socket, "getfqdn", socket.getfqdn)
105+ socket.getfqdn = lambda: "foo.bar"
106+ self.assertEqual("foo.bar", get_fqdn())
107+
108+ def test_getaddrinfo_fallback(self):
109+ """
110+ C{get_fqdn} falls back to C{socket.getaddrinfo} with the
111+ C{AI_CANONNAME} flag if C{socket.getfqdn} returns a local hostname.
112+ """
113+ self.addCleanup(setattr, socket, "getfqdn", socket.getfqdn)
114+ socket.getfqdn = lambda: "localhost6.localdomain6"
115+ self.assertNotIn("localhost", get_fqdn())
116
117=== modified file 'landscape/monitor/computerinfo.py'
118--- landscape/monitor/computerinfo.py 2010-04-23 13:39:22 +0000
119+++ landscape/monitor/computerinfo.py 2010-12-07 14:52:20 +0000
120@@ -1,7 +1,7 @@
121 import logging
122-import socket
123
124 from landscape.lib.lsb_release import LSB_RELEASE_FILENAME, parse_lsb_release
125+from landscape.lib.network import get_fqdn
126 from landscape.monitor.plugin import MonitorPlugin
127
128
129@@ -14,7 +14,7 @@
130
131 persist_name = "computer-info"
132
133- def __init__(self, get_fqdn=socket.getfqdn,
134+ def __init__(self, get_fqdn=get_fqdn,
135 meminfo_file="/proc/meminfo",
136 lsb_release_filename=LSB_RELEASE_FILENAME):
137 self._get_fqdn = get_fqdn

Subscribers

People subscribed via source and target branches

to all changes: