CumulusNetworks / ifupdown2

GNU General Public License v2.0
160 stars 75 forks source link

Support for metric option in dhcp method #207

Open petefromcze opened 3 years ago

petefromcze commented 3 years ago

Unlike ifupdown the iface section with dhcp method ignores the metric option.

The isc-dhcp-client (/sbin/dhclient) supports custom metric by -e IF_METRIC=<value> option. In case of inet iface the change should be as easy as


index ff6f770..8fc1f94 100644                                                                                                                                                                                                                                                                                                                                         
--- a/ifupdown2/addons/dhcp.py                                                                                                                                                                                                                                                                                                                                        
+++ b/ifupdown2/addons/dhcp.py                                                                                                                                                                                                                                                                                                                                        
@@ -181,6 +181,7 @@ class dhcp(Addon, moduleBase):                                                                                                                                                                                                                                                                                                                    
                         "inet",                                                                                                                                                                                                                                                                                                                                      
                         self.dhclientcmd.start,                                                                                                                                                                                                                                                                                                                      
                         wait=wait,                                                                                                                                                                                                                                                                                                                                   
+                        metric=ifaceobj.get_attr_value_first('metric'),                                                                                                                                                                                                                                                                                              
                         cmd_prefix=dhclient_cmd_prefix                                                                                                                                                                                                                                                                                                               
                     )                                                                                                                                                                                                                                                                                                                                                

diff --git a/ifupdown2/ifupdownaddons/dhclient.py b/ifupdown2/ifupdownaddons/dhclient.py                                                                                                                                                                                                                                                                              
index c10db65..f8ef9fa 100644
--- a/ifupdown2/ifupdownaddons/dhclient.py
+++ b/ifupdown2/ifupdownaddons/dhclient.py
@@ -65,7 +65,7 @@ class dhclient(utilsBase):
                    '%s' %ifacename]
         self._run_dhclient_cmd(cmd, cmd_prefix)

-    def start(self, ifacename, wait=True, cmd_prefix=None):
+    def start(self, ifacename, wait=True, metric=None, cmd_prefix=None):
         retries = 0
         out = "0"

@@ -88,6 +88,8 @@ class dhclient(utilsBase):
                    '/run/dhclient.%s.pid' %ifacename, '-lf',
                    '/var/lib/dhcp/dhclient.%s.leases' %ifacename,
                    '%s' %ifacename]
+            if metric:
+                cmd.extend(['-e', 'IF_METRIC=%s' %metric])
         if not wait:
             cmd.append('-nw')
         self._run_dhclient_cmd(cmd, cmd_prefix)
julienfortin commented 3 years ago

@petefromcze can you please submit your patch as a pull-request?

hadogenes commented 2 years ago

+1