Skip to content

Commit

Permalink
Merge 6d5f15c into master-ci
Browse files Browse the repository at this point in the history
  • Loading branch information
github-actions[bot] authored May 11, 2022
2 parents 23a503e + 6d5f15c commit 38d2cbf
Show file tree
Hide file tree
Showing 10 changed files with 121 additions and 11 deletions.
6 changes: 3 additions & 3 deletions bb_billing/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,11 @@ To determine if an account is place on the payment tier or the credit method a t

New user accounts or accounts with strikes are billed at the following tiers:

**$1** - Any unsucessful payments result in an imediate account suspension. The VM is shutdown and the user has 24 hours to provide a valid payment method. The user receives 3 strikes resulting in their account being imeditly transfered to the credit method with a $100 minimum to maintain their account.
**$1** - Any unsucessful payments result in an imediate account suspension. The VM is shutdown and the user has 24 hours to provide a valid payment method. The user receives *three (3) strikes* resulting in their account being imeditly transfered to the credit method with a $100 minimum to maintain their account.

**$10** - Any unsucessful payments will result in the VM being suspended, the user has 48 hours to provide a valid payment method. Two strikes to their account is issued and if it occurs again the account to transfered to the credit method.
**$10** - Any unsucessful payments will result in the VM being suspended, the user has 48 hours to provide a valid payment method. *Two (2) strikes* to their account is issued and if it occurs again the account to transfered to the credit method.

**$100** - Upon an unsucessful payment the VM is suspended, the user has 5 business days to provide a valid payment method. A strike to their account is issued and if it occurs again the account is transfered to the credit method.
**$100** - Upon an unsucessful payment the VM is suspended, the user has 5 business days to provide a valid payment method. *One (1) strike* is issued to their account and if it occurs again the account is transfered to the credit method.

**$1,000** - Upon an unsucessful payment the user is given 24 hours to provied a valid payment method before the VM is suspended. Once the VM is suspended the user has 3 business days to pay the balance of their account. Each day that passes the user recieves a strike. If the user fails to pay the balance of their account the account is transfered to the credit method.

Expand Down
36 changes: 33 additions & 3 deletions bb_billing/views/views_invoice.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
''' bb_billing - views_invoice.py '''

# pylint: disable=R0912,R0915

import json
import stripe

from django.shortcuts import HttpResponse
from django.views.decorators.csrf import csrf_exempt

from bb_tasks.tasks import(pause_vm_subprocess)
from bb_tasks.tasks import(pause_vm_subprocess, destroy_vm_with_open_tabs)

