]> git.proxmox.com Git - mirror_ifupdown2.git/blobdiff - pkg/iface.py
execute 'up' on upper devices if ifup is called with --with-depends
[mirror_ifupdown2.git] / pkg / iface.py
index 045096c1159f8a62a7a6b6d9ebbd4ccaba162297..3ea64894eb4eed5162c5bccfedccacd3497f9770 100644 (file)
@@ -34,7 +34,7 @@ class ifaceStatus():
         elif state == cls.ERROR:
             return 'error'
         elif state == cls.NOTFOUND:
-            return 'not found'
+            return 'notfound'
     
     @classmethod
     def from_str(cls, state_str):
@@ -107,19 +107,26 @@ class ifaceState():
         elif state_str == 'query-running':
             return cls.QUERY_RUNNING
 
-
 class ifaceJsonEncoder(json.JSONEncoder):
     def default(self, o):
+        retconfig = {}
+        if o.config:
+            for k, v in o.config.items():
+                if len(v) == 1:
+                    retconfig[k] = v[0]
+                else:
+                    retconfig[k] = v
+
         return OrderedDict({'name' : o.name,
                             'addr_method' : o.addr_method,
                             'addr_family' : o.addr_family,
                             'auto' : o.auto,
-                            'config' : o.config})
+                            'config' : retconfig})
 
 class iface():
-    """ config flags """
-    AUTO = 0x1
-    HOT_PLUG = 0x2
+    """ flags """
+    # flag to indicate that the object was created from pickled state
+    PICKLED = 0x1
 
     version = '0.1'
 
@@ -128,9 +135,9 @@ class iface():
         self.addr_family = None
         self.addr_method = None
         self.config = OrderedDict()
+        self.config_status = {}
         self.state = ifaceState.NEW
         self.status = ifaceStatus.UNKNOWN
-        self.errstr = ''
         self.flags = 0x0
         self.priv_flags = 0x0
         self.refcnt = 0
@@ -139,7 +146,6 @@ class iface():
         self.auto = False
         self.classes = []
         self.env = None
-        self.config_current = {}
         self.raw_lines = []
         self.linkstate = None
 
@@ -181,21 +187,14 @@ class iface():
 
     def is_config_present(self):
         addr_method = self.get_addr_method()
-        if addr_method is not None:
+        if addr_method:
             if (addr_method.find('dhcp') != -1 or
                     addr_method.find('dhcp6') != -1):
                 return True
-
-        if self.config is None:
+        if not self.config:
             return False
-
-        return (len(self.config) != 0)
-
-    def set_config_current(self, config_current):
-        self.config_current = config_current
-
-    def get_config_current(self):
-        return self.config_current
+        else:
+            return True
 
     def get_auto(self):
         return self.auto
@@ -220,9 +219,14 @@ class iface():
     def belongs_to_class(self, intfclass):
         if intfclass in self.classes:
             return True
-
         return False
 
+    def set_priv_flags(self, priv_flags):
+        self.priv_flags = priv_flags
+
+    def get_priv_flags(self):
+        return self.priv_flags
+
     def get_state(self):
         return self.state
 
@@ -241,6 +245,10 @@ class iface():
     def set_status(self, status):
         self.status = status
 
+    def set_state_n_status(self, state, status):
+        self.state = state
+        self.status = status
+
     def state_str_to_hex(self, state_str):
         return self.state_str_map.get(state_str)
 
@@ -283,7 +291,7 @@ class iface():
     def get_attr_value_first(self, attr_name):
         config = self.get_config()
         attr_value_list = config.get(attr_name)
-        if attr_value_list is not None:
+        if attr_value_list:
             return attr_value_list[0]
         return None
 
@@ -291,16 +299,15 @@ class iface():
         config = self.get_config()
 
         attr_value_list = config.get(attr_name)
-        if attr_value_list is not None:
+        if attr_value_list:
             try:
                 return attr_value_list[attr_index]
             except:
                 return None
-
         return None
 
     def get_env(self):
-        if self.env is None or len(self.env) == 0:
+        if not self.env:
             self.generate_env()
         return self.env
 
@@ -315,11 +322,11 @@ class iface():
             attr_env_name = 'IF_%s' %attr.upper()
             env[attr_env_name] = attr_value[0]
 
-        if len(env) > 0:
+        if env:
             self.set_env(env)
 
     def update_config(self, attr_name, attr_value):
-        if self.config.get(attr_name) is None:
+        if not self.config.get(attr_name):
             self.config[attr_name] = [attr_value]
         else:
             self.config[attr_name].append(attr_value)
