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.
34 from image_creator.util import output, FatalError
40 def add_prefix(target):
41 def wrapper(self, *args):
43 return map(lambda x: prefix + x, target(self, *args))
47 def sysprep(enabled=True):
50 func.enabled = enabled
56 """Basic operating system class"""
58 def __init__(self, rootdev, ghandler):
62 def _is_sysprep(self, obj):
63 return getattr(obj, 'sysprep', False) and callable(obj)
65 def list_syspreps(self):
67 objs = [getattr(self, name) for name in dir(self) \
68 if not name.startswith('_')]
70 enabled = [x for x in objs if self._is_sysprep(x) and x.enabled]
71 disabled = [x for x in objs if self._is_sysprep(x) and not x.enabled]
73 return enabled, disabled
75 def _sysprep_change_status(self, name, status):
77 error_msg = "Syprep operation %s does not exist for %s" % \
78 (name, self.__class__.__name__)
80 method_name = name.replace('-', '_')
83 method = getattr(self, method_name)
84 except AttributeError:
85 raise FatalError(error_msg)
87 if not self._is_sysprep(method):
88 raise FatalError(error_msg)
90 setattr(method.im_func, 'enabled', status)
92 def enable_sysprep(self, name):
93 """Enable a system preperation operation"""
94 self._sysprep_change_status(name, True)
96 def disable_sysprep(self, name):
97 """Disable a system preperation operation"""
98 self._sysprep_change_status(name, False)
100 def print_syspreps(self):
101 """Print enabled and disabled system preperation operations."""
103 enabled, disabled = self.list_syspreps()
105 wrapper = textwrap.TextWrapper()
106 wrapper.subsequent_indent = '\t'
107 wrapper.initial_indent = '\t'
109 output("Enabled system preperation operations:")
110 if len(enabled) == 0:
113 for sysprep in enabled:
114 name = sysprep.__name__.replace('_', '-')
115 descr = wrapper.fill(sysprep.__doc__)
116 output(' %s:\n%s\n' % (name, descr))
118 output("Disabled system preperation operations:")
119 if len(disabled) == 0:
122 for sysprep in disabled:
123 name = sysprep.__name__.replace('_', '-')
124 descr = wrapper.fill(sysprep.__doc__)
125 output(' %s:\n%s\n' % (name, descr))
128 def ls(self, directory):
129 """List the name of all files under a directory"""
130 return self.g.ls(directory)
133 def find(self, directory):
134 """List the name of all files recursively under a directory"""
135 return self.g.find(directory)
137 def foreach_file(self, directory, action, **kargs):
138 """Perform an action recursively on all files under a directory.
140 The following options are allowed:
142 * maxdepth: If defined the action will not be performed on
143 files that are below this level of directories under the
146 * ftype: The action will only be performed on files of this
147 type. For a list of all allowed filetypes, see here:
148 http://libguestfs.org/guestfs.3.html#guestfs_readdir
150 * exclude: Exclude all files that follow this pattern.
152 maxdepth = None if 'maxdepth' not in kargs else kargs['maxdepth']
157 maxdepth = None if maxdepth is None else maxdepth - 1
158 kargs['maxdepth'] = maxdepth
160 exclude = None if 'exclude' not in kargs else kargs['exclude']
161 ftype = None if 'ftype' not in kargs else kargs['ftype']
162 has_ftype = lambda x, y: y is None and True or x['ftyp'] == y
164 for f in self.g.readdir(directory):
165 if f['name'] in ('.', '..'):
168 full_path = "%s/%s" % (directory, f['name'])
170 if exclude and re.match(exclude, full_path):
173 if has_ftype(f, 'd'):
174 self.foreach_file(full_path, action, **kargs)
176 if has_ftype(f, ftype):
179 def get_metadata(self):
180 """Returns some descriptive metadata about the OS."""
182 meta['ROOT_PARTITION'] = "%d" % self.g.part_to_partnum(self.root)
183 meta['OSFAMILY'] = self.g.inspect_get_type(self.root)
184 meta['OS'] = self.g.inspect_get_distro(self.root)
185 meta['DESCRIPTION'] = self.g.inspect_get_product_name(self.root)
189 def do_sysprep(self):
190 """Prepere system for image creation."""
192 output('Preparing system for image creation:')
194 tasks, _ = self.list_sysprep()
199 output(('(%d/%d)' % (cnt, size)).ljust(7), False)
203 # vim: set sta sts=4 shiftwidth=4 sw=4 et ai :