root / image_creator / os_type / __init__.py @ 0144d7c7
History | View | Annotate | Download (5.7 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 |
35 |
|
36 |
import re |
37 |
|
38 |
|
39 |
def add_prefix(target): |
40 |
def wrapper(self, *args): |
41 |
prefix = args[0]
|
42 |
return map(lambda x: prefix + x, target(self, *args)) |
43 |
return wrapper
|
44 |
|
45 |
def exclude_task(func): |
46 |
func.excluded = True
|
47 |
return func
|
48 |
|
49 |
class OSBase(object): |
50 |
"""Basic operating system class"""
|
51 |
def __init__(self, rootdev, ghandler): |
52 |
self.root = rootdev
|
53 |
self.g = ghandler
|
54 |
|
55 |
@add_prefix
|
56 |
def ls(self, directory): |
57 |
"""List the name of all files under a directory"""
|
58 |
return self.g.ls(directory) |
59 |
|
60 |
@add_prefix
|
61 |
def find(self, directory): |
62 |
"""List the name of all files recursively under a directory"""
|
63 |
return self.g.find(directory) |
64 |
|
65 |
def foreach_file(self, directory, action, **kargs): |
66 |
"""Perform an action recursively on all files under a directory.
|
67 |
|
68 |
The following options are allowed:
|
69 |
|
70 |
* maxdepth: If defined the action will not be performed on
|
71 |
files that are below this level of directories under the
|
72 |
directory parameter.
|
73 |
|
74 |
* ftype: The action will only be performed on files of this
|
75 |
type. For a list of all allowed filetypes, see here:
|
76 |
http://libguestfs.org/guestfs.3.html#guestfs_readdir
|
77 |
|
78 |
* exclude: Exclude all files that follow this pattern.
|
79 |
"""
|
80 |
maxdepth = None if 'maxdepth' not in kargs else kargs['maxdepth'] |
81 |
if maxdepth == 0: |
82 |
return
|
83 |
|
84 |
# maxdepth -= 1
|
85 |
maxdepth = None if maxdepth is None else maxdepth - 1 |
86 |
kargs['maxdepth'] = maxdepth
|
87 |
|
88 |
exclude = None if 'exclude' not in kargs else kargs['exclude'] |
89 |
ftype = None if 'ftype' not in kargs else kargs['ftype'] |
90 |
has_ftype = lambda x, y: y is None and True or x['ftyp'] == y |
91 |
|
92 |
for f in self.g.readdir(directory): |
93 |
if f['name'] in ('.', '..'): |
94 |
continue
|
95 |
|
96 |
full_path = "%s/%s" % (directory, f['name']) |
97 |
|
98 |
if exclude and re.match(exclude, full_path): |
99 |
continue
|
100 |
|
101 |
if has_ftype(f, 'd'): |
102 |
self.foreach_file(full_path, action, **kargs)
|
103 |
|
104 |
if has_ftype(f, ftype):
|
105 |
action(full_path) |
106 |
|
107 |
def get_metadata(self): |
108 |
"""Returns some descriptive metadata about the OS."""
|
109 |
meta = {} |
110 |
meta['ROOT_PARTITION'] = "%d" % self.g.part_to_partnum(self.root) |
111 |
meta['OSFAMILY'] = self.g.inspect_get_type(self.root) |
112 |
meta['OS'] = self.g.inspect_get_distro(self.root) |
113 |
meta['DESCRIPTION'] = self.g.inspect_get_product_name(self.root) |
114 |
|
115 |
return meta
|
116 |
|
117 |
def data_cleanup(self): |
118 |
"""Cleanup sensitive data out of the OS image."""
|
119 |
|
120 |
output('Cleaning up sensitive data out of the OS image:')
|
121 |
|
122 |
tasks, _ = self.list_data_cleanup()
|
123 |
size = len(tasks)
|
124 |
cnt = 0
|
125 |
for task in tasks: |
126 |
cnt += 1
|
127 |
output(('(%d/%d)' % (cnt, size)).ljust(7), False) |
128 |
task() |
129 |
output() |
130 |
|
131 |
def sysprep(self): |
132 |
"""Prepere system for image creation."""
|
133 |
|
134 |
output('Preparing system for image creation:')
|
135 |
|
136 |
tasks, _ = self.list_sysprep()
|
137 |
size = len(tasks)
|
138 |
cnt = 0
|
139 |
for task in tasks: |
140 |
cnt += 1
|
141 |
output(('(%d/%d)' % (cnt, size)).ljust(7), False) |
142 |
task() |
143 |
output() |
144 |
|
145 |
def list_sysprep(self): |
146 |
"""List all sysprep actions"""
|
147 |
|
148 |
is_sysprep = lambda x: x.startswith('sysprep_') and \ |
149 |
callable(getattr(self, x)) |
150 |
tasks = [getattr(self, x) for x in dir(self) if is_sysprep(x)] |
151 |
|
152 |
included = [t for t in tasks if not getattr(t, "excluded", False)] |
153 |
excluded = [t for t in tasks if getattr(t, "excluded", False)] |
154 |
|
155 |
return included, excluded
|
156 |
|
157 |
def list_data_cleanup(self): |
158 |
"""List all data_cleanup actions"""
|
159 |
|
160 |
is_cleanup = lambda x: x.startswith('data_cleanup_') and \ |
161 |
callable(getattr(self, x)) |
162 |
tasks = [getattr(self, x) for x in dir(self) if is_cleanup(x)] |
163 |
|
164 |
included = [t for t in tasks if not getattr(t, "excluded", False)] |
165 |
excluded = [t for t in tasks if getattr(t, "excluded", False)] |
166 |
|
167 |
return included, excluded
|
168 |
|
169 |
# vim: set sta sts=4 shiftwidth=4 sw=4 et ai :
|