Don't copy the guestfs handler in the os_type cls
authorNikos Skalkotos <skalkoto@grnet.gr>
Thu, 1 Aug 2013 08:10:48 +0000 (11:10 +0300)
committerNikos Skalkotos <skalkoto@grnet.gr>
Thu, 1 Aug 2013 08:10:48 +0000 (11:10 +0300)
image_creator/os_type/__init__.py
image_creator/os_type/freebsd.py
image_creator/os_type/linux.py
image_creator/os_type/slackware.py
image_creator/os_type/ubuntu.py
image_creator/os_type/unix.py
image_creator/os_type/windows.py

index cc7d39a..7f8c786 100644 (file)
@@ -128,7 +128,6 @@ class OSBase(object):
         self.image = image
 
         self.root = image.root
-        self.g = image.g
         self.out = image.out
 
         self.needed_sysprep_params = {}
@@ -141,7 +140,7 @@ class OSBase(object):
         # Many guestfs compilations don't support scrub
         self._scrub_support = True
         try:
-            self.g.available(['scrub'])
+            self.image.g.available(['scrub'])
         except RuntimeError:
             self._scrub_support = False
 
@@ -288,7 +287,7 @@ class OSBase(object):
         """Umount all mounted filesystems."""
 
         self.out.output("Umounting the media ...", False)
-        self.g.umount_all()
+        self.image.g.umount_all()
         self.mounted = False
         self.out.success('done')
 
@@ -299,12 +298,12 @@ class OSBase(object):
     @add_prefix
     def _ls(self, directory):
         """List the name of all files under a directory"""
-        return self.g.ls(directory)
+        return self.image.g.ls(directory)
 
     @add_prefix
     def _find(self, directory):
         """List the name of all files recursively under a directory"""
