2013-11-04 06:06:11 -08:00
|
|
|
#!/usr/bin/python
|
2013-11-13 16:07:15 -08:00
|
|
|
#
|
2014-07-22 11:15:56 -07:00
|
|
|
# Copyright 2014 Cumulus Networks, Inc. All rights reserved.
|
2013-11-13 16:07:15 -08:00
|
|
|
# Author: Roopa Prabhu, roopa@cumulusnetworks.com
|
|
|
|
#
|
|
|
|
# ifupdownBase --
|
|
|
|
# base object for various ifupdown objects
|
|
|
|
#
|
2013-11-04 06:06:11 -08:00
|
|
|
|
|
|
|
import logging
|
|
|
|
import subprocess
|
|
|
|
import re
|
2014-02-12 22:29:41 -08:00
|
|
|
import os
|
2014-07-17 11:44:36 -07:00
|
|
|
from iface import *
|
2014-10-12 13:50:05 -07:00
|
|
|
import rtnetlink_api as rtnetlink_api
|
2013-11-04 06:06:11 -08:00
|
|
|
|
|
|
|
class ifupdownBase(object):
|
|
|
|
|
|
|
|
def __init__(self):
|
|
|
|
modulename = self.__class__.__name__
|
|
|
|
self.logger = logging.getLogger('ifupdown.' + modulename)
|
|
|
|
|
|
|
|
def exec_command(self, cmd, cmdenv=None, nowait=False):
|
|
|
|
cmd_returncode = 0
|
|
|
|
cmdout = ''
|
|
|
|
try:
|
2014-02-26 08:09:44 -08:00
|
|
|
self.logger.info('Executing ' + cmd)
|
2014-04-04 15:00:59 -07:00
|
|
|
if self.DRYRUN:
|
|
|
|
return cmdout
|
2013-11-04 06:06:11 -08:00
|
|
|
ch = subprocess.Popen(cmd.split(),
|
|
|
|
stdout=subprocess.PIPE,
|
|
|
|
shell=False, env=cmdenv,
|
2014-06-03 11:11:47 -07:00
|
|
|
stderr=subprocess.STDOUT,
|
|
|
|
close_fds=True)
|
2013-11-04 06:06:11 -08:00
|
|
|
cmdout = ch.communicate()[0]
|
|
|
|
cmd_returncode = ch.wait()
|
|
|
|
except OSError, e:
|
|
|
|
raise Exception('could not execute ' + cmd +
|
|
|
|
'(' + str(e) + ')')
|
|
|
|
if cmd_returncode != 0:
|
|
|
|
raise Exception('error executing cmd \'%s\'' %cmd +
|
|
|
|
'\n(' + cmdout.strip('\n ') + ')')
|
|
|
|
return cmdout
|
2013-11-13 16:07:15 -08:00
|
|
|
|
|
|
|
def ignore_error(self, errmsg):
|
|
|
|
if (self.FORCE == True or re.search(r'exists', errmsg,
|
|
|
|
re.IGNORECASE | re.MULTILINE) is not None):
|
|
|
|
return True
|
|
|
|
return False
|
|
|
|
|
|
|
|
def log_warn(self, str):
|
|
|
|
if self.ignore_error(str) == False:
|
|
|
|
if self.logger.getEffectiveLevel() == logging.DEBUG:
|
|
|
|
traceback.print_stack()
|
|
|
|
self.logger.warn(str)
|
|
|
|
pass
|
|
|
|
|
|
|
|
def log_error(self, str):
|
|
|
|
if self.ignore_error(str) == False:
|
2014-01-16 06:46:17 -08:00
|
|
|
raise
|
|
|
|
#raise Exception(str)
|
2013-11-13 16:07:15 -08:00
|
|
|
else:
|
|
|
|
pass
|
2014-02-12 22:29:41 -08:00
|
|
|
|
|
|
|
def link_exists(self, ifacename):
|
|
|
|
return os.path.exists('/sys/class/net/%s' %ifacename)
|
2014-02-17 19:01:37 -08:00
|
|
|
|
|
|
|
def link_up(self, ifacename):
|
2014-10-12 13:50:05 -07:00
|
|
|
rtnetlink_api.rtnl_api.link_set(ifacename, "up")
|
2014-02-17 19:01:37 -08:00
|
|
|
|
|
|
|
def link_down(self, ifacename):
|
2014-10-12 13:50:05 -07:00
|
|
|
rtnetlink_api.rtnl_api.link_set(ifacename, "down")
|