diff --git a/datacenterlight/tasks.py b/datacenterlight/tasks.py new file mode 100644 index 00000000..b897c54a --- /dev/null +++ b/datacenterlight/tasks.py @@ -0,0 +1,169 @@ +from dynamicweb.celery import app +from celery.utils.log import get_task_logger +from django.conf import settings +from opennebula_api.models import OpenNebulaManager +from opennebula_api.serializers import VirtualMachineSerializer +from hosting.models import HostingOrder, HostingBill +from utils.forms import UserBillingAddressForm +from datetime import datetime +from membership.models import StripeCustomer +from django.core.mail import EmailMessage +from utils.models import BillingAddress +from celery.exceptions import MaxRetriesExceededError + +logger = get_task_logger(__name__) + + +def retry_task(task, exception=None): + """Retries the specified task using a "backing off countdown", + meaning that the interval between retries grows exponentially + with every retry. + + Arguments: + task: + The task to retry. + + exception: + Optionally, the exception that caused the retry. + """ + + def backoff(attempts): + return 2 ** attempts + + kwargs = { + 'countdown': backoff(task.request.retries), + } + + if exception: + kwargs['exc'] = exception + + raise task.retry(**kwargs) + + +@app.task(bind=True, max_retries=settings.CELERY_MAX_RETRIES) +def create_vm_task(self, vm_template_id, user, specs, template, stripe_customer_id, billing_address_data, + billing_address_id, + charge): + vm_id = None + try: + final_price = specs.get('price') + billing_address = BillingAddress.objects.filter(id=billing_address_id).first() + customer = StripeCustomer.objects.filter(id=stripe_customer_id).first() + # Create OpenNebulaManager + manager = OpenNebulaManager(email=settings.OPENNEBULA_USERNAME, + password=settings.OPENNEBULA_PASSWORD) + + # Create a vm using oneadmin, also specify the name + vm_id = manager.create_vm( + template_id=vm_template_id, + specs=specs, + ssh_key=settings.ONEADMIN_USER_SSH_PUBLIC_KEY, + vm_name="{email}-{template_name}-{date}".format( + email=user.get('email'), + template_name=template.get('name'), + date=int(datetime.now().strftime("%s"))) + ) + + if vm_id is None: + raise Exception("Could not create VM") + + # Create a Hosting Order + order = HostingOrder.create( + price=final_price, + vm_id=vm_id, + customer=customer, + billing_address=billing_address + ) + + # Create a Hosting Bill + HostingBill.create( + customer=customer, billing_address=billing_address) + + # Create Billing Address for User if he does not have one + if not customer.user.billing_addresses.count(): + billing_address_data.update({ + 'user': customer.user.id + }) + billing_address_user_form = UserBillingAddressForm( + billing_address_data) + billing_address_user_form.is_valid() + billing_address_user_form.save() + + # Associate an order with a stripe payment + charge_object = DictDotLookup(charge) + order.set_stripe_charge(charge_object) + + # If the Stripe payment succeeds, set order status approved + order.set_approved() + + vm = VirtualMachineSerializer(manager.get_vm(vm_id)).data + + context = { + 'name': user.get('name'), + 'email': user.get('email'), + 'cores': specs.get('cpu'), + 'memory': specs.get('memory'), + 'storage': specs.get('disk_size'), + 'price': specs.get('price'), + 'template': template.get('name'), + 'vm.name': vm['name'], + 'vm.id': vm['vm_id'], + 'order.id': order.id + } + email_data = { + 'subject': settings.DCL_TEXT + " Order from %s" % context['email'], + 'from_email': settings.DCL_SUPPORT_FROM_ADDRESS, + 'to': ['info@ungleich.ch'], + 'body': "\n".join(["%s=%s" % (k, v) for (k, v) in context.items()]), + 'reply_to': [context['email']], + } + email = EmailMessage(**email_data) + email.send() + except Exception as e: + logger.error(str(e)) + try: + retry_task(self) + except MaxRetriesExceededError: + msg_text = 'Finished {} retries for create_vm_task'.format(self.request.retries) + logger.error(msg_text) + # Try sending email and stop + email_data = { + 'subject': '{} CELERY TASK ERROR: {}'.format(settings.DCL_TEXT, msg_text), + 'from_email': settings.DCL_SUPPORT_FROM_ADDRESS, + 'to': ['info@ungleich.ch'], + 'body': ',\n'.join(str(i) for i in self.request.args) + } + email = EmailMessage(**email_data) + email.send() + return + + return vm_id + + +class DictDotLookup(object): + """ + Creates objects that behave much like a dictionaries, but allow nested + key access using object '.' (dot) lookups. + """ + + def __init__(self, d): + for k in d: + if isinstance(d[k], dict): + self.__dict__[k] = DictDotLookup(d[k]) + elif isinstance(d[k], (list, tuple)): + l = [] + for v in d[k]: + if isinstance(v, dict): + l.append(DictDotLookup(v)) + else: + l.append(v) + self.__dict__[k] = l + else: + self.__dict__[k] = d[k] + + def __getitem__(self, name): + if name in self.__dict__: + return self.__dict__[name] + + def __iter__(self): + return iter(self.__dict__.keys()) diff --git a/datacenterlight/views.py b/datacenterlight/views.py index 0dd5e5ad..db7f2e53 100644 --- a/datacenterlight/views.py +++ b/datacenterlight/views.py @@ -4,7 +4,6 @@ from .forms import BetaAccessForm from .models import BetaAccess, BetaAccessVMType, BetaAccessVM, VMTemplate from django.contrib import messages from django.core.urlresolvers import reverse -from django.core.mail import EmailMessage from utils.mailer import BaseEmail from django.shortcuts import render from django.shortcuts import redirect @@ -13,14 +12,14 @@ from django.core.exceptions import ValidationError from django.views.decorators.cache import cache_control from django.conf import settings from django.utils.translation import ugettext_lazy as _ -from utils.forms import BillingAddressForm, UserBillingAddressForm +from utils.forms import BillingAddressForm from utils.models import BillingAddress -from hosting.models import HostingOrder, HostingBill +from hosting.models import HostingOrder from utils.stripe_utils import StripeUtils -from datetime import datetime from membership.models import CustomUser, StripeCustomer from opennebula_api.models import OpenNebulaManager -from opennebula_api.serializers import VirtualMachineTemplateSerializer, VirtualMachineSerializer, VMTemplateSerializer +from opennebula_api.serializers import VirtualMachineTemplateSerializer, VMTemplateSerializer +from datacenterlight.tasks import create_vm_task class LandingProgramView(TemplateView): @@ -33,7 +32,6 @@ class SuccessView(TemplateView): def get(self, request, *args, **kwargs): if 'specs' not in request.session or 'user' not in request.session: return HttpResponseRedirect(reverse('datacenterlight:index')) - elif 'token' not in request.session: return HttpResponseRedirect(reverse('datacenterlight:payment')) elif 'order_confirmation' not in request.session: @@ -79,8 +77,7 @@ class PricingView(TemplateView): manager = OpenNebulaManager() template = manager.get_template(template_id) - request.session['template'] = VirtualMachineTemplateSerializer( - template).data + request.session['template'] = VirtualMachineTemplateSerializer(template).data if not request.user.is_authenticated(): request.session['next'] = reverse('hosting:payment') @@ -132,8 +129,7 @@ class BetaAccessView(FormView): email = BaseEmail(**email_data) email.send() - messages.add_message( - self.request, messages.SUCCESS, self.success_message) + messages.add_message(self.request, messages.SUCCESS, self.success_message) return render(self.request, 'datacenterlight/beta_success.html', {}) @@ -185,8 +181,7 @@ class BetaProgramView(CreateView): email = BaseEmail(**email_data) email.send() - messages.add_message( - self.request, messages.SUCCESS, self.success_message) + messages.add_message(self.request, messages.SUCCESS, self.success_message) return HttpResponseRedirect(self.get_success_url()) @@ -230,8 +225,7 @@ class IndexView(CreateView): storage_field = forms.IntegerField(validators=[self.validate_storage]) price = request.POST.get('total') template_id = int(request.POST.get('config')) - template = VMTemplate.objects.filter( - opennebula_vm_template_id=template_id).first() + template = VMTemplate.objects.filter(opennebula_vm_template_id=template_id).first() template_data = VMTemplateSerializer(template).data name = request.POST.get('name') @@ -243,40 +237,35 @@ class IndexView(CreateView): cores = cores_field.clean(cores) except ValidationError as err: msg = '{} : {}.'.format(cores, str(err)) - messages.add_message( - self.request, messages.ERROR, msg, extra_tags='cores') + messages.add_message(self.request, messages.ERROR, msg, extra_tags='cores') return HttpResponseRedirect(reverse('datacenterlight:index') + "#order_form") try: memory = memory_field.clean(memory) except ValidationError as err: msg = '{} : {}.'.format(memory, str(err)) - messages.add_message( - self.request, messages.ERROR, msg, extra_tags='memory') + messages.add_message(self.request, messages.ERROR, msg, extra_tags='memory') return HttpResponseRedirect(reverse('datacenterlight:index') + "#order_form") try: storage = storage_field.clean(storage) except ValidationError as err: msg = '{} : {}.'.format(storage, str(err)) - messages.add_message( - self.request, messages.ERROR, msg, extra_tags='storage') + messages.add_message(self.request, messages.ERROR, msg, extra_tags='storage') return HttpResponseRedirect(reverse('datacenterlight:index') + "#order_form") try: name = name_field.clean(name) except ValidationError as err: msg = '{} {}.'.format(name, _('is not a proper name')) - messages.add_message( - self.request, messages.ERROR, msg, extra_tags='name') + messages.add_message(self.request, messages.ERROR, msg, extra_tags='name') return HttpResponseRedirect(reverse('datacenterlight:index') + "#order_form") try: email = email_field.clean(email) except ValidationError as err: msg = '{} {}.'.format(email, _('is not a proper email')) - messages.add_message( - self.request, messages.ERROR, msg, extra_tags='email') + messages.add_message(self.request, messages.ERROR, msg, extra_tags='email') return HttpResponseRedirect(reverse('datacenterlight:index') + "#order_form") specs = { @@ -341,8 +330,7 @@ class IndexView(CreateView): email = BaseEmail(**email_data) email.send() - messages.add_message( - self.request, messages.SUCCESS, self.success_message) + messages.add_message(self.request, messages.SUCCESS, self.success_message) return super(IndexView, self).form_valid(form) @@ -411,7 +399,6 @@ class PaymentOrderView(FormView): # Create Billing Address billing_address = form.save() - request.session['billing_address_data'] = billing_address_data request.session['billing_address'] = billing_address.id request.session['token'] = token @@ -436,13 +423,11 @@ class OrderConfirmationView(DetailView): stripe_customer_id = request.session.get('customer') customer = StripeCustomer.objects.filter(id=stripe_customer_id).first() stripe_utils = StripeUtils() - card_details = stripe_utils.get_card_details( - customer.stripe_id, request.session.get('token')) + card_details = stripe_utils.get_card_details(customer.stripe_id, request.session.get('token')) if not card_details.get('response_object') and not card_details.get('paid'): msg = card_details.get('error') messages.add_message(self.request, messages.ERROR, msg, extra_tags='failed_payment') return HttpResponseRedirect(reverse('datacenterlight:payment') + '#payment_error') - context = { 'site_url': reverse('datacenterlight:index'), 'cc_last4': card_details.get('response_object').get('last4'), @@ -458,8 +443,6 @@ class OrderConfirmationView(DetailView): customer = StripeCustomer.objects.filter(id=stripe_customer_id).first() billing_address_data = request.session.get('billing_address_data') billing_address_id = request.session.get('billing_address') - billing_address = BillingAddress.objects.filter( - id=billing_address_id).first() vm_template_id = template.get('id', 1) final_price = specs.get('price') @@ -475,72 +458,8 @@ class OrderConfirmationView(DetailView): return HttpResponseRedirect(reverse('datacenterlight:payment') + '#payment_error') charge = charge_response.get('response_object') - - # Create OpenNebulaManager - manager = OpenNebulaManager(email=settings.OPENNEBULA_USERNAME, - password=settings.OPENNEBULA_PASSWORD) - - # Create a vm using oneadmin, also specify the name - vm_id = manager.create_vm( - template_id=vm_template_id, - specs=specs, - ssh_key=settings.ONEADMIN_USER_SSH_PUBLIC_KEY, - vm_name="{email}-{template_name}-{date}".format( - email=user.get('email'), - template_name=template.get('name'), - date=int(datetime.now().strftime("%s"))) - ) - - # Create a Hosting Order - order = HostingOrder.create( - price=final_price, - vm_id=vm_id, - customer=customer, - billing_address=billing_address - ) - - # Create a Hosting Bill - HostingBill.create( - customer=customer, billing_address=billing_address) - - # Create Billing Address for User if he does not have one - if not customer.user.billing_addresses.count(): - billing_address_data.update({ - 'user': customer.user.id - }) - billing_address_user_form = UserBillingAddressForm( - billing_address_data) - billing_address_user_form.is_valid() - billing_address_user_form.save() - - # Associate an order with a stripe payment - order.set_stripe_charge(charge) - - # If the Stripe payment was successed, set order status approved - order.set_approved() - - vm = VirtualMachineSerializer(manager.get_vm(vm_id)).data - - context = { - 'name': user.get('name'), - 'email': user.get('email'), - 'cores': specs.get('cpu'), - 'memory': specs.get('memory'), - 'storage': specs.get('disk_size'), - 'price': specs.get('price'), - 'template': template.get('name'), - 'vm.name': vm['name'], - 'vm.id': vm['vm_id'], - 'order.id': order.id - } - email_data = { - 'subject': settings.DCL_TEXT + " Order from %s" % context['email'], - 'from_email': settings.DCL_SUPPORT_FROM_ADDRESS, - 'to': ['info@ungleich.ch'], - 'body': "\n".join(["%s=%s" % (k, v) for (k, v) in context.items()]), - 'reply_to': [context['email']], - } - email = EmailMessage(**email_data) - email.send() + create_vm_task.delay(vm_template_id, user, specs, template, stripe_customer_id, billing_address_data, + billing_address_id, + charge) request.session['order_confirmation'] = True return HttpResponseRedirect(reverse('datacenterlight:order_success')) diff --git a/deploy.sh b/deploy.sh index f2a1d59e..04a7b04c 100755 --- a/deploy.sh +++ b/deploy.sh @@ -13,6 +13,7 @@ while true; do case "$1" in -h | --help ) HELP=true; shift ;; -v | --verbose ) VERBOSE=true; shift ;; + -D | --dbmakemigrations ) DB_MAKE_MIGRATIONS=true; shift ;; -d | --dbmigrate ) DB_MIGRATE=true; shift ;; -n | --nogit ) NO_GIT=true; shift ;; -b | --branch ) BRANCH="$2"; shift 2 ;; @@ -31,13 +32,15 @@ if [ "$HELP" == "true" ]; then echo "options are : " echo " -h, --help: Print this help message" echo " -v, --verbose: Show verbose output to stdout. Without this a deploy.log is written to ~/app folder" - echo " -d, --dbmigrate: Do DB migrate" - echo " -n, --nogit: Don't execute git commands. With this --branch has no effect." + echo " -D, --dbmakemigrations: Do DB makemigrations" + echo " -d, --dbmigrate: Do DB migrate. To do both makemigrations and migrate, supply both switches -D and -d" + echo " -n, --nogit: Don't execute git commands. This is used to deploy the current code in the project repo. With this --branch has no effect." echo " -b, --branch: The branch to pull from origin repo." exit fi echo "BRANCH="$BRANCH +echo "DB_MAKE_MIGRATIONS="$DB_MAKE_MIGRATIONS echo "DB_MIGRATE="$DB_MIGRATE echo "NO_GIT="$NO_GIT echo "VERBOSE="$VERBOSE @@ -45,7 +48,7 @@ echo "VERBOSE="$VERBOSE # The project directory exists, we pull the specified branch cd $APP_HOME_DIR if [ -z "$NO_GIT" ]; then - echo 'We are executing default git commands. Please -no_git to not use this.' + echo 'We are executing default git commands. Please add --nogit to not do this.' # Save any modified changes before git pulling git stash # Fetch all branches/tags @@ -59,16 +62,23 @@ fi source ~/pyvenv/bin/activate pip install -r requirements.txt > deploy.log 2>&1 echo "###" >> deploy.log -if [ -z "$DB_MIGRATE" ]; then - echo 'We are not doing DB migration' +if [ -z "$DB_MAKE_MIGRATIONS" ]; then + echo 'We are not doing DB makemigrations' else + echo 'Doing DB makemigrations' ./manage.py makemigrations >> deploy.log 2>&1 echo "###" >> deploy.log +fi +if [ -z "$DB_MIGRATE" ]; then + echo 'We are not doing DB migrate' +else + echo 'Doing DB migrate' ./manage.py migrate >> deploy.log 2>&1 echo "###" >> deploy.log fi printf 'yes' | ./manage.py collectstatic >> deploy.log 2>&1 echo "###" >> deploy.log django-admin compilemessages +sudo systemctl restart celery.service sudo systemctl restart uwsgi diff --git a/dynamicweb/__init__.py b/dynamicweb/__init__.py index e69de29b..1a6c551d 100644 --- a/dynamicweb/__init__.py +++ b/dynamicweb/__init__.py @@ -0,0 +1,5 @@ +# This will make sure the app is always imported when +# Django starts so that shared_task will use this app. +from .celery import app as celery_app + +__all__ = ['celery_app'] diff --git a/dynamicweb/celery.py b/dynamicweb/celery.py new file mode 100644 index 00000000..609ef5c4 --- /dev/null +++ b/dynamicweb/celery.py @@ -0,0 +1,21 @@ +import os +from celery import Celery + +# set the default Django settings module for the 'celery' program. +os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'dynamicweb.settings') + +app = Celery('dynamicweb') + +# Using a string here means the worker don't have to serialize +# the configuration object to child processes. +# - namespace='CELERY' means all celery-related configuration keys +# should have a `CELERY_` prefix. +app.config_from_object('django.conf:settings', namespace='CELERY') + +# Load task modules from all registered Django app configs. +app.autodiscover_tasks() + + +@app.task(bind=True) +def debug_task(self): + print('Request: {0!r}'.format(self.request)) diff --git a/dynamicweb/settings/base.py b/dynamicweb/settings/base.py index a837f0f2..a724b38e 100644 --- a/dynamicweb/settings/base.py +++ b/dynamicweb/settings/base.py @@ -10,6 +10,9 @@ from django.utils.translation import ugettext_lazy as _ # dotenv import dotenv +import logging + +logger = logging.getLogger(__name__) def gettext(s): @@ -25,6 +28,21 @@ def bool_env(val): return True if os.environ.get(val, False) == 'True' else False +def int_env(val, default_value=0): + """Replaces string based environment values with Python integers + Return default_value if val is not set or cannot be parsed, otherwise + returns the python integer equal to the passed val + """ + return_value = default_value + try: + return_value = int(os.environ.get(val)) + except Exception as e: + logger.error("Encountered exception trying to get env value for {}\nException details: {}".format( + val, str(e))) + + return return_value + + BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__))) PROJECT_DIR = os.path.abspath( @@ -120,7 +138,8 @@ INSTALLED_APPS = ( 'datacenterlight.templatetags', 'alplora', 'rest_framework', - 'opennebula_api' + 'opennebula_api', + 'django_celery_results', ) MIDDLEWARE_CLASSES = ( @@ -524,6 +543,15 @@ GOOGLE_ANALYTICS_PROPERTY_IDS = { 'dynamicweb-staging.ungleich.ch': 'staging' } +# CELERY Settings +CELERY_BROKER_URL = env('CELERY_BROKER_URL') +CELERY_RESULT_BACKEND = env('CELERY_RESULT_BACKEND') +CELERY_ACCEPT_CONTENT = ['application/json'] +CELERY_TASK_SERIALIZER = 'json' +CELERY_RESULT_SERIALIZER = 'json' +CELERY_TIMEZONE = 'Europe/Zurich' +CELERY_MAX_RETRIES = int_env('CELERY_MAX_RETRIES', 5) + ENABLE_DEBUG_LOGGING = bool_env('ENABLE_DEBUG_LOGGING') if ENABLE_DEBUG_LOGGING: diff --git a/hosting/locale/de/LC_MESSAGES/django.po b/hosting/locale/de/LC_MESSAGES/django.po index 49a05548..155a840a 100644 --- a/hosting/locale/de/LC_MESSAGES/django.po +++ b/hosting/locale/de/LC_MESSAGES/django.po @@ -392,38 +392,76 @@ msgstr "Möchtest Du den Schlüssel löschen?" msgid "Show" msgstr "Anzeigen" -msgid "Public SSH Key" +#, fuzzy +#| msgid "Public SSH Key" +msgid "Public SSH key" msgstr "Public SSH Key" msgid "Download" msgstr "" -msgid "Settings" -msgstr "Einstellungen" +msgid "Your Virtual Machine Detail" +msgstr "Virtuelle Maschinen Detail" -msgid "Billing" -msgstr "Abrechnungen" +msgid "VM Settings" +msgstr "VM Einstellungen" -msgid "Ip not assigned yet" -msgstr "Ip nicht zugewiesen" +msgid "Copied" +msgstr "Kopiert" msgid "Disk" msgstr "Festplatte" -msgid "Current pricing" +msgid "Billing" +msgstr "Abrechnungen" + +msgid "Current Pricing" msgstr "Aktueller Preis" -msgid "Current status" -msgstr "Aktueller Status" +msgid "Month" +msgstr "Monat" -msgid "Terminate Virtual Machine" -msgstr "Virtuelle Maschine beenden" +msgid "See Invoice" +msgstr "Rechnung" + +msgid "Your VM is" +msgstr "Deine VM ist" + +msgid "Pending" +msgstr "In Vorbereitung" + +msgid "Online" +msgstr "" + +msgid "Failed" +msgstr "Fehlgeschlagen" + +msgid "Terminate VM" +msgstr "VM Beenden" + +msgid "Support / Contact" +msgstr "Support / Kontakt" + +msgid "Something doesn't work?" +msgstr "Etwas funktioniert nicht?" + +msgid "We are here to help you!" +msgstr "Wir sind hier, um Dir zu helfen!" + +msgid "CONTACT" +msgstr "KONTACT" + +msgid "BACK TO LIST" +msgstr "ZURÜCK ZUR LISTE" msgid "Terminate your Virtual Machine" -msgstr "Ihre virtuelle Maschine beenden" +msgstr "Deine Virtuelle Maschine beenden" -msgid "Are you sure do you want to cancel your Virtual Machine " -msgstr "Sind Sie sicher, dass Sie ihre virtuelle Maschine beenden wollen " +msgid "Do you want to cancel your Virtual Machine" +msgstr "Bist Du sicher, dass Du Deine virtuelle Maschine beenden willst" + +msgid "OK" +msgstr "" msgid "Virtual Machines" msgstr "Virtuelle Maschinen" @@ -499,6 +537,15 @@ msgstr "" #~ "Kreditkateninformationen wirst du auf die Bestellbestätigungsseite " #~ "weitergeleitet." +#~ msgid "Ip not assigned yet" +#~ msgstr "Ip nicht zugewiesen" + +#~ msgid "Current status" +#~ msgstr "Aktueller Status" + +#~ msgid "Terminate Virtual Machine" +#~ msgstr "Virtuelle Maschine beenden" + #~ msgid "Ipv4" #~ msgstr "IPv4" diff --git a/hosting/models.py b/hosting/models.py index 88386913..8cdc6114 100644 --- a/hosting/models.py +++ b/hosting/models.py @@ -1,13 +1,9 @@ import os import logging - from django.db import models from django.utils.functional import cached_property - - from Crypto.PublicKey import RSA - from membership.models import StripeCustomer, CustomUser from utils.models import BillingAddress from utils.mixins import AssignPermissionsMixin @@ -42,7 +38,6 @@ class HostingPlan(models.Model): class HostingOrder(AssignPermissionsMixin, models.Model): - ORDER_APPROVED_STATUS = 'Approved' ORDER_DECLINED_STATUS = 'Declined' @@ -101,7 +96,7 @@ class HostingOrder(AssignPermissionsMixin, models.Model): class UserHostingKey(models.Model): user = models.ForeignKey(CustomUser) public_key = models.TextField() - private_key = models.FileField(upload_to='private_keys', blank=True) + private_key = models.FileField(upload_to='private_keys', blank=True) created_at = models.DateTimeField(auto_now_add=True) name = models.CharField(max_length=100) diff --git a/hosting/static/hosting/css/virtual-machine.css b/hosting/static/hosting/css/virtual-machine.css index be5addf1..45aa68ff 100644 --- a/hosting/static/hosting/css/virtual-machine.css +++ b/hosting/static/hosting/css/virtual-machine.css @@ -1,3 +1,6 @@ +.virtual-machine-container { + max-width: 900px; +} .virtual-machine-container .tabs-left, .virtual-machine-container .tabs-right { border-bottom: none; padding-top: 2px; @@ -229,6 +232,204 @@ } } +/* Vm Details */ + +.vm-detail-item, .vm-contact-us { + overflow: hidden; + border: 1px solid #ccc; + padding: 15px; + color: #555; + font-weight: 300; + margin-bottom: 15px; +} + +.vm-detail-title { + margin-top: 0; + font-size: 20px; + font-weight: 300; +} + +.vm-detail-title .un-icon { + float: right; + height: 24px; + width: 21px; + margin-top: 0; +} + +.vm-detail-item .vm-name { + font-size: 16px; + margin-bottom: 15px; +} + +.vm-detail-item p { + margin-bottom: 5px; + position: relative; +} + +.vm-detail-ip { + padding-bottom: 5px; + border-bottom: 1px solid #ddd; + margin-bottom: 10px; +} + +.vm-detail-ip .un-icon { + height: 14px; + width: 14px; +} + +.vm-detail-ip .to_copy { + position: absolute; + right: 0; + top: 1px; + padding: 0; + line-height: 1; +} + +.vm-vmid { + padding: 50px 0 70px; + text-align: center; +} + +.vm-item-lg { + font-size: 22px; + margin-top: 5px; + margin-bottom: 15px; + letter-spacing: 0.6px; +} + +.vm-color-online { + color: #37B07B; +} + +.vm-color-pending { + color: #e47f2f; +} + +.vm-detail-item .value{ + font-weight: 400; +} + +.vm-detail-config .value { + float: right; + font-weight: 600; +} + +.vm-detail-contain { + margin-top: 25px; +} + +.vm-contact-us { + margin: 25px 0 30px; + /* text-align: center; */ +} + +@media(min-width: 768px) { + .vm-detail-contain { + display: flex; + margin-left: -15px; + margin-right: -15px; + } + .vm-detail-item { + width: 33.333333%; + margin: 0 15px; + } + .vm-contact-us { + display: flex; + align-items: center; + justify-content: space-between; + } + .vm-contact-us .vm-detail-title { + margin-bottom: 0; + } + .vm-contact-us .un-icon { + width: 22px; + height: 22px; + margin-right: 5px; + } + .vm-contact-us div { + padding: 0 15px; + position: relative; + } + .vm-contact-us-text { + display: flex; + align-items: center; + } +} + +.value-sm-block { + display: block; + padding-top: 2px; +} + +@media(max-width: 767px) { + .vm-contact-us div { + margin-bottom: 30px; + } + .vm-contact-us div span { + display: block; + margin-bottom: 3px; + } + .dashboard-title-thin { + font-size: 22px; + } +} + +.btn-vm-invoice { + color: #87B6EA; + border: 2px solid #87B6EA; + padding: 4px 18px; + letter-spacing: 0.6px; +} +.btn-vm-invoice:hover, .btn-vm-invoice:focus { + color : #fff; + background: #87B6EA; +} + + +.btn-vm-term { + color: #aaa; + border: 2px solid #ccc; + background: #fff; + padding: 4px 18px; + letter-spacing: 0.6px; +} +.btn-vm-term:hover, .btn-vm-term:focus, .btn-vm-term:active { + color: #eb4d5c; + border-color: #eb4d5c; +} + +.btn-vm-contact { + color: #fff; + background: #A3C0E2; + border: 2px solid #A3C0E2; + padding: 5px 25px; + font-size: 12px; + letter-spacing: 1.3px; +} +.btn-vm-contact:hover, .btn-vm-contact:focus { + background: #fff; + color: #a3c0e2; +} + +.btn-vm-back { + color: #fff; + background: #C4CEDA; + border: 2px solid #C4CEDA; + padding: 5px 25px; + font-size: 12px; + letter-spacing: 1.3px; +} +.btn-vm-back:hover, .btn-vm-back:focus { + color: #fff; + background: #8da4c0; + border-color: #8da4c0; +} + +.vm-contact-us-text { + letter-spacing: 0.4px; +} + + /* New styles */ .dashboard-container-head { padding: 0 8px; diff --git a/hosting/static/hosting/img/24-hours-support.svg b/hosting/static/hosting/img/24-hours-support.svg new file mode 100644 index 00000000..4db05be3 --- /dev/null +++ b/hosting/static/hosting/img/24-hours-support.svg @@ -0,0 +1,61 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/hosting/static/hosting/img/billing.svg b/hosting/static/hosting/img/billing.svg new file mode 100644 index 00000000..d002fa6c --- /dev/null +++ b/hosting/static/hosting/img/billing.svg @@ -0,0 +1 @@ +billing icon \ No newline at end of file diff --git a/hosting/static/hosting/img/connected.svg b/hosting/static/hosting/img/connected.svg new file mode 100644 index 00000000..fa3875dc --- /dev/null +++ b/hosting/static/hosting/img/connected.svg @@ -0,0 +1,45 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/hosting/static/hosting/img/settings.svg b/hosting/static/hosting/img/settings.svg new file mode 100644 index 00000000..61dc8613 --- /dev/null +++ b/hosting/static/hosting/img/settings.svg @@ -0,0 +1,53 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/hosting/static/hosting/js/initial.js b/hosting/static/hosting/js/initial.js index ea391ce2..50975806 100644 --- a/hosting/static/hosting/js/initial.js +++ b/hosting/static/hosting/js/initial.js @@ -21,4 +21,54 @@ $( document ).ready(function() { $this.attr('data-alt', txt); }); +}); + +function getScrollbarWidth() { + var outer = document.createElement("div"); + outer.style.visibility = "hidden"; + outer.style.width = "100px"; + outer.style.msOverflowStyle = "scrollbar"; // needed for WinJS apps + + document.body.appendChild(outer); + + var widthNoScroll = outer.offsetWidth; + // force scrollbars + outer.style.overflow = "scroll"; + + // add innerdiv + var inner = document.createElement("div"); + inner.style.width = "100%"; + outer.appendChild(inner); + + var widthWithScroll = inner.offsetWidth; + + // remove divs + outer.parentNode.removeChild(outer); + + return widthNoScroll - widthWithScroll; +} + +// globally stores the width of scrollbar +var scrollbarWidth = getScrollbarWidth(); +var paddingAdjusted = false; + +$( document ).ready(function() { + // add proper padding to fixed topnav on modal show + $('body').on('click', '[data-toggle=modal]', function(){ + var $body = $('body'); + if ($body[0].scrollHeight > $body.height()) { + scrollbarWidth = getScrollbarWidth(); + var topnavPadding = parseInt($('.navbar-fixed-top.topnav').css('padding-right')); + $('.navbar-fixed-top.topnav').css('padding-right', topnavPadding+scrollbarWidth); + paddingAdjusted = true; + } + }); + + // remove added padding on modal hide + $('body').on('hidden.bs.modal', function(){ + if (paddingAdjusted) { + var topnavPadding = parseInt($('.navbar-fixed-top.topnav').css('padding-right')); + $('.navbar-fixed-top.topnav').css('padding-right', topnavPadding-scrollbarWidth); + } + }); }); \ No newline at end of file diff --git a/hosting/templates/hosting/virtual_machine_detail.html b/hosting/templates/hosting/virtual_machine_detail.html index 1ae26dc0..59a36bdc 100644 --- a/hosting/templates/hosting/virtual_machine_detail.html +++ b/hosting/templates/hosting/virtual_machine_detail.html @@ -3,193 +3,110 @@ {% load i18n %} {% block content %} -
-
-
-
-
-

