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

Fix bugs in api/

Fix bugs in server creation, in api/
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):
# 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 and vm.backend_id is valid.
if request.META.get('SERVER_NAME', None) == 'testserver':
name = 'test-server'
backend_name = 'test-server'
dry_run = True
name = vm.backend_id
backend_name = vm.backend_id
dry_run = False
jobId = rapi.CreateInstance(
disks=[{"size": 2000}], #FIXME: Always ask for a 2GB disk for now
......@@ -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)
......@@ -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
raise VirtualMachine.InvalidBackendIdError(" is None")
return '%s%s' % (settings.BACKEND_PREFIX_ID, str(
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