Description: Ubuntu versions >= 18.04 look for systemd-networkd lease files.
    Currently, the package was configuring systemd-networkd but
    checking for dhclient lease files.
Forwarded: yes, https://github.com/Azure/WALinuxAgent/pull/2979
Author: Calvin Mwadime Makokha <calvin.mwadime@canonical.com>

--- a/azurelinuxagent/common/logcollector_manifests.py
+++ b/azurelinuxagent/common/logcollector_manifests.py
@@ -83,6 +83,7 @@
 copy,/var/lib/dhcp/dhclient.eth0.leases
 copy,/var/lib/dhclient/dhclient-eth0.leases
 copy,/var/lib/wicked/lease-eth0-dhcp-ipv4.xml
+copy,/run/systemd/netif/leases/2
 echo,

 echo,### Gathering Log Files ###
--- a/azurelinuxagent/common/osutil/ubuntu.py
+++ b/azurelinuxagent/common/osutil/ubuntu.py
@@ -16,6 +16,8 @@
 # Requires Python 2.6+ and Openssl 1.0+
 #

+import glob
+import textwrap
 import time

 import azurelinuxagent.common.logger as logger
@@ -132,6 +134,25 @@
     def stop_agent_service(self):
         return shellutil.run("systemctl stop {0}".format(self.service_name), chk_err=False)

+    def get_dhcp_lease_endpoint(self):
+        pathglob = "/run/systemd/netif/leases/*"
+        logger.info("looking for leases in path [{0}]".format(pathglob))
+        endpoint = None
+        for lease_file in glob.glob(pathglob):
+            with open(lease_file) as f:
+                lease = f.read()
+            for line in lease.splitlines():
+                if line.startswith("OPTION_245"):
+                    option_245 = line.split("=")[1]
+                    options = [int(i, 16) for i in textwrap.wrap(option_245, 2)]
+                    endpoint = "{0}.{1}.{2}.{3}".format(*options)
+                    logger.info("found endpoint [{0}]".format(endpoint))
+        if endpoint is not None:
+            logger.info("cached endpoint found [{0}]".format(endpoint))
+        else:
+            logger.info("cached endpoint not found")
+        return endpoint
+

 class UbuntuOSUtil(Ubuntu16OSUtil):
     def __init__(self):  # pylint: disable=W0235
--- a/azurelinuxagent/pa/deprovision/default.py
+++ b/azurelinuxagent/pa/deprovision/default.py
@@ -131,6 +131,10 @@
         actions.append(DeprovisionAction(fileutil.rm_files,
                                          ["/var/lib/NetworkManager/dhclient-*.lease"]))

+        # For Ubuntu >= 18.04, using systemd-networkd
+        actions.append(DeprovisionAction(fileutil.rm_files,
+                                         ["/run/systemd/netif/leases/*"]))
+
     def del_ext_handler_files(self, warnings, actions):  # pylint: disable=W0613
         ext_dirs = [d for d in os.listdir(conf.get_lib_dir())
                     if os.path.isdir(os.path.join(conf.get_lib_dir(), d))
