summaryrefslogtreecommitdiff
path: root/kpov_judge/create_disk_images.py
blob: ce79f85e47d5039328d266eb0d3b9b037db18e95 (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
#!/usr/bin/env python3

import hashlib
import collections
import fcntl
import glob
import inspect
import os
import re
import subprocess
import sys

import guestfs
import pymongo

import settings
import kpov_util
from util import write_default_config

def get_prepare_disks(db, class_id, task_id):
    prepare_disks_source = db.prepare_disks.find_one({'class_id': class_id, 'task_id': task_id})['source']
    d = {}
    exec(compile(prepare_disks_source, 'prepare_disks.py', 'exec'), globals(), d)
    return d['prepare_disks']

def create_snapshot(class_id, task_id, student_id, disk_name, overwrite=True):
    # add a hash to filename to allow multiple students using the same directory
    snap_hash = hashlib.sha1((disk_name+class_id+task_id+student_id).encode()).hexdigest()[:4]
    snap = '{}-{}-{}.{}'.format(
        task_id, disk_name, snap_hash, settings.STUDENT_DISK_FORMAT)
    backing = []

    template = disk_name + '.' + settings.STUDENT_DISK_FORMAT
    task_dir = os.path.join(student_id, class_id, task_id)
    task_path = os.path.join(settings.STUDENT_DISK_PATH, task_dir)

    if not os.path.exists(os.path.join(task_path)) or overwrite:
        if settings.STUDENT_DISK_COW:
            # don’t use backing files, just copy the template
            # (requires a cow-capable filesystem)
            subprocess.call(['cp', '--reflink=always', template, snap])

        else:
            # create task dir
            os.makedirs(task_path, exist_ok=True)

            # qemu-img create stores backing-file path as given, so link all
            # backing images to task directory where target image will be
            # generated
            os.chdir(settings.DISK_TEMPLATE_PATH) # qemu-img info is saner when called from image directory
            output = subprocess.check_output(
                ['qemu-img', 'info', '--backing-chain', template], universal_newlines=True)
            for image in [template] + [m.group(1) for m in re.finditer(r'backing file: (.*)', output)]:
                backing += [image]
                dest = os.path.join(task_path, image)
                if not os.path.exists(dest):
                    os.symlink(os.path.join(settings.DISK_TEMPLATE_PATH, image), dest)
            # would be great if someone finds a way to avoid the stuff above

            # make overlay image
            os.chdir(task_path)
            subprocess.call(['qemu-img', 'create',
                '-f', settings.STUDENT_DISK_FORMAT,
                '-b', template, snap])

    return task_dir, snap, backing

if __name__ == '__main__':
    if len(sys.argv) != 1:
        print("Usage: {0}")
        print("Create the pending disk images")

    db = pymongo.MongoClient(settings.DB_URI).get_default_database()

    all_computers = collections.defaultdict(list)
    for computer in db.student_computers.find({"disk_urls": {"$exists": False}}):
        all_computers[(computer['class_id'], computer['task_id'], computer['student_id'])] += [computer]

    for (class_id, task_id, student_id), computers in all_computers.items():
        # TODO check why we iterate over student_computers twice
        l = db.student_computers.find_one({'class_id': class_id, 'task_id': task_id, 'student_id': student_id})
        if l is None:
            continue

        print("Creating {}/{} for {}".format(class_id, task_id, student_id))

        lock_file = os.path.join(settings.STUDENT_LOCKFILE_PATH, 
            '{0}-{1}-{2}.lock'.format(student_id, class_id, task_id))
        lock_fp = open(lock_file, 'w')
        try:
            fcntl.lockf(lock_fp, fcntl.LOCK_EX | fcntl.LOCK_NB)
        except IOError:
            continue

        task_params = db.task_params.find_one({'class_id': class_id, 'task_id': task_id, 'student_id': student_id})['params']
        prepare_disks = get_prepare_disks(db, class_id, task_id)

        # tule odpri, ustvari snapshote za vajo
        templates = {}
        all_disks = collections.defaultdict(list)
        parts = {}
        for computer in computers:
            lock_fp.write('creating computer ' + computer['name'] + '\n')
            if len(computer['disks']) == 0:
                continue

            manual_disks = []
            try_automount = False

            g = guestfs.GuestFS()
            for disk in computer['disks']:
                lock_fp.write("register " + disk['name'] + '\n')
                task_dir, snap, backing = create_snapshot(class_id, task_id, student_id, disk['name'])
                snap_file = os.path.join(settings.STUDENT_DISK_PATH, task_dir, snap)
                if 'options' in disk:
                    g.add_drive_opts(snap_file, **(disk['options']))
                else:
                    g.add_drive(snap_file)
                if 'parts' in disk:
                    for p in disk['parts']:
                        lock_fp.write("part {}: {}\n".format(
                            settings.GUESTFS_DEV_PREFIX + p['dev'], p['path']))
                        manual_disks.append(
                            (settings.GUESTFS_DEV_PREFIX + p['dev'], p['path'], p.get('options', None)))
                else:
                    try_automount = True

                templates[disk['name']] = g
                lock_fp.write("  templates[{}] = {}\n".format(disk['name'], disk))

                all_disks[computer['name']] += [{
                    'name': disk['name'],
                    'file': snap,
                    'backing': backing,
                }]

            g.launch()
            mounted = set()
            if try_automount:
                roots = g.inspect_os()
                for root in roots:
                    mps = g.inspect_get_mountpoints(root)
                    lock_fp.write('detected: ' + str(mps) + '\n')
                    for mountpoint, device in sorted(mps):
                        if mountpoint not in mounted:
                            try:
                                g.mount(device, mountpoint, )
                                lock_fp.write( 'mounted ' + device + ' on ' + mountpoint + '\n')
                            except RuntimeError as msg:
                                lock_fp.write( "%s (ignored)\n" % msg)
                            mounted.add(mountpoint)

            for device, mountpoint, opts in manual_disks:
                try:
                    if opts is not None:
                        g.mount_options(opts, device, mountpoint)
                    else:
                        g.mount(device, mountpoint)
                    lock_fp.write('manually mounted ' + device + " on " + mountpoint + '\n')
                except RuntimeError as msg:
                    lock_fp.write( "%s (ignored)\n" % msg)

        lock_fp.write("preparing disks\n")
        global_params = {
            'task_name': task_id,
            'class_id': class_id,
            'username': student_id
        }
        if 'TASK_URL' in vars(settings):
            global_params['task_url'] = settings.TASK_URL + '/' + class_id + '/'

        prepare_disks(templates, task_params, global_params)

        # pospravi za seboj.
        lock_fp.write("unmounting\n")
        for g in set(templates.values()):
            g.umount_all()
            g.close()

        lock_fp.write("saving URLs\n")
        for computer in computers:
            comp_name = computer['name']
            disks = all_disks[comp_name]
            lock_fp.write('urls: '+ str(disks) + '\n')
            l = db.student_computers.update({
                'disk_urls': {'$exists': False}, 
                'student_id': student_id, 
                'task_id': task_id,
                'class_id': class_id,
                'name': comp_name},
                {'$set': { 'disk_urls': disks }
            })

        os.unlink(lock_file)
        lock_fp.close()