Statistics
| Branch: | Tag: | Revision:

root / image_creator / os_type / __init__.py @ d144e954

History | View | Annotate | Download (5.1 kB)

1 ae48a082 Nikos Skalkotos
# Copyright 2012 GRNET S.A. All rights reserved.
2 ae48a082 Nikos Skalkotos
#
3 ae48a082 Nikos Skalkotos
# Redistribution and use in source and binary forms, with or
4 ae48a082 Nikos Skalkotos
# without modification, are permitted provided that the following
5 ae48a082 Nikos Skalkotos
# conditions are met:
6 ae48a082 Nikos Skalkotos
#
7 ae48a082 Nikos Skalkotos
#   1. Redistributions of source code must retain the above
8 ae48a082 Nikos Skalkotos
#      copyright notice, this list of conditions and the following
9 ae48a082 Nikos Skalkotos
#      disclaimer.
10 ae48a082 Nikos Skalkotos
#
11 ae48a082 Nikos Skalkotos
#   2. Redistributions in binary form must reproduce the above
12 ae48a082 Nikos Skalkotos
#      copyright notice, this list of conditions and the following
13 ae48a082 Nikos Skalkotos
#      disclaimer in the documentation and/or other materials
14 ae48a082 Nikos Skalkotos
#      provided with the distribution.
15 ae48a082 Nikos Skalkotos
#
16 ae48a082 Nikos Skalkotos
# THIS SOFTWARE IS PROVIDED BY GRNET S.A. ``AS IS'' AND ANY EXPRESS
17 ae48a082 Nikos Skalkotos
# OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
18 ae48a082 Nikos Skalkotos
# WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
19 ae48a082 Nikos Skalkotos
# PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL GRNET S.A OR
20 ae48a082 Nikos Skalkotos
# CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21 ae48a082 Nikos Skalkotos
# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22 ae48a082 Nikos Skalkotos
# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
23 ae48a082 Nikos Skalkotos
# USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
24 ae48a082 Nikos Skalkotos
# AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 ae48a082 Nikos Skalkotos
# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
26 ae48a082 Nikos Skalkotos
# ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
27 ae48a082 Nikos Skalkotos
# POSSIBILITY OF SUCH DAMAGE.
28 ae48a082 Nikos Skalkotos
#
29 ae48a082 Nikos Skalkotos
# The views and conclusions contained in the software and
30 ae48a082 Nikos Skalkotos
# documentation are those of the authors and should not be
31 ae48a082 Nikos Skalkotos
# interpreted as representing official policies, either expressed
32 ae48a082 Nikos Skalkotos
# or implied, of GRNET S.A.
33 aa2062ba Nikos Skalkotos
34 8c574358 Nikos Skalkotos
import re
35 22a6d232 Nikos Skalkotos
from clint.textui import indent, puts
36 8c574358 Nikos Skalkotos
37 8c574358 Nikos Skalkotos
38 0d5a999d Nikos Skalkotos
def add_prefix(target):
39 0d5a999d Nikos Skalkotos
    def wrapper(self, *args):
40 0d5a999d Nikos Skalkotos
        prefix = args[0]
41 0d5a999d Nikos Skalkotos
        return map(lambda x: prefix + x, target(self, *args))
42 0d5a999d Nikos Skalkotos
    return wrapper
43 0d5a999d Nikos Skalkotos
44 8c574358 Nikos Skalkotos
45 aa2062ba Nikos Skalkotos
class OSBase(object):
46 3b2f6619 Nikos Skalkotos
    """Basic operating system class"""
47 aa2062ba Nikos Skalkotos
    def __init__(self, rootdev, ghandler):
48 aa2062ba Nikos Skalkotos
        self.root = rootdev
49 aa2062ba Nikos Skalkotos
        self.g = ghandler
50 aa2062ba Nikos Skalkotos
51 0d5a999d Nikos Skalkotos
    @add_prefix
52 8c574358 Nikos Skalkotos
    def ls(self, directory):
53 3b2f6619 Nikos Skalkotos
        """List the name of all files under a directory"""
54 8c574358 Nikos Skalkotos
        return self.g.ls(directory)
55 0d5a999d Nikos Skalkotos
56 0d5a999d Nikos Skalkotos
    @add_prefix
57 8c574358 Nikos Skalkotos
    def find(self, directory):
58 3b2f6619 Nikos Skalkotos
        """List the name of all files recursively under a directory"""
59 8c574358 Nikos Skalkotos
        return self.g.find(directory)
60 8c574358 Nikos Skalkotos
61 8c574358 Nikos Skalkotos
    def foreach_file(self, directory, action, **kargs):
62 3b2f6619 Nikos Skalkotos
        """Perform an action recursively on all files under a directory.
63 8c574358 Nikos Skalkotos

64 3b2f6619 Nikos Skalkotos
        The following options are allowed:
65 3b2f6619 Nikos Skalkotos

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

70 3b2f6619 Nikos Skalkotos
        * ftype: The action will only be performed on files of this
71 3b2f6619 Nikos Skalkotos
          type. For a list of all allowed filetypes, see here:
72 3b2f6619 Nikos Skalkotos
          http://libguestfs.org/guestfs.3.html#guestfs_readdir
73 3b2f6619 Nikos Skalkotos

74 3b2f6619 Nikos Skalkotos
        * exclude: Exclude all files that follow this pattern.
75 3b2f6619 Nikos Skalkotos
        """
76 8c574358 Nikos Skalkotos
        maxdepth = None if 'maxdepth' not in kargs else kargs['maxdepth']
