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

Use PATH to find tools primarily #257

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
21 changes: 18 additions & 3 deletions ifupdown2/ifupdown/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

from functools import partial
from ipaddress import IPv4Address
from shutil import which

try:
from ifupdown2.ifupdown.iface import *
Expand Down Expand Up @@ -108,6 +109,7 @@ class utils():
systemctl_cmd = '/bin/systemctl'
dpkg_cmd = '/usr/bin/dpkg'


logger.info("utils init command paths")
for cmd in ['bridge',
'ip',
Expand All @@ -125,16 +127,29 @@ class utils():
'systemctl',
'dpkg'
]:
if os.path.exists(vars()[cmd + '_cmd']):
# If we can find utilities in $PATH we take them.
which_cmd = which(cmd)
var_name = cmd + '_cmd'

logger.setLevel(logging.DEBUG)
Copy link
Author

Choose a reason for hiding this comment

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

This is just for testing and discussion

if which(cmd):
vars()[var_name] = which_cmd
print('ASDF: %s is set to %s through PATH' % (var_name, which_cmd))
logger.debug('%s is set to %s through PATH', var_name, which_cmd)
Copy link
Author

Choose a reason for hiding this comment

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

Even though I've set loglevel to debug on line 134 there's no output from line 138. There's output from line 137 which is just a print statement though.

continue
if os.path.exists(vars()[var_name]):
Copy link
Author

Choose a reason for hiding this comment

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

This won't be called again since the added continue above.

continue
for path in ['/bin/',
'/sbin/',
'/usr/bin/',
'/usr/sbin/',]:
if os.path.exists(path + cmd):
vars()[cmd + '_cmd'] = path + cmd
vars()[var_name] = path + cmd
print('ASDF: %s is set to %s through common bin paths' % (var_name, path + cmd))
logger.debug('%s is set to %s through common bin paths', var_name, path + cmd)
else:
logger.debug('warning: path %s not found: %s won\'t be usable' % (path + cmd, cmd))
print('ASDF: warning: path %s not found: %s won\'t be usable' % (path + cmd, cmd))
logger.debug('warning: path %s not found: %s won\'t be usable', path + cmd, cmd)

mac_translate_tab = str.maketrans(":.-,", " ")

Expand Down
2 changes: 1 addition & 1 deletion ifupdown2/ifupdownaddons/modulebase.py
Original file line number Diff line number Diff line change
Expand Up @@ -447,7 +447,7 @@ def _get_reserved_vlan_range(self):
def _get_vrf_context(self):
vrfid = 'default'
try:
vrfid = utils.exec_command('/usr/sbin/ip vrf id').strip()
vrfid = utils.exec_command('%s vrf id' % utils.ip_cmd).strip()
except Exception as e:
self.logger.debug('failed to get vrf id (%s)' %str(e))
# ignore errors
Expand Down