Skip to content
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

fix(ubuntu): Point to correct dhcp lease files #2979

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 26 additions & 0 deletions azurelinuxagent/common/osutil/ubuntu.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@
# Requires Python 2.6+ and Openssl 1.0+
#

import glob
import textwrap
import time

import azurelinuxagent.common.logger as logger
Expand Down Expand Up @@ -132,6 +134,30 @@ def start_agent_service(self):
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):
try:
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))
except Exception as e:
logger.info(
"Failed to parse {0}: {1}".format(lease_file, str(e))
)
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
Expand Down
1 change: 1 addition & 0 deletions azurelinuxagent/ga/logcollector_manifests.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 ###
Expand Down
4 changes: 4 additions & 0 deletions azurelinuxagent/pa/deprovision/default.py
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,10 @@ def del_dhcp_lease(self, warnings, actions):
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))
Expand Down
16 changes: 15 additions & 1 deletion tests/common/osutil/test_default.py
Original file line number Diff line number Diff line change
Expand Up @@ -298,7 +298,7 @@ def test_no_primary_does_not_throw(self):
def test_dhcp_lease_default(self):
self.assertTrue(osutil.DefaultOSUtil().get_dhcp_lease_endpoint() is None)

def test_dhcp_lease_ubuntu(self):
def test_dhcp_lease_older_ubuntu(self):
with patch.object(glob, "glob", return_value=['/var/lib/dhcp/dhclient.eth0.leases']):
with patch(open_patch(), mock.mock_open(read_data=load_data("dhcp.leases"))):
endpoint = get_osutil(distro_name='ubuntu', distro_version='12.04').get_dhcp_lease_endpoint() # pylint: disable=assignment-from-none
Expand All @@ -313,6 +313,20 @@ def test_dhcp_lease_ubuntu(self):
self.assertTrue(endpoint is not None)
self.assertEqual(endpoint, "168.63.129.16")

endpoint = get_osutil(distro_name='ubuntu', distro_version='18.04').get_dhcp_lease_endpoint() # pylint: disable=assignment-from-none
self.assertTrue(endpoint is None)

def test_dhcp_lease_newer_ubuntu(self):
with patch.object(glob, "glob", return_value=['/run/systemd/netif/leases/2']):
with patch(open_patch(), mock.mock_open(read_data=load_data("2"))):
endpoint = get_osutil(distro_name='ubuntu', distro_version='18.04').get_dhcp_lease_endpoint() # pylint: disable=assignment-from-none
self.assertTrue(endpoint is not None)
self.assertEqual(endpoint, "168.63.129.16")

endpoint = get_osutil(distro_name='ubuntu', distro_version='20.04').get_dhcp_lease_endpoint() # pylint: disable=assignment-from-none
self.assertTrue(endpoint is not None)
self.assertEqual(endpoint, "168.63.129.16")

def test_dhcp_lease_custom_dns(self):
"""
Validate that the wireserver address is coming from option 245
Expand Down
14 changes: 14 additions & 0 deletions tests/data/2
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
# This is private data. Do not parse.
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

lol "Do not parse"

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

😆 I will remove it. Just copied the system file.

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

not required, I'm just amused that we're parsing the data we're told not to parse. Is there a better interface to query these details from networkd?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am not sure there is. But let me get back to you on this.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

After research, I see there is no proper way of getting the lease file details especially OPTION_245.

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for looking!

ADDRESS=10.0.0.69
NETMASK=255.255.255.0
ROUTER=10.0.0.1
SERVER_ADDRESS=168.63.129.16
NEXT_SERVER=168.63.129.16
T1=4294967295
T2=4294967295
LIFETIME=4294967295
DNS=168.63.129.16
DOMAINNAME=2rdlxelcdvjkok2emfc.bx.internal.cloudapp.net
ROUTES=0.0.0.0/0,10.0.0.1 168.63.129.16/32,10.0.0.1 169.254.169.254/32,10.0.0.1
CLIENTID=ff0406a3a3000201120dc9092eccd2344
OPTION_245=a83f8110
Loading