Add input options and populate image metadata
[snf-image-creator] / image_creator / os_type / unix.py
1 #!/usr/bin/env python
2
3 import re
4 import sys
5
6 from image_creator.os_type import OSBase
7
8
9 class Unix(OSBase):
10
11     sensitive_userdata = ['.bash_history']
12
13     def get_metadata(self):
14         meta = super(Unix, self).get_metadata()
15         meta["USERS"] = " ".join(self.get_passworded_users())
16         return meta
17
18     def get_passworded_users(self):
19         users = []
20         regexp = re.compile('(\S+):((?:!\S+)|(?:[^!*]\S+)|):(?:\S*:){6}')
21
22         for line in open('/etc/shadow', 'r').readlines():
23             match = regexp.match(line)
24             if not match:
25                 continue
26
27             user, passwd = match.groups()
28             if len(passwd) > 0 and passwd[0] == '!':
29                 print "Warning: %s is locked" % user
30             else:
31                 users.append(user)
32
33         return users
34
35     def data_cleanup(self):
36         self.cleanup_userdata()
37         self.cleanup_tmp()
38         self.cleanup_log()
39
40     def cleanup_tmp(self):
41         self.foreach_file('/tmp', self.g.rm_rf, maxdepth=1)
42
43     def cleanup_log(self):
44         self.foreach_file('/var/log', self.g.truncate, ftype='r')
45
46     def cleanup_userdata(self):
47         homedirs = ['/root'] + self.ls('/home/')
48
49         for homedir in homedirs:
50             for data in self.sensitive_userdata:
51                 fname = "%s/%s" % (homedir, data)
52                 if self.g.is_file(fname):
53                     self.g.scrub_file(fname)
54
55 # vim: set sta sts=4 shiftwidth=4 sw=4 et ai :