Merge ~pjdc/ubuntu-mirror-charm/+git/ubuntu-mirror-charm:default-v6 into ubuntu-mirror-charm:master

Proposed by Paul Collins
Status: Merged
Approved by: Haw Loeung
Approved revision: 53baf86bc3cfd68eb753dd23e7aaeb00056b0e56
Merged at revision: 3775701f7862cf739540a60f76120a9f66ec9737
Proposed branch: ~pjdc/ubuntu-mirror-charm/+git/ubuntu-mirror-charm:default-v6
Merge into: ubuntu-mirror-charm:master
Diff against target: 56 lines (+23/-14)
1 file modified
hooks/hooks.py (+23/-14)
Reviewer Review Type Date Requested Status
Haw Loeung +1 Approve
Canonical IS Reviewers Pending
Review via email: mp+381482@code.launchpad.net

Commit message

rewrite my_ipaddr as get_default_address

To post a comment you must log in.
Revision history for this message
🤖 Canonical IS Merge Bot (canonical-is-mergebot) wrote :

This merge proposal is being monitored by mergebot. Change the status to Approved to merge.

Revision history for this message
Haw Loeung (hloeung) wrote :

LGTM

review: Approve (+1)
Revision history for this message
🤖 Canonical IS Merge Bot (canonical-is-mergebot) wrote :

Change successfully merged at revision 3775701f7862cf739540a60f76120a9f66ec9737

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1diff --git a/hooks/hooks.py b/hooks/hooks.py
2index 448c752..f5b932e 100755
3--- a/hooks/hooks.py
4+++ b/hooks/hooks.py
5@@ -80,20 +80,24 @@ def juju_header():
6 return header
7
8
9-#
10-# Make an educated guess at our external IP address
11-#
12-def my_ipaddr():
13- s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
14+# Google Public DNS addresses are good "cannot possibly be internal" destinations
15+GET_DEFAULT_ADDRESS_DESTINATIONS = {
16+ socket.AF_INET: ("8.8.8.8", 53),
17+ socket.AF_INET6: ("2001:4860:4860::8888", 53),
18+}
19+
20+
21+def get_default_address(af):
22+ """Determine our default address for the given address family."""
23 try:
24- # Use some arbitrary external address here
25- s.connect(("ntp.ubuntu.com", 123))
26- except socket.error:
27- log("CHARM: Unable to determine local IP address - possible DNS issues")
28- return "127.0.1.1"
29- ipaddr = s.getsockname()[0]
30- s.close()
31- return ipaddr
32+ s = socket.socket(af, socket.SOCK_DGRAM)
33+ s.connect(GET_DEFAULT_ADDRESS_DESTINATIONS[af])
34+ return s.getsockname()[0]
35+ except Exception as e:
36+ log("CHARM: Unable to determine local {} address: {}".format(af.name, str(e)))
37+ return None
38+ finally:
39+ s.close()
40
41
42 def get_pkg_version(name):
43@@ -608,7 +612,12 @@ def configure_nrpe():
44 if 'addresses' in role_config:
45 tmpl_data["ipaddr"] = ' '.join(role_config['addresses'])
46 else:
47- tmpl_data["ipaddr"] = my_ipaddr()
48+ ipaddr = get_default_address(socket.AF_INET)
49+ if not ipaddr:
50+ error = 'Could not determine default IP address!'
51+ status_set('blocked', error)
52+ raise Exception(error)
53+ tmpl_data["ipaddr"] = ipaddr
54 tmpl_data["port"] = 80
55 tmpl_data["role"] = role
56 tmpl_data["path"] = mirror["path"]

Subscribers

People subscribed via source and target branches