-
Notifications
You must be signed in to change notification settings - Fork 1.3k
[WIP] Ensure VM IP is removed from VR DHCP records in dnsmasq.leases after expunge VM #13194
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Draft
sureshanaparti
wants to merge
1
commit into
apache:4.20
Choose a base branch
from
shapeblue:remove-vm-ip-from-dhcp-record-in-dnsmasq
base: 4.20
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+70
−1
Draft
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -20,6 +20,10 @@ | |
| from netaddr import * | ||
| from random import randint | ||
| import json | ||
| import fcntl | ||
| import shutil | ||
| import tempfile | ||
| import signal | ||
| from .CsGuestNetwork import CsGuestNetwork | ||
| from cs.CsDatabag import CsDataBag | ||
| from cs.CsFile import CsFile | ||
|
|
@@ -139,7 +143,8 @@ def configure_server(self): | |
| # Listen Address | ||
| if self.cl.is_redundant(): | ||
| listen_address.append(gateway) | ||
| listen_address.append(ip) | ||
| else: | ||
| listen_address.append(ip) | ||
| # Add localized "data-server" records in /etc/hosts for VPC routers | ||
| if self.config.is_vpc() or self.config.is_router(): | ||
| self.add_host(gateway, "%s data-server" % CsHelper.get_hostname()) | ||
|
|
@@ -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") | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. what's the purpose of this ?
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @weizhouapache is it good to have this, to cleanup cache entries? will remove it if not necessary. |
||
| else: | ||
| os.remove(tmp_path) | ||
| finally: | ||
| fcntl.flock(fp.fileno(), fcntl.LOCK_UN) | ||
|
|
||
| return removed | ||
|
|
||
| def ensure_lease_removed(self, ip): | ||
| if self.lease_exists(ip): | ||
| return self.remove_lease(ip) | ||
| return False | ||
|
|
||
| def preseed(self): | ||
| self.add_host("127.0.0.1", "localhost") | ||
| self.add_host("127.0.1.1", "%s" % CsHelper.get_hostname()) | ||
|
|
||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
can use
CsHelper.service("dnsmasq", "reload")?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ok, will check