root / snf-cyclades-app / synnefo / volume / management / commands / volume-import.py @ f19474fb
History | View | Annotate | Download (5.5 kB)
1 |
# Copyright 2013 GRNET S.A. All rights reserved.
|
---|---|
2 |
#
|
3 |
# Redistribution and use in source and binary forms, with or
|
4 |
# without modification, are permitted provided that the following
|
5 |
# conditions are met:
|
6 |
#
|
7 |
# 1. Redistributions of source code must retain the above
|
8 |
# copyright notice, this list of conditions and the following
|
9 |
# disclaimer.
|
10 |
#
|
11 |
# 2. Redistributions in binary form must reproduce the above
|
12 |
# copyright notice, this list of conditions and the following
|
13 |
# disclaimer in the documentation and/or other materials
|
14 |
# provided with the distribution.
|
15 |
#
|
16 |
# THIS SOFTWARE IS PROVIDED BY GRNET S.A. ``AS IS'' AND ANY EXPRESS
|
17 |
# OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
|
18 |
# WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
|
19 |
# PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL GRNET S.A OR
|
20 |
# CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
21 |
# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
|
22 |
# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
|
23 |
# USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
|
24 |
# AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
25 |
# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
|
26 |
# ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
27 |
# POSSIBILITY OF SUCH DAMAGE.
|
28 |
#
|
29 |
# The views and conclusions contained in the software and
|
30 |
# documentation are those of the authors and should not be
|
31 |
# interpreted as representing official policies, either expressed
|
32 |
# or implied, of GRNET S.A.
|
33 |
|
34 |
from optparse import make_option |
35 |
|
36 |
from snf_django.management.commands import SynnefoCommand, CommandError |
37 |
|
38 |
from synnefo.management import common, pprint |
39 |
from synnefo.logic import backend as backend_mod, reconciliation, utils |
40 |
from synnefo.db.models import Volume |
41 |
|
42 |
HELP_MSG = """Import an existing Ganeti disk to Cyclades
|
43 |
|
44 |
Create a Cyclades Volume from an existing Ganeti disk. This command is useful
|
45 |
to handle disks that have been created directly in the Ganeti backend (instead
|
46 |
of being created by Cyclades). The command will not create/delete/modify the
|
47 |
specified disk in the Ganeti backend. Instead, it will create a Volume in
|
48 |
Cyclades DB, and rename the Ganeti disk with the Volume name.
|
49 |
|
50 |
"""
|
51 |
|
52 |
|
53 |
class Command(SynnefoCommand): |
54 |
help = HELP_MSG |
55 |
|
56 |
option_list = SynnefoCommand.option_list + ( |
57 |
make_option( |
58 |
"--name",
|
59 |
dest="name",
|
60 |
default=None,
|
61 |
help="Display name of the volume."),
|
62 |
make_option( |
63 |
"--description",
|
64 |
dest="description",
|
65 |
default=None,
|
66 |
help="Display description of the volume."),
|
67 |
make_option( |
68 |
"--server",
|
69 |
dest="server_id",
|
70 |
default=None,
|
71 |
help="The ID of the server that the volume is currently attached"),
|
72 |
make_option( |
73 |
"--disk",
|
74 |
dest="disk_uuid",
|
75 |
default=None,
|
76 |
help="The UUID of the disk to be imported"),
|
77 |
) |
78 |
|
79 |
@common.convert_api_faults
|
80 |
def handle(self, *args, **options): |
81 |
if args:
|
82 |
raise CommandError("Command doesn't accept any arguments") |
83 |
|
84 |
display_name = options.get("name", "") |
85 |
display_description = options.get("description", "") |
86 |
|
87 |
server_id = options.get("server_id")
|
88 |
if server_id is None: |
89 |
raise CommandError("Please specify the server that the disk is" |
90 |
" currently attached.")
|
91 |
|
92 |
disk_uuid = options.get("disk_uuid")
|
93 |
if disk_uuid is None: |
94 |
raise CommandError("Please specify the UUID of the Ganeti disk") |
95 |
|
96 |
vm = common.get_vm(server_id) |
97 |
|
98 |
instance_info = backend_mod.get_instance_info(vm) |
99 |
instance_disks = reconciliation.disks_from_instance(instance_info) |
100 |
try:
|
101 |
disk = filter(lambda d: d["uuid"] == disk_uuid, instance_disks)[0] |
102 |
except IndexError: |
103 |
raise CommandError("Instance '%s' does not have a disk with" |
104 |
" UUID '%s" % (vm.id, disk_uuid))
|
105 |
|
106 |
# Check that the instance disk is not already a Cyclades Volume
|
107 |
try:
|
108 |
disk_id = utils.id_from_disk_name(disk["name"])
|
109 |
except:
|
110 |
pass
|
111 |
else:
|
112 |
raise CommandError("Disk '%s' of instance '%s' is already a" |
113 |
" Cyclades Volume. Volume ID: %s"
|
114 |
% (disk_uuid, vm.id, disk_id)) |
115 |
|
116 |
size = disk["size"] >> 10 # Convert to GB |
117 |
index = disk["index"]
|
118 |
|
119 |
self.stdout.write("Import disk/%s of instance %s, size: %s GB\n" |
120 |
% (index, vm.id, size)) |
121 |
|
122 |
volume = Volume.objects.create( |
123 |
userid=vm.userid, |
124 |
disk_template=vm.flavor.disk_template, |
125 |
size=size, |
126 |
machine_id=vm.id, |
127 |
name=display_name, |
128 |
description=display_description, |
129 |
delete_on_termination=True,
|
130 |
status="IN_USE",
|
131 |
index=index) |
132 |
|
133 |
self.stdout.write("Created Volume '%s' in DB\n" % volume.id) |
134 |
pprint.pprint_volume(volume, stdout=self.stdout)
|
135 |
self.stdout.write("\n") |
136 |
|
137 |
client = vm.get_client() |
138 |
jobId = client.ModifyInstance( |
139 |
instance=vm.backend_vm_id, |
140 |
disks=[("modify", disk["index"], |
141 |
{"name": volume.backend_volume_uuid})])
|
142 |
(status, error) = backend_mod.wait_for_job(client, jobId) |
143 |
vm.put_client(client) |
144 |
if status == "success": |
145 |
self.stdout.write("Successfully imported disk\n") |
146 |
else:
|
147 |
self.stdout.write("Failed to imported disk:\n %s\n" % error) |