-        return self.g.find(directory)
+        return self.image.g.find(directory)
 
     def _foreach_file(self, directory, action, **kargs):
         """Perform an action recursively on all files under a directory.
@@ -333,7 +332,7 @@ class OSBase(object):
         ftype = None if 'ftype' not in kargs else kargs['ftype']
         has_ftype = lambda x, y: y is None and True or x['ftyp'] == y
 
-        for f in self.g.readdir(directory):
+        for f in self.image.g.readdir(directory):
             if f['name'] in ('.', '..'):
                 continue
 
@@ -350,17 +349,19 @@ class OSBase(object):
 
     def _do_collect_metadata(self):
         """helper method for collect_metadata"""
-        self.meta['ROOT_PARTITION'] = "%d" % self.g.part_to_partnum(self.root)
-        self.meta['OSFAMILY'] = self.g.inspect_get_type(self.root)
-        self.meta['OS'] = self.g.inspect_get_distro(self.root)
+        self.meta['ROOT_PARTITION'] = \
+            "%d" % self.image.g.part_to_partnum(self.root)
+        self.meta['OSFAMILY'] = self.image.g.inspect_get_type(self.root)
+        self.meta['OS'] = self.image.g.inspect_get_distro(self.root)
         if self.meta['OS'] == "unknown":
             self.meta['OS'] = self.meta['OSFAMILY']
-        self.meta['DESCRIPTION'] = self.g.inspect_get_product_name(self.root)
+        self.meta['DESCRIPTION'] = self.image.g.inspect_get_product_name(self.root)
 
     def _do_mount(self, readonly):
         """helper method for mount"""
         try:
-            self.g.mount_options('ro' if readonly else 'rw', self.root, '/')
+            self.image.g.mount_options(
+                'ro' if readonly else 'rw', self.root, '/')
         except RuntimeError as msg:
             self.out.warn("unable to mount the root partition: %s" % msg)
             return False
index 7efd56e..5f81481 100644 (file)
@@ -49,7 +49,7 @@ class Freebsd(Unix):
 
         master_passwd = []
 
-        for line in self.g.cat('/etc/master.passwd').splitlines():
+        for line in self.image.g.cat('/etc/master.passwd').splitlines():
 
             # Check for empty or comment lines
             if len(line.split('#')[0]) == 0:
@@ -62,10 +62,11 @@ class Freebsd(Unix):
 
             master_passwd.append(":".join(fields))
 
-        self.g.write('/etc/master.passwd', "\n".join(master_passwd) + '\n')
+        self.image.g.write(
+            '/etc/master.passwd', "\n".join(master_passwd) + '\n')
 
         # Make sure no one can login on the system
-        self.g.rm_rf('/etc/spwd.db')
+        self.image.g.rm_rf('/etc/spwd.db')
 
     def _do_collect_metadata(self):
         """Collect metadata about the OS"""
@@ -88,7 +89,7 @@ class Freebsd(Unix):
             '^([^:]+):((?:![^:]+)|(?:[^!*][^:]+)|):(?:[^:]*:){7}(?:[^:]*)'
         )
 
-        for line in self.g.cat('/etc/master.passwd').splitlines():
+        for line in self.image.g.cat('/etc/master.passwd').splitlines():
             line = line.split('#')[0]
             match = regexp.match(line)
             if not match:
@@ -120,7 +121,7 @@ class Freebsd(Unix):
                 group3 = int(match.group(3))
                 dev = '/dev/sd%c%d' % (chr(ord('a') + group2), group3)
             try:
-                self.g.mount_vfs(mopts, 'ufs', dev, mp)
+                self.image.g.mount_vfs(mopts, 'ufs', dev, mp)
             except RuntimeError as msg:
                 if mp in critical_mpoints:
                     self.out.warn('unable to mount %s. Reason: %s' % (mp, msg))
index 71ce109..ada6ab4 100644 (file)
@@ -59,7 +59,7 @@ class Linux(Unix):
         passwd = []
         removed_users = {}
         metadata_users = self.meta['USERS'].split()
-        for line in self.g.cat('/etc/passwd').splitlines():
+        for line in self.image.g.cat('/etc/passwd').splitlines():
             fields = line.split(':')
             if int(fields[2]) > 1000:
                 removed_users[fields[0]] = fields
@@ -75,31 +75,31 @@ class Linux(Unix):
         if not len(self.meta['USERS']):
             del self.meta['USERS']
 
-        self.g.write('/etc/passwd', '\n'.join(passwd) + '\n')
+        self.image.g.write('/etc/passwd', '\n'.join(passwd) + '\n')
 
         # Remove the corresponding /etc/shadow entries
         shadow = []
-        for line in self.g.cat('/etc/shadow').splitlines():
+        for line in self.image.g.cat('/etc/shadow').splitlines():
             fields = line.split(':')
             if fields[0] not in removed_users:
                 shadow.append(':'.join(fields))
 
-        self.g.write('/etc/shadow', "\n".join(shadow) + '\n')
+        self.image.g.write('/etc/shadow', "\n".join(shadow) + '\n')
 
         # Remove the corresponding /etc/group entries
         group = []
-        for line in self.g.cat('/etc/group').splitlines():
+        for line in self.image.g.cat('/etc/group').splitlines():
             fields = line.split(':')
             # Remove groups tha have the same name as the removed users
             if fields[0] not in removed_users:
                 group.append(':'.join(fields))
 
-        self.g.write('/etc/group', '\n'.join(group) + '\n')
+        self.image.g.write('/etc/group', '\n'.join(group) + '\n')
 
         # Remove home directories
         for home in [field[5] for field in removed_users.values()]:
-            if self.g.is_dir(home) and home.startswith('/home/'):
-                self.g.rm_rf(home)
+            if self.image.g.is_dir(home) and home.startswith('/home/'):
+                self.image.g.rm_rf(home)
 
     @sysprep('Cleaning up password & locking all user accounts')
     def cleanup_passwords(self):
@@ -107,14 +107,14 @@ class Linux(Unix):
 
         shadow = []
 
-        for line in self.g.cat('/etc/shadow').splitlines():
+        for line in self.image.g.cat('/etc/shadow').splitlines():
             fields = line.split(':')
             if fields[1] not in ('*', '!'):
                 fields[1] = '!'
 
             shadow.append(":".join(fields))
 
-        self.g.write('/etc/shadow', "\n".join(shadow) + '\n')
+        self.image.g.write('/etc/shadow', "\n".join(shadow) + '\n')
 
     @sysprep('Fixing acpid powerdown action')
     def fix_acpid(self):
@@ -126,20 +126,20 @@ class Linux(Unix):
                           'shutdown -h now "Power button pressed"\n'
 
         events_dir = '/etc/acpi/events'
-        if not self.g.is_dir(events_dir):
+        if not self.image.g.is_dir(events_dir):
             self.out.warn("No acpid event directory found")
             return
 
         event_exp = re.compile('event=(.+)', re.I)
         action_exp = re.compile('action=(.+)', re.I)
-        for events_file in self.g.readdir(events_dir):
+        for events_file in self.image.g.readdir(events_dir):
             if events_file['ftyp'] != 'r':
                 continue
 
             fullpath = "%s/%s" % (events_dir, events_file['name'])
             event = ""
             action = ""
-            for line in self.g.cat(fullpath).splitlines():
+            for line in self.image.g.cat(fullpath).splitlines():
                 match = event_exp.match(line)
                 if match:
                     event = match.group(1)
@@ -151,14 +151,14 @@ class Linux(Unix):
 
             if event.strip() in ("button[ /]power", "button/power.*"):
                 if action:
-                    if not self.g.is_file(action):
+                    if not self.image.g.is_file(action):
                         self.out.warn("Acpid action file: %s does not exist" %
                                       action)
                         return
-                    self.g.copy_file_to_file(action,
+                    self.image.g.copy_file_to_file(action,
                                              "%s.orig.snf-image-creator-%d" %
                                              (action, time.time()))
-                    self.g.write(action, powerbtn_action)
+                    self.image.g.write(action, powerbtn_action)
                     return
                 else:
                     self.out.warn("Acpid event file %s does not contain and "
@@ -182,8 +182,8 @@ class Linux(Unix):
         """
 
         rule_file = '/etc/udev/rules.d/70-persistent-net.rules'
