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
...@@ -170,16 +170,24 @@ def create_server(request): ...@@ -170,16 +170,24 @@ def create_server(request):
ipsix='::1', ipsix='::1',
flavor=flavor) 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 and vm.backend_id is valid.
if request.META.get('SERVER_NAME', None) == 'testserver': if request.META.get('SERVER_NAME', None) == 'testserver':
name = 'test-server' backend_name = 'test-server'
dry_run = True dry_run = True
else: else:
name = vm.backend_id backend_name = vm.backend_id
dry_run = False dry_run = False
jobId = rapi.CreateInstance( jobId = rapi.CreateInstance(
mode='create', mode='create',
name=name, name=backend_name,
disk_template='plain', disk_template='plain',
disks=[{"size": 2000}], #FIXME: Always ask for a 2GB disk for now disks=[{"size": 2000}], #FIXME: Always ask for a 2GB disk for now
nics=[{}], nics=[{}],
...@@ -199,7 +207,7 @@ def create_server(request): ...@@ -199,7 +207,7 @@ def create_server(request):
server = vm_to_dict(vm, detail=True) server = vm_to_dict(vm, detail=True)
server['status'] = 'BUILD' server['status'] = 'BUILD'
server['adminPass'] = random_password() server['adminPass'] = passwd
return render_server(request, server, status=202) return render_server(request, server, status=202)
@api_method('GET') @api_method('GET')
...@@ -274,6 +274,8 @@ class VirtualMachine(models.Model): ...@@ -274,6 +274,8 @@ class VirtualMachine(models.Model):
def _get_backend_id(self): def _get_backend_id(self):
"""Returns the backend id for this VM by prepending backend-prefix.""" """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( return '%s%s' % (settings.BACKEND_PREFIX_ID, str(
backend_id = property(_get_backend_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