sailesh1993 / rpms / cloud-init

Forked from rpms/cloud-init 10 months ago
Clone
ef3f20
From 9c2b33562da7e5ca3359a2e70b893d19c44eb66c Mon Sep 17 00:00:00 2001
ef3f20
From: Brent Baude <bbaude@redhat.com>
ef3f20
Date: Mon, 20 Mar 2017 12:31:15 -0500
ef3f20
Subject: [PATCH] Bounce network interface for Azure when using the built-in
ef3f20
 path.
ef3f20
ef3f20
When deploying on Azure and using only cloud-init, you must "bounce" the
ef3f20
network interface to trigger a DDNS update. This allows dhclient to
ef3f20
register the hostname with Azure so that DNS works correctly on their
ef3f20
private networks (i.e. between vm and vm).
ef3f20
ef3f20
The agent path was already doing the bounce so this creates parity
ef3f20
between the built-in path and the agent.
ef3f20
ef3f20
LP: #1674685
ef3f20
Resolves: rhbz#1434109
ef3f20
(cherry picked from commit 86715c88aab8561e1ddadac95671f6095d16f9e7)
ef3f20
---
ef3f20
 cloudinit/sources/DataSourceAzure.py | 78 +++++++++++++++++++++---------------
ef3f20
 cloudinit/sources/__init__.py        |  2 +-
ef3f20
 2 files changed, 47 insertions(+), 33 deletions(-)
ef3f20
ef3f20
diff --git a/cloudinit/sources/DataSourceAzure.py b/cloudinit/sources/DataSourceAzure.py
ef3f20
index c5af8b8..48a3e1d 100644
ef3f20
--- a/cloudinit/sources/DataSourceAzure.py
ef3f20
+++ b/cloudinit/sources/DataSourceAzure.py
ef3f20
@@ -111,50 +111,62 @@ class DataSourceAzureNet(sources.DataSource):
ef3f20
         root = sources.DataSource.__str__(self)
ef3f20
         return "%s [seed=%s]" % (root, self.seed)
ef3f20
 
ef3f20
-    def get_metadata_from_agent(self):
ef3f20
-        temp_hostname = self.metadata.get('local-hostname')
ef3f20
+    def bounce_network_with_azure_hostname(self):
ef3f20
+        # When using cloud-init to provision, we have to set the hostname from
ef3f20
+        # the metadata and "bounce" the network to force DDNS to update via
ef3f20
+        # dhclient
ef3f20
+        azure_hostname = self.metadata.get('local-hostname')
ef3f20
+        LOG.debug("Hostname in metadata is {}".format(azure_hostname))
ef3f20
         hostname_command = self.ds_cfg['hostname_bounce']['hostname_command']
ef3f20
-        agent_cmd = self.ds_cfg['agent_command']
ef3f20
-        LOG.debug("Getting metadata via agent.  hostname=%s cmd=%s",
ef3f20
-                  temp_hostname, agent_cmd)
ef3f20
-        with temporary_hostname(temp_hostname, self.ds_cfg,
ef3f20
+
ef3f20
+        with temporary_hostname(azure_hostname, self.ds_cfg,
ef3f20
                                 hostname_command=hostname_command) \
ef3f20
                 as previous_hostname:
ef3f20
             if (previous_hostname is not None and
ef3f20
-               util.is_true(self.ds_cfg.get('set_hostname'))):
ef3f20
+                    util.is_true(self.ds_cfg.get('set_hostname'))):
ef3f20
                 cfg = self.ds_cfg['hostname_bounce']
ef3f20
+
ef3f20
+                # "Bouncing" the network
ef3f20
                 try:
