Merge lp:~cprov/core-image-watcher/proper-hostname into lp:core-image-watcher

Proposed by Celso Providelo
Status: Merged
Approved by: Celso Providelo
Approved revision: 13
Merged at revision: 10
Proposed branch: lp:~cprov/core-image-watcher/proper-hostname
Merge into: lp:core-image-watcher
Diff against target: 132 lines (+68/-9)
2 files modified
core_image_watcher/__init__.py (+21/-9)
core_image_watcher/constants.py (+47/-0)
To merge this branch: bzr merge lp:~cprov/core-image-watcher/proper-hostname
Reviewer Review Type Date Requested Status
Para Siva (community) Approve
Francis Ginther Approve
Evan (community) Approve
Review via email: mp+254699@code.launchpad.net

Commit message

Using /etc/hostname content directly for identifying service workers.

Description of the change

Using /etc/hostname content directly for identifying service workers.

It's required because juju-generated hostnames get too big due to our long environment names (<spec_name>-<MD5>).

Those names are to long to be qualified as proper hostnames and are not used in /etc/hosts, 'ubuntu' is used instead (for all nodes).

To post a comment you must log in.
Revision history for this message
Para Siva (psivaa) wrote :

+1.

review: Approve
Revision history for this message
Evan (ev) wrote :

Don't let me block this from landing, but can you elaborate on this a bit? Where are we hitting a limit on the length of the hostname? In Logstash?

Revision history for this message
Celso Providelo (cprov) wrote :

Evan,

No, it's not Logstash or anything from the late 20th century, but
instead DNS label size, which is limited to 64 chars [1].

Linux seems to take it serious on /usr/include/linux/utsname.h and all
the other tools around it seems to assume that we should not have
labels larger than 64 chars.

I claim ignorance at this point ...

[1] http://www.ietf.org/rfc/rfc1035.txt

On Tue, Mar 31, 2015 at 6:34 AM, Evan Dandrea
<email address hidden> wrote:
> Don't let me block this from landing, but can you elaborate on this a bit? Where are we hitting a limit on the length of the hostname? In Logstash?
> --
> https://code.launchpad.net/~cprov/core-image-watcher/proper-hostname/+merge/254699
> You are the owner of lp:~cprov/core-image-watcher/proper-hostname.

--
Celso Providelo
<email address hidden>

11. By Celso Providelo

Adding 'service' key to the logging extra information, so we can filter this properly in logstash.

Revision history for this message
Evan (ev) wrote :

I think the rationale for this should be documented in the code, but it otherwise looks good. Celso explained on IRC the 64 character limit for hostnames and how this lets us avoid a truncated or "ubuntu" hostname in the logs.

review: Approve
12. By Celso Providelo

Adding a rationale for digging into /etc/hostname.

Revision history for this message
Celso Providelo (cprov) wrote :

Evan,

Rationale added in the docstring, thanks for pointing it.

Another blessing, please ?

Revision history for this message
Francis Ginther (fginther) wrote :

Thanks for the explanation. Also nice that this is easily migrated to other projects.

review: Approve
Revision history for this message
Para Siva (psivaa) wrote :

One inline question about 'SERVICE_NAME' in constants.py. Apart from that,+1.

review: Approve
13. By Celso Providelo

