]> git.proxmox.com Git - mirror_ifupdown2.git/commitdiff
statemanager: configure state_dir via ifupdown2.conf
authorJulien Fortin <julien@cumulusnetworks.com>
Fri, 11 Jan 2019 04:00:39 +0000 (12:00 +0800)
committerJulien Fortin <julien@cumulusnetworks.com>
Fri, 11 Jan 2019 04:08:10 +0000 (12:08 +0800)
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>
debian/changelog
etc/network/ifupdown2/ifupdown2.conf
ifupdown2/ifupdown/ifupdownmain.py
ifupdown2/ifupdown/statemanager.py

index 60639395e43710463e6ce4c1401524b47f764002..cd62e83b66a40fa9811cbdb27bb3a5e8b44cbb39 100644 (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)
index e1035d6ac3ab7d0237f7730ce2f9d7cfd09cad84..e05c35f689c9e87563088348ebdd037eab5153ae 100644 (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/
index 0633dc36ceefaaf73de687a7bc2b6830fc68f26b..f520994511d5486ecb585cc73cb7dc06779430d6 100644 (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,
index 06154562fdb04c36ead519a3574b667947138a15..5e8f1c748ab476d4545f3ea4560144890b5524d4 100644 (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,