Revision aa2062ba
b/image_creator/__init__.py | ||
---|---|---|
32 | 32 |
# or implied, of GRNET S.A. |
33 | 33 |
|
34 | 34 |
__version__ = '0.1' |
35 |
|
|
36 |
import image_creator.os_type |
|
37 |
|
|
38 |
def get_os_class(distro, osfamily): |
|
39 |
module = None |
|
40 |
classname = None |
|
41 |
try: |
|
42 |
module = __import__("image_creator.os_type.%s" |
|
43 |
% distro, fromlist=['image_creator.os_type']) |
|
44 |
classname = distro.capitalize() |
|
45 |
except ImportError: |
|
46 |
module = __import__("image_creator.os_type.%s" |
|
47 |
% osfamily, fromlist=['image_creator.os_type']) |
|
48 |
classname = osfamily.capitalize() |
|
49 |
|
|
50 |
return getattr(module, classname) |
|
51 |
|
|
52 |
|
|
53 |
# vim: set sta sts=4 shiftwidth=4 sw=4 et ai : |
b/image_creator/disk.py | ||
---|---|---|
95 | 95 |
raise DiskError("Multiple operating systems found") |
96 | 96 |
|
97 | 97 |
self.root = roots[0] |
98 |
self.ostype = self.g.inspect_get_type(self.root) |
|
99 |
self.distro = self.g.inspect_get_distro(self.root) |
|
98 | 100 |
|
99 | 101 |
def destroy(self): |
100 | 102 |
self.g.umount_all() |
101 | 103 |
self.g.sync() |
102 | 104 |
# Close the guestfs handler |
105 |
self.g.close() |
|
103 | 106 |
del self.g |
104 | 107 |
|
105 |
def get_image_metadata(self): |
|
106 |
meta = {} |
|
107 |
meta["OSFAMILY"] = self.g.inspect_get_type(self.root) |
|
108 |
meta["OS"] = self.g.inspect_get_distro(self.root) |
|
109 |
meta["description"] = self.g.inspect_get_product_name(self.root) |
|
110 |
return meta |
|
111 |
|
|
112 | 108 |
def mount(self): |
113 | 109 |
mps = g.inspect_get_mountpoints(self.root) |
114 | 110 |
# Sort the keys to mount the fs in a correct order. |
b/image_creator/main.py | ||
---|---|---|
31 | 31 |
# interpreted as representing official policies, either expressed |
32 | 32 |
# or implied, of GRNET S.A. |
33 | 33 |
|
34 |
from image_creator import get_os_class |
|
34 | 35 |
from image_creator.disk import Disk |
35 | 36 |
import sys |
36 | 37 |
import os |
... | ... | |
45 | 46 |
disk = Disk(source) |
46 | 47 |
try: |
47 | 48 |
dev = disk.get_device() |
48 |
metadata = dev.get_image_metadata() |
|
49 |
osclass = get_os_class(dev.distro, dev.ostype) |
|
50 |
image_os = osclass(dev.root, dev.g) |
|
51 |
metadata = image_os.get_metadata() |
|
49 | 52 |
for key, val in metadata.iteritems(): |
50 | 53 |
print "%s=%s" % (key,val) |
54 |
|
|
51 | 55 |
finally: |
52 | 56 |
disk.cleanup() |
53 | 57 |
|
b/image_creator/os_type/__init__.py | ||
---|---|---|
1 |
#!/usr/bin/env python |
|
2 |
|
|
3 |
class OSBase(object): |
|
4 |
def __init__(self, rootdev, ghandler): |
|
5 |
self.root = rootdev |
|
6 |
self.g = ghandler |
|
7 |
|
|
8 |
def get_metadata(self): |
|
9 |
meta = {} |
|
10 |
meta["OSFAMILY"] = self.g.inspect_get_type(self.root) |
|
11 |
meta["OS"] = self.g.inspect_get_distro(self.root) |
|
12 |
meta["description"] = self.g.inspect_get_product_name(self.root) |
|
13 |
|
|
14 |
return meta |
|
15 |
|
|
16 |
def mount_all(self): |
|
17 |
mps = g.inspect_get_mountpoints(self.root) |
|
18 |
# Sort the keys to mount the fs in a correct order. |
|
19 |
# / should be mounted befor /boot, etc |
|
20 |
def compare (a, b): |
|
21 |
if len(a[0]) > len(b[0]): return 1 |
|
22 |
elif len(a[0]) == len(b[0]): return 0 |
|
23 |
else: return -1 |
|
24 |
mps.sort(compare) |
|
25 |
for mp, dev in mps: |
|
26 |
try: |
|
27 |
self.g.mount(dev, mp) |
|
28 |
except RuntimeError as msg: |
|
29 |
print "%s (ignored)" % msg |
|
30 |
|
|
31 |
def cleanup_sensitive_data(self): |
|
32 |
raise NotImplementedError |
|
33 |
|
|
34 |
# vim: set sta sts=4 shiftwidth=4 sw=4 et ai : |
b/image_creator/os_type/freebsd.py | ||
---|---|---|
1 |
from image_creator.os_type.unix import Unix |
|
2 |
|
|
3 |
class Freebsd(Unix): |
|
4 |
pass |
|
5 |
|
|
6 |
# vim: set sta sts=4 shiftwidth=4 sw=4 et ai : |
b/image_creator/os_type/hurd.py | ||
---|---|---|
1 |
from image_creator.os_type.unix import Unix |
|
2 |
|
|
3 |
class Hard(Unix): |
|
4 |
pass |
|
5 |
|
|
6 |
# vim: set sta sts=4 shiftwidth=4 sw=4 et ai : |
b/image_creator/os_type/linux.py | ||
---|---|---|
1 |
from image_creator.os_type.unix import Unix |
|
2 |
|
|
3 |
class Linux(Unix): |
|
4 |
pass |
|
5 |
|
|
6 |
# vim: set sta sts=4 shiftwidth=4 sw=4 et ai : |
b/image_creator/os_type/netbsd.py | ||
---|---|---|
1 |
from image_creator.os_type.unix import Unix |
|
2 |
|
|
3 |
class Netbsd(Unix): |
|
4 |
pass |
|
5 |
|
|
6 |
# vim: set sta sts=4 shiftwidth=4 sw=4 et ai : |
b/image_creator/os_type/unix.py | ||
---|---|---|
1 |
#!/usr/bin/env python |
|
2 |
|
|
3 |
import re |
|
4 |
|
|
5 |
from image_creator.os_type import OSBase |
|
6 |
|
|
7 |
class Unix(OSBase): |
|
8 |
def get_metadata(self): |
|
9 |
meta = super(Unix, self).get_metadata() |
|
10 |
meta["USERS"] = " ".join(self.get_passworded_users()) |
|
11 |
return meta |
|
12 |
|
|
13 |
def get_passworded_users(self): |
|
14 |
|
|
15 |
users = [] |
|
16 |
|
|
17 |
regexp = re.compile('(\S+):((?:!\S+)|(?:[^!*]\S+)|):(?:\S*:){6}') |
|
18 |
|
|
19 |
for line in open('/etc/shadow', 'r').readlines(): |
|
20 |
match = regexp.match(line) |
|
21 |
if not match: |
|
22 |
continue |
|
23 |
|
|
24 |
user, passwd = match.groups() |
|
25 |
if len(passwd) > 0 and passwd[0] == '!': |
|
26 |
print "Warning: %s is locked" % user |
|
27 |
else: |
|
28 |
users.append(user) |
|
29 |
|
|
30 |
return users |
|
31 |
|
|
32 |
def cleanup_sensitive_data(self): |
|
33 |
cleanup_userdata() |
|
34 |
cleanup_tmp() |
|
35 |
cleanup_log() |
|
36 |
|
|
37 |
# vim: set sta sts=4 shiftwidth=4 sw=4 et ai : |
b/image_creator/os_type/windows.py | ||
---|---|---|
1 |
from image_creator.os_type import OSBase |
|
2 |
|
|
3 |
class Windows(OSBase): |
|
4 |
pass |
|
5 |
|
|
6 |
# vim: set sta sts=4 shiftwidth=4 sw=4 et ai : |
Also available in: Unified diff