statemanager: configure state_dir via ifupdown2.conf

ifupdown2 used /var/tmp/network/ to store its state file
upstream users reported that when /var/tmp is not mounted
before network configuration ifupdown2 fails. We now let
user define which location they want to use for the state
file.

closes: #918832

Reported-by: Maximilian Wilhelm <max@sdn.clinic>
Signed-off-by: Julien Fortin <julien@cumulusnetworks.com>
This commit is contained in:
Julien Fortin 2019-01-11 12:00:39 +08:00
parent 72ba456971
commit 9f98f3604e
4 changed files with 54 additions and 10 deletions

7
debian/changelog vendored
View File

@ -1,3 +1,10 @@
ifupdown2 (1.2.4-1) unstable; urgency=medium
* Fix: statemanager directory path customization via ifupdown2.conf
(closes: #918832)
-- Julien Fortin <julien@cumulusnetworks.com> Fri, 11 Jan 2019 23:42:42 +0000
ifupdown2 (1.2.3-1) unstable; urgency=medium
* Fix: log: use stderr if syslog initialization fails (closes: #917534)

View File

@ -80,3 +80,9 @@ ifaceobj_squash=0
# based on the physical interface they are running on top of.
# set this flag to 0 to disable this behaviour
adjust_logical_dev_mtu=1
# directory where the state file is stored
# if this directory doesn't exists ifupdown2 will create it
# if directory creation fails or state_dir variable is empty
# state_dir will default to /run/network/
state_dir=/run/network/

View File

@ -287,6 +287,11 @@ class ifupdownMain(ifupdownBase):
self._cache_no_repeats = {}
# initialize global config object with config passed by the user
# This makes config available to addon modules
ifupdownConfig.config = self.config
statemanager.statemanager_api.init()
if self.flags.STATEMANAGER_ENABLE:
self.statemanager = statemanager.statemanager_api
try:
@ -323,10 +328,6 @@ class ifupdownMain(ifupdownBase):
self._ifaceobj_squash_internal = True if self.config.get(
'ifaceobj_squash_internal', '1') == '1' else False
# initialize global config object with config passed by the user
# This makes config available to addon modules
ifupdownConfig.config = self.config
self.validate_keywords = {
'<mac>': self._keyword_mac,
'<text>': self._keyword_text,

View File

@ -15,10 +15,12 @@ try:
from ifupdown2.ifupdown.iface import *
import ifupdown2.ifupdown.exceptions as exceptions
import ifupdown2.ifupdown.ifupdownconfig as ifupdownConfig
except ImportError:
from ifupdown.iface import *
import ifupdown.exceptions as exceptions
import ifupdown.ifupdownconfig as ifupdownConfig
class pickling():
@ -61,8 +63,7 @@ class stateManager():
"""
state_dir = '/var/tmp/network/'
"""directory where the state file is stored """
__DEFAULT_STATE_DIR = "/run/network/"
state_filename = 'ifstatenew'
"""name of the satefile """
@ -78,14 +79,43 @@ class stateManager():
which includes a dictionary of last pickled iface objects
"""
self.state_dir = None
self.state_file = None
self.ifaceobjdict = OrderedDict()
self.logger = logging.getLogger('ifupdown.' +
self.__class__.__name__)
if not os.path.exists(self.state_dir):
os.mkdir(self.state_dir)
def init(self):
self.state_dir = ifupdownConfig.config.get("state_dir")
used_default = False
if not self.state_dir:
self.logger.debug("statemanager: state_dir not defined in config file, using default: %s" % self.__DEFAULT_STATE_DIR)
self.state_dir = self.__DEFAULT_STATE_DIR
used_default = True
try:
self._init_makedirs_state_dir()
except Exception as e:
if used_default:
# if the default path was used but still throws an exception...
raise
self.logger.info("statemanager: %s: using default directory: %s" % (e, self.__DEFAULT_STATE_DIR))
self.state_dir = self.__DEFAULT_STATE_DIR
try:
self._init_makedirs_state_dir()
except Exception as e:
raise Exception("statemanager: unable to create required directory: %s" % str(e))
if not os.path.exists(self.state_rundir):
os.mkdir(self.state_rundir)
self.state_file = self.state_dir + self.state_filename
os.makedirs(self.state_rundir)
self.state_file = "%s/%s" % (self.state_dir, self.state_filename)
def _init_makedirs_state_dir(self):
if not os.path.exists(self.state_dir):
os.makedirs(self.state_dir)
def save_ifaceobj(self, ifaceobj):
self.ifaceobjdict.setdefault(ifaceobj.name,