root / snf-cyclades-app / synnefo / api / management / commands / server-create.py @ 3fff9189
History | View | Annotate | Download (6.8 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 |
import json |
35 |
from optparse import make_option |
36 |
|
37 |
from django.db import transaction |
38 |
from django.core.management.base import BaseCommand, CommandError |
39 |
from synnefo.management import common |
40 |
|
41 |
from synnefo.db.models import VirtualMachine |
42 |
from synnefo.logic.backend import create_instance |
43 |
from synnefo.logic.backend_allocator import BackendAllocator |
44 |
from synnefo.api import util |
45 |
from synnefo.api.servers import server_created |
46 |
|
47 |
HELP_MSG = """
|
48 |
|
49 |
Create a new VM without authenticating the user or checking the resource
|
50 |
limits of the user. Also the allocator can be bypassed by specifing a
|
51 |
backend-id.
|
52 |
"""
|
53 |
|
54 |
|
55 |
class Command(BaseCommand): |
56 |
help = "Create a new VM." + HELP_MSG
|
57 |
|
58 |
option_list = BaseCommand.option_list + ( |
59 |
make_option("--backend-id", dest="backend_id", |
60 |
help="Unique identifier of the Ganeti backend."
|
61 |
" Use snf-manage backend-list to find out"
|
62 |
" available backends."),
|
63 |
make_option("--name", dest="name", |
64 |
help="An arbitrary string for naming the server"),
|
65 |
make_option("--user-id", dest="user_id", |
66 |
help="Unique identifier of the owner of the server"),
|
67 |
make_option("--image-id", dest="image_id", |
68 |
help="Unique identifier of the image."
|
69 |
" Use snf-manage image-list to find out"
|
70 |
" available images."),
|
71 |
make_option("--flavor-id", dest="flavor_id", |
72 |
help="Unique identifier of the flavor"
|
73 |
" Use snf-manage flavor-list to find out"
|
74 |
" available flavors."),
|
75 |
make_option("--password", dest="password", |
76 |
help="Password for the new server")
|
77 |
) |
78 |
|
79 |
@transaction.commit_manually
|
80 |
def handle(self, *args, **options): |
81 |
if args:
|
82 |
raise CommandError("Command doesn't accept any arguments") |
83 |
|
84 |
name = options['name']
|
85 |
user_id = options['user_id']
|
86 |
backend_id = options['backend_id']
|
87 |
image_id = options['image_id']
|
88 |
flavor_id = options['flavor_id']
|
89 |
password = options['password']
|
90 |
|
91 |
if not name: |
92 |
raise CommandError("name is mandatory") |
93 |
if not user_id: |
94 |
raise CommandError("user-id is mandatory") |
95 |
if not password: |
96 |
raise CommandError("password is mandatory") |
97 |
if not flavor_id: |
98 |
raise CommandError("flavor-id is mandatory") |
99 |
|
100 |
# Get Flavor
|
101 |
if flavor_id:
|
102 |
flavor = common.get_flavor(flavor_id) |
103 |
|
104 |
if image_id:
|
105 |
img = common.get_image(image_id, user_id) |
106 |
|
107 |
properties = img.get('properties', {})
|
108 |
image = {} |
109 |
image['backend_id'] = img['location'] |
110 |
image['format'] = img['disk_format'] |
111 |
image['metadata'] = dict((key.upper(), val) |
112 |
for key, val in properties.items()) |
113 |
else:
|
114 |
raise CommandError("image-id is mandatory") |
115 |
|
116 |
# Fix flavor for archipelago
|
117 |
disk_template, provider = util.get_flavor_provider(flavor) |
118 |
if provider:
|
119 |
flavor.disk_template = disk_template |
120 |
flavor.disk_provider = provider |
121 |
flavor.disk_origin = None
|
122 |
if provider == 'vlmc': |
123 |
flavor.disk_origin = image['checksum']
|
124 |
image['backend_id'] = 'null' |
125 |
else:
|
126 |
flavor.disk_provider = None
|
127 |
|
128 |
try:
|
129 |
# Get Backend
|
130 |
if backend_id:
|
131 |
backend = common.get_backend(backend_id) |
132 |
else:
|
133 |
ballocator = BackendAllocator() |
134 |
backend = ballocator.allocate(user_id, flavor) |
135 |
if not backend: |
136 |
raise CommandError("Can not allocate VM") |
137 |
|
138 |
# Get Public address
|
139 |
(network, address) = util.allocate_public_address(backend) |
140 |
if address is None: |
141 |
raise CommandError("Can not allocate a public address." |
142 |
" No available public network.")
|
143 |
nic = {'ip': address, 'network': network.backend_id} |
144 |
|
145 |
# Create the VM in DB
|
146 |
vm = VirtualMachine.objects.create(name=name, |
147 |
backend=backend, |
148 |
userid=user_id, |
149 |
imageid=image_id, |
150 |
flavor=flavor) |
151 |
# dispatch server created signal
|
152 |
server_created.send(sender=vm, created_vm_params={ |
153 |
'img_id': image['backend_id'], |
154 |
'img_passwd': password,
|
155 |
'img_format': str(image['format']), |
156 |
'img_personality': '[]', |
157 |
'img_properties': json.dumps(image['metadata']), |
158 |
}) |
159 |
except:
|
160 |
transaction.rollback() |
161 |
raise
|
162 |
else:
|
163 |
transaction.commit() |
164 |
|
165 |
try:
|
166 |
# Create the instance in Backend
|
167 |
jobID = create_instance(vm, nic, flavor, image) |
168 |
|
169 |
vm.backendjobid = jobID |
170 |
vm.save() |
171 |
self.stdout.write("Creating VM %s with IP %s in Backend %s." |
172 |
" JobID: %s\n" % (vm, address, backend, jobID))
|
173 |
except:
|
174 |
transaction.rollback() |
175 |
raise
|
176 |
else:
|
177 |
transaction.commit() |