from bb_data.models import (
UserProfile, ResourceTimeTracking, BillingHistory
Expand Down Expand Up @@ -67,6 +69,25 @@ def invoice_event(request):
# ------------------------------- $1 Threshold ------------------------------- #
if customer.threshold == 1.00:
customer.strikes = customer.strikes + 3
countdown_time=86400 # VMs destroyed if balance is not paid within 24 hours

# ------------------------------- $10 Threshold ------------------------------- #
if customer.threshold == 10.00:
customer.strikes = customer.strikes + 2
countdown_time=172800 # VMs destroyed if balance is not paid within 48 hours

# ------------------------------ $100 Threshold ------------------------------ #
if customer.threshold == 100.00:
customer.strikes = customer.strikes + 1
countdown_time=432000 # VMs destroyed if balance is not paid within 5 days

# ----------------------------- $1,000 Threshold ----------------------------- #
if customer.threshold == 1000.00:
countdown_time=259200 # VMs destroyed if balance is not paid within 3 days


# -------------------------------- Suspend VMs ------------------------------- #
if customer.threshold < 1000.00:
owned_bricks = VirtualBrickOwner.objects.filter(owner=customer)
for brick in owned_bricks:
pause_vm_subprocess.apply_async(
Expand All @@ -76,8 +97,17 @@ def invoice_event(request):
brick.virt_brick.is_on=False
brick.virt_brick.save()

if customer.threshold == 10.00:
customer.strikes = customer.strikes + 2
# -------------------------------- Destroy VMs ------------------------------- #
bill = BillingHistory.objects.get(invoice_id=invoice.id)
tacker = ResourceTimeTracking.objects.get(id=bill.usage.id)
if not tacker.destroy_vms_countdown_started:
destroy_vm_with_open_tabs.apply_async(
(tacker.id,),
countdown=countdown_time,
queue='ssh_queue'
)
tacker.destroy_vms_countdown_started = True
tacker.save()

customer.save()

Expand Down
6 changes: 3 additions & 3 deletions bb_dashboard/views/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -122,13 +122,13 @@ def pages(request):
balance_paid = False,
billing_cycle_end__gte=datetime.datetime.today()
)
context['billing_history'] = BillingHistory.objects.filter(
user=context['profile'].user).order_by('-id')

if created:
tracker.billing_cycle_end = tracker.billing_cycle_start + datetime.timedelta(days=30)
tracker.save()

context['billing_history'] = BillingHistory.objects.filter(
user=context['profile'].user).order_by('-id')

context['tracker'] = tracker

# --------------------------------- API Token -------------------------------- #
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# Generated by Django 3.2.12 on 2022-05-11 17:21

from django.db import migrations, models


class Migration(migrations.Migration):

dependencies = [
('bb_data', '0057_userprofile_credit_balance'),
]

operations = [
migrations.AddField(
model_name='billinghistory',
name='destroy_vms_countdown_started',
field=models.BooleanField(default=False),
),
]
22 changes: 22 additions & 0 deletions bb_data/migrations/0059_auto_20220511_1324.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
# Generated by Django 3.2.12 on 2022-05-11 17:24

from django.db import migrations, models


class Migration(migrations.Migration):

dependencies = [
('bb_data', '0058_billinghistory_destroy_vms_countdown_started'),
]

operations = [
migrations.RemoveField(
model_name='billinghistory',
name='destroy_vms_countdown_started',
),
migrations.AddField(
model_name='resourcetimetracking',
name='destroy_vms_countdown_started',
field=models.BooleanField(default=False),
),
]
2 changes: 2 additions & 0 deletions bb_data/models/models_stripe.py
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,8 @@ class ResourceTimeTracking(models.Model):
balance_paid = models.BooleanField(default=False)
stripe_transaction = models.CharField(max_length=100, blank=True, null=True) # Transaction ID

destroy_vms_countdown_started = models.BooleanField(default=False)

@property
def cycle_total(self):
'''
Expand Down
17 changes: 17 additions & 0 deletions bb_public/migrations/0004_alter_contactus_options.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# Generated by Django 3.2.12 on 2022-05-11 17:21

from django.db import migrations


class Migration(migrations.Migration):

dependencies = [
('bb_public', '0003_alter_contactus_name'),
]

operations = [
migrations.AlterModelOptions(
name='contactus',
options={'verbose_name': 'Contact Us', 'verbose_name_plural': 'Contact Us'},
),
]
6 changes: 5 additions & 1 deletion bb_tasks/tasks/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,16 @@
from .callable import (
pause_vm_subprocess, play_vm_subprocess,
reboot_vm_subprocess, destroy_vm_subprocess, close_ssh_port,
destroy_vm_with_open_tabs,
)

from .system import (
prepare_gpu_background_task, stop_bg, start_bg,
)

from .billing import threshold_resource_invoicing, monthly_resource_invoicing
from .billing import (
threshold_resource_invoicing, monthly_resource_invoicing,
)

__all__ = [
'verify_host_connectivity', 'reconnect_host', 'host_cleanup',
Expand All @@ -22,4 +25,5 @@
'catch_clone_errors', 'remove_stale_clone',
'prepare_gpu_background_task', 'stop_bg', 'start_bg',
'threshold_resource_invoicing', 'monthly_resource_invoicing',
'destroy_vm_with_open_tabs'
]
1 change: 1 addition & 0 deletions bb_tasks/tasks/billing.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@

from bb_data.models import UserProfile, ResourceRates, ResourceTimeTracking, BillingHistory


if settings.DEBUG is False:
stripe.api_key = settings.STRIPE_SECRET_KEY
stripePubKey = settings.STRIPE_PUBLISHABLE_KEY
Expand Down
18 changes: 17 additions & 1 deletion bb_tasks/tasks/callable.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,9 @@

import box

from bb_data.models import UserProfile, ResourceTimeTracking
from bb_vm.models import (
PortTunnel, VirtualBrick, HostFoundation
PortTunnel, VirtualBrick, HostFoundation, VirtualBrickOwner
)


Expand Down Expand Up @@ -72,3 +73,18 @@ def close_ssh_port(port_number):
Called when a VM is being destryed, a delay is set before the port becomes available again.
'''
PortTunnel.objects.filter(port_number=port_number).delete()

# ------------------------ Destroy VMs With Open Tabs ------------------------ #
@shared_task
def destroy_vm_with_open_tabs(resource_usage_id):
'''
Started with a countdown timer when a payment fails.
If the balance remains unpaid after the timer expires, the VM is destroyed.
'''
resource_usage = ResourceTimeTracking.objects.get(id=resource_usage_id)
user_profile = UserProfile.objects.get(user=resource_usage.user)

if not resource_usage.balance_paid and not resource_usage.user.is_superuser:
owned_bricks = VirtualBrickOwner.objects.filter(owner=user_profile)
for brick in owned_bricks:
destroy_vm_subprocess.apply_async((brick.virt_brick.id,), queue='ssh_queue')

0 comments on commit 38d2cbf

Please sign in to comment.