sureshanaparti commented on code in PR #13194:
URL: https://github.com/apache/cloudstack/pull/13194#discussion_r3272959376


##########
systemvm/debian/opt/cloud/bin/cs/CsDhcp.py:
##########
@@ -165,15 +170,79 @@ def delete_leases(self):
                 mac = lease[1]
                 ip = lease[2]
                 if mac not in macs_dhcphosts:
+                    logging.info("Releasing DHCP lease for IP: %s, mac: %s", 
ip, mac)
                     cmd = "dhcp_release $(ip route get %s | grep eth | head -1 
| awk '{print $3}') %s %s" % (ip, ip, mac)
                     logging.info(cmd)
                     CsHelper.execute(cmd)
+                    if self.ensure_lease_removed(ip):
+                        logging.info("Lease for %s still existed after 
dhcp_release; removed manually", ip)
                     removed = removed + 1
                     self.del_host(ip)
             logging.info("Deleted %s entries from dnsmasq.leases file" % 
str(removed))
         except Exception as e:
             logging.error("Caught error while trying to delete entries from 
dnsmasq.leases file: %s" % e)
 
+    def lease_exists(self, ip):
+        if not os.path.exists(LEASES):
+            return False
+
+        with open(LEASES, "r") as fp:
+            for line in fp:
+                fields = line.split()
+                if len(fields) >= 3 and fields[2] == ip:
+                    return True
+
+        return False
+
+    def remove_lease(self, ip):
+        if not os.path.exists(LEASES):
+            return False
+
+        removed = False
+
+        with open(LEASES, "r+") as fp:
+            fcntl.flock(fp.fileno(), fcntl.LOCK_EX)
+            lines = fp.readlines()
+
+            fd, tmp_path = tempfile.mkstemp(
+                prefix="dnsmasq.leases.",
+                dir=os.path.dirname(LEASES)
+            )
+
+            try:
+                with os.fdopen(fd, "w") as tmp:
+                    for line in lines:
+                        fields = line.split()
+
+                        if len(fields) >= 3 and fields[2] == ip:
+                            removed = True
+                            continue
+
+                        tmp.write(line)
+
+                if removed:
+                    shutil.move(tmp_path, LEASES)
+
+                    # reload dnsmasq
+                    try:
+                        with open("/var/run/dnsmasq.pid") as pidf:
+                            os.kill(int(pidf.read().strip()), signal.SIGHUP)
+                    except Exception:
+                        pass
+
+                    os.system("ip neigh flush all")

Review Comment:
   is it good to have this, to cleanup cache entries?



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to