diff --git a/image_creator/__init__.py b/image_creator/__init__.py index 817a9c6569e1225a6f13fa40ec6bf6d1921c4972..aec50d661ecec0ddf5ac19fc2fbab511ac769c45 100644 --- a/image_creator/__init__.py +++ b/image_creator/__init__.py @@ -32,3 +32,22 @@ # or implied, of GRNET S.A. __version__ = '0.1' + +import image_creator.os_type + +def get_os_class(distro, osfamily): + module = None + classname = None + try: + module = __import__("image_creator.os_type.%s" + % distro, fromlist=['image_creator.os_type']) + classname = distro.capitalize() + except ImportError: + module = __import__("image_creator.os_type.%s" + % osfamily, fromlist=['image_creator.os_type']) + classname = osfamily.capitalize() + + return getattr(module, classname) + + +# vim: set sta sts=4 shiftwidth=4 sw=4 et ai : diff --git a/image_creator/disk.py b/image_creator/disk.py index 4a6b78f27e43fae2291746f4b0bd5200443840e3..170d0fbe4818f9fcaf69fbad627f07ee390e114c 100644 --- a/image_creator/disk.py +++ b/image_creator/disk.py @@ -95,20 +95,16 @@ class DiskDevice(object): raise DiskError("Multiple operating systems found") self.root = roots[0] + self.ostype = self.g.inspect_get_type(self.root) + self.distro = self.g.inspect_get_distro(self.root) def destroy(self): self.g.umount_all() self.g.sync() # Close the guestfs handler + self.g.close() del self.g - def get_image_metadata(self): - meta = {} - meta["OSFAMILY"] = self.g.inspect_get_type(self.root) - meta["OS"] = self.g.inspect_get_distro(self.root) - meta["description"] = self.g.inspect_get_product_name(self.root) - return meta - def mount(self): mps = g.inspect_get_mountpoints(self.root) # Sort the keys to mount the fs in a correct order. diff --git a/image_creator/main.py b/image_creator/main.py index 70409310e98e66096808cc2542cbc4748417b598..574d1a221f18708897b4aefeb430657ffb6c6d3e 100644 --- a/image_creator/main.py +++ b/image_creator/main.py @@ -31,6 +31,7 @@ # interpreted as representing official policies, either expressed # or implied, of GRNET S.A. +from image_creator import get_os_class from image_creator.disk import Disk import sys import os @@ -45,9 +46,12 @@ def main(): disk = Disk(source) try: dev = disk.get_device() - metadata = dev.get_image_metadata() + 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) + finally: disk.cleanup() diff --git a/image_creator/os_type/__init__.py b/image_creator/os_type/__init__.py new file mode 100644 index 0000000000000000000000000000000000000000..59da31aad57322b1abaa2c4ec17081d3bdc40ba2 --- /dev/null +++ b/image_creator/os_type/__init__.py @@ -0,0 +1,34 @@ +#!/usr/bin/env python + +class OSBase(object): + def __init__(self, rootdev, ghandler): + self.root = rootdev + self.g = ghandler + + def get_metadata(self): + meta = {} + meta["OSFAMILY"] = self.g.inspect_get_type(self.root) + meta["OS"] = self.g.inspect_get_distro(self.root) + meta["description"] = self.g.inspect_get_product_name(self.root) + + return meta + + def mount_all(self): + mps = 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): + if len(a[0]) > len(b[0]): return 1 + elif len(a[0]) == len(b[0]): return 0 + else: return -1 + mps.sort(compare) + for mp, dev in mps: + try: + self.g.mount(dev, mp) + except RuntimeError as msg: + print "%s (ignored)" % msg + + def cleanup_sensitive_data(self): + raise NotImplementedError + +# vim: set sta sts=4 shiftwidth=4 sw=4 et ai : diff --git a/image_creator/os_type/freebsd.py b/image_creator/os_type/freebsd.py new file mode 100644 index 0000000000000000000000000000000000000000..9a09f288c9ce731f427347b06c49a2e501a45c3d --- /dev/null +++ b/image_creator/os_type/freebsd.py @@ -0,0 +1,6 @@ +from image_creator.os_type.unix import Unix + +class Freebsd(Unix): + pass + +# vim: set sta sts=4 shiftwidth=4 sw=4 et ai : diff --git a/image_creator/os_type/hurd.py b/image_creator/os_type/hurd.py new file mode 100644 index 0000000000000000000000000000000000000000..f385b10bcd285a953b5821f1de14b118e04866db --- /dev/null +++ b/image_creator/os_type/hurd.py @@ -0,0 +1,6 @@ +from image_creator.os_type.unix import Unix + +class Hard(Unix): + pass + +# vim: set sta sts=4 shiftwidth=4 sw=4 et ai : diff --git a/image_creator/os_type/linux.py b/image_creator/os_type/linux.py new file mode 100644 index 0000000000000000000000000000000000000000..a82d52c8b0367b9416a70a5c226801cb67dc47e7 --- /dev/null +++ b/image_creator/os_type/linux.py @@ -0,0 +1,6 @@ +from image_creator.os_type.unix import Unix + +class Linux(Unix): + pass + +# vim: set sta sts=4 shiftwidth=4 sw=4 et ai : diff --git a/image_creator/os_type/netbsd.py b/image_creator/os_type/netbsd.py new file mode 100644 index 0000000000000000000000000000000000000000..bc81c1719d7bf6e26f98c20d91441270866de430 --- /dev/null +++ b/image_creator/os_type/netbsd.py @@ -0,0 +1,6 @@ +from image_creator.os_type.unix import Unix + +class Netbsd(Unix): + pass + +# vim: set sta sts=4 shiftwidth=4 sw=4 et ai : diff --git a/image_creator/os_type/unix.py b/image_creator/os_type/unix.py new file mode 100644 index 0000000000000000000000000000000000000000..ad65c151af9b703205a665bc72a27725867ad537 --- /dev/null +++ b/image_creator/os_type/unix.py @@ -0,0 +1,37 @@ +#!/usr/bin/env python + +import re + +from image_creator.os_type import OSBase + +class Unix(OSBase): + 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(): + match = regexp.match(line) + if not match: + continue + + user, passwd = match.groups() + if len(passwd) > 0 and passwd[0] == '!': + print "Warning: %s is locked" % user + else: + users.append(user) + + return users + + def cleanup_sensitive_data(self): + cleanup_userdata() + cleanup_tmp() + cleanup_log() + +# vim: set sta sts=4 shiftwidth=4 sw=4 et ai : diff --git a/image_creator/os_type/windows.py b/image_creator/os_type/windows.py new file mode 100644 index 0000000000000000000000000000000000000000..72c1031a8fd5b43731e69bb95a3c0179610f6ab5 --- /dev/null +++ b/image_creator/os_type/windows.py @@ -0,0 +1,6 @@ +from image_creator.os_type import OSBase + +class Windows(OSBase): + pass + +# vim: set sta sts=4 shiftwidth=4 sw=4 et ai :