File: bootstrapinfo.py

package info (click to toggle)
bootstrap-vz 0.9.11%2B20180121git-1
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 2,244 kB
  • sloc: python: 8,800; sh: 813; makefile: 16
file content (164 lines) | stat: -rw-r--r-- 7,121 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164


class BootstrapInformation(object):
    """The BootstrapInformation class holds all information about the bootstrapping process.
    The nature of the attributes of this class are rather diverse.
    Tasks may set their own attributes on this class for later retrieval by another task.
    Information that becomes invalid (e.g. a path to a file that has been deleted) must be removed.
    """
    def __init__(self, manifest=None, debug=False):
        """Instantiates a new bootstrap info object.

        :param Manifest manifest: The manifest
        :param bool debug: Whether debugging is turned on
        """
        # Set the manifest attribute.
        self.manifest = manifest
        self.debug = debug

        # Create a run_id. This id may be used to uniquely identify the currrent bootstrapping process
        import random
        self.run_id = '{id:08x}'.format(id=random.randrange(16 ** 8))

        # Define the path to our workspace
        import os.path
        self.workspace = os.path.join(manifest.bootstrapper['workspace'], self.run_id)

        # Load all the volume information
        from fs import load_volume
        self.volume = load_volume(self.manifest.volume, manifest.system['bootloader'])

        # The default apt mirror
        self.apt_mirror = self.manifest.packages.get('mirror', 'http://deb.debian.org/debian/')

        # The default apt security mirror
        self.apt_security = self.manifest.packages.get('security', 'http://security.debian.org/')

        # Create the manifest_vars dictionary
        self.manifest_vars = self.__create_manifest_vars(self.manifest, {'apt_security': self.apt_security,
                                                                         'apt_mirror': self.apt_mirror})

        # Keep a list of apt sources,
        # so that tasks may add to that list without having to fiddle with apt source list files.
        from pkg.sourceslist import SourceLists
        self.source_lists = SourceLists(self.manifest_vars)
        # Keep a list of apt preferences
        from pkg.preferenceslist import PreferenceLists
        self.preference_lists = PreferenceLists(self.manifest_vars)
        # Keep a list of packages that should be installed, tasks can add and remove things from this list
        from pkg.packagelist import PackageList
        self.packages = PackageList(self.manifest_vars, self.source_lists)

        # These sets should rarely be used and specify which packages the debootstrap invocation
        # should be called with.
        self.include_packages = set()
        self.exclude_packages = set()

        # Dictionary to specify which commands are required on the host.
        # The keys are commands, while the values are either package names or urls
        # that hint at how a command may be made available.
        self.host_dependencies = {}

        # Path to optional bootstrapping script for modifying the behaviour of debootstrap
        # (will be used instead of e.g. /usr/share/debootstrap/scripts/jessie)
        self.bootstrap_script = None

        # Lists of startup scripts that should be installed and disabled
        self.initd = {'install': {}, 'disable': []}

        # Add a dictionary that can be accessed via info._pluginname for the provider and every plugin
        # Information specific to the module can be added to that 'namespace', this avoids clutter.
        providername = manifest.modules['provider'].__name__.split('.')[-1]
        setattr(self, '_' + providername, {})
        for plugin in manifest.modules['plugins']:
            pluginname = plugin.__name__.split('.')[-1]
            setattr(self, '_' + pluginname, {})

    def __create_manifest_vars(self, manifest, additional_vars={}):
        """Creates the manifest variables dictionary, based on the manifest contents
        and additional data.

        :param Manifest manifest: The Manifest
        :param dict additional_vars: Additional values (they will take precedence and overwrite anything else)
        :return: The manifest_vars dictionary
        :rtype: dict
        """

        def set_manifest_vars(obj, data):
            """Runs through the manifest and creates DictClasses for every key

            :param dict obj: dictionary to set the values on
            :param dict data: dictionary of values to set on the obj
            """
            for key, value in data.iteritems():
                if isinstance(value, dict):
                    obj[key] = DictClass()
                    set_manifest_vars(obj[key], value)
                    continue
                # Lists are not supported
                if not isinstance(value, list):
                    obj[key] = value

        # manifest_vars is a dictionary of all the manifest values,
        # with it users can cross-reference values in the manifest, so that they do not need to be written twice
        manifest_vars = {}
        set_manifest_vars(manifest_vars, manifest.data)

        # Populate the manifest_vars with datetime information
        # and map the datetime variables directly to the dictionary
        from datetime import datetime
        now = datetime.now()
        time_vars = ['%a', '%A', '%b', '%B', '%c', '%d', '%f', '%H',
                     '%I', '%j', '%m', '%M', '%p', '%S', '%U', '%w',
                     '%W', '%x', '%X', '%y', '%Y', '%z', '%Z']
        for key in time_vars:
            manifest_vars[key] = now.strftime(key)

        # Add any additional manifest variables
        # They are added last so that they may override previous variables
        set_manifest_vars(manifest_vars, additional_vars)
        return manifest_vars

    def __getstate__(self):
        from bootstrapvz.remote import supported_classes

        def can_serialize(obj):
            if hasattr(obj, '__class__') and hasattr(obj, '__module__'):
                class_name = obj.__module__ + '.' + obj.__class__.__name__
                return class_name in supported_classes or isinstance(obj, (BaseException, Exception))
            return True

        def filter_state(state):
            if isinstance(state, dict):
                return {key: filter_state(val) for key, val in state.items() if can_serialize(val)}
            if isinstance(state, (set, tuple, list, frozenset)):
                return type(state)(filter_state(val) for val in state if can_serialize(val))
            return state

        state = filter_state(self.__dict__)
        state['__class__'] = self.__module__ + '.' + self.__class__.__name__
        return state

    def __setstate__(self, state):
        for key in state:
            self.__dict__[key] = state[key]


class DictClass(dict):
    """Tiny extension of dict to allow setting and getting keys via attributes
    """
    def __getattr__(self, name):
        return self[name]

    def __setattr__(self, name, value):
        self[name] = value

    def __delattr__(self, name):
        del self[name]

    def __getstate__(self):
        return self.__dict__

    def __setstate__(self, state):
        for key in state:
            self[key] = state[key]