Bump version to 0.2.2
[snf-image-creator] / image_creator / os_type / unix.py
index 63f56ad..2cd8452 100644 (file)
@@ -35,7 +35,6 @@ import re
 import sys
 
 from image_creator.os_type import OSBase, sysprep
-from image_creator.util import warn, output
 
 
 class Unix(OSBase):
@@ -44,16 +43,20 @@ class Unix(OSBase):
         '.bash_history',
         '.gnupg',
         '.ssh',
-        '.mozilla',
-        '.thunderbird'
+        '.kamakirc',
+        '.kamaki.history'
     ]
 
-    def get_metadata(self):
-        meta = super(Unix, self).get_metadata()
-        meta["USERS"] = " ".join(self.get_passworded_users())
-        return meta
+    def __init__(self, rootdev, ghandler, output):
+        super(Unix, self).__init__(rootdev, ghandler, output)
 
-    def get_passworded_users(self):
+        self.meta["USERS"] = " ".join(self._get_passworded_users())
+        # Delete the USERS metadata if empty
+        if not len(self.meta['USERS']):
+            self.out.warn("No passworded users found!")
+            del self.meta['USERS']
+
+    def _get_passworded_users(self):
         users = []
         regexp = re.compile('(\S+):((?:!\S+)|(?:[^!*]\S+)|):(?:\S*:){6}')
 
@@ -64,7 +67,7 @@ class Unix(OSBase):
 
             user, passwd = match.groups()
             if len(passwd) > 0 and passwd[0] == '!':
-                warn("Ignoring locked %s account." % user)
+                self.out.warn("Ignoring locked %s account." % user)
             else:
                 users.append(user)
 
@@ -75,18 +78,32 @@ class Unix(OSBase):
         """Remove all user accounts with id greater than 1000"""
 
         if print_header:
-            output('Removing all user accounts with id greater than 1000')
+            self.out.output("Removing all user accounts with id greater than "
+                            "1000")
+
+        if 'USERS' not in self.meta:
+            return
 
         # 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)
+
+        # Delete the USERS metadata if empty
+        if not len(self.meta['USERS']):
+            del self.meta['USERS']
+
         self.g.write('/etc/passwd', '\n'.join(passwd) + '\n')
 
         # Remove the corresponding /etc/shadow entries
@@ -118,7 +135,8 @@ class Unix(OSBase):
         """Remove all passwords and lock all user accounts"""
 
         if print_header:
-            output('Cleaning up passwords & locking all user accounts')
+            self.out.output("Cleaning up passwords & locking all user "
+                            "accounts")
 
         shadow = []
 
@@ -136,7 +154,7 @@ class Unix(OSBase):
         """Remove all regular files under /var/cache"""
 
         if print_header:
-            output('Removing files under /var/cache')
+            self.out.output('Removing files under /var/cache')
 
         self.foreach_file('/var/cache', self.g.rm, ftype='r')
 
@@ -145,7 +163,7 @@ class Unix(OSBase):
         """Remove all files under /tmp and /var/tmp"""
 
         if print_header:
-            output('Removing files under /tmp and /var/tmp')
+            self.out.output('Removing files under /tmp and /var/tmp')
 
         self.foreach_file('/tmp', self.g.rm_rf, maxdepth=1)
         self.foreach_file('/var/tmp', self.g.rm_rf, maxdepth=1)
@@ -155,7 +173,7 @@ class Unix(OSBase):
         """Empty all files under /var/log"""
 
         if print_header:
-            output('Emptying all files under /var/log')
+            self.out.output('Emptying all files under /var/log')
 
         self.foreach_file('/var/log', self.g.truncate, ftype='r')
 
@@ -164,7 +182,7 @@ class Unix(OSBase):
         """Remove all files under /var/mail and /var/spool/mail"""
 
         if print_header:
-            output('Removing files under /var/mail and /var/spool/mail')
+            self.out.output('Removing files under /var/mail & /var/spool/mail')
 
         self.foreach_file('/var/spool/mail', self.g.rm_rf, maxdepth=1)
         self.foreach_file('/var/mail', self.g.rm_rf, maxdepth=1)
@@ -176,13 +194,15 @@ class Unix(OSBase):
         homedirs = ['/root'] + self.ls('/home/')
 
         if print_header:
-            output('Removing sensitive user data under %s' % " ".
-                                                        join(homedirs))
+            self.out.output("Removing sensitive user data under %s" %
+                            " ".join(homedirs))
 
         for homedir in homedirs:
             for data in self.sensitive_userdata:
                 fname = "%s/%s" % (homedir, data)
                 if self.g.is_file(fname):
                     self.g.scrub_file(fname)
+                elif self.g.is_dir(fname):
+                    self.foreach_file(fname, self.g.scrub_file, ftype='r')
 
 # vim: set sta sts=4 shiftwidth=4 sw=4 et ai :