Statistics
| Branch: | Tag: | Revision:

root / image_creator / os_type / __init__.py @ 22a6d232

History | View | Annotate | Download (4.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
import re
35
from clint.textui import indent, puts
36

    
37

    
38
def add_prefix(target):
39
    def wrapper(self, *args):
40
        prefix = args[0]
41
        return map(lambda x: prefix + x, target(self, *args))
42
    return wrapper
43

    
44

    
45
class OSBase(object):
46
    """Basic operating system class"""
47
    def __init__(self, rootdev, ghandler):
48
        self.root = rootdev
49
        self.g = ghandler
50

    
51
    @add_prefix
52
    def ls(self, directory):
53
        """List the name of all files under a directory"""
54
        return self.g.ls(directory)
55

    
56
    @add_prefix
57
    def find(self, directory):
58
        """List the name of all files recursively under a directory"""
59
        return self.g.find(directory)
60

    
61
    def foreach_file(self, directory, action, **kargs):
62
        """Perform an action recursively on all files under a directory.
63

64
        The following options are allowed:
65

66
        * maxdepth: If defined the action will not be performed on
67
          files that are below this level of directories under the
68
          directory parameter.
69

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
73

74
        * exclude: Exclude all files that follow this pattern.
75
        """
76
        maxdepth = None if 'maxdepth' not in kargs else kargs['maxdepth']
77
        if maxdepth == 0:
78
            return
79

    
80
        # maxdepth -= 1
81
        maxdepth = None if maxdepth is None else maxdepth - 1
82
        kargs['maxdepth'] = maxdepth
83

    
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
87

    
88
        for f in self.g.readdir(directory):
89
            if f['name'] in ('.', '..'):
90
                continue
91

    
92
            full_path = "%s/%s" % (directory, f['name'])
93

    
94
            if exclude and re.match(exclude, full_path):
95
                continue
96

    
97
            if has_ftype(f, 'd'):
98
                self.foreach_file(full_path, action, **kargs)
99

    
100
            if has_ftype(f, ftype):
101
                action(full_path)
102

    
103
    def get_metadata(self):
104
        """Returns some descriptive metadata about the OS."""
105
        meta = {}
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)
110

    
111
        return meta
112

    
113
    def data_cleanup(self):
114
        """Cleanup sensitive data out of the OS image."""
115

    
116
        puts('Cleaning up sensitive data out of the OS image:')
117
        with indent(4):
118
            for name in dir(self):
119
                attr = getattr(self, name)
120
                if name.startswith('data_cleanup_') and callable(attr):
121
                    attr()
122
        puts()
123

    
124
    def sysprep(self):
125
        """Prepere system for image creation."""
126

    
127
        puts('Preparing system for image creation:')
128
        with indent(4):
129
            for name in dir(self):
130
                attr = getattr(self, name)
131
                if name.startswith('sysprep_') and callable(attr):
132
                    attr()
133
        puts()
134

    
135
# vim: set sta sts=4 shiftwidth=4 sw=4 et ai :