root / snf-cyclades-app / synnefo / volume / snapshots.py @ 19b2c29d
History | View | Annotate | Download (3.3 kB)
1 |
import datetime |
---|---|
2 |
from django.utils import simplejson as json |
3 |
from django.db import transaction |
4 |
from snf_django.lib.api import faults |
5 |
from snf_django.lib.api.utils import isoformat |
6 |
from synnefo.plankton.utils import image_backend |
7 |
from synnefo.logic import backend |
8 |
from synnefo.volume import util |
9 |
|
10 |
SNAPSHOTS_CONTAINER = "snapshots"
|
11 |
SNAPSHOTS_DOMAIN = "plankton"
|
12 |
SNAPSHOTS_PREFIX = "plankton:"
|
13 |
SNAPSHOTS_TYPE = "application/octet-stream"
|
14 |
SNAPSHOTS_MAPFILE_PREFIX = "archip:"
|
15 |
|
16 |
|
17 |
@transaction.commit_on_success
|
18 |
def create(user_id, volume, name, description, metadata, force=False): |
19 |
|
20 |
if volume.machine is None: |
21 |
raise faults.BadRequest("Can not snapshot detached volume!") |
22 |
|
23 |
flavor = volume.machine.flavor |
24 |
if not flavor.disk_template.startswith("ext_"): |
25 |
msg = ("Snapshots are supported only for volumes of ext_*"
|
26 |
" disk template")
|
27 |
raise faults.BadRequest(msg)
|
28 |
|
29 |
volume.snapshot_counter += 1
|
30 |
volume.save() |
31 |
transaction.commit() |
32 |
|
33 |
snapshot_metadata = {} |
34 |
snapshot_metadata[SNAPSHOTS_PREFIX + "name"] = name
|
35 |
snapshot_metadata[SNAPSHOTS_PREFIX + "description"] = description
|
36 |
snapshot_metadata[SNAPSHOTS_PREFIX + "metadata"] = json.dumps(metadata)
|
37 |
snapshot_metadata[SNAPSHOTS_PREFIX + "volume_id"] = volume.id
|
38 |
snapshot_metadata[SNAPSHOTS_PREFIX + "status"] = "CREATING" |
39 |
#XXX: just to work
|
40 |
snapshot_metadata[SNAPSHOTS_PREFIX + "is_snapshot"] = True |
41 |
#XXX: for images
|
42 |
snapshot_metadata[SNAPSHOTS_PREFIX + "store"] = "pithos" |
43 |
snapshot_metadata[SNAPSHOTS_PREFIX + "disk_format"] = "diskdump" |
44 |
snapshot_metadata[SNAPSHOTS_PREFIX + "default_container_format"] = "bare" |
45 |
# XXX: Hack-ish way to clone the metadata
|
46 |
image_properties = {"EXCLUDE_ALL_TASKS": "yes", |
47 |
"description": description}
|
48 |
vm_metadata = dict(volume.machine.metadata.values_list("meta_key", "meta_value")) |
49 |
for key in ["OS", "users"]: |
50 |
val = vm_metadata.get(key) |
51 |
if val is not None: |
52 |
image_properties[key] = val |
53 |
snapshot_metadata[SNAPSHOTS_PREFIX + "properties"] = json.dumps(image_properties)
|
54 |
|
55 |
snapshot_name = generate_snapshot_name(volume) |
56 |
mapfile = SNAPSHOTS_MAPFILE_PREFIX + snapshot_name |
57 |
|
58 |
size = volume.size << 30
|
59 |
with image_backend(user_id) as pithos_backend: |
60 |
# move this to plankton backend
|
61 |
snapshot_uuid = pithos_backend.backend.register_object_map( |
62 |
user=user_id, |
63 |
account=user_id, |
64 |
container=SNAPSHOTS_CONTAINER, |
65 |
name=snapshot_name, |
66 |
size=size, |
67 |
domain=SNAPSHOTS_DOMAIN, |
68 |
type=SNAPSHOTS_TYPE, |
69 |
mapfile=mapfile, |
70 |
meta=snapshot_metadata, |
71 |
replace_meta=True,
|
72 |
permissions=None)
|
73 |
#checksum=None,
|
74 |
|
75 |
backend.snapshot_instance(volume.machine, snapshot_name=snapshot_name) |
76 |
|
77 |
snapshot = util.get_snapshot(user_id, snapshot_uuid) |
78 |
|
79 |
return snapshot
|
80 |
|
81 |
|
82 |
def generate_snapshot_name(volume): |
83 |
time = isoformat(datetime.datetime.now()) |
84 |
return "snf-snapshot-of-volume-%s-%s" % (volume.id, |
85 |
volume.snapshot_counter) |
86 |
|
87 |
|
88 |
@transaction.commit_on_success
|
89 |
def delete(snapshot): |
90 |
user_id = snapshot["owner"]
|
91 |
with image_backend(user_id) as pithos_backend: |
92 |
pithos_backend.delete_snapshot(snapshot["uuid"])
|
93 |
return snapshot
|