2014-10-09 16:02:46 -07:00
|
|
|
#!/usr/bin/python
|
|
|
|
#
|
|
|
|
# Copyright 2014 Cumulus Networks, Inc. All rights reserved.
|
|
|
|
# Author: Roopa Prabhu, roopa@cumulusnetworks.com
|
|
|
|
#
|
|
|
|
|
|
|
|
import logging
|
|
|
|
import re
|
|
|
|
import io
|
2016-04-10 18:55:56 +02:00
|
|
|
|
|
|
|
from ifupdown.utils import utils
|
2016-04-14 14:45:47 -07:00
|
|
|
import ifupdown.ifupdownflags as ifupdownflags
|
2014-10-09 16:02:46 -07:00
|
|
|
from ifupdown.iface import *
|
|
|
|
from cache import *
|
|
|
|
import time
|
|
|
|
import logging
|
|
|
|
|
|
|
|
def profile(func):
|
|
|
|
def wrap(*args, **kwargs):
|
|
|
|
started_at = time.time()
|
|
|
|
result = func(*args, **kwargs)
|
|
|
|
print str(func)
|
|
|
|
print (time.time() - started_at)
|
|
|
|
return result
|
|
|
|
return wrap
|
|
|
|
|
|
|
|
class utilsBase(object):
|
|
|
|
""" Base class for ifupdown addon utilities """
|
|
|
|
|
|
|
|
def __init__(self, *args, **kargs):
|
|
|
|
modulename = self.__class__.__name__
|
|
|
|
self.logger = logging.getLogger('ifupdown.' + modulename)
|
|
|
|
|
|
|
|
def write_file(self, filename, strexpr):
|
|
|
|
try:
|
|
|
|
self.logger.info('writing \'%s\'' %strexpr +
|
|
|
|
' to file %s' %filename)
|
2016-04-14 14:45:47 -07:00
|
|
|
if ifupdownflags.flags.DRYRUN:
|
2014-10-09 16:02:46 -07:00
|
|
|
return 0
|
|
|
|
with open(filename, 'w') as f:
|
|
|
|
f.write(strexpr)
|
|
|
|
except IOError, e:
|
|
|
|
self.logger.warn('error writing to file %s'
|
|
|
|
%filename + '(' + str(e) + ')')
|
|
|
|
return -1
|
|
|
|
return 0
|
|
|
|
|
|
|
|
def read_file(self, filename):
|
|
|
|
try:
|
|
|
|
self.logger.debug('reading \'%s\'' %filename)
|
|
|
|
with open(filename, 'r') as f:
|
|
|
|
return f.readlines()
|
|
|
|
except:
|
|
|
|
return None
|
|
|
|
return None
|
|
|
|
|
|
|
|
def read_file_oneline(self, filename):
|
|
|
|
try:
|
|
|
|
self.logger.debug('reading \'%s\'' %filename)
|
|
|
|
with open(filename, 'r') as f:
|
|
|
|
return f.readline().strip('\n')
|
|
|
|
except:
|
|
|
|
return None
|
|
|
|
return None
|
|
|
|
|
|
|
|
def sysctl_set(self, variable, value):
|
2016-05-13 19:52:57 +02:00
|
|
|
utils.exec_command('sysctl %s=%s' % (variable, value))
|
2014-10-09 16:02:46 -07:00
|
|
|
|
|
|
|
def sysctl_get(self, variable):
|
2016-05-13 19:52:57 +02:00
|
|
|
return utils.exec_command('sysctl %s' % variable).split('=')[1].strip()
|