2013-11-04 06:06:11 -08:00
|
|
|
#!/usr/bin/python
|
2013-11-13 16:07:15 -08:00
|
|
|
#
|
2018-12-13 11:43:32 -08:00
|
|
|
# Copyright 2014-2017 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 re
|
2014-02-12 22:29:41 -08:00
|
|
|
import os
|
2018-12-13 11:43:32 -08:00
|
|
|
import logging
|
2016-05-20 17:00:50 +02:00
|
|
|
import traceback
|
2016-04-10 18:55:56 +02:00
|
|
|
|
2018-12-13 11:43:32 -08:00
|
|
|
try:
|
|
|
|
from ifupdown2.ifupdown.netlink import netlink
|
|
|
|
|
|
|
|
import ifupdown2.ifupdown.ifupdownflags as ifupdownflags
|
|
|
|
except ImportError:
|
|
|
|
from ifupdown.netlink import netlink
|
|
|
|
|
|
|
|
import ifupdown.ifupdownflags as ifupdownflags
|
|
|
|
|
2013-11-04 06:06:11 -08:00
|
|
|
|
|
|
|
class ifupdownBase(object):
|
|
|
|
|
|
|
|
def __init__(self):
|
|
|
|
modulename = self.__class__.__name__
|
|
|
|
self.logger = logging.getLogger('ifupdown.' + modulename)
|
|
|
|
|
2013-11-13 16:07:15 -08:00
|
|
|
def ignore_error(self, errmsg):
|
2016-04-14 14:45:47 -07:00
|
|
|
if (ifupdownflags.flags.FORCE == True or re.search(r'exists', errmsg,
|
2013-11-13 16:07:15 -08:00
|
|
|
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()
|
2018-12-13 11:43:32 -08:00
|
|
|
traceback.print_exc()
|
2013-11-13 16:07:15 -08:00
|
|
|
self.logger.warn(str)
|
|
|
|
pass
|
|
|
|
|
|
|
|
def log_error(self, str):
|
|
|
|
if self.ignore_error(str) == False:
|
2018-12-13 11:43:32 -08:00
|
|
|
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):
|
2016-05-29 18:04:23 +01:00
|
|
|
netlink.link_set_updown(ifacename, "up")
|
2014-02-17 19:01:37 -08:00
|
|
|
|
|
|
|
def link_down(self, ifacename):
|
2016-05-29 18:04:23 +01:00
|
|
|
netlink.link_set_updown(ifacename, "down")
|