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