{{virtual_machine.name}}

-
- - -
- -
-
-
-
-

{{virtual_machine.hosting_company_name}}

- - {% if virtual_machine.ipv6 %} -
- - -
- {% else %} - -
- {% trans "Ip not assigned yet"%} - -
- - {% endif %} - -
- -
-
-
-
-
-
-
- - {% trans "Cores"%} - {{virtual_machine.cores}} -
-
-
-
- {% trans "Memory"%}
- {{virtual_machine.memory}} GB -
-
-
-
- - {% trans "Disk"%} - {{virtual_machine.disk_size|floatformat:2}} GB -
-
-
-
-
-
-
- {% trans "Configuration"%}: {{virtual_machine.configuration}} -
-
- - -
-
- -
-
-

{% trans "Current pricing"%}

- {{virtual_machine.price|floatformat}} CHF/month -
-
-
-
-
-
-
-

{% trans "Current status"%}

- -
- {% if virtual_machine.state == 'PENDING' %} - Pending - {% elif virtual_machine.state == 'ACTIVE' %} - Online - {% elif virtual_machine.state == 'FAILED'%} - Failed - {% endif %} -
-
-
- {% if not virtual_machine.status == 'canceled' %} -
-
-
-
- {% csrf_token %} -
- - - -
- -
-
-
- {% if messages %} -
- {% for message in messages %} - {{ message }} - {% endfor %} -
- {% endif %} -
- - - - -
- {% endif %} -
-
-
- -
+ {% if messages %} +
+ {% for message in messages %} + {{ message }} + {% endfor %} +
+ {% endif %} +
+