@@ -328,19 +335,32 @@ class iface():
         self.config.update(attrdict)
 
     def update_config_with_status(self, attr_name, attr_value, attr_status=0):
-        if attr_value is None:
+        if not attr_value:
             attr_value = ''
+
+        if self.config.get(attr_name):
+            self.config[attr_name].append(attr_value)
+            self.config_status[attr_name].append(attr_status)
+        else:
+            self.config[attr_name] = [attr_value]
+            self.config_status[attr_name] = [attr_status]
+
+        # set global iface state
         if attr_status:
             self.set_status(ifaceStatus.ERROR)
-            new_attr_value = '%s (%s)' %(attr_value, crossmark)
-        else:
-            new_attr_value = '%s (%s)' %(attr_value, tickmark)
-            if self.get_status() != ifaceStatus.ERROR:
-                self.set_status(ifaceStatus.SUCCESS)
-        if self.config.get(attr_name) is not None:
-            self.config[attr_name].append(new_attr_value)
+        elif self.get_status() != ifaceStatus.ERROR:
+            # Not already error, mark success
+            self.set_status(ifaceStatus.SUCCESS)
+
+    def get_config_attr_status(self, attr_name, idx=0):
+        self.config_status.get(attr_name, [])[idx]
+
+    def get_config_attr_status_str(self, attr_name, idx=0):
+        ret = self.config_status.get(attr_name, [])[idx]
+        if ret:
+            return crossmark
         else:
-            self.config[attr_name] = [new_attr_value]
+            return tickmark
 
     def is_different(self, dstiface):
         if self.name != dstiface.name: return True
@@ -362,17 +382,30 @@ class iface():
         del odict['state']
         del odict['status']
         del odict['lowerifaces']
+        del odict['upperifaces']
         del odict['refcnt']
-
+        del odict['config_status']
+        del odict['flags']
+        del odict['priv_flags']
+        del odict['raw_lines']
+        del odict['linkstate']
+        del odict['env']
         return odict
 
     def __setstate__(self, dict):
         self.__dict__.update(dict)
+        self.config_status = {}
         self.state = ifaceState.NEW
         self.status = ifaceStatus.UNKNOWN
         self.refcnt = 0
+        self.flags = 0
         self.lowerifaces = None
+        self.upperifaces = None
         self.linkstate = None
+        self.env = None
+        self.priv_flags = 0
+        self.raw_lines = []
+        self.flags |= self.PICKLED
         
     def dump_raw(self, logger):
         indent = '  '
@@ -385,44 +418,48 @@ class iface():
         logger.info(self.get_name() + ' : {')
         logger.info(indent + 'family: %s' %self.get_addr_family())
         logger.info(indent + 'method: %s' %self.get_addr_method())
+        logger.info(indent + 'flags: %x' %self.flags)
         logger.info(indent + 'state: %s'
                 %ifaceState.to_str(self.get_state()))
         logger.info(indent + 'status: %s'
                 %ifaceStatus.to_str(self.get_status()))
         logger.info(indent + 'refcnt: %d' %self.get_refcnt())
-        d = self.get_lowerdevs()
-        if d is not None:
+        d = self.get_lowerifaces()
+        if d:
             logger.info(indent + 'lowerdevs: %s' %str(d))
         else:
             logger.info(indent + 'lowerdevs: None')
 
         logger.info(indent + 'config: ')
         config = self.get_config()
-        if config is not None:
+        if config:
             logger.info(indent + indent + str(config))
         logger.info('}')
 
-    def dump_pretty(self):
+    def dump_pretty(self, with_status=False):
         indent = '\t'
         outbuf = ''
         if self.get_auto():
             outbuf += 'auto %s\n' %self.get_name()
         outbuf += 'iface %s' %self.get_name()
-        if self.get_addr_family() is not None:
+        if self.get_addr_family():
             outbuf += ' %s' %self.get_addr_family()
-
-        if self.get_addr_method() is not None:
+        if self.get_addr_method():
             outbuf += ' %s' %self.get_addr_method()
-
         outbuf += '\n'
-
         config = self.get_config()
-        if config is not None:
+        if config:
             for cname, cvaluelist in config.items():
+                idx = 0
                 for cv in cvaluelist:
-                    outbuf += indent + '%s' %cname + ' %s\n' %cv
+                    if with_status:
+                        outbuf += indent + '%s %s %s\n' %(cname, cv,
+                                    self.get_config_attr_status_str(cname, idx))
+                    else:
+                        outbuf += indent + '%s %s\n' %(cname, cv)
+                    idx += 1
 
         print outbuf
 
-    def dump_json(self):
+    def dump_json(self, with_status=False):
         print json.dumps(self, cls=ifaceJsonEncoder, indent=4)