Commit 36cf941d authored by Vangelis Koukis's avatar Vangelis Koukis
Browse files

Fix bugs in api/server.py:create_server()

Fix bugs in server creation, in api/server.py:create_server().
More specifically:
 * Make sure to save() the vm object before calling vm.backend_id
 * Delete the vm instance if the RAPI call fails for any reason
 * Use different variable names for the server name at the API level
   and at the Ganeti backend level
 * Compute a random password *before* the CreateInstance() call to Ganeti
   [not currently used though]

Also patch the VirtualMachine model to safeguard against getting the
backend_id of a yet-unsaved instance of VirtualMachine.
parent 6801da35
......@@ -169,17 +169,25 @@ def create_server(request):
ipfour='0.0.0.0',
ipsix='::1',
flavor=flavor)
# Pick a random password for the VM.
# FIXME: This must be passed to the Ganeti OS provider via CreateInstance()
passwd = random_password()
# We *must* save the VM instance now,
# so that it gets a vm.id and vm.backend_id is valid.
vm.save()
if request.META.get('SERVER_NAME', None) == 'testserver':
name = 'test-server'
backend_name = 'test-server'
dry_run = True
else:
name = vm.backend_id
backend_name = vm.backend_id
dry_run = False
jobId = rapi.CreateInstance(
mode='create',
name=name,
name=backend_name,
disk_template='plain',
disks=[{"size": 2000}], #FIXME: Always ask for a 2GB disk for now
nics=[{}],
......@@ -199,7 +207,7 @@ def create_server(request):
server = vm_to_dict(vm, detail=True)
server['status'] = 'BUILD'
server['adminPass'] = random_password()
server['adminPass'] = passwd
return render_server(request, server, status=202)
@api_method('GET')
......
......@@ -274,6 +274,8 @@ class VirtualMachine(models.Model):
def _get_backend_id(self):
"""Returns the backend id for this VM by prepending backend-prefix."""
if not self.id:
raise VirtualMachine.InvalidBackendIdError("self.id is None")
return '%s%s' % (settings.BACKEND_PREFIX_ID, str(self.id))
backend_id = property(_get_backend_id)
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment