From 23be09ce35ecac058fdfd97fd769e16f452186f5 Mon Sep 17 00:00:00 2001
From: Antoine Musso <hashar@free.fr>
Date: Fri, 30 Oct 2015 22:34:10 +0100
Subject: [PATCH] debian: cloud hostname ignored by Jessie

5af25b5f fixed the hostname of Debian images to "debian" since a lack of
hostname definition set the hostname to "(None)".

It has been done by introducing /etc/cloud/cloud.cfg.d/01_hostname.cfg
with content:

  hostname: debian

Review supposed the hostname would be overriden by cloud meta-data. That
might have stand true for Wheezy but it is not the case for Jessie.

cloud-init 0.7.6 ignores cloud metadata whenever "hostname" or "fqdn"
are set in a config file. Roughly:

    # no fqdn set, get fqdn from cloud
    # get hostname from cfg if available otherwise cloud
    fqdn = cloud.get_hostname(fqdn=True)
    if "hostname" in cfg:
        # hashar: set from config file NOT cloud
        hostname = cfg['hostname']
    else:
        # fallback to cloud
        hostname = cloud.get_hostname()

Relevant code is
https://github.com/number5/cloud-init/blob/0.7.6/cloudinit/util.py#L839-L860

Only inject "hostname: debian" for the Wheezy release.

Bug: https://phabricator.wikimedia.org/T117283
Change-Id: I6e2522bd725cbf9651f11c76ecdc72ecbc92f402
---
 elements/debian/install.d/10-cloud-opinions | 9 +++++++--
 1 file changed, 7 insertions(+), 2 deletions(-)

diff --git a/elements/debian/install.d/10-cloud-opinions b/elements/debian/install.d/10-cloud-opinions
index fc160f21c..8fd581250 100755
--- a/elements/debian/install.d/10-cloud-opinions
+++ b/elements/debian/install.d/10-cloud-opinions
@@ -37,5 +37,10 @@ if [ "$DIB_DEBIAN_ALT_INIT_PACKAGE" != "sysvinit" ]; then
         /etc/apt/preferences.d/sysvinit > /etc/apt/preferences.d/sysvinit
 fi
 
-# specify a hostname so that cloud-init does not default to (None)
-echo 'hostname: debian' > /etc/cloud/cloud.cfg.d/01_hostname.cfg
+# Since Jessie, when either 'hostname' or 'fqdn' are in a config file, it
+# considers them as authoritative and would only use cloud meta data as a
+# fallback.
+if [ "$DIB_RELEASE" = 'wheezy' ]; then
+    # specify a hostname so that cloud-init does not default to (None)
+    echo 'hostname: debian' > /etc/cloud/cloud.cfg.d/01_hostname.cfg
+fi