Remove get_metadata() from os_type classes
authorNikos Skalkotos <skalkoto@grnet.gr>
Fri, 18 May 2012 16:14:12 +0000 (19:14 +0300)
committerNikos Skalkotos <skalkoto@grnet.gr>
Fri, 18 May 2012 16:14:12 +0000 (19:14 +0300)
The metadata are collected in the constructor and are saved in the
meta variable. This way they can be accessed and altered by the
sysprep methods. There are cases where some metadata are not valid
and need to be updated after a sysprep action is performed. For
example, remove_user_accounts sysprep action may remove a user that
is present in the USERS metadata.

image_creator/main.py
image_creator/os_type/__init__.py
image_creator/os_type/ubuntu.py
image_creator/os_type/unix.py

index cbbc42b..afcdfcd 100644 (file)
@@ -185,7 +185,6 @@ def image_creator():
 
         osclass = get_os_class(dev.distro, dev.ostype)
         image_os = osclass(dev.root, dev.g)
-        metadata = image_os.get_metadata()
         output()
 
         for sysprep in options.disabled_syspreps:
@@ -204,6 +203,7 @@ def image_creator():
         if options.sysprep:
             image_os.do_sysprep()
 
+        metadata = image_os.meta
         dev.umount()
 
         size = options.shrink and dev.shrink() or dev.size
index 052a3e3..8a39846 100644 (file)
@@ -74,6 +74,13 @@ class OSBase(object):
         self.root = rootdev
         self.g = ghandler
 
+        # Collect metadata about the OS
+        self.meta = {}
+        self.meta['ROOT_PARTITION'] = "%d" % self.g.part_to_partnum(self.root)
+        self.meta['OSFAMILY'] = self.g.inspect_get_type(self.root)
+        self.meta['OS'] = self.g.inspect_get_distro(self.root)
+        self.meta['DESCRIPTION'] = self.g.inspect_get_product_name(self.root)
+
     def _is_sysprep(self, obj):
         return getattr(obj, 'sysprep', False) and callable(obj)
 
@@ -192,16 +199,6 @@ class OSBase(object):
             if has_ftype(f, ftype):
                 action(full_path)
 
-    def get_metadata(self):
-        """Returns some descriptive metadata about the OS."""
-        meta = {}
-        meta['ROOT_PARTITION'] = "%d" % self.g.part_to_partnum(self.root)
-        meta['OSFAMILY'] = self.g.inspect_get_type(self.root)
-        meta['OS'] = self.g.inspect_get_distro(self.root)
-        meta['DESCRIPTION'] = self.g.inspect_get_product_name(self.root)
-
-        return meta
-
     def do_sysprep(self):
         """Prepere system for image creation."""
 
index f5a82ea..f992981 100644 (file)
@@ -35,15 +35,15 @@ from image_creator.os_type.linux import Linux, sysprep
 
 
 class Ubuntu(Linux):
-    def get_metadata(self):
-        meta = super(Ubuntu, self).get_metadata()
+    def __init__(self, rootdev, ghandler):
+        super(Ubuntu, self).__init__(rootdev, ghandler)
+
         apps = self.g.inspect_list_applications(self.root)
         for app in apps:
             if app['app_name'] == 'kubuntu-desktop':
-                meta['OS'] = 'kubuntu'
-                meta['DESCRIPTION'] = \
-                            meta['DESCRIPTION'].replace('Ubuntu', 'Kubuntu')
+                self.meta['OS'] = 'kubuntu'
+                self.meta['DESCRIPTION'] = \
+                        self.meta['DESCRIPTION'].replace('Ubuntu', 'Kubuntu')
                 break
-        return meta
 
 # vim: set sta sts=4 shiftwidth=4 sw=4 et ai :
index 63f56ad..7eb7f76 100644 (file)
@@ -48,12 +48,12 @@ class Unix(OSBase):
         '.thunderbird'
     ]
 
-    def get_metadata(self):
-        meta = super(Unix, self).get_metadata()
-        meta["USERS"] = " ".join(self.get_passworded_users())
-        return meta
+    def __init__(self, rootdev, ghandler):
+        super(Unix, self).__init__(rootdev, ghandler)
 
-    def get_passworded_users(self):
+        self.meta["USERS"] = " ".join(self._get_passworded_users())
+
+    def _get_passworded_users(self):
         users = []
         regexp = re.compile('(\S+):((?:!\S+)|(?:[^!*]\S+)|):(?:\S*:){6}')
 
@@ -80,13 +80,18 @@ class Unix(OSBase):
         # Remove users from /etc/passwd
         passwd = []
         removed_users = {}
+        metadata_users = self.meta['USERS'].split()
         for line in self.g.cat('/etc/passwd').splitlines():
             fields = line.split(':')
             if int(fields[2]) > 1000:
                 removed_users[fields[0]] = fields
+                # remove it from the USERS metadata too
+                if fields[0] in metadata_users:
+                    metadata_users.remove(fields[0])
             else:
                 passwd.append(':'.join(fields))
 
+        self.meta['USERS'] = " ".join(metadata_users)
         self.g.write('/etc/passwd', '\n'.join(passwd) + '\n')
 
         # Remove the corresponding /etc/shadow entries