{% trans "Your Virtual Machine Detail" %}

+
+
+

{% trans "VM Settings" %}

+

{{virtual_machine.name}}

+ {% if virtual_machine.ipv6 %} +
+

+ IPv4: + {{virtual_machine.ipv4}} + +

+

+ IPv6: + {{virtual_machine.ipv6}} + +

+
+ {% endif %} +
+

{% trans "Cores" %}:{{virtual_machine.cores}}

+

{% trans "Memory" %}:{{virtual_machine.memory}} GB

+

{% trans "Disk" %}:{{virtual_machine.disk_size|floatformat:2}} GB

+

{% trans "Configuration" %}:{{virtual_machine.configuration}}

- +
+

{% trans "Billing" %}

+
+
{% trans "Current Pricing" %}
+
{{virtual_machine.price|floatformat}} CHF/{% trans "Month" %}
+ {% trans "See Invoice" %} +
+
+
+

{% trans "Status" %}

+
+
{% trans "Your VM is" %}
+ {% if virtual_machine.state == 'PENDING' %} +
{% trans "Pending" %}
+ {% elif virtual_machine.state == 'ACTIVE' %} +
{% trans "Online" %}
+ {% elif virtual_machine.state == 'FAILED'%} +
{% trans "Failed" %}
+ {% endif %} + {% if not virtual_machine.status == 'canceled' %} +
+ {% csrf_token %} +
+ + {% endif %} +
+
+
+
+
+

