2019-12-17 16:40:10 +01:00
|
|
|
#!/usr/bin/env python3
|
2014-10-09 16:02:46 -07:00
|
|
|
#
|
2018-12-13 11:43:32 -08:00
|
|
|
# Copyright 2014-2017 Cumulus Networks, Inc. All rights reserved.
|
2014-10-09 16:02:46 -07:00
|
|
|
# Author: Roopa Prabhu, roopa@cumulusnetworks.com
|
|
|
|
#
|
|
|
|
|
|
|
|
import time
|
|
|
|
import logging
|
|
|
|
|
2018-12-13 11:43:32 -08:00
|
|
|
try:
|
|
|
|
from ifupdown2.ifupdown.iface import *
|
|
|
|
from ifupdown2.ifupdown.utils import utils
|
|
|
|
from ifupdown2.ifupdownaddons.cache import *
|
|
|
|
|
|
|
|
import ifupdown2.ifupdown.ifupdownflags as ifupdownflags
|
2019-12-17 17:25:32 +01:00
|
|
|
except (ImportError, ModuleNotFoundError):
|
2018-12-13 11:43:32 -08:00
|
|
|
from ifupdown.iface import *
|
|
|
|
from ifupdown.utils import utils
|
|
|
|
from ifupdownaddons.cache import *
|
|
|
|
|
|
|
|
import ifupdown.ifupdownflags as ifupdownflags
|
|
|
|
|
|
|
|
|
2014-10-09 16:02:46 -07:00
|
|
|
def profile(func):
|
|
|
|
def wrap(*args, **kwargs):
|
|
|
|
started_at = time.time()
|
|
|
|
result = func(*args, **kwargs)
|
2019-12-17 16:55:49 +01:00
|
|
|
print(str(func))
|
|
|
|
print((time.time() - started_at))
|
2014-10-09 16:02:46 -07:00
|
|
|
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)
|
2019-12-17 16:55:49 +01:00
|
|
|
except IOError as e:
|
2019-12-09 22:31:46 +01:00
|
|
|
self.logger.warning('error writing to file %s'
|
2014-10-09 16:02:46 -07:00
|
|
|
%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()
|
2020-06-24 01:12:24 +02:00
|
|
|
except Exception:
|
2014-10-09 16:02:46 -07:00
|
|
|
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')
|
2020-06-24 01:12:24 +02:00
|
|
|
except Exception:
|
2014-10-09 16:02:46 -07:00
|
|
|
return None
|
|
|
|
return None
|
|
|
|
|
|
|
|
def sysctl_set(self, variable, value):
|
2018-12-13 11:43:32 -08:00
|
|
|
utils.exec_command('%s %s=%s' %
|
|
|
|
(utils.sysctl_cmd, variable, value))
|
2014-10-09 16:02:46 -07:00
|
|
|
|
|
|
|
def sysctl_get(self, variable):
|
2018-12-13 11:43:32 -08:00
|
|
|
return utils.exec_command('%s %s' %
|
|
|
|
(utils.sysctl_cmd,
|
|
|
|
variable)).split('=')[1].strip()
|