root / snf-cyclades-app / synnefo / api / management / commands / server-import.py @ b344501e
History | View | Annotate | Download (9.6 kB)
1 |
# Copyright 2012 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 django.core.management.base import BaseCommand, CommandError |
37 |
from synnefo.management import common |
38 |
|
39 |
from synnefo.db.models import VirtualMachine, Network, Flavor |
40 |
from synnefo.logic.utils import id_from_network_name, id_from_instance_name |
41 |
from synnefo.logic.backend import wait_for_job |
42 |
from synnefo.logic.rapi import GanetiApiError |
43 |
from synnefo.api.util import allocate_public_address |
44 |
|
45 |
import sys |
46 |
|
47 |
|
48 |
HELP_MSG = """
|
49 |
|
50 |
Import an existing Ganeti instance into Synnefo, with the attributes specified
|
51 |
by the command line options. In order to be imported, the instance will be
|
52 |
turned off, renamed and then turned on again.
|
53 |
|
54 |
Importing an instance will fail, if the instance has NICs that are connected to
|
55 |
a network not belonging to Synnefo. You can either manually modify the instance
|
56 |
or use --new-nics option, that will remove all old NICs, and create a new one
|
57 |
connected to a public network of Synnefo.
|
58 |
|
59 |
"""
|
60 |
|
61 |
|
62 |
class Command(BaseCommand): |
63 |
help = "Import an existing Ganeti VM into Synnefo." + HELP_MSG
|
64 |
args = "<ganeti_instance_name>"
|
65 |
output_transaction = True
|
66 |
|
67 |
option_list = BaseCommand.option_list + ( |
68 |
make_option("--backend-id", dest="backend_id", |
69 |
help="Unique identifier of the Ganeti backend that"
|
70 |
" hosts the VM. Use snf-manage backend-list to"
|
71 |
" find out available backends."),
|
72 |
make_option("--user-id", dest="user_id", |
73 |
help="Unique identifier of the owner of the server"),
|
74 |
make_option("--image-id", dest="image_id", |
75 |
default=None,
|
76 |
help="Unique identifier of the image."
|
77 |
" Use snf-manage image-list to find out"
|
78 |
" available images."),
|
79 |
make_option("--flavor-id", dest="flavor_id", |
80 |
help="Unique identifier of the flavor"
|
81 |
" Use snf-manage flavor-list to find out"
|
82 |
" available flavors."),
|
83 |
make_option("--new-nics", dest='new_nics', |
84 |
default=False,
|
85 |
action="store_true",
|
86 |
help="Remove old NICs of instance, and create"
|
87 |
" a new NIC connected to a public network of"
|
88 |
" Synnefo.")
|
89 |
) |
90 |
|
91 |
REQUIRED = ("user-id", "backend-id", "image-id", "flavor-id") |
92 |
|
93 |
def handle(self, *args, **options): |
94 |
if len(args) < 1: |
95 |
raise CommandError("Please specify a Ganeti instance") |
96 |
|
97 |
instance_name = args[0]
|
98 |
|
99 |
try:
|
100 |
id_from_instance_name(instance_name) |
101 |
raise CommandError("%s is already a synnefo instance") |
102 |
except:
|
103 |
pass
|
104 |
|
105 |
user_id = options['user_id']
|
106 |
backend_id = options['backend_id']
|
107 |
image_id = options['image_id']
|
108 |
flavor_id = options['flavor_id']
|
109 |
new_public_nic = options['new_nics']
|
110 |
|
111 |
for field in self.REQUIRED: |
112 |
if not locals()[field.replace("-", "_")]: |
113 |
raise CommandError(field + " is mandatory") |
114 |
|
115 |
import_server(instance_name, backend_id, flavor_id, image_id, user_id, |
116 |
new_public_nic, self.stdout)
|
117 |
|
118 |
|
119 |
def import_server(instance_name, backend_id, flavor_id, image_id, user_id, |
120 |
new_public_nic, stream=sys.stdout): |
121 |
flavor = common.get_flavor(flavor_id) |
122 |
backend = common.get_backend(backend_id) |
123 |
|
124 |
backend_client = backend.get_client() |
125 |
|
126 |
try:
|
127 |
instance = backend_client.GetInstance(instance_name) |
128 |
except GanetiApiError as e: |
129 |
if e.code == 404: |
130 |
raise CommandError("Instance %s does not exist in backend %s"\ |
131 |
% (instance_name, backend)) |
132 |
else:
|
133 |
raise CommandError("Unexpected error" + str(e)) |
134 |
|
135 |
if new_public_nic:
|
136 |
remove_instance_nics(instance, backend_client, |
137 |
stream=stream) |
138 |
(network, address) = allocate_public_address(backend) |
139 |
if address is None: |
140 |
raise CommandError("Can not allocate a public address."\ |
141 |
" No available public network.")
|
142 |
nic = {'ip': address, 'network': network.backend_id} |
143 |
add_public_nic(instance_name, nic, backend_client, |
144 |
stream=stream) |
145 |
else:
|
146 |
check_instance_nics(instance) |
147 |
|
148 |
shutdown_instance(instance, backend_client, stream=stream) |
149 |
|
150 |
# Create the VM in DB
|
151 |
stream.write("Creating VM entry in DB\n")
|
152 |
vm = VirtualMachine.objects.create(name=instance_name, |
153 |
backend=backend, |
154 |
userid=user_id, |
155 |
imageid=image_id, |
156 |
flavor=flavor) |
157 |
|
158 |
# Rename instance
|
159 |
rename_instance(instance_name, vm.backend_vm_id, backend_client, |
160 |
stream) |
161 |
# Startup instance
|
162 |
startup_instance(vm.backend_vm_id, backend_client, stream=stream) |
163 |
|
164 |
backend.put_client(backend_client) |
165 |
return
|
166 |
|
167 |
|
168 |
def flavor_from_instance(instance, flavor, stream=sys.stdout): |
169 |
beparams = instance['beparams']
|
170 |
disk_sizes = instance['disk.sizes']
|
171 |
if len(disk_sizes) != 1: |
172 |
stream.write("Instance has more than one disk.\n")
|
173 |
|
174 |
disk = disk_sizes[0]
|
175 |
disk_template = instance['disk_template']
|
176 |
cpu = beparams['vcpus']
|
177 |
ram = beparams['memory']
|
178 |
|
179 |
return Flavor.objects.get_or_create(disk=disk, disk_template=disk_template,
|
180 |
cpu=cpu, ram=ram) |
181 |
|
182 |
|
183 |
def check_instance_nics(instance): |
184 |
instance_name = instance['name']
|
185 |
networks = instance['nic.networks']
|
186 |
try:
|
187 |
networks = map(id_from_network_name, networks)
|
188 |
except Network.InvalidBackendIdError:
|
189 |
raise CommandError("Instance %s has NICs that do not belong to a" |
190 |
" network belonging to synnefo. Either manually"
|
191 |
" modify the instance NICs or specify --new-nics"
|
192 |
" to clear the old NICs and create a new NIC to"
|
193 |
" a public network of synnefo." % instance_name)
|
194 |
|
195 |
|
196 |
def remove_instance_nics(instance, backend_client, stream=sys.stdout): |
197 |
instance_name = instance['name']
|
198 |
ips = instance['nic.ips']
|
199 |
nic_indexes = xrange(0, len(ips)) |
200 |
op = map(lambda x: ('remove', x, {}), nic_indexes) |
201 |
stream.write("Removing instance nics\n")
|
202 |
op.reverse() |
203 |
jobid = backend_client.ModifyInstance(instance_name, nics=op) |
204 |
(status, error) = wait_for_job(backend_client, jobid) |
205 |
if status != 'success': |
206 |
raise CommandError("Can not rename instance: %s" % error) |
207 |
|
208 |
|
209 |
def add_public_nic(instance_name, nic, backend_client, stream=sys.stdout): |
210 |
stream.write("Adding public NIC %s\n" % nic)
|
211 |
jobid = backend_client.ModifyInstance(instance_name, nics=[('add', nic)])
|
212 |
(status, error) = wait_for_job(backend_client, jobid) |
213 |
if status != 'success': |
214 |
raise CommandError("Can not rename instance: %s" % error) |
215 |
|
216 |
|
217 |
def shutdown_instance(instance, backend_client, stream=sys.stdout): |
218 |
instance_name = instance['name']
|
219 |
if instance['status'] != 'ADMIN_down': |
220 |
stream.write("Instance is not down. Shutting down"
|
221 |
" instance\n")
|
222 |
jobid = backend_client.ShutdownInstance(instance_name) |
223 |
(status, error) = wait_for_job(backend_client, jobid) |
224 |
if status != 'success': |
225 |
raise CommandError("Can not shutdown instance: %s" % error) |
226 |
|
227 |
|
228 |
def rename_instance(old_name, new_name, backend_client, stream=sys.stdout): |
229 |
stream.write("Renaming instance to %s\n" % new_name)
|
230 |
|
231 |
jobid = backend_client.RenameInstance(old_name, new_name, |
232 |
ip_check=False, name_check=False) |
233 |
(status, error) = wait_for_job(backend_client, jobid) |
234 |
if status != 'success': |
235 |
raise CommandError("Can not rename instance: %s" % error) |
236 |
|
237 |
|
238 |
def startup_instance(name, backend_client, stream=sys.stdout): |
239 |
stream.write("Starting instance %s\n" % name)
|
240 |
jobid = backend_client.StartupInstance(name) |
241 |
(status, error) = wait_for_job(backend_client, jobid) |
242 |
if status != 'success': |
243 |
raise CommandError("Can not rename instance: %s" % error) |