Skip to content
Snippets Groups Projects
Commit aa2062ba authored by Nikos Skalkotos's avatar Nikos Skalkotos
Browse files

Add OS specific classes

parent 1377b8a7
No related branches found
Tags v0.2
No related merge requests found
...@@ -32,3 +32,22 @@ ...@@ -32,3 +32,22 @@
# or implied, of GRNET S.A. # or implied, of GRNET S.A.
__version__ = '0.1' __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 :
...@@ -95,20 +95,16 @@ class DiskDevice(object): ...@@ -95,20 +95,16 @@ class DiskDevice(object):
raise DiskError("Multiple operating systems found") raise DiskError("Multiple operating systems found")
self.root = roots[0] 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): def destroy(self):
self.g.umount_all() self.g.umount_all()
self.g.sync() self.g.sync()
# Close the guestfs handler # Close the guestfs handler
self.g.close()
del self.g 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): def mount(self):
mps = g.inspect_get_mountpoints(self.root) mps = g.inspect_get_mountpoints(self.root)
# Sort the keys to mount the fs in a correct order. # Sort the keys to mount the fs in a correct order.
......
...@@ -31,6 +31,7 @@ ...@@ -31,6 +31,7 @@
# interpreted as representing official policies, either expressed # interpreted as representing official policies, either expressed
# or implied, of GRNET S.A. # or implied, of GRNET S.A.
from image_creator import get_os_class
from image_creator.disk import Disk from image_creator.disk import Disk
import sys import sys
import os import os
...@@ -45,9 +46,12 @@ def main(): ...@@ -45,9 +46,12 @@ def main():
disk = Disk(source) disk = Disk(source)
try: try:
dev = disk.get_device() 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(): for key, val in metadata.iteritems():
print "%s=%s" % (key,val) print "%s=%s" % (key,val)
finally: finally:
disk.cleanup() disk.cleanup()
......
#!/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 :
from image_creator.os_type.unix import Unix
class Freebsd(Unix):
pass
# vim: set sta sts=4 shiftwidth=4 sw=4 et ai :
from image_creator.os_type.unix import Unix
class Hard(Unix):
pass
# vim: set sta sts=4 shiftwidth=4 sw=4 et ai :
from image_creator.os_type.unix import Unix
class Linux(Unix):
pass
# vim: set sta sts=4 shiftwidth=4 sw=4 et ai :
from image_creator.os_type.unix import Unix
class Netbsd(Unix):
pass
# vim: set sta sts=4 shiftwidth=4 sw=4 et ai :
#!/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 :
from image_creator.os_type import OSBase
class Windows(OSBase):
pass
# vim: set sta sts=4 shiftwidth=4 sw=4 et ai :
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment