summaryrefslogtreecommitdiff
path: root/python/vyos/migrator.py
blob: 87c74e1eaf0ccba7994f47df4eff0634483ed33c (plain)
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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
# Copyright 2019-2022 VyOS maintainers and contributors <maintainers@vyos.io>
#
# This library is free software; you can redistribute it and/or
# modify it under the terms of the GNU Lesser General Public
# License as published by the Free Software Foundation; either
# version 2.1 of the License, or (at your option) any later version.
#
# This library is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
# Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public License
# along with this library.  If not, see <http://www.gnu.org/licenses/>.

import sys
import os
import json
import logging

import vyos.defaults
import vyos.component_version as component_version
from vyos.util import cmd

log_file = os.path.join(vyos.defaults.directories['config'], 'vyos-migrate.log')

class MigratorError(Exception):
    pass

class Migrator(object):
    def __init__(self, config_file, force=False, set_vintage='vyos'):
        self._config_file = config_file
        self._force = force
        self._set_vintage = set_vintage
        self._config_file_vintage = None
        self._changed = False

    def init_logger(self):
        self.logger = logging.getLogger(__name__)
        self.logger.setLevel(logging.DEBUG)

        # on adding the file handler, allow write permission for cfg_group;
        # restore original umask on exit
        mask = os.umask(0o113)
        fh = logging.FileHandler(log_file)
        formatter = logging.Formatter('%(message)s')
        fh.setFormatter(formatter)
        self.logger.addHandler(fh)
        os.umask(mask)

    def read_config_file_versions(self):
        """
        Get component versions from config file footer and set vintage;
        return empty dictionary if config string is missing.
        """
        cfg_file = self._config_file
        component_versions = {}

        cfg_versions = component_version.from_file(cfg_file, vintage='vyatta')

        if cfg_versions:
            self._config_file_vintage = 'vyatta'
            component_versions = cfg_versions

        cfg_versions = component_version.from_file(cfg_file, vintage='vyos')

        if cfg_versions:
            self._config_file_vintage = 'vyos'
            component_versions = cfg_versions

        return component_versions

    def update_vintage(self):
        old_vintage = self._config_file_vintage

        if self._set_vintage:
            self._config_file_vintage = self._set_vintage

        if self._config_file_vintage not in ['vyatta', 'vyos']:
            raise MigratorError("Unknown vintage.")

        if self._config_file_vintage == old_vintage:
            return False
        else:
            return True

    def run_migration_scripts(self, config_file_versions, system_versions):
        """
        Run migration scripts iteratively, until config file version equals
        system component version.
        """
        os.environ['VYOS_MIGRATION'] = '1'
        self.init_logger()

        self.logger.info("List of executed migration scripts:")

        cfg_versions = config_file_versions
        sys_versions = system_versions

        sys_keys = list(sys_versions.keys())
        sys_keys.sort()

        # XXX 'bgp' needs to follow 'quagga':
        if 'bgp' in sys_keys and 'quagga' in sys_keys:
            sys_keys.insert(sys_keys.index('quagga'),
                            sys_keys.pop(sys_keys.index('bgp')))

        rev_versions = {}

        for key in sys_keys:
            sys_ver = sys_versions[key]
            if key in cfg_versions:
                cfg_ver = cfg_versions[key]
            else:
                cfg_ver = 0

            migrate_script_dir = os.path.join(
                    vyos.defaults.directories['migrate'], key)

            while cfg_ver < sys_ver:
                next_ver = cfg_ver + 1

                migrate_script = os.path.join(migrate_script_dir,
                        '{}-to-{}'.format(cfg_ver, next_ver))

                try:
                    out = cmd([migrate_script, self._config_file])
                    self.logger.info(f'{migrate_script}')
                    if out: self.logger.info(out)
                except FileNotFoundError:
                    pass
                except Exception as err:
                    print("\nMigration script error: {0}: {1}."
                          "".format(migrate_script, err))
                    sys.exit(1)

                cfg_ver = next_ver
            rev_versions[key] = cfg_ver

        del os.environ['VYOS_MIGRATION']
        return rev_versions

    def write_config_file_versions(self, cfg_versions):
        """
        Write new versions string.
        """
        if self._config_file_vintage == 'vyatta':
            component_version.write_version_footer(cfg_versions,
                                                   self._config_file,
                                                   vintage='vyatta')

        if self._config_file_vintage == 'vyos':
            component_version.write_version_footer(cfg_versions,
                                                   self._config_file,
                                                   vintage='vyos')

    def save_json_record(self, component_versions: dict):
        """
        Write component versions to a json file
        """
        mask = os.umask(0o113)
        version_file = vyos.defaults.component_version_json
        try:
            with open(version_file, 'w') as f:
                f.write(json.dumps(component_versions, indent=2, sort_keys=True))
        except OSError:
            pass
        finally:
            os.umask(mask)

    def run(self):
        """
        Gather component versions from config file and system.
        Run migration scripts.
        Update vintage ('vyatta' or 'vyos'), if needed.
        If changed, remove old versions string from config file, and
            write new versions string.
        """
        cfg_file = self._config_file

        cfg_versions = self.read_config_file_versions()
        if self._force:
            # This will force calling all migration scripts:
            cfg_versions = {}

        sys_versions = component_version.from_system()

        # save system component versions in json file for easy reference
        self.save_json_record(sys_versions)

        rev_versions = self.run_migration_scripts(cfg_versions, sys_versions)

        if rev_versions != cfg_versions:
            self._changed = True

        if self.update_vintage():
            self._changed = True

        if not self._changed:
            return

        component_version.remove_footer(cfg_file)

        self.write_config_file_versions(rev_versions)

    def config_changed(self):
        return self._changed

class VirtualMigrator(Migrator):
    def run(self):
        cfg_file = self._config_file

        cfg_versions = self.read_config_file_versions()
        if not cfg_versions:
            return

        if self.update_vintage():
            self._changed = True

        if not self._changed:
            return

        component_version.remove_footer(cfg_file)

        self.write_config_file_versions(cfg_versions)