Merge branch 'hotfix-0.4.4' into develop
[snf-image-creator] / image_creator / os_type / unix.py
index 23f9d3e..7e3ba22 100644 (file)
-#!/usr/bin/env python
-
-import re
-import sys
-
-from image_creator.os_type import OSBase
+# -*- coding: utf-8 -*-
+#
+# Copyright 2012 GRNET S.A. All rights reserved.
+#
+# Redistribution and use in source and binary forms, with or
+# without modification, are permitted provided that the following
+# conditions are met:
+#
+#   1. Redistributions of source code must retain the above
+#      copyright notice, this list of conditions and the following
+#      disclaimer.
+#
+#   2. Redistributions in binary form must reproduce the above
+#      copyright notice, this list of conditions and the following
+#      disclaimer in the documentation and/or other materials
+#      provided with the distribution.
+#
+# THIS SOFTWARE IS PROVIDED BY GRNET S.A. ``AS IS'' AND ANY EXPRESS
+# OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
+# WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
+# PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL GRNET S.A OR
+# CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
+# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
+# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
+# USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
+# AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
+# ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+# POSSIBILITY OF SUCH DAMAGE.
+#
+# The views and conclusions contained in the software and
+# documentation are those of the authors and should not be
+# interpreted as representing official policies, either expressed
+# or implied, of GRNET S.A.
+
+"""This module hosts OS-specific code common to all Unix-like OSs."""
+
+from image_creator.os_type import OSBase, sysprep
 
 
 class Unix(OSBase):
+    """OS class for Unix"""
+    sensitive_userdata = [
+        '.history',
+        '.bash_history',
+        '.gnupg',
+        '.ssh',
+        '.kamakirc',
+        '.kamaki.history'
+    ]
+
+    def _mountpoints(self):
+        """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)
+
+        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)
 
-    sensitive_userdata = ['.bash_history']
-
-    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 mp in mps:
+            yield mp
 
-        for line in self.g.cat('/etc/shadow').splitlines():
-            match = regexp.match(line)
-            if not match:
-                continue
+    def _do_mount(self, readonly):
+        """Mount partitions in the correct order"""
 
-            user, passwd = match.groups()
-            if len(passwd) > 0 and passwd[0] == '!':
-                print "Warning: Ignoring locked %s account." % user
-            else:
-                users.append(user)
+        critical_mpoints = ('/', '/etc', '/root', '/home', '/var')
 
-        return users
+        mopts = 'ro' if readonly else 'rw'
+        for mp, dev in self._mountpoints():
+            try:
+                self.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))
+                    return False
+                else:
+                    self.out.warn('%s (ignored)' % msg)
 
-    def data_cleanup(self):
-        self.cleanup_userdata()
-        self.cleanup_tmp()
-        self.cleanup_log()
-        self.cleanup_mail()
-        self.cleanup_cache()
+        return True
 
+    @sysprep('Removing files under /var/cache')
     def cleanup_cache(self):
-        self.foreach_file('/var/cache', self.g.rm, ftype='r')
+        """Remove all regular files under /var/cache"""
+
+        self._foreach_file('/var/cache', self.g.rm, ftype='r')
 
+    @sysprep('Removing files under /tmp and /var/tmp')
     def cleanup_tmp(self):
-        self.foreach_file('/tmp', self.g.rm_rf, maxdepth=1)
-        self.foreach_file('/var/tmp', self.g.rm_rf, maxdepth=1)
+        """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)
+
+    @sysprep('Emptying all files under /var/log')
     def cleanup_log(self):
-        self.foreach_file('/var/log', self.g.truncate, ftype='r')
+        """Empty all files under /var/log"""
+
+        self._foreach_file('/var/log', self.g.truncate, ftype='r')
 
+    @sysprep('Removing files under /var/mail & /var/spool/mail', enabled=False)
     def cleanup_mail(self):
-        self.foreach_file('var/spool/mail', self.g.rm_rf, maxdepth=1)
-        self.foreach_file('var/mail', self.g.rm_rf, maxdepth=1)
+        """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)
 
+        self._foreach_file('/var/mail', self.g.rm_rf, maxdepth=1)
+
+    @sysprep('Removing sensitive user data')
     def cleanup_userdata(self):
-        homedirs = ['/root'] + self.ls('/home/')
+        """Delete sensitive userdata"""
+
+        homedirs = ['/root']
+        if self.g.is_dir('/home/'):
+            homedirs += self._ls('/home/')
+
+        action = self.g.rm_rf
+        if self._scrub_support:
+            action = self.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):
-                    self.g.scrub_file(fname)
+                    action(fname)
+                elif self.g.is_dir(fname):
+                    self._foreach_file(fname, action, ftype='r')
 
 # vim: set sta sts=4 shiftwidth=4 sw=4 et ai :