{% trans "Support / Contact" %}

+
+
+ +
+ {% trans "Something doesn't work?" %} {% trans "We are here to help you!" %} +
+
+ +
+ +
+ + - -
- + {%endblock%} diff --git a/hosting/urls.py b/hosting/urls.py index ea96af77..23709904 100644 --- a/hosting/urls.py +++ b/hosting/urls.py @@ -20,9 +20,12 @@ urlpatterns = [ url(r'orders/(?P\d+)/?$', OrdersHostingDetailView.as_view(), name='orders'), url(r'bills/?$', HostingBillListView.as_view(), name='bills'), url(r'bills/(?P\d+)/?$', HostingBillDetailView.as_view(), name='bills'), - url(r'cancel_order/(?P\d+)/?$', OrdersHostingDeleteView.as_view(), name='delete_order'), - url(r'create_virtual_machine/?$', CreateVirtualMachinesView.as_view(), name='create_virtual_machine'), - url(r'my-virtual-machines/?$', VirtualMachinesPlanListView.as_view(), name='virtual_machines'), + url(r'cancel_order/(?P\d+)/?$', + OrdersHostingDeleteView.as_view(), name='delete_order'), + url(r'create_virtual_machine/?$', CreateVirtualMachinesView.as_view(), + name='create_virtual_machine'), + url(r'my-virtual-machines/?$', + VirtualMachinesPlanListView.as_view(), name='virtual_machines'), url(r'my-virtual-machines/(?P\d+)/?$', VirtualMachineView.as_view(), name='virtual_machines'), url(r'ssh_keys/?$', SSHKeyListView.as_view(), @@ -44,5 +47,6 @@ urlpatterns = [ PasswordResetConfirmView.as_view(), name='reset_password_confirm'), url(r'^logout/?$', auth_views.logout, {'next_page': '/hosting/login?logged_out=true'}, name='logout'), - url(r'^validate/(?P.*)/$', SignupValidatedView.as_view(), name='validate') + url(r'^validate/(?P.*)/$', + SignupValidatedView.as_view(), name='validate') ] diff --git a/hosting/views.py b/hosting/views.py index 2b4c8d21..9cc78bf9 100644 --- a/hosting/views.py +++ b/hosting/views.py @@ -244,7 +244,8 @@ class SignupValidatedView(SignupValidateView): lurl=login_url) else: home_url = 'Data Center Light' + reverse('datacenterlight:index') + \ + '">Data Center Light' message = '{sorry_message}
{go_back_to} {hurl}'.format( sorry_message=_("Sorry. Your request is invalid."), go_back_to=_('Go back to'), @@ -831,6 +832,7 @@ class VirtualMachineView(LoginRequiredMixin, View): serializer = VirtualMachineSerializer(vm) context = { 'virtual_machine': serializer.data, + 'order': HostingOrder.objects.get(vm_id=serializer.data['vm_id']) } except: pass diff --git a/opennebula_api/serializers.py b/opennebula_api/serializers.py index 12b313af..662b2fb6 100644 --- a/opennebula_api/serializers.py +++ b/opennebula_api/serializers.py @@ -1,5 +1,6 @@ import ipaddress +from builtins import hasattr from rest_framework import serializers from oca import OpenNebulaException @@ -32,7 +33,7 @@ class VirtualMachineTemplateSerializer(serializers.Serializer): return 0 def get_memory(self, obj): - return int(obj.template.memory)/1024 + return int(obj.template.memory) / 1024 def get_name(self, obj): return obj.name.strip('public-') @@ -57,13 +58,13 @@ class VirtualMachineSerializer(serializers.Serializer): configuration = serializers.SerializerMethodField() template_id = serializers.ChoiceField( - choices=[(key.id, key.name) for key in - OpenNebulaManager().try_get_templates() - ], - source='template.template_id', - write_only=True, - default=[] - ) + choices=[(key.id, key.name) for key in + OpenNebulaManager().try_get_templates() + ], + source='template.template_id', + write_only=True, + default=[] + ) def create(self, validated_data): owner = validated_data['owner'] @@ -74,10 +75,10 @@ class VirtualMachineSerializer(serializers.Serializer): template_id = validated_data['template']['template_id'] specs = { - 'cpu': cores, - 'disk_size': disk, - 'memory': memory, - } + 'cpu': cores, + 'disk_size': disk, + 'memory': memory, + } try: manager = OpenNebulaManager(email=owner.email, @@ -92,7 +93,7 @@ class VirtualMachineSerializer(serializers.Serializer): return manager.get_vm(opennebula_id) def get_memory(self, obj): - return int(obj.template.memory)/1024 + return int(obj.template.memory) / 1024 def get_disk_size(self, obj): template = obj.template @@ -104,9 +105,9 @@ class VirtualMachineSerializer(serializers.Serializer): def get_price(self, obj): template = obj.template price = float(template.vcpu) * 5.0 - price += (int(template.memory)/1024 * 2.0) + price += (int(template.memory) / 1024 * 2.0) for disk in template.disks: - price += int(disk.size)/1024 * 0.6 + price += int(disk.size) / 1024 * 0.6 return price def get_configuration(self, obj): @@ -115,15 +116,30 @@ class VirtualMachineSerializer(serializers.Serializer): return template.name.strip('public-') def get_ipv4(self, obj): - nic = obj.template.nics[0] - if 'vm-ipv6-nat64-ipv4' in nic.network and is_in_v4_range(nic.mac): - return str(v4_from_mac(nic.mac)) + """ + Get the IPv4s from the given VM + + :param obj: The VM in contention + :return: Returns csv string of all IPv4s added to this VM otherwise returns "-" if no IPv4 is available + """ + ipv4 = [] + for nic in obj.template.nics: + if hasattr(nic, 'ip'): + ipv4.append(nic.ip) + if len(ipv4) > 0: + return ', '.join(ipv4) else: return '-' def get_ipv6(self, obj): - nic = obj.template.nics[0] - return nic.ip6_global + ipv6 = [] + for nic in obj.template.nics: + if hasattr(nic, 'ip6_global'): + ipv6.append(nic.ip6_global) + if len(ipv6) > 0: + return ', '.join(ipv6) + else: + return '-' def get_name(self, obj): return obj.name.strip('public-')