config, rootfs: support for removing temporary user and group accounts
authorTimo Savola <tsavola@movial.fi>
Tue, 22 Jul 2008 16:10:16 +0000 (19:10 +0300)
committerTimo Savola <tsavola@movial.fi>
Tue, 22 Jul 2008 16:10:46 +0000 (19:10 +0300)
RootFSRemoveAccount(uid=<int>, gid=<int>)

matrix/config.py
matrix/rootfs.py

index 384a2ce..a759968 100644 (file)
@@ -178,6 +178,10 @@ def directory_owners(owners):
 def device_nodes(devices):
        rootfs.devices.update(devices)
 
+def remove_account(uid=None, gid=None):
+       rootfs.remove_uid = uid
+       rootfs.remove_gid = gid
+
 class RootFS(object):
        def __init__(self):
                self.path = None
@@ -189,6 +193,8 @@ class RootFS(object):
                self.created_paths = []
                self.devices = {}
                self.directory_owners = {}
+               self.remove_uid = None
+               self.remove_gid = None
 
 class Config(object):
        def __init__(self):
@@ -240,6 +246,7 @@ actions = dict(
        RootFSCreatePaths = created_paths,
        RootFSDirectoryOwners = directory_owners,
        RootFSDeviceNodes = device_nodes,
+       RootFSRemoveAccount = remove_account,
 )
 
 config = Config()
index 9ba2148..76da00c 100644 (file)
@@ -327,6 +327,32 @@ class Builder(object):
                else:
                        os.system('touch /tmp/env.faked')
 
+               # Remove temporary accounts
+
+               def rewrite_accounts(path, filter):
+                       file = open(path)
+                       lines = [line for line in file if filter(line)]
+                       file.close()
+
+                       file = open(path, 'w')
+                       for line in lines:
+                               print >>file, line
+                       file.close()
+
+               def filter_passwd(line):
+                       return int(line.split(':')[2]) != rootfs.remove_uid
+
+               def filter_group(line):
+                       return int(line.split(':')[2]) != rootfs.remove_gid
+
+               if rootfs.remove_uid is not None:
+                       path = os.path.join(basepath, 'etc/passwd')
+                       rewrite_accounts(path, filter_passwd)
+
+               if rootfs.remove_gid is not None:
+                       path = os.path.join(basepath, 'etc/group')
+                       rewrite_accounts(path, filter_group)
+
                # Create device nodes
                print "Creating device nodes..."
                for d in self.devices.keys():