Revision 121f3bc0 image_creator/os_type/__init__.py
b/image_creator/os_type/__init__.py | ||
---|---|---|
1 |
# -*- coding: utf-8 -*- |
|
2 |
# |
|
1 | 3 |
# Copyright 2012 GRNET S.A. All rights reserved. |
2 | 4 |
# |
3 | 5 |
# Redistribution and use in source and binary forms, with or |
... | ... | |
31 | 33 |
# interpreted as representing official policies, either expressed |
32 | 34 |
# or implied, of GRNET S.A. |
33 | 35 |
|
36 |
"""This package provides various classes for preparing different Operating |
|
37 |
Systems for image creation. |
|
38 |
""" |
|
39 |
|
|
34 | 40 |
from image_creator.util import FatalError |
35 | 41 |
|
36 | 42 |
import textwrap |
... | ... | |
81 | 87 |
|
82 | 88 |
def collect_metadata(self): |
83 | 89 |
"""Collect metadata about the OS""" |
84 |
|
|
85 | 90 |
try: |
86 | 91 |
if not self.mount(readonly=True): |
87 | 92 |
raise FatalError("Unable to mount the media read-only") |
... | ... | |
92 | 97 |
finally: |
93 | 98 |
self.umount() |
94 | 99 |
|
95 |
def _do_collect_metadata(self): |
|
96 |
|
|
97 |
self.meta['ROOT_PARTITION'] = "%d" % self.g.part_to_partnum(self.root) |
|
98 |
self.meta['OSFAMILY'] = self.g.inspect_get_type(self.root) |
|
99 |
self.meta['OS'] = self.g.inspect_get_distro(self.root) |
|
100 |
if self.meta['OS'] == "unknown": |
|
101 |
self.meta['OS'] = self.meta['OSFAMILY'] |
|
102 |
self.meta['DESCRIPTION'] = self.g.inspect_get_product_name(self.root) |
|
103 |
|
|
104 |
def _is_sysprep(self, obj): |
|
105 |
return getattr(obj, 'sysprep', False) and callable(obj) |
|
100 |
self.out.output() |
|
106 | 101 |
|
107 | 102 |
def list_syspreps(self): |
108 |
|
|
103 |
"""Returns a list of sysprep objects""" |
|
109 | 104 |
objs = [getattr(self, name) for name in dir(self) |
110 | 105 |
if not name.startswith('_')] |
111 | 106 |
|
112 | 107 |
return [x for x in objs if self._is_sysprep(x) and x.executed is False] |
113 | 108 |
|
114 | 109 |
def sysprep_info(self, obj): |
110 |
"""Returns information about a sysprep object""" |
|
115 | 111 |
assert self._is_sysprep(obj), "Object is not a sysprep" |
116 | 112 |
|
117 | 113 |
return (obj.__name__.replace('_', '-'), textwrap.dedent(obj.__doc__)) |
... | ... | |
171 | 167 |
descr = wrapper.fill(textwrap.dedent(sysprep.__doc__)) |
172 | 168 |
self.out.output(' %s:\n%s\n' % (name, descr)) |
173 | 169 |
|
170 |
def do_sysprep(self): |
|
171 |
"""Prepare system for image creation.""" |
|
172 |
|
|
173 |
try: |
|
174 |
if not self.mount(readonly=False): |
|
175 |
raise FatalError("Unable to mount the media read-write") |
|
176 |
|
|
177 |
self.out.output('Preparing system for image creation:') |
|
178 |
|
|
179 |
tasks = self.list_syspreps() |
|
180 |
enabled = filter(lambda x: x.enabled, tasks) |
|
181 |
|
|
182 |
size = len(enabled) |
|
183 |
cnt = 0 |
|
184 |
for task in enabled: |
|
185 |
cnt += 1 |
|
186 |
self.out.output(('(%d/%d)' % (cnt, size)).ljust(7), False) |
|
187 |
task() |
|
188 |
setattr(task.im_func, 'executed', True) |
|
189 |
finally: |
|
190 |
self.umount() |
|
191 |
|
|
192 |
self.out.output() |
|
193 |
|
|
194 |
def mount(self, readonly=False): |
|
195 |
"""Mount image.""" |
|
196 |
|
|
197 |
if getattr(self, "mounted", False): |
|
198 |
return True |
|
199 |
|
|
200 |
mount_type = 'read-only' if readonly else 'read-write' |
|
201 |
self.out.output("Mounting the media %s ..." % mount_type, False) |
|
202 |
|
|
203 |
if not self._do_mount(readonly): |
|
204 |
return False |
|
205 |
|
|
206 |
self.mounted = True |
|
207 |
self.out.success('done') |
|
208 |
return True |
|
209 |
|
|
210 |
def umount(self): |
|
211 |
"""Umount all mounted filesystems.""" |
|
212 |
|
|
213 |
self.out.output("Umounting the media ...", False) |
|
214 |
self.g.umount_all() |
|
215 |
self.mounted = False |
|
216 |
self.out.success('done') |
|
217 |
|
|
218 |
def _is_sysprep(self, obj): |
|
219 |
"""Checks if an object is a sysprep""" |
|
220 |
return getattr(obj, 'sysprep', False) and callable(obj) |
|
221 |
|
|
174 | 222 |
@add_prefix |
175 |
def ls(self, directory): |
|
223 |
def _ls(self, directory):
|
|
176 | 224 |
"""List the name of all files under a directory""" |
177 | 225 |
return self.g.ls(directory) |
178 | 226 |
|
179 | 227 |
@add_prefix |
180 |
def find(self, directory): |
|
228 |
def _find(self, directory):
|
|
181 | 229 |
"""List the name of all files recursively under a directory""" |
182 | 230 |
return self.g.find(directory) |
183 | 231 |
|
184 |
def foreach_file(self, directory, action, **kargs): |
|
232 |
def _foreach_file(self, directory, action, **kargs):
|
|
185 | 233 |
"""Perform an action recursively on all files under a directory. |
186 | 234 |
|
187 | 235 |
The following options are allowed: |
... | ... | |
218 | 266 |
continue |
219 | 267 |
|
220 | 268 |
if has_ftype(f, 'd'): |
221 |
self.foreach_file(full_path, action, **kargs) |
|
269 |
self._foreach_file(full_path, action, **kargs)
|
|
222 | 270 |
|
223 | 271 |
if has_ftype(f, ftype): |
224 | 272 |
action(full_path) |
225 | 273 |
|
226 |
def do_sysprep(self): |
|
227 |
"""Prepere system for image creation.""" |
|
228 |
|
|
229 |
try: |
|
230 |
if not self.mount(readonly=False): |
|
231 |
raise FatalError("Unable to mount the media read-write") |
|
232 |
|
|
233 |
self.out.output('Preparing system for image creation:') |
|
234 |
|
|
235 |
tasks = self.list_syspreps() |
|
236 |
enabled = filter(lambda x: x.enabled, tasks) |
|
237 |
|
|
238 |
size = len(enabled) |
|
239 |
cnt = 0 |
|
240 |
for task in enabled: |
|
241 |
cnt += 1 |
|
242 |
self.out.output(('(%d/%d)' % (cnt, size)).ljust(7), False) |
|
243 |
task() |
|
244 |
setattr(task.im_func, 'executed', True) |
|
245 |
self.out.output() |
|
246 |
finally: |
|
247 |
self.umount() |
|
274 |
def _do_collect_metadata(self): |
|
275 |
"""helper method for collect_metadata""" |
|
276 |
self.meta['ROOT_PARTITION'] = "%d" % self.g.part_to_partnum(self.root) |
|
277 |
self.meta['OSFAMILY'] = self.g.inspect_get_type(self.root) |
|
278 |
self.meta['OS'] = self.g.inspect_get_distro(self.root) |
|
279 |
if self.meta['OS'] == "unknown": |
|
280 |
self.meta['OS'] = self.meta['OSFAMILY'] |
|
281 |
self.meta['DESCRIPTION'] = self.g.inspect_get_product_name(self.root) |
|
248 | 282 |
|
249 | 283 |
def _do_mount(self, readonly): |
284 |
"""helper method for mount""" |
|
250 | 285 |
try: |
251 | 286 |
self.g.mount_options('ro' if readonly else 'rw', self.root, '/') |
252 | 287 |
except RuntimeError as msg: |
... | ... | |
255 | 290 |
|
256 | 291 |
return True |
257 | 292 |
|
258 |
def mount(self, readonly=False): |
|
259 |
"""Mount image.""" |
|
260 |
|
|
261 |
if getattr(self, "mounted", False): |
|
262 |
return True |
|
263 |
|
|
264 |
mount_type = 'read-only' if readonly else 'read-write' |
|
265 |
self.out.output("Mount the media %s ..." % mount_type, False) |
|
266 |
|
|
267 |
if not self._do_mount(readonly): |
|
268 |
return False |
|
269 |
|
|
270 |
self.mounted = True |
|
271 |
self.out.success('done') |
|
272 |
return True |
|
273 |
|
|
274 |
def umount(self): |
|
275 |
"""Umount all mounted filesystems.""" |
|
276 |
self.g.umount_all() |
|
277 |
self.mounted = False |
|
278 |
|
|
279 | 293 |
# vim: set sta sts=4 shiftwidth=4 sw=4 et ai : |
Also available in: Unified diff