disk.py 7.41 KB
Newer Older
1
import json
2 3 4
import os
import subprocess
import re
Guba Sándor committed
5
import logging
6 7 8
import requests

logger = logging.getLogger(__name__)
9 10 11 12 13 14 15

re_qemu_img = re.compile(r'(file format: (?P<format>(qcow2|raw))|'
                         r'virtual size: \w+ \((?P<size>[0-9]+) bytes\)|'
                         r'backing file: \S+ \(actual path: (?P<base>\S+)\))$')


class Disk(object):
Guba Sándor committed
16 17 18 19
    ''' Storage driver DISK object.
        Handle qcow2, raw and iso images.
        TYPES, CREATE_TYPES, SNAPSHOT_TYPES are hand managed restrictions.
    '''
20 21 22
    TYPES = ['snapshot', 'normal']
    FORMATS = ['qcow2', 'raw', 'iso']
    CREATE_FORMATS = ['qcow2', 'raw']
23

24
    def __init__(self, dir, name, format, type, size, base_name):
25 26 27
        # TODO: tests
        self.name = name
        self.dir = os.path.realpath(dir)
28
        if format not in self.FORMATS:
29 30
            raise Exception('Invalid format: %s' % format)
        self.format = format
31 32 33
        if type not in self.TYPES:
            raise Exception('Invalid type: %s' % format)
        self.type = type
34 35 36
        self.size = int(size)
        self.base_name = base_name

Dudás Ádám committed
37 38
    @classmethod
    def deserialize(cls, desc):
39 40 41
        logging.info(desc)
        if isinstance(desc, basestring):
            desc = json.loads(desc)
Dudás Ádám committed
42 43 44 45 46 47 48 49 50 51 52
        return cls(**desc)

    def get_desc(self):
        return {
            'name': self.name,
            'dir': self.dir,
            'format': self.format,
            'size': self.size,
            'base_name': self.base_name,
        }

53 54 55 56 57 58 59 60 61 62 63 64
    def get_path(self):
        return os.path.realpath(self.dir + '/' + self.name)

    def get_base(self):
        return os.path.realpath(self.dir + '/' + self.base_name)

    def __unicode__(self):
        return u'%s %s %s %s' % (self.get_path(), self.format,
                                 self.size, self.get_base())

    @classmethod
    def get(cls, dir, name):
65 66
        ''' Create disk from path
        '''
67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86
        path = os.path.realpath(dir + '/' + name)
        output = subprocess.check_output(['qemu-img', 'info', path])

        type = 'normal'
        base = None
        for line in output.split('\n'):
            m = re_qemu_img.search(line)
            if m:
                res = m.groupdict()
                if res.get('format', None) is not None:
                    format = res['format']
                if res.get('size', None) is not None:
                    size = res['size']
                if res.get('base', None) is not None:
                    base = os.path.basename(res['base'])
                    type = 'snapshot'

        return Disk(dir, name, format, size, base, type)

    def create(self):
87 88 89 90
        ''' Creating new image format specified at self.format.
            self.format van be "qcow2-normal"
        '''
        # Check if type is avaliable to create
91
        if self.format not in self.CREATE_FORMATS:
92
            raise Exception('Invalid format: %s' % self.format)
93 94
        if self.type != 'normal':
            raise Exception('Invalid type: %s' % self.format)
95 96 97 98 99 100
        # Check for file if already exist
        if os.path.isfile(self.get_path()):
            raise Exception('File already exists: %s' % self.get_path())
        # Build list of Strings as command parameters
        cmdline = ['qemu-img',
                   'create',
Guba Sándor committed
101
                   '-f', self.format,
Guba Sándor committed
102
                   self.get_path(),
103
                   str(self.size)]
Guba Sándor committed
104
        logging.info("Create file: %s " % cmdline)
105 106 107
        # Call subprocess
        subprocess.check_output(cmdline)

108
    def download(self, task, url, parent_id=None):
109 110 111 112 113 114 115
        ''' Download image from url. '''
        disk_path = self.get_path()
        logger.info("Downloading image from %s to %s", url, disk_path)
        r = requests.get(url, stream=True)
        if r.status_code == 200:
            class AbortException(Exception):
                pass
116 117 118 119 120 121
            if parent_id is None:
                parent_id = task.request.id
            percent_size = float(r.headers['content-length']) / 100
            percent = 0
            actual_size = 0
            chunk_size = 256 * 1024
122 123
            try:
                with open(disk_path, 'wb') as f:
124
                    for chunk in r.iter_content(chunk_size=chunk_size):
125 126 127 128
                        if task.is_aborted():
                            raise AbortException()
                        if chunk:
                            f.write(chunk)
129 130 131 132 133 134 135 136
                            actual_size += chunk_size
                            if actual_size > (percent_size * percent):
                                percent += 1
                                task.update_state(
                                    task_id=parent_id,
                                    state=task.AsyncResult(parent_id).state,
                                    meta={'size': actual_size,
                                          'percent': percent})
137 138 139 140 141 142 143 144 145 146
                    f.flush()
                self.size = os.path.getsize(disk_path)
                logger.debug("Download finished %s (%s bytes)",
                             self.name, self.size)
            except AbortException:
                # Cleanup file:
                os.unlink(self.get_path())
                logger.info("Download %s aborted %s removed.",
                            url, disk_path)

147 148 149
    def snapshot(self):
        ''' Creating qcow2 snapshot with base image.
        '''
150
        # Check if snapshot type and qcow2 format matchmatch
151
        if self.format not in ['qcow2', 'iso']:
152
            raise Exception('Invalid format: %s' % self.format)
153 154
        if self.type != 'snapshot':
            raise Exception('Invalid type: %s' % self.format)
155
        # Check if file already exists
156 157
        if os.path.isfile(self.get_path()):
            raise Exception('File already exists: %s' % self.get_path())
158 159 160 161
        # Check if base file exist
        if not os.path.isfile(self.get_base()):
            raise Exception('Image Base does not exists: %s' % self.get_base())
        # Build list of Strings as command parameters
162 163 164 165 166 167 168 169 170 171 172
        if self.format == 'iso':
            cmdline = ['ln',
                       '-s',
                       self.get_base(),
                       self.get_path()]
        else:
            cmdline = ['qemu-img',
                       'create',
                       '-b', self.get_base(),
                       '-f', self.format,
                       self.get_path()]
173
        # Call subprocess
174 175
        subprocess.check_output(cmdline)

Guba Sándor committed
176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196
    def merge(self, new_disk):
        ''' Merging a new_disk from the actual disk and its base.
        '''
        # Check if snapshot type match
        if self.format != 'qcow2':
            raise Exception('Invalid format: %s' % self.format)
        # Check if file already exists
        if os.path.isfile(new_disk.get_path()):
            raise Exception('File already exists: %s' % self.get_path())
        # Check if base file exist
        if not os.path.isfile(self.get_path()):
            raise Exception('Original image does not exists: %s'
                            % self.get_base())
        cmdline = ['qemu-img',
                   'convert',
                   self.get_path(),
                   '-O', new_disk.format,
                   new_disk.get_path()]
        # Call subprocess
        subprocess.check_output(cmdline)

197
    def delete(self):
198 199
        ''' Delete file
        '''
200 201 202 203 204 205
        if os.path.isfile(self.get_path()):
            os.unlink(self.get_path())

    @classmethod
    def list(cls, dir):
        return [cls.get(dir, file) for file in os.listdir(dir)]