Commit cfdc07d9 by Guba Sándor

disk: added download function

parent 75467ac1
...@@ -3,6 +3,9 @@ import os ...@@ -3,6 +3,9 @@ import os
import subprocess import subprocess
import re import re
import logging import logging
import requests
logger = logging.getLogger(__name__)
re_qemu_img = re.compile(r'(file format: (?P<format>(qcow2|raw))|' re_qemu_img = re.compile(r'(file format: (?P<format>(qcow2|raw))|'
r'virtual size: \w+ \((?P<size>[0-9]+) bytes\)|' r'virtual size: \w+ \((?P<size>[0-9]+) bytes\)|'
...@@ -102,6 +105,31 @@ class Disk(object): ...@@ -102,6 +105,31 @@ class Disk(object):
# Call subprocess # Call subprocess
subprocess.check_output(cmdline) subprocess.check_output(cmdline)
def download(self, task, url):
''' 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
try:
with open(disk_path, 'wb') as f:
for chunk in r.iter_content(chunk_size=4096):
if task.is_aborted():
raise AbortException()
if chunk:
f.write(chunk)
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)
def snapshot(self): def snapshot(self):
''' Creating qcow2 snapshot with base image. ''' Creating qcow2 snapshot with base image.
''' '''
......
celery==3.0.23 celery==3.0.23
requests==2.2.1
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or sign in to comment