root / image_creator / os_type / __init__.py @ 4a2fd05c
History | View | Annotate | Download (7.2 kB)
1 |
# Copyright 2012 GRNET S.A. All rights reserved.
|
---|---|
2 |
#
|
3 |
# Redistribution and use in source and binary forms, with or
|
4 |
# without modification, are permitted provided that the following
|
5 |
# conditions are met:
|
6 |
#
|
7 |
# 1. Redistributions of source code must retain the above
|
8 |
# copyright notice, this list of conditions and the following
|
9 |
# disclaimer.
|
10 |
#
|
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.
|
15 |
#
|
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.
|
28 |
#
|
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.
|
33 |
|
34 |
from image_creator.util import output, FatalError |
35 |
|
36 |
import textwrap |
37 |
import re |
38 |
|
39 |
|
40 |
def get_os_class(distro, osfamily): |
41 |
module = None
|
42 |
classname = None
|
43 |
try:
|
44 |
module = __import__("image_creator.os_type.%s" |
45 |
% distro, fromlist=['image_creator.os_type'])
|
46 |
classname = distro.capitalize() |
47 |
except ImportError: |
48 |
module = __import__("image_creator.os_type.%s" |
49 |
% osfamily, fromlist=['image_creator.os_type'])
|
50 |
classname = osfamily.capitalize() |
51 |
|
52 |
return getattr(module, classname) |
53 |
|
54 |
|
55 |
def add_prefix(target): |
56 |
def wrapper(self, *args): |
57 |
prefix = args[0]
|
58 |
return map(lambda x: prefix + x, target(self, *args)) |
59 |
return wrapper
|
60 |
|
61 |
|
62 |
def sysprep(enabled=True): |
63 |
def wrapper(func): |
64 |
func.sysprep = True
|
65 |
func.enabled = enabled |
66 |
return func
|
67 |
return wrapper
|
68 |
|
69 |
|
70 |
class OSBase(object): |
71 |
"""Basic operating system class"""
|
72 |
|
73 |
def __init__(self, rootdev, ghandler): |
74 |
self.root = rootdev
|
75 |
self.g = ghandler
|
76 |
|
77 |
def _is_sysprep(self, obj): |
78 |
return getattr(obj, 'sysprep', False) and callable(obj) |
79 |
|
80 |
def list_syspreps(self): |
81 |
|
82 |
objs = [getattr(self, name) for name in dir(self) \ |
83 |
if not name.startswith('_')] |
84 |
|
85 |
enabled = [x for x in objs if self._is_sysprep(x) and x.enabled] |
86 |
disabled = [x for x in objs if self._is_sysprep(x) and not x.enabled] |
87 |
|
88 |
return enabled, disabled
|
89 |
|
90 |
def _sysprep_change_status(self, name, status): |
91 |
|
92 |
error_msg = "Syprep operation %s does not exist for %s" % \
|
93 |
(name, self.__class__.__name__)
|
94 |
|
95 |
method_name = name.replace('-', '_') |
96 |
method = None
|
97 |
try:
|
98 |
method = getattr(self, method_name) |
99 |
except AttributeError: |
100 |
raise FatalError(error_msg)
|
101 |
|
102 |
if not self._is_sysprep(method): |
103 |
raise FatalError(error_msg)
|
104 |
|
105 |
setattr(method.im_func, 'enabled', status) |
106 |
|
107 |
def enable_sysprep(self, name): |
108 |
"""Enable a system preperation operation"""
|
109 |
self._sysprep_change_status(name, True) |
110 |
|
111 |
def disable_sysprep(self, name): |
112 |
"""Disable a system preperation operation"""
|
113 |
self._sysprep_change_status(name, False) |
114 |
|
115 |
def print_syspreps(self): |
116 |
"""Print enabled and disabled system preperation operations."""
|
117 |
|
118 |
enabled, disabled = self.list_syspreps()
|
119 |
|
120 |
wrapper = textwrap.TextWrapper() |
121 |
wrapper.subsequent_indent = '\t'
|
122 |
wrapper.initial_indent = '\t'
|
123 |
wrapper.width = 72
|
124 |
|
125 |
output("Enabled system preperation operations:")
|
126 |
if len(enabled) == 0: |
127 |
output("(none)")
|
128 |
else:
|
129 |
for sysprep in enabled: |
130 |
name = sysprep.__name__.replace('_', '-') |
131 |
descr = wrapper.fill(textwrap.dedent(sysprep.__doc__)) |
132 |
output(' %s:\n%s\n' % (name, descr))
|
133 |
|
134 |
output("Disabled system preperation operations:")
|
135 |
if len(disabled) == 0: |
136 |
output("(none)")
|
137 |
else:
|
138 |
for sysprep in disabled: |
139 |
name = sysprep.__name__.replace('_', '-') |
140 |
descr = wrapper.fill(textwrap.dedent(sysprep.__doc__)) |
141 |
output(' %s:\n%s\n' % (name, descr))
|
142 |
|
143 |
@add_prefix
|
144 |
def ls(self, directory): |
145 |
"""List the name of all files under a directory"""
|
146 |
return self.g.ls(directory) |
147 |
|
148 |
@add_prefix
|
149 |
def find(self, directory): |
150 |
"""List the name of all files recursively under a directory"""
|
151 |
return self.g.find(directory) |
152 |
|
153 |
def foreach_file(self, directory, action, **kargs): |
154 |
"""Perform an action recursively on all files under a directory.
|
155 |
|
156 |
The following options are allowed:
|
157 |
|
158 |
* maxdepth: If defined the action will not be performed on
|
159 |
files that are below this level of directories under the
|
160 |
directory parameter.
|
161 |
|
162 |
* ftype: The action will only be performed on files of this
|
163 |
type. For a list of all allowed filetypes, see here:
|
164 |
http://libguestfs.org/guestfs.3.html#guestfs_readdir
|
165 |
|
166 |
* exclude: Exclude all files that follow this pattern.
|
167 |
"""
|
168 |
maxdepth = None if 'maxdepth' not in kargs else kargs['maxdepth'] |
169 |
if maxdepth == 0: |
170 |
return
|
171 |
|
172 |
# maxdepth -= 1
|
173 |
maxdepth = None if maxdepth is None else maxdepth - 1 |
174 |
kargs['maxdepth'] = maxdepth
|
175 |
|
176 |
exclude = None if 'exclude' not in kargs else kargs['exclude'] |
177 |
ftype = None if 'ftype' not in kargs else kargs['ftype'] |
178 |
has_ftype = lambda x, y: y is None and True or x['ftyp'] == y |
179 |
|
180 |
for f in self.g.readdir(directory): |
181 |
if f['name'] in ('.', '..'): |
182 |
continue
|
183 |
|
184 |
full_path = "%s/%s" % (directory, f['name']) |
185 |
|
186 |
if exclude and re.match(exclude, full_path): |
187 |
continue
|
188 |
|
189 |
if has_ftype(f, 'd'): |
190 |
self.foreach_file(full_path, action, **kargs)
|
191 |
|
192 |
if has_ftype(f, ftype):
|
193 |
action(full_path) |
194 |
|
195 |
def get_metadata(self): |
196 |
"""Returns some descriptive metadata about the OS."""
|
197 |
meta = {} |
198 |
meta['ROOT_PARTITION'] = "%d" % self.g.part_to_partnum(self.root) |
199 |
meta['OSFAMILY'] = self.g.inspect_get_type(self.root) |
200 |
meta['OS'] = self.g.inspect_get_distro(self.root) |
201 |
meta['DESCRIPTION'] = self.g.inspect_get_product_name(self.root) |
202 |
|
203 |
return meta
|
204 |
|
205 |
def do_sysprep(self): |
206 |
"""Prepere system for image creation."""
|
207 |
|
208 |
output('Preparing system for image creation:')
|
209 |
|
210 |
tasks, _ = self.list_syspreps()
|
211 |
size = len(tasks)
|
212 |
cnt = 0
|
213 |
for task in tasks: |
214 |
cnt += 1
|
215 |
output(('(%d/%d)' % (cnt, size)).ljust(7), False) |
216 |
task() |
217 |
output() |
218 |
|
219 |
# vim: set sta sts=4 shiftwidth=4 sw=4 et ai :
|