Better error handling, Efforts to run non-root with occasional sudo

This commit is contained in:
ahmadbilalkhalid 2019-12-29 23:14:39 +05:00
commit f980cdb464
7 changed files with 90 additions and 47 deletions

View file

@ -30,14 +30,14 @@ def generate_mac(uaa=False, multicast=False, oui=None, separator=':', byte_fmt='
def create_dev(script, _id, dev, ip=None):
command = [script, str(_id), dev]
command = ['sudo', '-p', 'Enter password to create network devices for vm: ',
script, str(_id), dev]
if ip:
command.append(ip)
try:
output = sp.check_output(command, stderr=sp.PIPE)
except Exception as e:
except Exception:
logger.exception('Creation of interface %s failed.', dev)
print(e)
return None
else:
return output.decode('utf-8').strip()
@ -45,9 +45,14 @@ def create_dev(script, _id, dev, ip=None):
def delete_network_interface(iface):
try:
sp.check_output(['ip', 'link', 'del', iface])
sp.check_output(
[
'sudo', '-p', 'Enter password to remove {} network device: '.format(iface),
'ip', 'link', 'del', iface
], stderr=sp.PIPE
)
except Exception:
logger.exception('Interface Deletion failed')
logger.exception('Interface %s Deletion failed', iface)
def find_free_port():