Add ssh key to new vm

Create_vm now stes to public key correctly
This commit is contained in:
modulos 2017-06-02 00:49:17 +02:00
parent ef0589f691
commit 715d092b96
3 changed files with 24 additions and 52 deletions

View file

@ -355,17 +355,6 @@ class GenerateVMSSHKeysView(LoginRequiredMixin, FormView):
return render(self.request, self.template_name, context)
def post(self, request, *args, **kwargs):
# try:
# UserHostingKey.objects.get(
# user=self.request.user
# )
# return HttpResponseRedirect(reverse('hosting:key_pair'))
# except UserHostingKey.DoesNotExist:
# pass
form = self.get_form()
if form.is_valid():
return self.form_valid(form)
@ -418,11 +407,7 @@ class PaymentVMView(LoginRequiredMixin, FormView):
return context
def get(self, request, *args, **kwargs):
try:
UserHostingKey.objects.get(
user=self.request.user
)
except UserHostingKey.DoesNotExist:
if not UserHostingKey.objects.filter( user=self.request.user).exists():
messages.success(
request,
'In order to create a VM, you create/upload your SSH KEY first.'
@ -484,14 +469,16 @@ class PaymentVMView(LoginRequiredMixin, FormView):
manager = OpenNebulaManager(email=owner.email,
password=owner.password)
# Get user ssh key
try:
user_key = UserHostingKey.objects.get(
user=self.request.user
)
except UserHostingKey.DoesNotExist:
pass
if not UserHostingKey.objects.filter( user=self.request.user).exists():
context.update({
'sshError': 'error',
'form': form
})
return render(request, self.template_name, context)
# For now just get first one
user_key = UserHostingKey.objects.filter(
user=self.request.user).first()
# Create a vm using logged user
vm_id = manager.create_vm(
template_id=vm_template_id,
@ -636,11 +623,7 @@ class CreateVirtualMachinesView(LoginRequiredMixin, View):
def get(self, request, *args, **kwargs):
try:
UserHostingKey.objects.get(
user=self.request.user
)
except UserHostingKey.DoesNotExist:
if not UserHostingKey.objects.filter( user=self.request.user).exists():
messages.success(
request,
'In order to create a VM, you need to create/upload your SSH KEY first.'
@ -664,7 +647,7 @@ class CreateVirtualMachinesView(LoginRequiredMixin, View):
)
context = {
'error': 'connection'
}
}
return render(request, self.template_name, context)

View file

@ -260,7 +260,6 @@ class OpenNebulaManager():
<DEV_PREFIX>vd</DEV_PREFIX>
<IMAGE_ID>{image_id}</IMAGE_ID>
</DISK>
</TEMPLATE>
""".format(size=1024 * int(specs['disk_size']),
image_id=image_id)
@ -282,10 +281,18 @@ class OpenNebulaManager():
<IMAGE>{image}</IMAGE>
<IMAGE_UNAME>{image_uname}</IMAGE_UNAME>
</DISK>
</TEMPLATE>
""".format(size=1024 * int(specs['disk_size']),
image=image,
image_uname=image_uname)
if ssh_key:
vm_specs += """<CONTEXT>
<SSH_PUBLIC_KEY>{ssh}</SSH_PUBLIC_KEY>
<NETWORK>YES</NETWORK>
</CONTEXT>
</TEMPLATE>
""".format(ssh=public_key)
vm_id = self.client.call(oca.VmTemplate.METHODS['instantiate'],
template.id,
'',
@ -293,25 +300,6 @@ class OpenNebulaManager():
vm_specs,
False)
self.oneadmin_client.call(
'vm.update',
vm_id,
"""<CONTEXT>
<SSH_PUBLIC_KEY>{ssh}</SSH_PUBLIC_KEY>
</CONTEXT>
""".format(ssh=ssh_key)
)
try:
self.oneadmin_client.call(
oca.VirtualMachine.METHODS['chown'],
vm_id,
self.opennebula_user.id,
self.opennebula_user.group_ids[0]
)
except AttributeError:
logger.info(
'Could not change owner for vm with id: {}.'.format(vm_id))
self.oneadmin_client.call(
oca.VirtualMachine.METHODS['action'],
'release',

View file

@ -15,7 +15,6 @@ class VirtualMachineTemplateSerializer(serializers.Serializer):
cores = serializers.SerializerMethodField()
disk_size = serializers.SerializerMethodField()
memory = serializers.SerializerMethodField()
price = serializers.SerializerMethodField()
def get_cores(self, obj):
if hasattr(obj.template, 'vcpu'):
@ -39,6 +38,8 @@ class VirtualMachineTemplateSerializer(serializers.Serializer):
def get_name(self, obj):
return obj.name.strip('public-')
class VirtualMachineSerializer(serializers.Serializer):
"""Serializer to map the virtual machine instance into JSON format."""