Logging 'solution' & 'service' information.

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1=== modified file 'core_image_watcher/__init__.py'
2--- core_image_watcher/__init__.py 2015-03-28 11:58:10 +0000
3+++ core_image_watcher/__init__.py 2015-03-31 15:43:19 +0000
4@@ -29,23 +29,33 @@
5 import logstash
6
7
8+from core_image_watcher import constants
9+
10+
11 logger = logging.getLogger(__name__)
12
13-API_VERSION = "v1"
14+
15+logging_extra = {
16+ 'solution': constants.SOLUTION_NAME,
17+ 'service': constants.SERVICE_NAME,
18+ 'hostname': constants.HOSTNAME,
19+}
20
21
22 def _enqueue(body, amqp_uris):
23 """Enqueues a message of `body` to the rabbit queue"""
24+ extra = logging_extra.copy()
25+ extra.update(body)
26 with kombu.Connection(amqp_uris) as connection:
27- logger.info('Triggering request: %s', body, extra=body)
28+ logger.info('Triggering request: %s', body, extra=extra)
29 try:
30 queue = connection.SimpleQueue(
31- 'core.image.{}'.format(API_VERSION))
32+ 'core.image.{}'.format(constants.API_VERSION))
33 queue.put(body)
34 queue.close()
35 except Exception as exc:
36- logger.error(exc, exc_info=True, extra=body)
37- logger.info('Done!', extra=body)
38+ logger.error(exc, exc_info=True, extra=extra)
39+ logger.info('Done!', extra=extra)
40
41
42 def _get_version_string_output(channel, device):
43@@ -61,7 +71,7 @@
44 try:
45 images = subprocess.check_output(cmd)
46 except subprocess.CalledProcessError as e:
47- logger.error(e, exc_info=True)
48+ logger.error(e, exc_info=True, extra=logging_extra)
49 finally:
50 return images
51
52@@ -74,7 +84,8 @@
53 if (len(images) >= 2):
54 return images[-2].decode('utf-8').replace(':', '')
55
56- logger.error('Could not get the image list by u-d-f: %s', images)
57+ logger.error('Could not get the image list by u-d-f: %s',
58+ images, extra=logging_extra)
59 return None
60
61
62@@ -93,7 +104,8 @@
63 f.write(latest_version)
64 except IOError as e:
65 latest_version = None
66- logger.error('Writing the latest image info failed: %s', str(e))
67+ logger.error('Writing the latest image info failed: %s',
68+ str(e), extra=logging_extra)
69 finally:
70 return latest_version
71
72@@ -112,7 +124,7 @@
73 # Do not progress
74 return None
75 except Exception as e:
76- logger.error(e, exc_info=True)
77+ logger.error(e, exc_info=True, extra=logging_extra)
78 return None
79 body = {
80 'image_name': latest_version,
81
82=== added file 'core_image_watcher/constants.py'
83--- core_image_watcher/constants.py 1970-01-01 00:00:00 +0000
84+++ core_image_watcher/constants.py 2015-03-31 15:43:19 +0000
85@@ -0,0 +1,47 @@
86+# core-image-watcher
87+# Copyright (C) 2015 Canonical
88+#
89+# This program is free software: you can redistribute it and/or modify
90+# it under the terms of the GNU General Public License as published by
91+# the Free Software Foundation, either version 3 of the License, or
92+# (at your option) any later version.
93+#
94+# This program is distributed in the hope that it will be useful,
95+# but WITHOUT ANY WARRANTY; without even the implied warranty of
96+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
97+# GNU General Public License for more details.
98+#
99+# You should have received a copy of the GNU General Public License
100+# along with this program. If not, see <http://www.gnu.org/licenses/>.
101+#
102+
103+"""Constants for this service."""
104+
105+
106+API_VERSION = "v1"
107+
108+SOLUTION_NAME = "core-image-testing"
109+
110+SERVICE_NAME = "core-image-watcher"
111+
112+def _get_hostname():
113+ """Return sanitized contents of /etc/hostname.
114+
115+ It is necessary because current juju hostnames (juju-<env-name>-machine-#)
116+ are too big due to our long environment names ('<spec_name>-<MD5>').
117+ Linux (DNS for RFC1035, really) only supports labels up to 64 chars and
118+ the fallback varies from tool to tool, `cloud-init` chokes on longer
119+ names and sets 'ubuntu', `hostnamectl` (systemd) would truncate the
120+ given data.
121+
122+ None of this is ideal to our applications, that's why we will operate
123+ on the pristine /etc/hostname and remove the 'juju-' and '-machine' terms
124+ added by juju.
125+ """
126+ with open('/etc/hostname') as fd:
127+ hostname = fd.read()
128+ return hostname.replace('juju-', '').replace('-machine', '').strip()
129+
130+
131+HOSTNAME = _get_hostname()
132+

Subscribers

People subscribed via source and target branches