Commit 4f115f9c authored by Nikos Skalkotos's avatar Nikos Skalkotos

Merge pull request #383 from KostasPelelis/feature-create-server-key_name

Add Server Create key_name
parents 0bbd3d86 d53f4784
......@@ -231,7 +231,7 @@ def vm_to_dict(vm, detail=False):
d['diagnostics'] = []
# Fixed
d["security_groups"] = [{"name": "default"}]
d["key_name"] = None
d["key_name"] = vm.key_name
d["config_drive"] = ""
d["accessIPv4"] = ""
d["accessIPv6"] = ""
......@@ -413,6 +413,7 @@ def create_server(request):
assert isinstance(networks, list)
project = server.get("project")
shared_to_project=server.get("shared_to_project", False)
key_name = server.get('key_name')
except (KeyError, AssertionError):
raise faults.BadRequest("Malformed request")
......@@ -439,7 +440,8 @@ def create_server(request):
metadata=metadata, personality=personality,
project=project, networks=networks, volumes=volumes,
shared_to_project=shared_to_project,
user_projects=request.user_projects)
user_projects=request.user_projects,
key_name=key_name)
log.info("User %s created VM %s, shared: %s", user_id, vm.id,
shared_to_project)
......
# -*- coding: utf-8 -*-
from __future__ import unicode_literals
from django.db import models, migrations
class Migration(migrations.Migration):
dependencies = [
('db', '0003_auto_ip_ownership'),
]
operations = [
migrations.AddField(
model_name='virtualmachine',
name='key_name',
field=models.CharField(max_length=100, null=True),
preserve_default=True,
),
]
......@@ -360,6 +360,7 @@ class VirtualMachine(models.Model):
created = models.DateTimeField(auto_now_add=True)
updated = models.DateTimeField(auto_now=True)
imageid = models.CharField(max_length=100, null=False)
key_name = models.CharField(max_length=100, null=True)
image_version = models.IntegerField(null=True)
hostid = models.CharField(max_length=100)
flavor = models.ForeignKey(Flavor, on_delete=models.PROTECT)
......
......@@ -47,7 +47,7 @@ server_created = dispatch.Signal(providing_args=["created_vm_params"])
def create(userid, name, password, flavor, image_id, metadata={},
personality=[], networks=None, use_backend=None, project=None,
volumes=None, helper=False, user_projects=None,
shared_to_project=False):
shared_to_project=False, key_name=None):
utils.check_name_length(name, VirtualMachine.VIRTUAL_MACHINE_NAME_LENGTH,
"Server name is too long")
......@@ -126,6 +126,7 @@ def create(userid, name, password, flavor, image_id, metadata={},
shared_to_project=shared_to_project,
imageid=image["id"],
image_version=image["version"],
key_name=key_name,
flavor=flavor,
operstate="BUILD",
helper=helper)
......@@ -172,9 +173,14 @@ def create(userid, name, password, flavor, image_id, metadata={},
meta_value=val,
vm=vm)
public_key = None
if key_name is not None:
keypair = util.get_keypair(key_name, userid)
public_key = keypair.content
# Create the server in Ganeti.
vm = create_server(vm, ports, server_volumes, flavor, image, personality,
password)
password, public_key)
return vm
......@@ -200,7 +206,8 @@ def allocate_new_server(userid, flavor):
@commands.server_command("BUILD")
def create_server(vm, nics, volumes, flavor, image, personality, password):
def create_server(vm, nics, volumes, flavor, image, personality, password,
public_key):
# dispatch server created signal needed to trigger the 'vmapi', which
# enriches the vm object with the 'config_url' attribute which must be
# passed to the Ganeti job.
......@@ -212,13 +219,18 @@ def create_server(vm, nics, volumes, flavor, image, personality, password):
if root_volume.volume_type.provider in settings.GANETI_CLONE_PROVIDERS:
image_id = "null"
server_created.send(sender=vm, created_vm_params={
created_vm_params = {
'img_id': image_id,
'img_passwd': password,
'img_format': str(image['format']),
'img_personality': json.dumps(personality),
'img_properties': json.dumps(image['metadata']),
})
}
if public_key is not None:
created_vm_params['auth_keys'] = public_key
server_created.send(sender=vm, created_vm_params=created_vm_params)
# send job to Ganeti
try:
......
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