summaryrefslogtreecommitdiff
path: root/extensions/rawdisk.write
blob: 6be546a1cb64cdab00f21e301281365fbee9b687 (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
#!/usr/bin/python
# Copyright (C) 2012-2015  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, see <http://www.gnu.org/licenses/>.


'''A Morph deployment write extension for raw disk images.'''


import contextlib
import os
import pyfdisk
import re
import subprocess
import sys
import time
import tempfile

import writeexts


class RawDiskWriteExtension(writeexts.WriteExtension):

    '''See rawdisk.write.help for documentation'''

    def process_args(self, args):
        if len(args) != 2:
            raise writeexts.ExtensionError(
                'Wrong number of command line args')

        temp_root, location = args
        upgrade = self.get_environment_boolean('UPGRADE')

        if upgrade:
            self.upgrade_local_system(location, temp_root)
        else:
            try:
                if not self.is_device(location):
                    with self.created_disk_image(location):
                        self.create_partitioned_system(temp_root, location)
                    self.status(msg='Disk image has been created at %s' %
                                     location)
                else:
                    self.create_partitioned_system(temp_root, location)
                    self.status(msg='System deployed to %s' % location)
            except Exception:
                self.status(msg='Failure to deploy system to %s' %
                                 location)
                raise

    def upgrade_local_system(self, location, temp_root):
        self.complete_fstab_for_btrfs_layout(temp_root)

        try:
            with self.mount(location) as mp:
                self.do_upgrade(mp, temp_root)
                return
        except subprocess.CalledProcessError:
            pass

        # Failed to mount a raw image, search for a Baserock root filesystem
        # in the device's partitions
        with self.find_and_mount_rootfs(location) as mp:
            self.do_upgrade(mp, temp_root)

    def do_upgrade(self, mp, temp_root):
        version_label = self.get_version_label(mp)
        self.status(msg='Updating image to a new version with label %s' %
                    version_label)

        version_root = os.path.join(mp, 'systems', version_label)
        os.mkdir(version_root)

        old_orig = os.path.join(mp, 'systems', 'default', 'orig')
        new_orig = os.path.join(version_root, 'orig')
        subprocess.check_call(
            ['btrfs', 'subvolume', 'snapshot', old_orig, new_orig])

        subprocess.check_call(
            ['rsync', '-a', '--checksum', '--numeric-ids', '--delete',
             temp_root + os.path.sep, new_orig])

        self.create_run(version_root)

        default_path = os.path.join(mp, 'systems', 'default')
        if os.path.exists(default_path):
            os.remove(default_path)
        else:
            # we are upgrading and old system that does
            # not have an updated extlinux config file
            if self.bootloader_config_is_wanted():
                self.generate_bootloader_config(mp)
            self.install_bootloader(mp)
        os.symlink(version_label, default_path)

        if self.bootloader_config_is_wanted():
            self.install_kernel(version_root, temp_root)

    def get_version_label(self, mp):
        version_label = os.environ.get('VERSION_LABEL')

        if version_label is None:
            raise writeexts.ExtensionError('VERSION_LABEL was not given')

        if os.path.exists(os.path.join(mp, 'systems', version_label)):
            raise writeexts.ExtensionError('VERSION_LABEL %s already exists'
                                           % version_label)

        return version_label


RawDiskWriteExtension().run()