summaryrefslogtreecommitdiff
path: root/system-version-manager/system-version-manager
blob: cdf453b20df65d43a93d74b7e440e367f35e7161 (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
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
#!/usr/bin/python
#
# Copyright (C) 2014  Codethink Limited
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; version 2 of the License.
#
# This program 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 General Public License for more details.
#
# You should have received a copy of the GNU General Public License along
# with this program; if not, write to the Free Software Foundation, Inc.,
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.


import argparse
import errno
import subprocess
import tempfile
import json
import os
import sys
import shutil


class AppException(Exception):

    pass


class SystemNotCompatibleError(Exception):

    pass


class SystemVersionManager(object):

    def __init__(self, args, mount_dir):
        self.device, self.current_system = self._get_mount_info()
        self.mount_dir = mount_dir
        self.device_uuid = self._get_device_uuid(self.device)

        # create the top-level parser
        parser = argparse.ArgumentParser(prog='system-version-manager')
        subparsers = parser.add_subparsers(help='sub-command help')

        # create the parser for the "list" command
        parser_list = subparsers.add_parser('list',
            help='list show you a list of systems')
        parser_list.set_defaults(action='list')

        # create the parser for the "deploy" command
        parser_deploy= subparsers.add_parser('deploy',
            help='deploy an updated base OS version to the running Baserock system')
        parser_deploy.set_defaults(action='deploy')
        parser_deploy.add_argument('location')

        # create the parser for the "get-default" command
        parser_get_default = subparsers.add_parser('get-default',
            help='prints the default system')
        parser_get_default.set_defaults(action='get-default')

        # create the parser for the "get-running" command
        parser_get_running = subparsers.add_parser('get-running',
            help='prints the running system')
        parser_get_running.set_defaults(action='get-running')

        # create the parser for the "remove" command
        parser_remove = subparsers.add_parser('remove', help='remove a system')
        parser_remove.add_argument('system_name', help='name of the system to remove')
        parser_remove.set_defaults(action='remove')

        # create the parser for the "set-default" command
        parser_set_default = subparsers.add_parser('set-default',
            help='set a system as default')
        parser_set_default.add_argument('system_name', help='name of the system to set')
        parser_set_default.set_defaults(action='set-default')

        self.args = parser.parse_args(args[1:])

    def status(self, msg, *args):
        print msg % args

    def _check_system_exists(self, system_name):
        systems_list = self._get_systems()

        if system_name not in systems_list:
            sys.stderr.write("ERROR: the system %s doesn't exist\n" % system_name)
            sys.exit(1)

    # To get the systems the script lists the systems under the 'systems'
    # folder which are directories and are not symlinks
    def _get_systems(self):
        systems = os.path.join(self.mount_dir, 'systems')
        return [filename for filename in os.listdir(systems)
            if os.path.isdir(os.path.join(systems, filename))
            and not os.path.islink(os.path.join(systems, filename))]

    # To check which system is the default one, it checks the 'ontimeout'
    # value in the extlinux.conf file. If it's not present, then pick
    # the first of the present systems.
    def _get_default(self):
        extlinux = os.path.join(self.mount_dir, 'extlinux.conf')
        with open(extlinux, 'r') as f:
            for line in f:
                line = line.rstrip('\n')
                key, value= line.split(' ', 1)
                if key == "ontimeout":
                    return value
                if key == "label":
                    break

        return self.current_system

    def _get_deployment_config(self, system):
        try:
            meta = open(os.path.join(self.mount_dir, 'systems', system,
                                    'run/baserock/deployment.meta'))
        except IOError as e:
            if e.errno != errno.ENOENT:
                raise
            deployment_config = {}
        else:
            deployment_config = json.load(meta).get('configuration', {})
            meta.close()
        return deployment_config

    def _atomic_symlink_update(self, source, link_name):
        dirname = os.path.dirname(link_name)
        temp_dir = tempfile.mkdtemp(dir=dirname)
        temp_link = os.path.join(temp_dir, 'temp_link')
        try:
            os.symlink(source, temp_link)
            os.rename(temp_link, link_name)
        except (OSError, IOError):
            shutil.rmtree(temp_dir)
            raise
        # By this time, temp_dir will be empty.
        os.rmdir(temp_dir)

    def _rewrite_boot_menu(self, device, default, systems):
        # Logic copied from morphlib.SaveFile to not create
        # a morphlib dependency.
        fd, temp_config = tempfile.mkstemp(dir=self.mount_dir)
        config = os.path.join(self.mount_dir, 'extlinux.conf')
        with os.fdopen(fd, 'w') as f:
            # If theres no menu.c32 file, add a menu to the extlinux.conf file
            if self._check_system_syslinux():
                f.write('default menu.c32\n')
            else:
                f.write('default ' + default + '\n')
                f.write('menu title baserock boot options\n')
            f.write('default menu.c32\n')
            f.write('timeout 50\n')
            f.write('prompt 0\n')
            f.write('ontimeout ' + default +'\n')
            for system in systems:
                deployment_config = self._get_deployment_config(system)
                f.write('label ' + system + '\n')
                f.write('kernel /systems/' + system + '/kernel\n')
                kernel_args = ('rw init=/sbin/init rootfstype=btrfs '
                               'rootflags=subvol=systems/'+ system +'/run ')
                if 'INITRAMFS_PATH' in deployment_config:
                    f.write('initrd /systems/%s/initramfs\n' % system)
                    kernel_args += ('root=UUID=%s ' % self.device_uuid)
                else:
                    kernel_args += ('root=%s ' % self.device)
                if 'DTB_PATH' in deployment_config:
                    f.write('devicetree /systems/%s/dtb\n' % system)

                kernel_args += deployment_config.get('KERNEL_ARGS', '')
                f.write('append %s\n' % kernel_args)
        os.rename(temp_config, config)

        default_path = os.path.join(self.mount_dir, 'systems', 'default')
        if os.path.islink(default_path):
            self._atomic_symlink_update(default, default_path)

    def cmd_list(self):
        for system in self._get_systems():
            print system

    def cmd_get_default(self):
        print self._get_default()

    def cmd_get_running(self):
        print self.current_system

    def _parse_deploy_location(self, location):
        label, snapshot = os.path.split(location)
        root, label = os.path.split(label)
        if root != '/systems' or snapshot != 'orig':
            raise AppException(
                    "Invalid deploy location %s. Upgrades should be deployed "
                    "from a Btrfs snapshot following the pattern: "
                    "/systems/$LABEL/orig" % location)
        return label

    def cmd_deploy(self, location):
        '''Client-side deployment of a new base OS version.

        This code assumes that the 'orig' subvolume has been correctly created
        already. In future it could be extended to receive a delta of the
        upgraded version over network somehow.

        '''

        label = self._parse_deploy_location(location)
        version_root = os.path.join(self.mount_dir, 'systems', label)

        orig_dir = os.path.join(version_root, 'orig')
        run_dir = os.path.join(version_root, 'run')
        try:
            self.status(msg="Creating 'run' subvolume")
            subprocess.check_call(
                    ['btrfs', 'subvolume', 'snapshot', orig_dir, run_dir])

            self.status(msg='Updating system configuration')
            log = os.path.join('/var', 'log', 'baserock-system-config-sync.log')

            baserock_system_config_sync = os.environ.get(
                    'BASEROCK_SYSTEM_CONFIG_SYNC',
                    'baserock-system-config-sync')

            with open(log, 'w') as f:
                subprocess.check_call(
                        [baserock_system_config_sync, 'merge',
                         self.current_system, label], stdout=f)

            # Copy the content of /var of the system deployed.
            state_dir = os.path.join(self.mount_dir, 'state')
            shared_var = os.path.join(state_dir, 'var')
            new_var=os.path.join(run_dir, 'var')

            for file in os.listdir(new_var):
                subprocess.call(['cp', '-a', os.path.join(new_var, file), shared_var])

            self.status(msg="Installing the kernel")
            self._install_kernel(version_root)

            deployment_config = self._get_deployment_config(label)
            if 'INITRAMFS_PATH' in deployment_config:
                self.status(msg="Installing the initramfs")
                self._install_initramfs(deployment_config['INITRAMFS_PATH'],
                                        version_root)
            if 'DTB_PATH' in deployment_config:
                self.status(msg="Installing the device tree")
                self._install_dtb(deployment_config['DTB_PATH'],
                                  version_root)

        except Exception as e:
            # We are not controlling if deleting the suvolume fails
            subprocess.call(['btrfs', 'subvolume', 'delete', run_dir])
            raise

        self.status(msg="Rewriting boot menu")
        self._rewrite_boot_menu(self.device, self._get_default(), self._get_systems())

    def _install_kernel(self, version_root):
        '''Install the kernel outside of 'orig' or 'run' subvolumes

        This code is kind of duplicated in morphlib/writeexts.py.

        '''
        image_names = ['vmlinuz', 'zImage', 'uImage']
        kernel_dest = os.path.join(version_root, 'kernel')
        for name in image_names:
            try_path = os.path.join(version_root, 'run', 'boot', name)
            if os.path.exists(try_path):
                shutil.copy2(try_path, kernel_dest)
                break

    def _install_dtb(self, dtb_path, version_root):
        '''Install the devicetree outside of 'orig' or 'run' subvolumes

        This code is kind of duplicated in morphlib/writeexts.py.

        '''
        self.status(msg='Installing devicetree')
        self.status(msg='Device tree path=%s' % dtb_path)
        dtb_dest = os.path.join(version_root, 'dtb')
        try_path = os.path.join(version_root, 'run', dtb_path)
        shutil.copy2(try_path, dtb_dest)

    def _install_initramfs(self, initramfs_path, version_root):
        '''Install the initramfs outside of 'orig' or 'run' subvolumes

        This code is kind of duplicated in morphlib/writeexts.py.

        '''
        initramfs_dest = os.path.join(version_root, 'initramfs')
        initramfs_src = os.path.join(version_root, 'run', initramfs_path)
        shutil.copy2(initramfs_src, initramfs_dest)

    def _get_mount_info(self):
        mountpoint = subprocess.check_output(
            ['findmnt', '/', '-l', '-n', '-o', 'SOURCE'])
        device, subvolume = mountpoint.split('[', 1)
        subvolume = subvolume.split('/run', 1)[0]
        current_system = os.path.basename(subvolume)

        # Following the symlink of the device.
        device = os.path.realpath(device)
        return device, current_system

    def _get_device_uuid(self, device):
        # Find block device's UUID. Does not work with busybox blkid,
        # but given this is written in python, that's probably the least
        # of our worries
        return subprocess.check_output(
            ['blkid', '-s', 'UUID', '-o', 'value', device]).strip()

    def cmd_remove(self, system_name):
        self._check_system_exists(system_name)

        default_system = self._get_default()

        if system_name == default_system:
            sys.stderr.write("ERROR: you can't remove the default system\n")
            sys.exit(1)
        if system_name == self.current_system:
            sys.stderr.write("ERROR: you can't remove the running system\n")
            sys.exit(1)

        self.status(msg="Removing system: %s" % system_name)
        system_root = os.path.join(self.mount_dir, 'systems', system_name)

        # We are not controlling if deleting the suvolume fails
        subprocess.call(['btrfs', 'subvolume', 'delete',
            os.path.join(system_root, 'run')])
        subprocess.call(['btrfs', 'subvolume', 'delete',
            os.path.join(system_root, 'orig')])
        shutil.rmtree(system_root)

        self._rewrite_boot_menu(self.device, default_system, self._get_systems())

    def cmd_set_default(self, system_name):
        self._check_system_exists(system_name)
        self._rewrite_boot_menu(self.device, system_name, self._get_systems())

    def mount_fs(self):
        subprocess.check_call(['mount', self.device, self.mount_dir])

    def umount_fs(self):
        subprocess.call(['umount', self.mount_dir])

    def _check_system_syslinux(self):
        # It's not essential to have a menu.c32 file, if it's not there we can
        # add a menu directly to the extlinux.conf file later
        menu_file = os.path.join(self.mount_dir, 'menu.c32')
        if not os.path.isfile(menu_file):
            return False
        return True

    def run(self):
        args = self.args
        action = args.action

        self.mount_fs()
        try:
            if action == "list":
                self.cmd_list()
            elif action == "deploy":
                self.cmd_deploy(args.location)
            elif action == "remove":
                self.cmd_remove(args.system_name)
            elif action == "set-default":
                self.cmd_set_default(args.system_name)
            elif action == "get-default":
                self.cmd_get_default()
            elif action == "get-running":
                self.cmd_get_running()
            else:
                raise NotImplementedError("Unknown command %s" % action)
        except SystemNotCompatibleError, e:
            sys.stderr.write("ERROR, system not compatible: %s\n" % e.args[0])
            raise
        finally:
            self.umount_fs()

mount_dir = tempfile.mkdtemp()
try:
    SystemVersionManager(sys.argv, mount_dir).run()
finally:
    os.rmdir(mount_dir)