Revision c449760f snf-cyclades-app/synnefo/volume/snapshots.py
b/snf-cyclades-app/synnefo/volume/snapshots.py | ||
---|---|---|
1 | 1 |
import logging |
2 |
from django.utils import simplejson as json |
|
3 | 2 |
from django.db import transaction |
4 | 3 |
from snf_django.lib.api import faults |
5 | 4 |
from synnefo.plankton.utils import image_backend |
6 | 5 |
from synnefo.logic import backend |
7 | 6 |
from synnefo.volume import util |
8 | 7 |
|
9 |
#import datetime |
|
10 |
#from snf_django.lib.api.utils import isoformat |
|
11 |
|
|
12 | 8 |
log = logging.getLogger(__name__) |
13 | 9 |
|
10 |
PLANKTON_DOMAIN = "plankton" |
|
11 |
PLANKTON_PREFIX = "plankton:" |
|
12 |
|
|
13 |
PROPERTY_PREFIX = "property:" |
|
14 |
|
|
15 |
SNAPSHOT_PREFIX = "snapshot:" |
|
14 | 16 |
SNAPSHOTS_CONTAINER = "snapshots" |
15 |
SNAPSHOTS_DOMAIN = "plankton" |
|
16 |
SNAPSHOTS_PREFIX = "plankton:" |
|
17 | 17 |
SNAPSHOTS_TYPE = "application/octet-stream" |
18 | 18 |
SNAPSHOTS_MAPFILE_PREFIX = "archip:" |
19 | 19 |
|
... | ... | |
48 | 48 |
volume.save() |
49 | 49 |
transaction.commit() |
50 | 50 |
|
51 |
# Snapshot information are stored as metadata on the Pithos file |
|
52 | 51 |
snapshot_metadata = { |
53 |
SNAPSHOTS_PREFIX + "name": name, |
|
54 |
SNAPSHOTS_PREFIX + "description": description, |
|
55 |
SNAPSHOTS_PREFIX + "volume_id": volume.id, |
|
56 |
SNAPSHOTS_PREFIX + "status": "CREATING", |
|
52 |
PLANKTON_PREFIX + "name": name, |
|
53 |
PLANKTON_PREFIX + "status": "CREATING", |
|
54 |
PLANKTON_PREFIX + "disk_format": "diskdump", |
|
55 |
PLANKTON_PREFIX + "container_format": "bare", |
|
56 |
PLANKTON_PREFIX + "is_snapshot": True, |
|
57 |
# Snapshot specific |
|
58 |
PLANKTON_PREFIX + "description": description, |
|
59 |
PLANKTON_PREFIX + "volume_id": volume.id, |
|
57 | 60 |
} |
58 | 61 |
|
59 |
# TODO: The following are used in order plankton to work with snapshots |
|
60 |
# exactly as with iamges |
|
61 |
snapshot_metadata.update({ |
|
62 |
SNAPSHOTS_PREFIX + "store": "pithos", |
|
63 |
SNAPSHOTS_PREFIX + "disk_format": "diskdump", |
|
64 |
SNAPSHOTS_PREFIX + "default_container_format": "bare", |
|
65 |
SNAPSHOTS_PREFIX + "metadata": json.dumps(metadata)}) |
|
66 |
|
|
67 |
# Set a special attribute to distinquish snapshots from the images |
|
68 |
snapshot_metadata[SNAPSHOTS_PREFIX + "is_snapshot"] = True |
|
69 |
|
|
70 | 62 |
# Snapshots are used as images. We set the most important properties |
71 | 63 |
# that are being used for images. We set 'EXCLUDE_ALL_TASKS' to bypass |
72 | 64 |
# image customization. Also, we get some basic metadata for the volume from |
73 | 65 |
# the server that the volume is attached |
74 |
image_properties = {"EXCLUDE_ALL_TASKS": "yes",
|
|
75 |
"description": description}
|
|
66 |
metadata.update({"EXCLUDE_ALL_TASKS": "yes",
|
|
67 |
"description": description})
|
|
76 | 68 |
vm_metadata = dict(volume.machine.metadata |
69 |
.filter(meta_key__in=["OS", "users"]) |
|
77 | 70 |
.values_list("meta_key", "meta_value")) |
78 |
for key in ["OS", "users"]: |
|
79 |
val = vm_metadata.get(key) |
|
80 |
if val is not None: |
|
81 |
image_properties[key] = val |
|
82 |
snapshot_metadata[SNAPSHOTS_PREFIX + "properties"] = \ |
|
83 |
json.dumps(image_properties) |
|
71 |
metadata.update(vm_metadata) |
|
72 |
|
|
73 |
for key, val in metadata.items(): |
|
74 |
snapshot_metadata[PLANKTON_PREFIX + PROPERTY_PREFIX + key] = val |
|
84 | 75 |
|
85 | 76 |
# Generate a name for the Pithos file. Also, generate a name for the |
86 | 77 |
# Archipelago mapfile. |
... | ... | |
98 | 89 |
container=SNAPSHOTS_CONTAINER, |
99 | 90 |
name=snapshot_pithos_name, |
100 | 91 |
size=size, |
101 |
domain=SNAPSHOTS_DOMAIN,
|
|
92 |
domain=PLANKTON_DOMAIN,
|
|
102 | 93 |
type=SNAPSHOTS_TYPE, |
103 | 94 |
mapfile=mapfile, |
104 | 95 |
meta=snapshot_metadata, |
105 | 96 |
replace_meta=True, |
106 | 97 |
permissions=None) |
107 |
#checksum=None, |
|
108 | 98 |
|
109 | 99 |
backend.snapshot_instance(volume.machine, |
110 | 100 |
snapshot_name=snapshot_pithos_name) |
... | ... | |
117 | 107 |
def generate_snapshot_pithos_name(volume): |
118 | 108 |
"""Helper function to generate a name for the Pithos file.""" |
119 | 109 |
# time = isoformat(datetime.datetime.now()) |
120 |
return "snapshot-of-volume-%s-%s" % (volume.id,
|
|
121 |
volume.snapshot_counter)
|
|
110 |
return "snf-snap-%s-%s" % (volume.id,
|
|
111 |
volume.snapshot_counter) |
|
122 | 112 |
|
123 | 113 |
|
124 | 114 |
@transaction.commit_on_success |
Also available in: Unified diff