1 # Copyright 2012 GRNET S.A. All rights reserved.
3 # Redistribution and use in source and binary forms, with or
4 # without modification, are permitted provided that the following
7 # 1. Redistributions of source code must retain the above
8 # copyright notice, this list of conditions and the following
11 # 2. Redistributions in binary form must reproduce the above
12 # copyright notice, this list of conditions and the following
13 # disclaimer in the documentation and/or other materials
14 # provided with the distribution.
16 # THIS SOFTWARE IS PROVIDED BY GRNET S.A. ``AS IS'' AND ANY EXPRESS
17 # OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
18 # WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
19 # PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL GRNET S.A OR
20 # CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21 # SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22 # LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
23 # USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
24 # AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 # LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
26 # ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
27 # POSSIBILITY OF SUCH DAMAGE.
29 # The views and conclusions contained in the software and
30 # documentation are those of the authors and should not be
31 # interpreted as representing official policies, either expressed
32 # or implied, of GRNET S.A.
35 from clint.textui import indent, puts
38 def add_prefix(target):
39 def wrapper(self, *args):
41 return map(lambda x: prefix + x, target(self, *args))
46 """Basic operating system class"""
47 def __init__(self, rootdev, ghandler):
52 def ls(self, directory):
53 """List the name of all files under a directory"""
54 return self.g.ls(directory)
57 def find(self, directory):
58 """List the name of all files recursively under a directory"""
59 return self.g.find(directory)
61 def foreach_file(self, directory, action, **kargs):
62 """Perform an action recursively on all files under a directory.
64 The following options are allowed:
66 * maxdepth: If defined the action will not be performed on
67 files that are below this level of directories under the
70 * ftype: The action will only be performed on files of this
71 type. For a list of all allowed filetypes, see here:
72 http://libguestfs.org/guestfs.3.html#guestfs_readdir
74 * exclude: Exclude all files that follow this pattern.
76 maxdepth = None if 'maxdepth' not in kargs else kargs['maxdepth']
81 maxdepth = None if maxdepth is None else maxdepth - 1
82 kargs['maxdepth'] = maxdepth
84 exclude = None if 'exclude' not in kargs else kargs['exclude']
85 ftype = None if 'ftype' not in kargs else kargs['ftype']
86 has_ftype = lambda x, y: y is None and True or x['ftyp'] == y
88 for f in self.g.readdir(directory):
89 if f['name'] in ('.', '..'):
92 full_path = "%s/%s" % (directory, f['name'])
94 if exclude and re.match(exclude, full_path):
98 self.foreach_file(full_path, action, **kargs)
100 if has_ftype(f, ftype):
103 def get_metadata(self):
104 """Returns some descriptive metadata about the OS."""
106 meta['ROOT_PARTITION'] = "%d" % self.g.part_to_partnum(self.root)
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)
113 def data_cleanup(self):
114 """Cleanup sensitive data out of the OS image."""
116 puts('Cleaning up sensitive data out of the OS image:')
118 is_cleanup = lambda x: x.startswith('data_cleanup_') and \
119 callable(getattr(self, x))
120 tasks = [getattr(self, x) for x in dir(self) if is_cleanup(x)]
125 puts(('(%d/%d)' % (cnt, size)).ljust(7), False)
130 """Prepere system for image creation."""
132 puts('Preparing system for image creation:')
134 is_sysprep = lambda x: x.startswith('sysprep_') and \
135 callable(getattr(self, x))
136 tasks = [getattr(self, x) for x in dir(self) if is_sysprep(x)]
141 puts(('(%d/%d)' % (cnt, size)).ljust(7), False)
145 # vim: set sta sts=4 shiftwidth=4 sw=4 et ai :