Skip to content

Commit

Permalink
cable-guy: testing dynamic ip
Browse files Browse the repository at this point in the history
  • Loading branch information
Williangalvani committed Jan 14, 2025
1 parent 7b37558 commit 79855cd
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 6 deletions.
4 changes: 1 addition & 3 deletions core/services/cable_guy/api/manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -247,9 +247,7 @@ def trigger_dynamic_ip_acquisition(self, interface_name: str) -> None:
interface_name (str): Interface name
"""
logger.info(f"Restarting interface {interface_name} to trigger dynamic IP acquisition.")
self.enable_interface(interface_name, False)
time.sleep(1)
self.enable_interface(interface_name, True)
self.network_handler.trigger_dynamic_ip_acquisition(interface_name)

def add_static_ip(self, interface_name: str, ip: str) -> None:
"""Set ip address for a specific interface
Expand Down
50 changes: 47 additions & 3 deletions core/services/cable_guy/networksetup.py
Original file line number Diff line number Diff line change
Expand Up @@ -80,12 +80,13 @@ def add_static_ip(self, interface_name: str, ip: str) -> None:
logger.info(f"IP {ip} already exists for {interface_name}")
continue
new_ip = AddressData(address=ip, prefix=24)
data.ipv4.address_data.append(new_ip)
settings.update_profile(data)

properties = settings.get_settings()
properties["ipv4"]["method"] = ("s", "shared")
properties["ipv4"]["method"] = ("s", "manual")
settings.update(properties)
settings.save()
data.ipv4.address_data.append(new_ip)
settings.update_profile(data)
network_manager.activate_connection(connection_path)
return
except Exception as e:
Expand Down Expand Up @@ -135,6 +136,49 @@ def set_interfaces_priority(self, interfaces: List[NetworkInterfaceMetricApi]) -
settings.update(properties)
network_manager.activate_connection(connection_path)

def _get_dhcp_address_using_dhclient(self, interface_name: str) -> str:
"""Use dhclient to get a DHCP address and return it"""
import re
import subprocess

try:
# Release any existing DHCP lease
subprocess.run(["dhclient", "-r", interface_name], timeout=5)
time.sleep(1)

# Run dhclient and capture output
result = subprocess.run(
["dhclient", "-1", "-v", interface_name], capture_output=True, text=True, timeout=30
)

# Look for the bound IP in the output
match = re.search(r"bound to ([0-9.]+)", result.stderr)
if match:
return match.group(1)

logger.error(f"Could not find bound IP in dhclient output: {result.stderr}")
return ""
except Exception as e:
logger.error(f"Failed to run dhclient: {e}")
return ""

def trigger_dynamic_ip_acquisition(self, interface_name: str) -> None:
"""Get a new DHCP address using dhclient and add it as a static IP"""
try:
# Get a new IP using dhclient
new_ip = self._get_dhcp_address_using_dhclient(interface_name)
if not new_ip:
logger.error("Failed to get new IP from DHCP")
return

logger.info(f"Got new IP {new_ip} from DHCP for {interface_name}")

# Add it as a static IP
self.add_static_ip(interface_name, new_ip)

except Exception as e:
logger.error(f"Failed to acquire dynamic IP for {interface_name}: {e}")


class DHCPCD(AbstractNetworkHandler):
dhcpcd_conf_path = "/etc/dhcpcd.conf"
Expand Down

0 comments on commit 79855cd

Please sign in to comment.