Attempted to get hosts for adding ssh keys (to be tested)

This commit is contained in:
PCoder 2017-08-29 13:31:05 +05:30
parent 21f51692c4
commit 12255364b2

View file

@ -7,7 +7,8 @@ from django.shortcuts import render
from django.http import Http404 from django.http import Http404
from django.core.urlresolvers import reverse_lazy, reverse from django.core.urlresolvers import reverse_lazy, reverse
from django.contrib.auth.mixins import LoginRequiredMixin from django.contrib.auth.mixins import LoginRequiredMixin
from django.views.generic import View, CreateView, FormView, ListView, DetailView, \ from django.views.generic import View, CreateView, FormView, ListView, \
DetailView, \
DeleteView, TemplateView, UpdateView DeleteView, TemplateView, UpdateView
from django.http import HttpResponseRedirect from django.http import HttpResponseRedirect
from django.contrib import messages from django.contrib import messages
@ -24,17 +25,23 @@ from django.utils.safestring import mark_safe
from membership.models import CustomUser, StripeCustomer from membership.models import CustomUser, StripeCustomer
from utils.stripe_utils import StripeUtils from utils.stripe_utils import StripeUtils
from utils.forms import BillingAddressForm, PasswordResetRequestForm, UserBillingAddressForm from utils.forms import BillingAddressForm, PasswordResetRequestForm, \
from utils.views import PasswordResetViewMixin, PasswordResetConfirmViewMixin, LoginViewMixin UserBillingAddressForm
from utils.views import PasswordResetViewMixin, PasswordResetConfirmViewMixin, \
LoginViewMixin
from utils.mailer import BaseEmail from utils.mailer import BaseEmail
from .models import HostingOrder, HostingBill, HostingPlan, UserHostingKey from .models import HostingOrder, HostingBill, HostingPlan, UserHostingKey
from .forms import HostingUserSignupForm, HostingUserLoginForm, UserHostingKeyForm, generate_ssh_key_name from .forms import HostingUserSignupForm, HostingUserLoginForm, \
UserHostingKeyForm, generate_ssh_key_name
from .mixins import ProcessVMSelectionMixin from .mixins import ProcessVMSelectionMixin
from opennebula_api.models import OpenNebulaManager from opennebula_api.models import OpenNebulaManager
from opennebula_api.serializers import VirtualMachineSerializer, \ from opennebula_api.serializers import VirtualMachineSerializer, \
VirtualMachineTemplateSerializer VirtualMachineTemplateSerializer
from django.utils.translation import ugettext_lazy as _ from django.utils.translation import ugettext_lazy as _
import logging
logger = logging.getLogger(__name__)
CONNECTION_ERROR = "Your VMs cannot be displayed at the moment due to a backend \ CONNECTION_ERROR = "Your VMs cannot be displayed at the moment due to a backend \
connection error. please try again in a few minutes." connection error. please try again in a few minutes."
@ -284,7 +291,8 @@ class PasswordResetConfirmView(PasswordResetConfirmViewMixin):
form = self.form_class(request.POST) form = self.form_class(request.POST)
if user is not None and default_token_generator.check_token(user, token): if user is not None and default_token_generator.check_token(user,
token):
if form.is_valid(): if form.is_valid():
new_password = form.cleaned_data['new_password2'] new_password = form.cleaned_data['new_password2']
user.set_password(new_password) user.set_password(new_password)
@ -385,7 +393,8 @@ class SSHKeyListView(LoginRequiredMixin, ListView):
def render_to_response(self, context, **response_kwargs): def render_to_response(self, context, **response_kwargs):
if not self.queryset: if not self.queryset:
return HttpResponseRedirect(reverse('hosting:choice_ssh_keys')) return HttpResponseRedirect(reverse('hosting:choice_ssh_keys'))
return super(SSHKeyListView, self).render_to_response(context, **response_kwargs) return super(SSHKeyListView, self).render_to_response(context,
**response_kwargs)
class SSHKeyChoiceView(LoginRequiredMixin, View): class SSHKeyChoiceView(LoginRequiredMixin, View):
@ -448,18 +457,29 @@ class SSHKeyCreateView(LoginRequiredMixin, FormView):
}) })
owner = self.request.user owner = self.request.user
manager = OpenNebulaManager() # Get all VMs belonging to the user
allorders = HostingOrder.objects.filter(customer__user=owner)
if len(allorders) > 0:
logger.debug("The user {} has 1 or more VMs. We need to configure "
"the ssh keys.".format(self.request.user.email))
hosts = [order.vm_id for order in allorders]
# Get user ssh key
public_key = str(form.cleaned_data.get('public_key', '')) else:
# Add ssh key to user logger.debug("The user {} has no VMs. We don't need to configure "
try: "the ssh keys.".format(self.request.user.email))
manager.add_public_key( # manager = OpenNebulaManager()
user=owner, public_key=public_key, merge=True) #
except ConnectionError: # # Get user ssh key
pass # public_key = str(form.cleaned_data.get('public_key', ''))
except WrongNameError: # # Add ssh key to user
pass # try:
# manager.add_public_key(
# user=owner, public_key=public_key, merge=True)
# except ConnectionError:
# pass
# except WrongNameError:
# pass
return HttpResponseRedirect(self.success_url) return HttpResponseRedirect(self.success_url)
@ -558,8 +578,10 @@ class PaymentVMView(LoginRequiredMixin, FormView):
token=token) token=token)
if not customer: if not customer:
msg = _("Invalid credit card") msg = _("Invalid credit card")
messages.add_message(self.request, messages.ERROR, msg, extra_tags='make_charge_error') messages.add_message(self.request, messages.ERROR, msg,
return HttpResponseRedirect(reverse('hosting:payment') + '#payment_error') extra_tags='make_charge_error')
return HttpResponseRedirect(
reverse('hosting:payment') + '#payment_error')
# Create Billing Address # Create Billing Address
billing_address = form.save() billing_address = form.save()
@ -572,8 +594,10 @@ class PaymentVMView(LoginRequiredMixin, FormView):
# Check if the payment was approved # Check if the payment was approved
if not charge_response.get('response_object'): if not charge_response.get('response_object'):
msg = charge_response.get('error') msg = charge_response.get('error')
messages.add_message(self.request, messages.ERROR, msg, extra_tags='make_charge_error') messages.add_message(self.request, messages.ERROR, msg,
return HttpResponseRedirect(reverse('hosting:payment') + '#payment_error') extra_tags='make_charge_error')
return HttpResponseRedirect(
reverse('hosting:payment') + '#payment_error')
charge = charge_response.get('response_object') charge = charge_response.get('response_object')
@ -581,7 +605,8 @@ 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
if not UserHostingKey.objects.filter(user=self.request.user).exists(): if not UserHostingKey.objects.filter(
user=self.request.user).exists():
context.update({ context.update({
'sshError': 'error', 'sshError': 'error',
'form': form 'form': form
@ -633,7 +658,8 @@ class PaymentVMView(LoginRequiredMixin, FormView):
context = { context = {
'vm': vm, 'vm': vm,
'order': order, 'order': order,
'base_url': "{0}://{1}".format(request.scheme, request.get_host()) 'base_url': "{0}://{1}".format(request.scheme,
request.get_host())
} }
email_data = { email_data = {
@ -647,13 +673,15 @@ class PaymentVMView(LoginRequiredMixin, FormView):
email.send() email.send()
return HttpResponseRedirect( return HttpResponseRedirect(
"{url}?{query_params}".format(url=reverse('hosting:orders', kwargs={'pk': order.id}), "{url}?{query_params}".format(
url=reverse('hosting:orders', kwargs={'pk': order.id}),
query_params='page=payment')) query_params='page=payment'))
else: else:
return self.form_invalid(form) return self.form_invalid(form)
class OrdersHostingDetailView(PermissionRequiredMixin, LoginRequiredMixin, DetailView): class OrdersHostingDetailView(PermissionRequiredMixin, LoginRequiredMixin,
DetailView):
template_name = "hosting/order_detail.html" template_name = "hosting/order_detail.html"
context_object_name = "order" context_object_name = "order"
login_url = reverse_lazy('hosting:login') login_url = reverse_lazy('hosting:login')
@ -761,7 +789,8 @@ class CreateVirtualMachinesView(LoginRequiredMixin, View):
configuration_options = HostingPlan.get_serialized_configs() configuration_options = HostingPlan.get_serialized_configs()
context = { context = {
'templates': VirtualMachineTemplateSerializer(templates, many=True).data, 'templates': VirtualMachineTemplateSerializer(templates,
many=True).data,
'configuration_options': configuration_options, 'configuration_options': configuration_options,
} }
except: except:
@ -832,7 +861,8 @@ class VirtualMachineView(LoginRequiredMixin, View):
serializer = VirtualMachineSerializer(vm) serializer = VirtualMachineSerializer(vm)
context = { context = {
'virtual_machine': serializer.data, 'virtual_machine': serializer.data,
'order': HostingOrder.objects.get(vm_id=serializer.data['vm_id']) 'order': HostingOrder.objects.get(
vm_id=serializer.data['vm_id'])
} }
except: except:
pass pass
@ -863,7 +893,8 @@ class VirtualMachineView(LoginRequiredMixin, View):
context = { context = {
'vm': vm, 'vm': vm,
'base_url': "{0}://{1}".format(self.request.scheme, self.request.get_host()) 'base_url': "{0}://{1}".format(self.request.scheme,
self.request.get_host())
} }
email_data = { email_data = {
'subject': 'Virtual machine plan canceled', 'subject': 'Virtual machine plan canceled',
@ -883,7 +914,8 @@ class VirtualMachineView(LoginRequiredMixin, View):
return HttpResponseRedirect(self.get_success_url()) return HttpResponseRedirect(self.get_success_url())
class HostingBillListView(PermissionRequiredMixin, LoginRequiredMixin, ListView): class HostingBillListView(PermissionRequiredMixin, LoginRequiredMixin,
ListView):
template_name = "hosting/bills.html" template_name = "hosting/bills.html"
login_url = reverse_lazy('hosting:login') login_url = reverse_lazy('hosting:login')
permission_required = ['view_hostingview'] permission_required = ['view_hostingview']
@ -893,7 +925,8 @@ class HostingBillListView(PermissionRequiredMixin, LoginRequiredMixin, ListView)
ordering = '-id' ordering = '-id'
class HostingBillDetailView(PermissionRequiredMixin, LoginRequiredMixin, DetailView): class HostingBillDetailView(PermissionRequiredMixin, LoginRequiredMixin,
DetailView):
template_name = "hosting/bill_detail.html" template_name = "hosting/bill_detail.html"
login_url = reverse_lazy('hosting:login') login_url = reverse_lazy('hosting:login')
permission_required = ['view_hostingview'] permission_required = ['view_hostingview']