Revision c89622cd lib/backend.py
b/lib/backend.py | ||
---|---|---|
42 | 42 |
import logging |
43 | 43 |
import os |
44 | 44 |
import os.path |
45 |
import pycurl |
|
45 | 46 |
import random |
46 | 47 |
import re |
47 | 48 |
import shutil |
... | ... | |
52 | 53 |
import zlib |
53 | 54 |
|
54 | 55 |
from ganeti import errors |
56 |
from ganeti import http |
|
55 | 57 |
from ganeti import utils |
56 | 58 |
from ganeti import ssh |
57 | 59 |
from ganeti import hypervisor |
... | ... | |
2267 | 2269 |
result.fail_reason, result.output) |
2268 | 2270 |
|
2269 | 2271 |
|
2272 |
def _DownloadAndDumpDevice(source_url, target_path, size): |
|
2273 |
"""This function images a device using a downloaded image file. |
|
2274 |
|
|
2275 |
@type source_url: string |
|
2276 |
@param source_url: URL of image to dump to disk |
|
2277 |
|
|
2278 |
@type target_path: string |
|
2279 |
@param target_path: path of the device to image |
|
2280 |
|
|
2281 |
@type size: int |
|
2282 |
@param size: maximum size in MiB to write (data source might be smaller) |
|
2283 |
|
|
2284 |
@rtype: NoneType |
|
2285 |
@return: None |
|
2286 |
@raise RPCFail: in case of download or write failures |
|
2287 |
|
|
2288 |
""" |
|
2289 |
class DDParams(object): |
|
2290 |
def __init__(self, current_size, total_size): |
|
2291 |
self.current_size = current_size |
|
2292 |
self.total_size = total_size |
|
2293 |
self.image_size_error = False |
|
2294 |
|
|
2295 |
def dd_write(ddparams, out): |
|
2296 |
if ddparams.current_size < ddparams.total_size: |
|
2297 |
ddparams.current_size += len(out) |
|
2298 |
target_file.write(out) |
|
2299 |
else: |
|
2300 |
ddparams.image_size_error = True |
|
2301 |
return -1 |
|
2302 |
|
|
2303 |
target_file = open(target_path, "w") |
|
2304 |
ddparams = DDParams(0, 1024 * 1024 * size) |
|
2305 |
|
|
2306 |
curl = pycurl.Curl() |
|
2307 |
curl.setopt(pycurl.VERBOSE, True) |
|
2308 |
curl.setopt(pycurl.NOSIGNAL, True) |
|
2309 |
curl.setopt(pycurl.USERAGENT, http.HTTP_GANETI_VERSION) |
|
2310 |
curl.setopt(pycurl.URL, source_url) |
|
2311 |
curl.setopt(pycurl.WRITEFUNCTION, lambda out: dd_write(ddparams, out)) |
|
2312 |
|
|
2313 |
try: |
|
2314 |
curl.perform() |
|
2315 |
except pycurl.error: |
|
2316 |
if ddparams.image_size_error: |
|
2317 |
_Fail("Disk image larger than the disk") |
|
2318 |
else: |
|
2319 |
raise |
|
2320 |
|
|
2321 |
target_file.close() |
|
2322 |
|
|
2323 |
|
|
2270 | 2324 |
def BlockdevWipe(disk, offset, size): |
2271 | 2325 |
"""Wipes a block device. |
2272 | 2326 |
|
Also available in: Unified diff