forked from 3wordchant/capsul-flask
fine-tuning and starting to work on account-balance
This commit is contained in:
@ -1,5 +1,6 @@
|
||||
import re
|
||||
import sys
|
||||
from datetime import datetime
|
||||
from flask import Blueprint
|
||||
from flask import flash
|
||||
from flask import current_app
|
||||
@ -23,16 +24,16 @@ def makeCapsulId():
|
||||
lettersAndNumbers = generate(alphabet="1234567890qwertyuiopasdfghjklzxcvbnm", size=10)
|
||||
return f"capsul-{lettersAndNumbers}"
|
||||
|
||||
def double_check_capsul_address(db_model, email, id, ipv4):
|
||||
def double_check_capsul_address(id, ipv4):
|
||||
try:
|
||||
result = current_app.config["VIRTUALIZATION_MODEL"].get(id)
|
||||
if result.ipv4 != ipv4:
|
||||
ipv4 = result.ipv4
|
||||
db_model.updateVm(email=email, id=id, ipv4=result.ipv4)
|
||||
get_model().updateVm(email=session["account"], id=id, ipv4=result.ipv4)
|
||||
except:
|
||||
print(f"""
|
||||
error occurred in list capsuls endpoint while trying to grab ip address of {vm['id']}
|
||||
via the virtualization model: {my_exec_info_message(sys.exc_info())}"""
|
||||
the virtualization model threw an error in double_check_capsul_address of {id}:
|
||||
{my_exec_info_message(sys.exc_info())}"""
|
||||
)
|
||||
|
||||
return ipv4
|
||||
@ -40,13 +41,12 @@ def double_check_capsul_address(db_model, email, id, ipv4):
|
||||
@bp.route("/")
|
||||
@account_required
|
||||
def index():
|
||||
db_model = get_model()
|
||||
vms = db_model.list_vms_for_account(email=session["account"])
|
||||
vms = get_vms()
|
||||
|
||||
# for now we are going to check the IP according to the virt model
|
||||
# on every request. this could be done by a background job and cached later on...
|
||||
for vm in vms:
|
||||
vm["ipv4"] = double_check_capsul_address(db_model, session["account"], vm["id"], vm["ipv4"])
|
||||
vm["ipv4"] = double_check_capsul_address(vm["id"], vm["ipv4"])
|
||||
|
||||
vms = list(map(
|
||||
lambda x: dict(
|
||||
@ -65,13 +65,12 @@ def index():
|
||||
@bp.route("/<string:id>")
|
||||
@account_required
|
||||
def detail(id):
|
||||
db_model = get_model()
|
||||
vm = db_model.get_vm_detail(email=session["account"], id=id)
|
||||
vm = get_model().get_vm_detail(email=session["account"], id=id)
|
||||
|
||||
if vm is None:
|
||||
return abort(404, f"{id} doesn't exist.")
|
||||
|
||||
vm["ipv4"] = double_check_capsul_address(db_model, session["account"], vm["id"], vm["ipv4"])
|
||||
vm["ipv4"] = double_check_capsul_address(vm["id"], vm["ipv4"])
|
||||
vm["created"] = vm['created'].strftime("%b %d %Y %H:%M")
|
||||
vm["ssh_public_keys"] = ", ".join(vm["ssh_public_keys"]) if len(vm["ssh_public_keys"]) > 0 else "<deleted>"
|
||||
|
||||
@ -81,10 +80,9 @@ def detail(id):
|
||||
@bp.route("/create", methods=("GET", "POST"))
|
||||
@account_required
|
||||
def create():
|
||||
db_model = get_model()
|
||||
vm_sizes = db_model.vm_sizes_dict()
|
||||
operating_systems = db_model.operating_systems_dict()
|
||||
ssh_public_keys = db_model.list_ssh_public_keys_for_account(session["account"])
|
||||
vm_sizes = get_model().vm_sizes_dict()
|
||||
operating_systems = get_model().operating_systems_dict()
|
||||
ssh_public_keys = get_model().list_ssh_public_keys_for_account(session["account"])
|
||||
errors = list()
|
||||
created_os = None
|
||||
|
||||
@ -125,7 +123,7 @@ def create():
|
||||
|
||||
if len(errors) == 0:
|
||||
id = makeCapsulId()
|
||||
db_model.create_vm(
|
||||
get_model().create_vm(
|
||||
email=session["account"],
|
||||
id=id,
|
||||
size=size,
|
||||
@ -158,7 +156,6 @@ def create():
|
||||
@bp.route("/ssh", methods=("GET", "POST"))
|
||||
@account_required
|
||||
def ssh_public_keys():
|
||||
db_model = get_model()
|
||||
errors = list()
|
||||
|
||||
if request.method == "POST":
|
||||
@ -179,29 +176,58 @@ def ssh_public_keys():
|
||||
if not re.match(r"^(ssh|ecdsa)-[0-9A-Za-z+/_=@. -]+$", content):
|
||||
errors.append("Content must match \"^(ssh|ecdsa)-[0-9A-Za-z+/_=@. -]+$\"")
|
||||
|
||||
if db_model.ssh_public_key_name_exists(session["account"], name):
|
||||
if get_model().ssh_public_key_name_exists(session["account"], name):
|
||||
errors.append("A key with that name already exists")
|
||||
|
||||
if len(errors) == 0:
|
||||
db_model.create_ssh_public_key(session["account"], name, content)
|
||||
get_model().create_ssh_public_key(session["account"], name, content)
|
||||
|
||||
elif method == "DELETE":
|
||||
|
||||
if len(errors) == 0:
|
||||
db_model.delete_ssh_public_key(session["account"], name)
|
||||
get_model().delete_ssh_public_key(session["account"], name)
|
||||
|
||||
for error in errors:
|
||||
flash(error)
|
||||
|
||||
keys_list=list(map(
|
||||
lambda x: dict(name=x['name'], content=f"{x['content'][:20]}...{x['content'][len(x['content'])-20:]}"),
|
||||
db_model.list_ssh_public_keys_for_account(session["account"])
|
||||
get_model().list_ssh_public_keys_for_account(session["account"])
|
||||
))
|
||||
|
||||
return render_template("ssh-public-keys.html", ssh_public_keys=keys_list, has_ssh_public_keys=len(keys_list) > 0)
|
||||
|
||||
def get_vms():
|
||||
if 'user_vms' not in g:
|
||||
g.user_vms = get_model().list_vms_for_account(session["account"])
|
||||
return g.user_vms
|
||||
|
||||
@bp.route("/billing")
|
||||
def get_payments():
|
||||
if 'user_payments' not in g:
|
||||
g.user_payments = get_model().list_payments_for_account(session["account"])
|
||||
return g.user_payments
|
||||
|
||||
def get_account_balance():
|
||||
|
||||
|
||||
average_number_of_days_in_a_month = 30.44
|
||||
|
||||
vm_cost_dollars = 0.0
|
||||
for vm in get_vms():
|
||||
end_datetime = vm["deleted"] if vm["deleted"] else datetime.now()
|
||||
vm_months = ( end_datetime - vm["created"] ).days / average_number_of_days_in_a_month
|
||||
vm_cost_dollars += vm_months * vm["dollars_per_month"]
|
||||
|
||||
payment_total = sum(map(lambda x: x["dollars"], get_payments()))
|
||||
|
||||
|
||||
|
||||
@bp.route("/account-balance")
|
||||
@account_required
|
||||
def faq():
|
||||
return render_template("billing.html")
|
||||
def account_balance():
|
||||
errors = list()
|
||||
payments = get_payments()
|
||||
|
||||
|
||||
|
||||
return render_template("account-balance.html")
|
Reference in New Issue
Block a user