ef3f20
-                    perform_hostname_bounce(hostname=temp_hostname,
ef3f20
+                    perform_hostname_bounce(hostname=azure_hostname,
ef3f20
                                             cfg=cfg,
ef3f20
                                             prev_hostname=previous_hostname)
ef3f20
                 except Exception as e:
ef3f20
                     LOG.warn("Failed publishing hostname: %s", e)
ef3f20
                     util.logexc(LOG, "handling set_hostname failed")
ef3f20
 
ef3f20
-            try:
ef3f20
-                invoke_agent(agent_cmd)
ef3f20
-            except util.ProcessExecutionError:
ef3f20
-                # claim the datasource even if the command failed
ef3f20
-                util.logexc(LOG, "agent command '%s' failed.",
ef3f20
-                            self.ds_cfg['agent_command'])
ef3f20
-
ef3f20
-            ddir = self.ds_cfg['data_dir']
ef3f20
-
ef3f20
-            fp_files = []
ef3f20
-            key_value = None
ef3f20
-            for pk in self.cfg.get('_pubkeys', []):
ef3f20
-                if pk.get('value', None):
ef3f20
-                    key_value = pk['value']
ef3f20
-                    LOG.debug("ssh authentication: using value from fabric")
ef3f20
-                else:
ef3f20
-                    bname = str(pk['fingerprint'] + ".crt")
ef3f20
-                    fp_files += [os.path.join(ddir, bname)]
ef3f20
-                    LOG.debug("ssh authentication: "
ef3f20
-                              "using fingerprint from fabirc")
ef3f20
-
ef3f20
-            missing = util.log_time(logfunc=LOG.debug, msg="waiting for files",
ef3f20
-                                    func=wait_for_files,
ef3f20
-                                    args=(fp_files,))
ef3f20
+    def get_metadata_from_agent(self):
ef3f20
+        temp_hostname = self.metadata.get('local-hostname')
ef3f20
+        agent_cmd = self.ds_cfg['agent_command']
ef3f20
+        LOG.debug("Getting metadata via agent.  hostname=%s cmd=%s",
ef3f20
+                  temp_hostname, agent_cmd)
ef3f20
+
ef3f20
+        self.bounce_network_with_azure_hostname()
ef3f20
+
ef3f20
+        try:
ef3f20
+            invoke_agent(agent_cmd)
ef3f20
+        except util.ProcessExecutionError:
ef3f20
+            # claim the datasource even if the command failed
ef3f20
+            util.logexc(LOG, "agent command '%s' failed.",
ef3f20
+                        self.ds_cfg['agent_command'])
ef3f20
+
ef3f20
+        ddir = self.ds_cfg['data_dir']
ef3f20
+
ef3f20
+        fp_files = []
ef3f20
+        key_value = None
ef3f20
+        for pk in self.cfg.get('_pubkeys', []):
ef3f20
+            if pk.get('value', None):
ef3f20
+                key_value = pk['value']
ef3f20
+                LOG.debug("ssh authentication: using value from fabric")
ef3f20
+            else:
ef3f20
+                bname = str(pk['fingerprint'] + ".crt")
ef3f20
+                fp_files += [os.path.join(ddir, bname)]
ef3f20
+                LOG.debug("ssh authentication: "
ef3f20
+                          "using fingerprint from fabirc")
ef3f20
+
ef3f20
+        missing = util.log_time(logfunc=LOG.debug, msg="waiting for files",
ef3f20
+                                func=wait_for_files,
ef3f20
+                                args=(fp_files,))
ef3f20
         if len(missing):
ef3f20
             LOG.warn("Did not find files, but going on: %s", missing)
ef3f20
 
ef3f20
@@ -220,6 +232,8 @@ class DataSourceAzureNet(sources.DataSource):
ef3f20
         write_files(ddir, files, dirmode=0o700)
ef3f20
 
ef3f20
         if self.ds_cfg['agent_command'] == AGENT_START_BUILTIN:
ef3f20
+            self.bounce_network_with_azure_hostname()
ef3f20
+
ef3f20
             metadata_func = partial(get_metadata_from_fabric,
ef3f20
                                     fallback_lease_file=self.
ef3f20
                                     dhclient_lease_file)
ef3f20
diff --git a/cloudinit/sources/__init__.py b/cloudinit/sources/__init__.py
ef3f20
index 3d01072..1829450 100644
ef3f20
--- a/cloudinit/sources/__init__.py
ef3f20
+++ b/cloudinit/sources/__init__.py
ef3f20
@@ -210,7 +210,7 @@ class DataSource(object):
ef3f20
         else:
ef3f20
             hostname = toks[0]
ef3f20
 
ef3f20
-        if fqdn:
ef3f20
+        if fqdn and domain != defdomain:
ef3f20
             return "%s.%s" % (hostname, domain)
ef3f20
         else:
ef3f20
             return hostname