Fix code for cleanup of image data
authorNikos Skalkotos <skalkoto@grnet.gr>
Wed, 22 Feb 2012 16:22:17 +0000 (18:22 +0200)
committerNikos Skalkotos <skalkoto@grnet.gr>
Wed, 22 Feb 2012 16:22:17 +0000 (18:22 +0200)
image_creator/disk.py
image_creator/main.py
image_creator/os_type/__init__.py
image_creator/os_type/unix.py

index 170d0fb..b220dff 100644 (file)
@@ -86,6 +86,9 @@ class DiskDevice(object):
         self.bootable = bootable
 
         self.g = guestfs.GuestFS()
+
+        self.g.set_trace(1)
+
         self.g.add_drive_opts(device, readonly = 0)
         self.g.launch()
         roots = self.g.inspect_os()
@@ -106,7 +109,7 @@ class DiskDevice(object):
         del self.g
     
     def mount(self):
-        mps = g.inspect_get_mountpoints(self.root)
+        mps = self.g.inspect_get_mountpoints(self.root)
         # Sort the keys to mount the fs in a correct order.
         # / should be mounted befor /boot, etc
         def compare (a, b):
index 574d1a2..c79f1f1 100644 (file)
@@ -46,12 +46,15 @@ def main():
     disk = Disk(source)
     try:
         dev = disk.get_device()
+        dev.mount()
         osclass = get_os_class(dev.distro, dev.ostype)
         image_os = osclass(dev.root, dev.g)
         metadata = image_os.get_metadata()
         for key, val in metadata.iteritems():
             print "%s=%s" % (key,val)
 
+        image_os.data_cleanup()
+
     finally:
         disk.cleanup()
 
index 59da31a..e7d4c10 100644 (file)
@@ -1,10 +1,22 @@
 #!/usr/bin/env python
 
+def add_prefix(target):
+    def wrapper(self, *args):
+        prefix = args[0]
+        return map(lambda x: prefix + x, target(self, *args))
+    return wrapper
+
 class OSBase(object):
     def __init__(self, rootdev, ghandler):
         self.root = rootdev
         self.g = ghandler
 
+    @add_prefix
+    def ls(self, directory): return self.g.ls(directory)
+
+    @add_prefix
+    def find(self, directory): return self.g.find(directory)
+
     def get_metadata(self):
         meta = {}
         meta["OSFAMILY"] = self.g.inspect_get_type(self.root)
index ad65c15..f5049aa 100644 (file)
@@ -5,15 +5,16 @@ import re
 from image_creator.os_type import OSBase
 
 class Unix(OSBase):
+
+    sensitive_userdata = ['.bash_history']
+
     def get_metadata(self):
         meta = super(Unix, self).get_metadata()
         meta["USERS"] = " ".join(self.get_passworded_users())
         return meta
 
     def get_passworded_users(self):
-        
         users = []
-
         regexp = re.compile('(\S+):((?:!\S+)|(?:[^!*]\S+)|):(?:\S*:){6}')
 
         for line in open('/etc/shadow', 'r').readlines():
@@ -29,9 +30,33 @@ class Unix(OSBase):
 
         return users
 
-    def cleanup_sensitive_data(self):
-        cleanup_userdata()
-        cleanup_tmp()
-        cleanup_log()
+    def data_cleanup(self):
+        self.cleanup_userdata()
+        self.cleanup_tmp()
+        self.cleanup_log()
+
+    def cleanup_tmp(self):
+        files = []
+        files.extend(self.ls('/tmp/'))
+        files.extend(self.ls('/var/tmp/'))
+    
+        for filename in files:
+            self.g.rm_rf(filename)
+
+    def cleanup_log(self):
+        files = self.find( '/var/log/')
+
+        for filename in filter(self.g.is_file, files):
+            self.g.truncate(filename)
+        
+    def cleanup_userdata(self):
+        homedirs = ['/root'] + self.ls('/home/')
+
+        for homedir in homedirs:
+            for data in self.sensitive_userdata:
+                fname = "%s/%s" % (homedir, data)
+                print "Filename: %s\n" % fname
+                if self.g.is_file(fname):
+                    self.g.scrub_file(fname)
 
 # vim: set sta sts=4 shiftwidth=4 sw=4 et ai :