Statistics
| Branch: | Tag: | Revision:

root / snf-cyclades-app / synnefo / volume / snapshots.py @ 0a83201b

History | View | Annotate | Download (2.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

    
11
SNAPSHOTS_CONTAINER = "snapshots"
12
SNAPSHOTS_DOMAIN = "plankton"
13
SNAPSHOTS_PREFIX = "plankton:"
14
SNAPSHOTS_TYPE = "application/octet-stream"
15
SNAPSHOTS_MAPFILE_PREFIX = "archip:"
16

    
17

    
18
@transaction.commit_on_success
19
def create(user_id, volume, name, description, metadata, force=False):
20

    
21
    if volume.machine is None:
22
        raise faults.BadRequest("Can not snapshot detached volume!")
23

    
24
    volume.snapshot_counter += 1
25
    volume.save()
26

    
27
    snapshot_metadata = {}
28
    snapshot_metadata[SNAPSHOTS_PREFIX + "name"] = description
29
    snapshot_metadata[SNAPSHOTS_PREFIX + "description"] = description
30
    snapshot_metadata[SNAPSHOTS_PREFIX + "metadata"] = json.dumps(metadata)
31
    snapshot_metadata[SNAPSHOTS_PREFIX + "volume_id"] = volume.id
32
    snapshot_metadata[SNAPSHOTS_PREFIX + "status"] = "CREATING"
33
    #XXX: just to work
34
    snapshot_metadata[SNAPSHOTS_PREFIX + "is_snapshot"] = True
35

    
36
    snapshot_name = generate_snapshot_name(volume)
37
    mapfile = SNAPSHOTS_MAPFILE_PREFIX + snapshot_name
38

    
39
    with image_backend(user_id) as pithos_backend:
40
        # move this to plankton backend
41
        snapshot_uuid = pithos_backend.backend.register_object_map(
42
            user=user_id,
43
            account=user_id,
44
            container=SNAPSHOTS_CONTAINER,
45
            name=name,
46
            size=volume.size,
47
            type=SNAPSHOTS_TYPE,
48
            mapfile=mapfile,
49
            meta=snapshot_metadata,
50
            replace_meta=False,
51
            permissions=None)
52
            #checksum=None,
53

    
54
    backend.snapshot_instance(volume.machine, snapshot_name=snapshot_uuid)
55

    
56
    snapshot = util.get_snapshot(user_id, snapshot_uuid)
57

    
58
    return snapshot
59

    
60

    
61
def generate_snapshot_name(volume):
62
    time = isoformat(datetime.datetime.now())
63
    return "snf-snapshot-of-volume-%s-%s-%s" % (volume.id,
64
                                                volume.snapshot_counter, time)
65

    
66

    
67
@transaction.commit_on_success
68
def delete(snapshot):
69
    user_id = snapshot["owner"]
70
    with image_backend(user_id) as pithos_backend:
71
        pithos_backend.delete_snapshot(snapshot["uuid"])
72
    return snapshot