77 8c574358 Nikos Skalkotos
        if maxdepth == 0:
78 8c574358 Nikos Skalkotos
            return
79 8c574358 Nikos Skalkotos
80 8c574358 Nikos Skalkotos
        # maxdepth -= 1
81 8c574358 Nikos Skalkotos
        maxdepth = None if maxdepth is None else maxdepth - 1
82 8c574358 Nikos Skalkotos
        kargs['maxdepth'] = maxdepth
83 8c574358 Nikos Skalkotos
84 8c574358 Nikos Skalkotos
        exclude = None if 'exclude' not in kargs else kargs['exclude']
85 8c574358 Nikos Skalkotos
        ftype = None if 'ftype' not in kargs else kargs['ftype']
86 8c574358 Nikos Skalkotos
        has_ftype = lambda x, y: y is None and True or x['ftyp'] == y
87 8c574358 Nikos Skalkotos
88 8c574358 Nikos Skalkotos
        for f in self.g.readdir(directory):
89 8c574358 Nikos Skalkotos
            if f['name'] in ('.', '..'):
90 8c574358 Nikos Skalkotos
                continue
91 8c574358 Nikos Skalkotos
92 8c574358 Nikos Skalkotos
            full_path = "%s/%s" % (directory, f['name'])
93 8c574358 Nikos Skalkotos
94 8c574358 Nikos Skalkotos
            if exclude and re.match(exclude, full_path):
95 8c574358 Nikos Skalkotos
                continue
96 8c574358 Nikos Skalkotos
97 8c574358 Nikos Skalkotos
            if has_ftype(f, 'd'):
98 8c574358 Nikos Skalkotos
                self.foreach_file(full_path, action, **kargs)
99 8c574358 Nikos Skalkotos
100 8c574358 Nikos Skalkotos
            if has_ftype(f, ftype):
101 8c574358 Nikos Skalkotos
                action(full_path)
102 0d5a999d Nikos Skalkotos
103 aa2062ba Nikos Skalkotos
    def get_metadata(self):
104 a1a5ca4b Nikos Skalkotos
        """Returns some descriptive metadata about the OS."""
105 aa2062ba Nikos Skalkotos
        meta = {}
106 c408053f Nikos Skalkotos
        meta['ROOT_PARTITION'] = "%d" % self.g.part_to_partnum(self.root)
107 c408053f Nikos Skalkotos
        meta['OSFAMILY'] = self.g.inspect_get_type(self.root)
108 c408053f Nikos Skalkotos
        meta['OS'] = self.g.inspect_get_distro(self.root)
109 c408053f Nikos Skalkotos
        meta['description'] = self.g.inspect_get_product_name(self.root)
110 aa2062ba Nikos Skalkotos
111 aa2062ba Nikos Skalkotos
        return meta
112 486ea6e0 Nikos Skalkotos
113 8c574358 Nikos Skalkotos
    def data_cleanup(self):
114 a1a5ca4b Nikos Skalkotos
        """Cleanup sensitive data out of the OS image."""
115 22a6d232 Nikos Skalkotos
116 22a6d232 Nikos Skalkotos
        puts('Cleaning up sensitive data out of the OS image:')
117 3f70f242 Nikos Skalkotos
118 3f70f242 Nikos Skalkotos
        is_cleanup = lambda x: x.startswith('data_cleanup_') and \
119 3f70f242 Nikos Skalkotos
                                                    callable(getattr(self, x))
120 3f70f242 Nikos Skalkotos
        tasks = [getattr(self, x) for x in dir(self) if is_cleanup(x)]
121 3f70f242 Nikos Skalkotos
        size = len(tasks)
122 3f70f242 Nikos Skalkotos
        cnt = 0
123 3f70f242 Nikos Skalkotos
        for task in tasks:
124 3f70f242 Nikos Skalkotos
            cnt += 1
125 3f70f242 Nikos Skalkotos
            puts(('(%d/%d)' % (cnt, size)).ljust(7), False)
126 3f70f242 Nikos Skalkotos
            task()
127 22a6d232 Nikos Skalkotos
        puts()
128 aa2062ba Nikos Skalkotos
129 9cbb5794 Nikos Skalkotos
    def sysprep(self):
130 9cbb5794 Nikos Skalkotos
        """Prepere system for image creation."""
131 22a6d232 Nikos Skalkotos
132 22a6d232 Nikos Skalkotos
        puts('Preparing system for image creation:')
133 3f70f242 Nikos Skalkotos
134 3f70f242 Nikos Skalkotos
        is_sysprep = lambda x: x.startswith('sysprep_') and \
135 3f70f242 Nikos Skalkotos
                                                    callable(getattr(self, x))
136 3f70f242 Nikos Skalkotos
        tasks = [getattr(self, x) for x in dir(self) if is_sysprep(x)]
137 3f70f242 Nikos Skalkotos
        size = len(tasks)
138 3f70f242 Nikos Skalkotos
        cnt = 0
139 3f70f242 Nikos Skalkotos
        for task in tasks:
140 3f70f242 Nikos Skalkotos
            cnt += 1
141 3f70f242 Nikos Skalkotos
            puts(('(%d/%d)' % (cnt, size)).ljust(7), False)
142 3f70f242 Nikos Skalkotos
            task()
143 22a6d232 Nikos Skalkotos
        puts()
144 9cbb5794 Nikos Skalkotos
145 aa2062ba Nikos Skalkotos
# vim: set sta sts=4 shiftwidth=4 sw=4 et ai :