File proper_dhcp_config_set.patch of Package python-azure-agent.16193

--- azurelinuxagent/common/osutil/suse.py.orig
+++ azurelinuxagent/common/osutil/suse.py
@@ -16,20 +16,12 @@
 # Requires Python 2.6+ and Openssl 1.0+
 #
 
-import os
-import re
-import pwd
-import shutil
-import socket
-import array
-import struct
-import fcntl
 import time
+
 import azurelinuxagent.common.logger as logger
 import azurelinuxagent.common.utils.fileutil as fileutil
 import azurelinuxagent.common.utils.shellutil as shellutil
-import azurelinuxagent.common.utils.textutil as textutil
-from azurelinuxagent.common.version import DISTRO_NAME, DISTRO_VERSION, DISTRO_FULL_NAME
+
 from azurelinuxagent.common.osutil.default import DefaultOSUtil
 
 
@@ -82,11 +74,64 @@ class SUSEOSUtil(SUSE11OSUtil):
         super(SUSEOSUtil, self).__init__()
         self.dhclient_name = 'wickedd-dhcp4'
 
+    def publish_hostname(self, hostname):
+        self.set_dhcp_hostname(hostname)
+        self.set_hostname_record(hostname)
+        ifname = self.get_if_name()
+        # To push the hostname to the dhcp server we do not need to
+        # bring down the interface, just make the make ifup do whatever is
+        # necessary
+        self.ifup(ifname)
+
+    def ifup(self, ifname, retries=3, wait=5):
+        logger.info('Interface {0} bounce with ifup'.format(ifname))
+        retry_limit=retries+1
+        for attempt in range(1, retry_limit):
+            try:
+                shellutil.run_command(['ifup', ifname], log_error=True)
+            except Exception:
+                if attempt < retry_limit:
+                    logger.info("retrying in {0} seconds".format(wait))
+                    time.sleep(wait)
+                else:
+                    logger.warn("exceeded restart retries")
+
     def set_hostname(self, hostname):
         shellutil.run(
             "hostnamectl set-hostname {0}".format(hostname), chk_err=False
         )
 
+    def set_dhcp_hostname(self, hostname):
+        dhcp_config_file_path = '/etc/sysconfig/network/dhcp'
+        hostname_send_setting = fileutil.get_line_startingwith(
+            'DHCLIENT_HOSTNAME_OPTION', dhcp_config_file_path
+        )
+        if hostname_send_setting:
+            value = hostname_send_setting.split('=')[-1]
+            if value == "AUTO" or value == '"{0}"'.format(hostname):
+                # Return if auto send host-name is configured or the current
+                # hostname is already set up to be sent
+                return
+            else:
+                # Do not use update_conf_file as it moves the setting to the
+                # end of the file separating it from the contextual comment
+                new_conf = []
+                dhcp_conf = fileutil.read_file(
+                    dhcp_config_file_path).split('\n')
+                for entry in dhcp_conf:
+                    if entry.startswith('DHCLIENT_HOSTNAME_OPTION'):
+                        new_conf.append(
+                           'DHCLIENT_HOSTNAME_OPTION="{0}"'. format(hostname)
+                        )
+                        continue
+                    new_conf.append(entry)
+                fileutil.write_file(dhcp_config_file_path, '\n'.join(new_conf))
+        else:
+            fileutil.append_file(
+                dhcp_config_file_path,
+                'DHCLIENT_HOSTNAME_OPTION="{0}"'. format(hostname)
+            )
+
     def stop_dhcp_service(self):
         cmd = "systemctl stop {0}".format(self.dhclient_name)
         return shellutil.run(cmd, chk_err=False)
openSUSE Build Service is sponsored by