-        if self.g.is_file(rule_file):
-            self.g.rm(rule_file)
+        if self.image.g.is_file(rule_file):
+            self.image.g.rm(rule_file)
 
     @sysprep('Removing swap entry from fstab')
     def remove_swap_entry(self):
@@ -194,7 +194,7 @@ class Linux(Unix):
         """
 
         new_fstab = ""
-        fstab = self.g.cat('/etc/fstab')
+        fstab = self.image.g.cat('/etc/fstab')
         for line in fstab.splitlines():
 
             entry = line.split('#')[0].strip().split()
@@ -203,7 +203,7 @@ class Linux(Unix):
 
             new_fstab += "%s\n" % line
 
-        self.g.write('/etc/fstab', new_fstab)
+        self.image.g.write('/etc/fstab', new_fstab)
 
     @sysprep('Replacing fstab & grub non-persistent device references')
     def use_persistent_block_device_names(self):
@@ -221,32 +221,33 @@ class Linux(Unix):
         """Replaces non-persistent device name occurencies with persistent
         ones in GRUB1 configuration files.
         """
-        if self.g.is_file('/boot/grub/menu.lst'):
+        if self.image.g.is_file('/boot/grub/menu.lst'):
             grub1 = '/boot/grub/menu.lst'
-        elif self.g.is_file('/etc/grub.conf'):
+        elif self.image.g.is_file('/etc/grub.conf'):
             grub1 = '/etc/grub.conf'
         else:
             return
 
-        self.g.aug_init('/', 0)
+        self.image.g.aug_init('/', 0)
         try:
-            roots = self.g.aug_match('/files%s/title[*]/kernel/root' % grub1)
+            roots = self.image.g.aug_match(
+                '/files%s/title[*]/kernel/root' % grub1)
             for root in roots:
-                dev = self.g.aug_get(root)
+                dev = self.image.g.aug_get(root)
                 if not self._is_persistent(dev):
                     # This is not always correct. Grub may contain root entries
                     # for other systems, but we only support 1 OS per hard
                     # disk, so this shouldn't harm.
-                    self.g.aug_set(root, new_root)
+                    self.image.g.aug_set(root, new_root)
         finally:
-            self.g.aug_save()
-            self.g.aug_close()
+            self.image.g.aug_save()
+            self.image.g.aug_close()
 
     def _persistent_fstab(self):
         """Replaces non-persistent device name occurencies in /etc/fstab with
         persistent ones.
         """
-        mpoints = self.g.mountpoints()
+        mpoints = self.image.g.mountpoints()
         if len(mpoints) == 0:
             pass  # TODO: error handling
 
@@ -254,7 +255,7 @@ class Linux(Unix):
 
         root_dev = None
         new_fstab = ""
-        fstab = self.g.cat('/etc/fstab')
+        fstab = self.image.g.cat('/etc/fstab')
         for line in fstab.splitlines():
 
             line, dev, mpoint = self._convert_fstab_line(line, device_dict)
@@ -263,7 +264,7 @@ class Linux(Unix):
             if mpoint == '/':
                 root_dev = dev
 
-        self.g.write('/etc/fstab', new_fstab)
+        self.image.g.write('/etc/fstab', new_fstab)
         if root_dev is None:
             pass  # TODO: error handling
 
@@ -312,7 +313,7 @@ class Linux(Unix):
         users = []
         regexp = re.compile(r'(\S+):((?:!\S+)|(?:[^!*]\S+)|):(?:\S*:){6}')
 
-        for line in self.g.cat('/etc/shadow').splitlines():
+        for line in self.image.g.cat('/etc/shadow').splitlines():
             match = regexp.match(line)
             if not match:
                 continue
@@ -334,7 +335,7 @@ class Linux(Unix):
         if dev in self._uuid:
             return self._uuid[dev]
 
-        uuid = self.g.vfs_uuid(dev)
+        uuid = self.image.g.vfs_uuid(dev)
         assert len(uuid)
         self._uuid[dev] = uuid
         return uuid
index c708515..91c4a37 100644 (file)
@@ -47,7 +47,7 @@ class Slackware(Linux):
         # In slackware the metadata about installed packages are
         # stored in /var/log/packages. Clearing all /var/log files
         # will destroy the package management system.
-        self._foreach_file('/var/log', self.g.truncate, ftype='r',
+        self._foreach_file('/var/log', self.image.g.truncate, ftype='r',
                            exclude='/var/log/packages')
 
 # vim: set sta sts=4 shiftwidth=4 sw=4 et ai :
index bd2d6b5..0647c6f 100644 (file)
@@ -45,7 +45,7 @@ class Ubuntu(Linux):
         """Collect metadata about the OS"""
 
         super(Ubuntu, self)._do_collect_metadata()
-        apps = self.g.inspect_list_applications(self.root)
+        apps = self.image.g.inspect_list_applications(self.root)
         for app in apps:
             if app['app_name'] == 'kubuntu-desktop':
                 self.meta['OS'] = 'kubuntu'
index 7e3ba22..8779b76 100644 (file)
@@ -53,7 +53,7 @@ class Unix(OSBase):
         """Return mountpoints in the correct order.
         / should be mounted before /boot or /usr, /usr befor /usr/bin ...
         """
-        mps = self.g.inspect_get_mountpoints(self.root)
+        mps = self.image.g.inspect_get_mountpoints(self.root)
 
         def compare(a, b):
             if len(a[0]) > len(b[0]):
@@ -75,7 +75,7 @@ class Unix(OSBase):
         mopts = 'ro' if readonly else 'rw'
         for mp, dev in self._mountpoints():
             try:
-                self.g.mount_options(mopts, dev, mp)
+                self.image.g.mount_options(mopts, dev, mp)
             except RuntimeError as msg:
                 if mp in critical_mpoints:
                     self.out.warn('unable to mount %s. Reason: %s' % (mp, msg))
@@ -89,50 +89,51 @@ class Unix(OSBase):
     def cleanup_cache(self):
         """Remove all regular files under /var/cache"""
 
-        self._foreach_file('/var/cache', self.g.rm, ftype='r')
+        self._foreach_file('/var/cache', self.image.g.rm, ftype='r')
 
     @sysprep('Removing files under /tmp and /var/tmp')
     def cleanup_tmp(self):
         """Remove all files under /tmp and /var/tmp"""
 
-        self._foreach_file('/tmp', self.g.rm_rf, maxdepth=1)
-        self._foreach_file('/var/tmp', self.g.rm_rf, maxdepth=1)
+        self._foreach_file('/tmp', self.image.g.rm_rf, maxdepth=1)
+        self._foreach_file('/var/tmp', self.image.g.rm_rf, maxdepth=1)
 
     @sysprep('Emptying all files under /var/log')
     def cleanup_log(self):
         """Empty all files under /var/log"""
 
-        self._foreach_file('/var/log', self.g.truncate, ftype='r')
+        self._foreach_file('/var/log', self.image.g.truncate, ftype='r')
 
     @sysprep('Removing files under /var/mail & /var/spool/mail', enabled=False)
     def cleanup_mail(self):
         """Remove all files under /var/mail and /var/spool/mail"""
 
-        if self.g.is_dir('/var/spool/mail'):
-            self._foreach_file('/var/spool/mail', self.g.rm_rf, maxdepth=1)
+        if self.image.g.is_dir('/var/spool/mail'):
+            self._foreach_file('/var/spool/mail', self.image.g.rm_rf,
+                               maxdepth=1)
 
-        self._foreach_file('/var/mail', self.g.rm_rf, maxdepth=1)
+        self._foreach_file('/var/mail', self.image.g.rm_rf, maxdepth=1)
 
     @sysprep('Removing sensitive user data')
     def cleanup_userdata(self):
         """Delete sensitive userdata"""
 
         homedirs = ['/root']
-        if self.g.is_dir('/home/'):
+        if self.image.g.is_dir('/home/'):
             homedirs += self._ls('/home/')
 
-        action = self.g.rm_rf
+        action = self.image.g.rm_rf
         if self._scrub_support:
-            action = self.g.scrub_file
+            action = self.image.g.scrub_file
         else:
             self.out.warn("Sensitive data won't be scrubbed (not supported)")
 
         for homedir in homedirs:
             for data in self.sensitive_userdata:
                 fname = "%s/%s" % (homedir, data)
-                if self.g.is_file(fname):
+                if self.image.g.is_file(fname):
                     action(fname)
-                elif self.g.is_dir(fname):
+                elif self.image.g.is_dir(fname):
                     self._foreach_file(fname, action, ftype='r')
 
 # vim: set sta sts=4 shiftwidth=4 sw=4 et ai :
index a2a41f2..3592500 100644 (file)
@@ -116,13 +116,13 @@ class Windows(OSBase):
     def __init__(self, image, **kargs):
         super(Windows, self).__init__(image, **kargs)
 
-        device = self.g.part_to_dev(self.root)
+        device = self.image.g.part_to_dev(self.root)
 
-        self.last_part_num = self.g.part_list(device)[-1]['part_num']
+        self.last_part_num = self.image.g.part_list(device)[-1]['part_num']
         self.last_drive = None
         self.system_drive = None
 
-        for drive, partition in self.g.inspect_get_drive_mappings(self.root):
+        for drive, partition in self.image.g.inspect_get_drive_mappings(self.root):
             if partition == "%s%d" % (device, self.last_part_num):
                 self.last_drive = drive
             if partition == self.root:
@@ -130,7 +130,7 @@ class Windows(OSBase):
 
         assert self.system_drive
 
-        self.product_name = self.g.inspect_get_product_name(self.root)
+        self.product_name = self.image.g.inspect_get_product_name(self.root)
         self.syspreped = False
 
     @sysprep('Disabling IPv6 privacy extensions')
@@ -291,10 +291,10 @@ class Windows(OSBase):
             firewall_states = self._update_firewalls(0, 0, 0)
 
             # Delete the pagefile. It will be recreated when the system boots
-            systemroot = self.g.inspect_get_windows_systemroot(self.root)
+            systemroot = self.image.g.inspect_get_windows_systemroot(self.root)
             try:
                 pagefile = "%s/pagefile.sys" % systemroot
-                self.g.rm_rf(self.g.case_sensitive_path(pagefile))
+                self.image.g.rm_rf(self.image.g.case_sensitive_path(pagefile))
             except RuntimeError:
                 pass
 
@@ -302,13 +302,13 @@ class Windows(OSBase):
             self.umount()
 
         self.out.output("Shutting down helper VM ...", False)
-        self.g.sync()
+        self.image.g.sync()
         # guestfs_shutdown which is the prefered way to shutdown the backend
         # process was introduced in version 1.19.16
         if check_guestfs_version(self.g, 1, 19, 16) >= 0:
-            self.g.shutdown()
+            self.image.g.shutdown()
         else:
-            self.g.kill_subprocess()
+            self.image.g.kill_subprocess()
 
         self.out.success('done')
 
@@ -383,7 +383,7 @@ class Windows(OSBase):
             finally:
                 self.out.output("Relaunching helper VM (may take a while) ...",
                                 False)
-                self.g.launch()
+                self.image.g.launch()
                 self.out.success('done')
 
                 self.mount(readonly=False)
@@ -426,10 +426,10 @@ class Windows(OSBase):
     def _registry_file_path(self, regfile):
         """Retrieves the case sensitive path to a registry file"""
 
-        systemroot = self.g.inspect_get_windows_systemroot(self.root)
+        systemroot = self.image.g.inspect_get_windows_systemroot(self.root)
         path = "%s/system32/config/%s" % (systemroot, regfile)
         try:
-            path = self.g.case_sensitive_path(path)
+            path = self.image.g.case_sensitive_path(path)
         except RuntimeError as error:
             raise FatalError("Unable to retrieve registry file: %s. Reason: %s"
                              % (regfile, str(error)))
@@ -446,7 +446,7 @@ class Windows(OSBase):
         softwarefd, software = tempfile.mkstemp()
         try:
             os.close(softwarefd)
-            self.g.download(path, software)
+            self.image.g.download(path, software)
 
             h = hivex.Hivex(software, write=True)
 
@@ -511,7 +511,7 @@ class Windows(OSBase):
 
             h.commit(None)
 
-            self.g.upload(software, path)
+            self.image.g.upload(software, path)
         finally:
             os.unlink(software)
 
@@ -537,7 +537,7 @@ class Windows(OSBase):
         systemfd, system = tempfile.mkstemp()
         try:
             os.close(systemfd)
-            self.g.download(path, system)
+            self.image.g.download(path, system)
 
             h = hivex.Hivex(system, write=True)
 
@@ -569,7 +569,7 @@ class Windows(OSBase):
                            'value': struct.pack("<I", new_values.pop(0))})
 
             h.commit(None)
-            self.g.upload(system, path)
+            self.image.g.upload(system, path)
 
         finally:
             os.unlink(system)
@@ -598,7 +598,7 @@ class Windows(OSBase):
         softwarefd, software = tempfile.mkstemp()
         try:
             os.close(softwarefd)
-            self.g.download(path, software)
+            self.image.g.download(path, software)
 
             h = hivex.Hivex(software, write=True)
 
@@ -625,7 +625,7 @@ class Windows(OSBase):
             h.node_set_value(key, new_value)
             h.commit(None)
 
-            self.g.upload(software, path)
+            self.image.g.upload(software, path)
 
         finally:
             os.unlink(software)
@@ -642,7 +642,7 @@ class Windows(OSBase):
         samfd, sam = tempfile.mkstemp()
         try:
             os.close(samfd)
-            self.g.download(self._registry_file_path('SAM'), sam)
+            self.image.g.download(self._registry_file_path('SAM'), sam)
 
             h = hivex.Hivex(sam)