Revision bcd80cd9 snf-cyclades-app/synnefo/api/management/commands/server-create.py
b/snf-cyclades-app/synnefo/api/management/commands/server-create.py | ||
---|---|---|
31 | 31 |
# interpreted as representing official policies, either expressed |
32 | 32 |
# or implied, of GRNET S.A. |
33 | 33 |
|
34 |
import json |
|
35 | 34 |
from optparse import make_option |
36 | 35 |
|
37 |
from django.db import transaction |
|
38 | 36 |
from django.core.management.base import BaseCommand, CommandError |
39 | 37 |
from synnefo.management import common |
40 | 38 |
|
41 |
from synnefo.db.models import VirtualMachine |
|
42 |
from synnefo.logic.backend import create_instance |
|
43 |
from synnefo.logic.backend_allocator import BackendAllocator |
|
44 | 39 |
from synnefo.api import util |
45 |
from synnefo.api.servers import server_created |
|
46 |
from synnefo import quotas |
|
40 |
from synnefo.api.servers import do_create_server |
|
47 | 41 |
|
48 | 42 |
HELP_MSG = """ |
49 | 43 |
|
... | ... | |
77 | 71 |
help="Password for the new server") |
78 | 72 |
) |
79 | 73 |
|
80 |
@transaction.commit_manually |
|
81 | 74 |
def handle(self, *args, **options): |
82 | 75 |
if args: |
83 | 76 |
raise CommandError("Command doesn't accept any arguments") |
... | ... | |
97 | 90 |
raise CommandError("password is mandatory") |
98 | 91 |
if not flavor_id: |
99 | 92 |
raise CommandError("flavor-id is mandatory") |
100 |
|
|
101 |
# Get Flavor |
|
102 |
if flavor_id: |
|
103 |
flavor = common.get_flavor(flavor_id) |
|
104 |
|
|
105 |
if image_id: |
|
106 |
img = common.get_image(image_id, user_id) |
|
107 |
|
|
108 |
properties = img.get('properties', {}) |
|
109 |
image = {} |
|
110 |
image['backend_id'] = img['location'] |
|
111 |
image['format'] = img['disk_format'] |
|
112 |
image['metadata'] = dict((key.upper(), val) |
|
113 |
for key, val in properties.items()) |
|
114 |
else: |
|
93 |
if not image_id: |
|
115 | 94 |
raise CommandError("image-id is mandatory") |
116 | 95 |
|
117 |
# Fix flavor for archipelago |
|
118 |
disk_template, provider = util.get_flavor_provider(flavor) |
|
119 |
if provider: |
|
120 |
flavor.disk_template = disk_template |
|
121 |
flavor.disk_provider = provider |
|
122 |
flavor.disk_origin = None |
|
123 |
if provider == 'vlmc': |
|
124 |
flavor.disk_origin = image['checksum'] |
|
125 |
image['backend_id'] = 'null' |
|
126 |
else: |
|
127 |
flavor.disk_provider = None |
|
128 |
|
|
129 |
try: |
|
130 |
# Get Backend |
|
131 |
if backend_id: |
|
132 |
backend = common.get_backend(backend_id) |
|
133 |
else: |
|
134 |
ballocator = BackendAllocator() |
|
135 |
backend = ballocator.allocate(user_id, flavor) |
|
136 |
if not backend: |
|
137 |
raise CommandError("Can not allocate VM") |
|
138 |
|
|
139 |
# Get Public address |
|
140 |
(network, address) = util.allocate_public_address(backend) |
|
141 |
if address is None: |
|
142 |
raise CommandError("Can not allocate a public address." |
|
143 |
" No available public network.") |
|
144 |
nic = {'ip': address, 'network': network.backend_id} |
|
145 |
|
|
146 |
# Create the VM in DB |
|
147 |
vm = VirtualMachine.objects.create(name=name, |
|
148 |
backend=backend, |
|
149 |
userid=user_id, |
|
150 |
imageid=image_id, |
|
151 |
flavor=flavor) |
|
152 |
# dispatch server created signal |
|
153 |
server_created.send(sender=vm, created_vm_params={ |
|
154 |
'img_id': image['backend_id'], |
|
155 |
'img_passwd': password, |
|
156 |
'img_format': str(image['format']), |
|
157 |
'img_personality': '[]', |
|
158 |
'img_properties': json.dumps(image['metadata']), |
|
159 |
}) |
|
160 |
|
|
161 |
quotas.issue_and_accept_commission(vm) |
|
162 |
except: |
|
163 |
transaction.rollback() |
|
164 |
raise |
|
165 |
else: |
|
166 |
transaction.commit() |
|
167 |
|
|
168 |
try: |
|
169 |
# Create the instance in Backend |
|
170 |
jobID = create_instance(vm, nic, flavor, image) |
|
96 |
flavor = common.get_flavor(flavor_id) |
|
97 |
image = common.get_image(image_id, user_id) |
|
98 |
if backend_id: |
|
99 |
backend = common.get_backend(backend_id) |
|
171 | 100 |
|
172 |
vm.backendjobid = jobID |
|
173 |
vm.save() |
|
174 |
self.stdout.write("Creating VM %s with IP %s in Backend %s." |
|
175 |
" JobID: %s\n" % (vm, address, backend, jobID)) |
|
176 |
except: |
|
177 |
transaction.rollback() |
|
178 |
raise |
|
179 |
else: |
|
180 |
transaction.commit() |
|
101 |
do_create_server(user_id, name, password, flavor, image, |
|
102 |
backend=backend) |
Also available in: Unified diff