Add ssh key to new vm
Create_vm now stes to public key correctly
This commit is contained in:
parent
ef0589f691
commit
715d092b96
3 changed files with 24 additions and 52 deletions
|
@ -355,17 +355,6 @@ class GenerateVMSSHKeysView(LoginRequiredMixin, FormView):
|
||||||
return render(self.request, self.template_name, context)
|
return render(self.request, self.template_name, context)
|
||||||
|
|
||||||
def post(self, request, *args, **kwargs):
|
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()
|
form = self.get_form()
|
||||||
if form.is_valid():
|
if form.is_valid():
|
||||||
return self.form_valid(form)
|
return self.form_valid(form)
|
||||||
|
@ -418,11 +407,7 @@ class PaymentVMView(LoginRequiredMixin, FormView):
|
||||||
return context
|
return context
|
||||||
|
|
||||||
def get(self, request, *args, **kwargs):
|
def get(self, request, *args, **kwargs):
|
||||||
try:
|
if not UserHostingKey.objects.filter( user=self.request.user).exists():
|
||||||
UserHostingKey.objects.get(
|
|
||||||
user=self.request.user
|
|
||||||
)
|
|
||||||
except UserHostingKey.DoesNotExist:
|
|
||||||
messages.success(
|
messages.success(
|
||||||
request,
|
request,
|
||||||
'In order to create a VM, you create/upload your SSH KEY first.'
|
'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,
|
manager = OpenNebulaManager(email=owner.email,
|
||||||
password=owner.password)
|
password=owner.password)
|
||||||
# Get user ssh key
|
# Get user ssh key
|
||||||
try:
|
if not UserHostingKey.objects.filter( user=self.request.user).exists():
|
||||||
user_key = UserHostingKey.objects.get(
|
context.update({
|
||||||
user=self.request.user
|
'sshError': 'error',
|
||||||
)
|
'form': form
|
||||||
|
})
|
||||||
except UserHostingKey.DoesNotExist:
|
return render(request, self.template_name, context)
|
||||||
pass
|
# For now just get first one
|
||||||
|
user_key = UserHostingKey.objects.filter(
|
||||||
|
user=self.request.user).first()
|
||||||
|
|
||||||
# Create a vm using logged user
|
# Create a vm using logged user
|
||||||
vm_id = manager.create_vm(
|
vm_id = manager.create_vm(
|
||||||
template_id=vm_template_id,
|
template_id=vm_template_id,
|
||||||
|
@ -636,11 +623,7 @@ class CreateVirtualMachinesView(LoginRequiredMixin, View):
|
||||||
|
|
||||||
def get(self, request, *args, **kwargs):
|
def get(self, request, *args, **kwargs):
|
||||||
|
|
||||||
try:
|
if not UserHostingKey.objects.filter( user=self.request.user).exists():
|
||||||
UserHostingKey.objects.get(
|
|
||||||
user=self.request.user
|
|
||||||
)
|
|
||||||
except UserHostingKey.DoesNotExist:
|
|
||||||
messages.success(
|
messages.success(
|
||||||
request,
|
request,
|
||||||
'In order to create a VM, you need to create/upload your SSH KEY first.'
|
'In order to create a VM, you need to create/upload your SSH KEY first.'
|
||||||
|
@ -664,7 +647,7 @@ class CreateVirtualMachinesView(LoginRequiredMixin, View):
|
||||||
)
|
)
|
||||||
context = {
|
context = {
|
||||||
'error': 'connection'
|
'error': 'connection'
|
||||||
}
|
}
|
||||||
|
|
||||||
return render(request, self.template_name, context)
|
return render(request, self.template_name, context)
|
||||||
|
|
||||||
|
|
|
@ -260,7 +260,6 @@ class OpenNebulaManager():
|
||||||
<DEV_PREFIX>vd</DEV_PREFIX>
|
<DEV_PREFIX>vd</DEV_PREFIX>
|
||||||
<IMAGE_ID>{image_id}</IMAGE_ID>
|
<IMAGE_ID>{image_id}</IMAGE_ID>
|
||||||
</DISK>
|
</DISK>
|
||||||
</TEMPLATE>
|
|
||||||
""".format(size=1024 * int(specs['disk_size']),
|
""".format(size=1024 * int(specs['disk_size']),
|
||||||
image_id=image_id)
|
image_id=image_id)
|
||||||
|
|
||||||
|
@ -282,10 +281,18 @@ class OpenNebulaManager():
|
||||||
<IMAGE>{image}</IMAGE>
|
<IMAGE>{image}</IMAGE>
|
||||||
<IMAGE_UNAME>{image_uname}</IMAGE_UNAME>
|
<IMAGE_UNAME>{image_uname}</IMAGE_UNAME>
|
||||||
</DISK>
|
</DISK>
|
||||||
</TEMPLATE>
|
|
||||||
""".format(size=1024 * int(specs['disk_size']),
|
""".format(size=1024 * int(specs['disk_size']),
|
||||||
image=image,
|
image=image,
|
||||||
image_uname=image_uname)
|
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'],
|
vm_id = self.client.call(oca.VmTemplate.METHODS['instantiate'],
|
||||||
template.id,
|
template.id,
|
||||||
'',
|
'',
|
||||||
|
@ -293,25 +300,6 @@ class OpenNebulaManager():
|
||||||
vm_specs,
|
vm_specs,
|
||||||
False)
|
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(
|
self.oneadmin_client.call(
|
||||||
oca.VirtualMachine.METHODS['action'],
|
oca.VirtualMachine.METHODS['action'],
|
||||||
'release',
|
'release',
|
||||||
|
|
|
@ -15,7 +15,6 @@ class VirtualMachineTemplateSerializer(serializers.Serializer):
|
||||||
cores = serializers.SerializerMethodField()
|
cores = serializers.SerializerMethodField()
|
||||||
disk_size = serializers.SerializerMethodField()
|
disk_size = serializers.SerializerMethodField()
|
||||||
memory = serializers.SerializerMethodField()
|
memory = serializers.SerializerMethodField()
|
||||||
price = serializers.SerializerMethodField()
|
|
||||||
|
|
||||||
def get_cores(self, obj):
|
def get_cores(self, obj):
|
||||||
if hasattr(obj.template, 'vcpu'):
|
if hasattr(obj.template, 'vcpu'):
|
||||||
|
@ -39,6 +38,8 @@ class VirtualMachineTemplateSerializer(serializers.Serializer):
|
||||||
def get_name(self, obj):
|
def get_name(self, obj):
|
||||||
return obj.name.strip('public-')
|
return obj.name.strip('public-')
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
class VirtualMachineSerializer(serializers.Serializer):
|
class VirtualMachineSerializer(serializers.Serializer):
|
||||||
"""Serializer to map the virtual machine instance into JSON format."""
|
"""Serializer to map the virtual machine instance into JSON format."""
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue