2021-07-08 18:04:27 +00:00
|
|
|
from base64 import b64encode
|
|
|
|
from datetime import datetime, timedelta
|
|
|
|
import json
|
2020-05-11 16:57:39 +00:00
|
|
|
import re
|
2020-05-11 21:24:37 +00:00
|
|
|
import sys
|
2021-07-08 18:04:27 +00:00
|
|
|
|
2020-05-11 02:43:06 +00:00
|
|
|
from flask import Blueprint
|
|
|
|
from flask import flash
|
|
|
|
from flask import current_app
|
|
|
|
from flask import g
|
|
|
|
from flask import request
|
|
|
|
from flask import session
|
|
|
|
from flask import render_template
|
2020-05-17 04:02:02 +00:00
|
|
|
from flask import redirect
|
|
|
|
from flask import url_for
|
2020-05-11 02:43:06 +00:00
|
|
|
from werkzeug.exceptions import abort
|
2020-05-11 06:47:14 +00:00
|
|
|
from nanoid import generate
|
|
|
|
|
2020-05-13 05:28:53 +00:00
|
|
|
from capsulflask.metrics import durations as metric_durations
|
2020-05-11 06:47:14 +00:00
|
|
|
from capsulflask.auth import account_required
|
2021-01-04 19:32:52 +00:00
|
|
|
from capsulflask.db import get_model
|
|
|
|
from capsulflask.shared import my_exec_info_message
|
2020-05-17 03:04:51 +00:00
|
|
|
from capsulflask.payment import poll_btcpay_session
|
2020-05-15 23:18:19 +00:00
|
|
|
from capsulflask import cli
|
2020-05-11 02:43:06 +00:00
|
|
|
|
|
|
|
bp = Blueprint("console", __name__, url_prefix="/console")
|
|
|
|
|
2021-07-09 19:13:28 +00:00
|
|
|
def make_capsul_id():
|
|
|
|
letters_n_nummers = generate(alphabet="1234567890qwertyuiopasdfghjklzxcvbnm", size=10)
|
|
|
|
return f"capsul-{letters_n_nummers}"
|
2020-05-11 06:47:14 +00:00
|
|
|
|
2021-01-30 07:39:48 +00:00
|
|
|
def double_check_capsul_address(id, ipv4, get_ssh_host_keys):
|
2021-02-16 02:27:17 +00:00
|
|
|
try:
|
|
|
|
result = current_app.config["HUB_MODEL"].get(id, get_ssh_host_keys)
|
2021-02-17 03:13:51 +00:00
|
|
|
if result != None and result.ipv4 != None and result.ipv4 != ipv4:
|
2021-02-16 02:27:17 +00:00
|
|
|
ipv4 = result.ipv4
|
|
|
|
get_model().update_vm_ip(email=session["account"], id=id, ipv4=result.ipv4)
|
2021-02-16 02:32:24 +00:00
|
|
|
|
2021-02-18 02:50:17 +00:00
|
|
|
if result != None and result.ssh_host_keys != None and get_ssh_host_keys:
|
2021-02-16 02:27:17 +00:00
|
|
|
get_model().update_vm_ssh_host_keys(email=session["account"], id=id, ssh_host_keys=result.ssh_host_keys)
|
|
|
|
except:
|
|
|
|
current_app.logger.error(f"""
|
|
|
|
the virtualization model threw an error in double_check_capsul_address of {id}:
|
|
|
|
{my_exec_info_message(sys.exc_info())}"""
|
|
|
|
)
|
|
|
|
return None
|
2020-05-11 21:24:37 +00:00
|
|
|
|
2021-01-30 07:39:48 +00:00
|
|
|
return result
|
2020-05-11 21:24:37 +00:00
|
|
|
|
2020-05-11 06:47:14 +00:00
|
|
|
@bp.route("/")
|
|
|
|
@account_required
|
|
|
|
def index():
|
2020-05-12 01:34:12 +00:00
|
|
|
vms = get_vms()
|
2020-05-20 23:37:53 +00:00
|
|
|
vms = list(filter(lambda x: not x['deleted'], vms))
|
2020-05-17 04:02:02 +00:00
|
|
|
created = request.args.get('created')
|
2020-05-17 04:21:09 +00:00
|
|
|
|
2020-05-17 04:05:45 +00:00
|
|
|
# this is here to prevent xss
|
2020-05-17 04:28:21 +00:00
|
|
|
if created and not re.match(r"^(cvm|capsul)-[a-z0-9]{10}$", created):
|
2020-05-17 04:02:02 +00:00
|
|
|
created = '___________'
|
2020-05-11 21:24:37 +00:00
|
|
|
|
|
|
|
# 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:
|
2021-01-30 08:10:22 +00:00
|
|
|
result = double_check_capsul_address(vm["id"], vm["ipv4"], False)
|
|
|
|
if result is not None:
|
|
|
|
vm["ipv4"] = result.ipv4
|
2021-02-18 02:50:17 +00:00
|
|
|
vm["state"] = result.state
|
2021-02-18 02:56:52 +00:00
|
|
|
else:
|
|
|
|
vm["state"] = "unknown"
|
|
|
|
|
2020-05-11 21:24:37 +00:00
|
|
|
|
2021-02-18 03:57:09 +00:00
|
|
|
mappedVms = []
|
|
|
|
for vm in vms:
|
2021-02-18 03:57:57 +00:00
|
|
|
ip_display = vm['ipv4']
|
2021-02-18 03:57:09 +00:00
|
|
|
if not ip_display:
|
|
|
|
if vm["state"] == "running":
|
|
|
|
ip_display = "..booting.."
|
|
|
|
else:
|
|
|
|
ip_display = "unknown"
|
|
|
|
|
|
|
|
ip_display_class = "ok"
|
2021-02-18 03:57:57 +00:00
|
|
|
if not vm['ipv4']:
|
2021-02-18 03:57:09 +00:00
|
|
|
if vm["state"] == "running":
|
|
|
|
ip_display_class = "waiting-pulse"
|
|
|
|
else:
|
|
|
|
ip_display_class = "yellow"
|
|
|
|
|
|
|
|
mappedVms.append(dict(
|
2021-02-18 03:57:57 +00:00
|
|
|
id=vm['id'],
|
|
|
|
size=vm['size'],
|
|
|
|
state=vm['state'],
|
2021-02-18 03:57:09 +00:00
|
|
|
ipv4=ip_display,
|
|
|
|
ipv4_status=ip_display_class,
|
2021-02-18 03:57:57 +00:00
|
|
|
os=vm['os'],
|
|
|
|
created=vm['created'].strftime("%b %d %Y")
|
2021-02-18 03:57:09 +00:00
|
|
|
))
|
2020-05-11 21:24:37 +00:00
|
|
|
|
2021-02-18 03:57:09 +00:00
|
|
|
return render_template("capsuls.html", vms=mappedVms, has_vms=len(vms) > 0, created=created)
|
2020-05-11 16:57:39 +00:00
|
|
|
|
2020-05-15 17:23:42 +00:00
|
|
|
@bp.route("/<string:id>", methods=("GET", "POST"))
|
2020-05-11 16:57:39 +00:00
|
|
|
@account_required
|
2020-05-11 21:24:37 +00:00
|
|
|
def detail(id):
|
2020-05-13 05:28:53 +00:00
|
|
|
duration=request.args.get('duration')
|
|
|
|
if not duration:
|
|
|
|
duration = "5m"
|
|
|
|
|
2020-05-12 01:34:12 +00:00
|
|
|
vm = get_model().get_vm_detail(email=session["account"], id=id)
|
2020-05-11 16:57:39 +00:00
|
|
|
|
2020-05-11 21:24:37 +00:00
|
|
|
if vm is None:
|
|
|
|
return abort(404, f"{id} doesn't exist.")
|
2020-05-11 16:57:39 +00:00
|
|
|
|
2020-05-15 17:23:42 +00:00
|
|
|
if vm['deleted']:
|
2020-05-15 23:18:19 +00:00
|
|
|
return render_template("capsul-detail.html", vm=vm, delete=True, deleted=True)
|
2020-05-11 16:57:39 +00:00
|
|
|
|
2021-02-18 02:50:17 +00:00
|
|
|
vm["created"] = vm['created'].strftime("%b %d %Y %H:%M")
|
|
|
|
vm["ssh_authorized_keys"] = ", ".join(vm["ssh_authorized_keys"]) if len(vm["ssh_authorized_keys"]) > 0 else "<missing>"
|
|
|
|
|
2020-05-15 17:23:42 +00:00
|
|
|
if request.method == "POST":
|
2020-05-22 21:04:47 +00:00
|
|
|
if "csrf-token" not in request.form or request.form['csrf-token'] != session['csrf-token']:
|
|
|
|
return abort(418, f"u want tea")
|
2020-05-15 17:23:42 +00:00
|
|
|
|
2021-02-18 02:50:17 +00:00
|
|
|
if 'action' not in request.form:
|
|
|
|
return abort(400, "action is required")
|
|
|
|
|
|
|
|
if request.form['action'] == "start":
|
|
|
|
current_app.config["HUB_MODEL"].vm_state_command(email=session['account'], id=id, command="start")
|
|
|
|
|
|
|
|
vm["state"] = "starting"
|
|
|
|
return render_template("capsul-detail.html", vm=vm)
|
|
|
|
elif request.form['action'] == "delete":
|
|
|
|
if 'are_you_sure' not in request.form or not request.form['are_you_sure']:
|
|
|
|
return render_template(
|
|
|
|
"capsul-detail.html",
|
|
|
|
csrf_token = session["csrf-token"],
|
|
|
|
vm=vm,
|
|
|
|
delete=True
|
|
|
|
)
|
|
|
|
else:
|
|
|
|
current_app.logger.info(f"deleting {vm['id']} per user request ({session['account']})")
|
|
|
|
current_app.config["HUB_MODEL"].destroy(email=session['account'], id=id)
|
|
|
|
get_model().delete_vm(email=session['account'], id=id)
|
|
|
|
|
|
|
|
return render_template("capsul-detail.html", vm=vm, deleted=True)
|
|
|
|
elif request.form['action'] == "force-stop":
|
|
|
|
if 'are_you_sure' not in request.form or not request.form['are_you_sure']:
|
|
|
|
return render_template(
|
|
|
|
"capsul-detail.html",
|
|
|
|
csrf_token = session["csrf-token"],
|
|
|
|
vm=vm,
|
|
|
|
force_stop=True,
|
|
|
|
)
|
|
|
|
else:
|
|
|
|
current_app.logger.info(f"force stopping {vm['id']} per user request ({session['account']})")
|
|
|
|
current_app.config["HUB_MODEL"].vm_state_command(email=session['account'], id=id, command="force-stop")
|
|
|
|
|
|
|
|
vm["state"] = "stopped"
|
|
|
|
return render_template(
|
|
|
|
"capsul-detail.html",
|
|
|
|
csrf_token = session["csrf-token"],
|
|
|
|
vm=vm,
|
|
|
|
durations=list(map(lambda x: x.strip("_"), metric_durations.keys())),
|
|
|
|
duration=duration
|
|
|
|
)
|
2020-05-15 17:23:42 +00:00
|
|
|
else:
|
2021-02-18 02:50:17 +00:00
|
|
|
return abort(400, "action must be either delete, force-stop, or start")
|
2020-05-15 17:23:42 +00:00
|
|
|
|
|
|
|
|
|
|
|
else:
|
2021-01-30 07:39:48 +00:00
|
|
|
needs_ssh_host_keys = "ssh_host_keys" not in vm or len(vm["ssh_host_keys"]) == 0
|
2021-01-30 08:10:22 +00:00
|
|
|
|
2021-01-30 07:39:48 +00:00
|
|
|
vm_from_virt_model = double_check_capsul_address(vm["id"], vm["ipv4"], needs_ssh_host_keys)
|
2021-01-30 08:10:22 +00:00
|
|
|
|
|
|
|
if vm_from_virt_model is not None:
|
|
|
|
vm["ipv4"] = vm_from_virt_model.ipv4
|
2021-02-18 02:50:17 +00:00
|
|
|
vm["state"] = vm_from_virt_model.state
|
2021-01-30 08:10:22 +00:00
|
|
|
if needs_ssh_host_keys:
|
|
|
|
vm["ssh_host_keys"] = vm_from_virt_model.ssh_host_keys
|
2021-02-18 03:10:20 +00:00
|
|
|
else:
|
|
|
|
vm["state"] = "unknown"
|
2021-02-18 02:50:17 +00:00
|
|
|
|
|
|
|
if vm["state"] == "running" and not vm["ipv4"]:
|
|
|
|
vm["state"] = "starting"
|
2020-05-15 17:23:42 +00:00
|
|
|
|
|
|
|
return render_template(
|
|
|
|
"capsul-detail.html",
|
2020-05-22 21:04:47 +00:00
|
|
|
csrf_token = session["csrf-token"],
|
|
|
|
vm=vm,
|
2020-05-15 17:23:42 +00:00
|
|
|
durations=list(map(lambda x: x.strip("_"), metric_durations.keys())),
|
|
|
|
duration=duration
|
|
|
|
)
|
2020-05-11 16:57:39 +00:00
|
|
|
|
2021-07-08 18:04:27 +00:00
|
|
|
def _create(vm_sizes, operating_systems, public_keys_for_account, server_data):
|
|
|
|
errors = list()
|
|
|
|
|
|
|
|
size = server_data.get("size")
|
|
|
|
os = server_data.get("os")
|
|
|
|
posted_keys_count = int(server_data.get("ssh_authorized_key_count"))
|
|
|
|
|
|
|
|
if not size:
|
|
|
|
errors.append("Size is required")
|
|
|
|
elif size not in vm_sizes:
|
|
|
|
errors.append(f"Invalid size {size}")
|
|
|
|
|
|
|
|
if not os:
|
|
|
|
errors.append("OS is required")
|
|
|
|
elif os not in operating_systems:
|
|
|
|
errors.append(f"Invalid os {os}")
|
|
|
|
|
|
|
|
posted_keys = list()
|
|
|
|
|
|
|
|
if posted_keys_count > 1000:
|
|
|
|
errors.append("something went wrong with ssh keys")
|
|
|
|
else:
|
|
|
|
for i in range(0, posted_keys_count):
|
|
|
|
if f"ssh_key_{i}" in server_data:
|
|
|
|
posted_name = server_data.get(f"ssh_key_{i}")
|
|
|
|
key = None
|
|
|
|
for x in public_keys_for_account:
|
|
|
|
if x['name'] == posted_name:
|
|
|
|
key = x
|
|
|
|
if key:
|
|
|
|
posted_keys.append(key)
|
|
|
|
else:
|
|
|
|
errors.append(f"SSH Key \"{posted_name}\" doesn't exist")
|
|
|
|
|
|
|
|
if len(posted_keys) == 0:
|
|
|
|
errors.append("At least one SSH Public Key is required")
|
|
|
|
|
|
|
|
capacity_avaliable = current_app.config["HUB_MODEL"].capacity_avaliable(
|
|
|
|
vm_sizes[size]['memory_mb']*1024*1024
|
|
|
|
)
|
|
|
|
|
|
|
|
if not capacity_avaliable:
|
|
|
|
errors.append("""
|
|
|
|
host(s) at capacity. no capsuls can be created at this time. sorry.
|
|
|
|
""")
|
|
|
|
|
|
|
|
if len(errors) == 0:
|
2021-07-11 10:35:35 +00:00
|
|
|
id = make_capsul_id()
|
2021-07-08 18:04:27 +00:00
|
|
|
get_model().create_vm(
|
|
|
|
email=session["account"],
|
|
|
|
id=id,
|
|
|
|
size=size,
|
|
|
|
os=os,
|
|
|
|
ssh_authorized_keys=list(map(lambda x: x["name"], posted_keys))
|
|
|
|
)
|
|
|
|
current_app.config["HUB_MODEL"].create(
|
|
|
|
email = session["account"],
|
|
|
|
id=id,
|
|
|
|
template_image_file_name=operating_systems[os]['template_image_file_name'],
|
|
|
|
vcpus=vm_sizes[size]['vcpus'],
|
|
|
|
memory_mb=vm_sizes[size]['memory_mb'],
|
|
|
|
ssh_authorized_keys=list(map(lambda x: x["content"], posted_keys))
|
|
|
|
)
|
|
|
|
return id, errors
|
|
|
|
|
|
|
|
return None, errors
|
2020-05-11 06:47:14 +00:00
|
|
|
|
|
|
|
@bp.route("/create", methods=("GET", "POST"))
|
|
|
|
@account_required
|
|
|
|
def create():
|
2020-05-12 01:34:12 +00:00
|
|
|
vm_sizes = get_model().vm_sizes_dict()
|
|
|
|
operating_systems = get_model().operating_systems_dict()
|
2021-01-30 07:39:48 +00:00
|
|
|
public_keys_for_account = get_model().list_ssh_public_keys_for_account(session["account"])
|
2020-05-15 04:40:27 +00:00
|
|
|
account_balance = get_account_balance(get_vms(), get_payments(), datetime.utcnow())
|
2021-01-02 23:10:01 +00:00
|
|
|
capacity_avaliable = current_app.config["HUB_MODEL"].capacity_avaliable(512*1024*1024)
|
2020-05-11 06:47:14 +00:00
|
|
|
|
|
|
|
if request.method == "POST":
|
2020-05-22 21:04:47 +00:00
|
|
|
if "csrf-token" not in request.form or request.form['csrf-token'] != session['csrf-token']:
|
|
|
|
return abort(418, f"u want tea")
|
2021-07-08 18:04:27 +00:00
|
|
|
id, errors = _create(
|
|
|
|
vm_sizes,
|
|
|
|
operating_systems,
|
|
|
|
public_keys_for_account,
|
|
|
|
request.form)
|
|
|
|
if len(errors) == 0:
|
2021-07-11 21:47:12 +00:00
|
|
|
for error in errors:
|
|
|
|
flash(error)
|
2020-05-17 04:02:02 +00:00
|
|
|
return redirect(f"{url_for('console.index')}?created={id}")
|
2020-05-12 03:59:36 +00:00
|
|
|
|
|
|
|
affordable_vm_sizes = dict()
|
|
|
|
for key, vm_size in vm_sizes.items():
|
2021-01-30 21:27:24 +00:00
|
|
|
# if a user deposits $7.50 and then creates an f1-s vm which costs 7.50 a month,
|
|
|
|
# then they have to delete the vm and re-create it, they will not be able to, they will have to pay again.
|
|
|
|
# so for UX it makes a lot of sense to give a small margin of 25 cents for usability sake
|
|
|
|
if vm_size["dollars_per_month"] <= account_balance+0.25:
|
2020-05-12 03:59:36 +00:00
|
|
|
affordable_vm_sizes[key] = vm_size
|
|
|
|
|
2020-05-13 18:56:43 +00:00
|
|
|
if not capacity_avaliable:
|
2020-05-16 04:19:01 +00:00
|
|
|
current_app.logger.warning(f"when capsul capacity is restored, send an email to {session['account']}")
|
2020-05-13 18:56:43 +00:00
|
|
|
|
2020-05-11 06:47:14 +00:00
|
|
|
return render_template(
|
2020-05-11 16:57:39 +00:00
|
|
|
"create-capsul.html",
|
2020-05-22 21:04:47 +00:00
|
|
|
csrf_token = session["csrf-token"],
|
2020-05-13 18:56:43 +00:00
|
|
|
capacity_avaliable=capacity_avaliable,
|
2020-05-12 05:45:37 +00:00
|
|
|
account_balance=format(account_balance, '.2f'),
|
2021-02-16 01:44:26 +00:00
|
|
|
ssh_authorized_keys=public_keys_for_account,
|
|
|
|
ssh_authorized_key_count=len(public_keys_for_account),
|
2021-01-30 07:39:48 +00:00
|
|
|
no_ssh_public_keys=len(public_keys_for_account) == 0,
|
2020-05-11 06:47:14 +00:00
|
|
|
operating_systems=operating_systems,
|
2020-05-12 03:59:36 +00:00
|
|
|
cant_afford=len(affordable_vm_sizes) == 0,
|
|
|
|
vm_sizes=affordable_vm_sizes
|
2020-05-11 06:47:14 +00:00
|
|
|
)
|
|
|
|
|
2021-07-08 18:04:27 +00:00
|
|
|
@bp.route("/keys", methods=("GET", "POST"))
|
2020-05-11 21:24:37 +00:00
|
|
|
@account_required
|
2021-07-08 18:04:27 +00:00
|
|
|
def ssh_api_keys():
|
2020-05-11 21:24:37 +00:00
|
|
|
errors = list()
|
2020-05-17 19:19:10 +00:00
|
|
|
|
2021-07-08 18:04:27 +00:00
|
|
|
token = None
|
|
|
|
|
2020-05-11 21:24:37 +00:00
|
|
|
if request.method == "POST":
|
2020-05-22 21:04:47 +00:00
|
|
|
if "csrf-token" not in request.form or request.form['csrf-token'] != session['csrf-token']:
|
|
|
|
return abort(418, f"u want tea")
|
|
|
|
|
2021-07-08 18:04:27 +00:00
|
|
|
action = request.form["action"]
|
|
|
|
|
|
|
|
if action == 'upload_ssh_key':
|
|
|
|
content = None
|
2021-05-12 17:21:26 +00:00
|
|
|
content = request.form["content"].replace("\r", " ").replace("\n", " ").strip()
|
2021-07-08 18:04:27 +00:00
|
|
|
|
|
|
|
name = request.form["name"]
|
|
|
|
if not name or len(name.strip()) < 1:
|
2021-05-12 17:21:26 +00:00
|
|
|
parts = re.split(" +", content)
|
2020-05-12 05:58:17 +00:00
|
|
|
if len(parts) > 2 and len(parts[2].strip()) > 0:
|
2020-05-22 20:20:26 +00:00
|
|
|
name = parts[2].strip()
|
2020-05-12 05:58:17 +00:00
|
|
|
else:
|
2020-05-22 20:20:26 +00:00
|
|
|
name = parts[0].strip()
|
2020-05-12 05:58:17 +00:00
|
|
|
else:
|
|
|
|
errors.append("Name is required")
|
2021-07-08 18:04:27 +00:00
|
|
|
if not re.match(r"^[0-9A-Za-z_@:. -]+$", name):
|
|
|
|
errors.append(f"Key name '{name}' must match \"^[0-9A-Za-z_@:. -]+$\"")
|
2020-05-11 21:24:37 +00:00
|
|
|
|
|
|
|
if not content or len(content.strip()) < 1:
|
|
|
|
errors.append("Content is required")
|
|
|
|
else:
|
2021-05-12 16:48:55 +00:00
|
|
|
if not re.match(r"^(ssh|ecdsa)-[0-9A-Za-z+/_=@:. -]+$", content):
|
|
|
|
errors.append("Content must match \"^(ssh|ecdsa)-[0-9A-Za-z+/_=@:. -]+$\"")
|
2020-05-11 21:24:37 +00:00
|
|
|
|
2020-05-12 01:34:12 +00:00
|
|
|
if get_model().ssh_public_key_name_exists(session["account"], name):
|
2020-05-11 21:24:37 +00:00
|
|
|
errors.append("A key with that name already exists")
|
|
|
|
|
|
|
|
if len(errors) == 0:
|
2020-05-12 01:34:12 +00:00
|
|
|
get_model().create_ssh_public_key(session["account"], name, content)
|
2020-05-11 21:24:37 +00:00
|
|
|
|
2021-07-08 18:04:27 +00:00
|
|
|
elif action == "delete_ssh_key":
|
|
|
|
get_model().delete_ssh_public_key(session["account"], name)
|
2020-05-11 21:24:37 +00:00
|
|
|
|
2021-07-08 18:04:27 +00:00
|
|
|
elif action == "generate_api_token":
|
|
|
|
name = request.form["name"]
|
|
|
|
if name == '':
|
|
|
|
name = datetime.utcnow().strftime('%y-%m-%d %H:%M:%S')
|
|
|
|
token = b64encode(
|
|
|
|
get_model().generate_api_token(session["account"], name).encode('utf-8')
|
|
|
|
).decode('utf-8')
|
|
|
|
|
|
|
|
elif action == "delete_api_token":
|
|
|
|
get_model().delete_api_token(session["account"], request.form["id"])
|
2020-05-11 21:24:37 +00:00
|
|
|
|
|
|
|
for error in errors:
|
|
|
|
flash(error)
|
|
|
|
|
2021-07-08 18:04:27 +00:00
|
|
|
ssh_keys_list=list(map(
|
2020-05-11 21:24:37 +00:00
|
|
|
lambda x: dict(name=x['name'], content=f"{x['content'][:20]}...{x['content'][len(x['content'])-20:]}"),
|
2020-05-12 01:34:12 +00:00
|
|
|
get_model().list_ssh_public_keys_for_account(session["account"])
|
2020-05-11 21:24:37 +00:00
|
|
|
))
|
|
|
|
|
2021-07-08 18:04:27 +00:00
|
|
|
api_tokens_list = get_model().list_api_tokens(session["account"])
|
|
|
|
|
2020-05-22 21:04:47 +00:00
|
|
|
return render_template(
|
2021-07-08 18:04:27 +00:00
|
|
|
"keys.html",
|
2020-05-22 21:04:47 +00:00
|
|
|
csrf_token = session["csrf-token"],
|
2021-07-08 18:04:27 +00:00
|
|
|
api_tokens=api_tokens_list,
|
|
|
|
ssh_public_keys=ssh_keys_list,
|
|
|
|
generated_api_token=token,
|
2020-05-22 21:04:47 +00:00
|
|
|
)
|
2020-05-11 21:24:37 +00:00
|
|
|
|
2020-05-12 01:34:12 +00:00
|
|
|
def get_vms():
|
|
|
|
if 'user_vms' not in g:
|
|
|
|
g.user_vms = get_model().list_vms_for_account(session["account"])
|
|
|
|
return g.user_vms
|
2020-05-11 21:24:37 +00:00
|
|
|
|
2020-05-12 01:34:12 +00:00
|
|
|
def get_payments():
|
|
|
|
if 'user_payments' not in g:
|
|
|
|
g.user_payments = get_model().list_payments_for_account(session["account"])
|
|
|
|
return g.user_payments
|
|
|
|
|
2020-05-12 05:45:37 +00:00
|
|
|
|
|
|
|
average_number_of_days_in_a_month = 30.44
|
|
|
|
|
2020-05-17 04:24:11 +00:00
|
|
|
def get_vm_months_float(vm, as_of):
|
|
|
|
end_datetime = vm["deleted"] if vm["deleted"] else as_of
|
|
|
|
days = float((end_datetime - vm["created"]).total_seconds())/float(60*60*24)
|
|
|
|
if days < 1:
|
|
|
|
days = float(1)
|
|
|
|
return days / average_number_of_days_in_a_month
|
|
|
|
|
2020-05-15 04:40:27 +00:00
|
|
|
def get_account_balance(vms, payments, as_of):
|
2020-05-12 01:34:12 +00:00
|
|
|
vm_cost_dollars = 0.0
|
2020-05-15 04:40:27 +00:00
|
|
|
for vm in vms:
|
2020-05-17 04:24:11 +00:00
|
|
|
vm_months = get_vm_months_float(vm, as_of)
|
2020-05-12 03:59:36 +00:00
|
|
|
vm_cost_dollars += vm_months * float(vm["dollars_per_month"])
|
2020-05-12 01:34:12 +00:00
|
|
|
|
2020-05-15 04:40:27 +00:00
|
|
|
payment_dollars_total = float( sum(map(lambda x: 0 if x["invalidated"] else x["dollars"], payments)) )
|
2020-05-12 01:34:12 +00:00
|
|
|
|
2020-05-12 03:59:36 +00:00
|
|
|
return payment_dollars_total - vm_cost_dollars
|
2020-05-12 01:34:12 +00:00
|
|
|
|
|
|
|
@bp.route("/account-balance")
|
2020-05-11 06:47:14 +00:00
|
|
|
@account_required
|
2020-05-12 01:34:12 +00:00
|
|
|
def account_balance():
|
2020-05-17 03:04:51 +00:00
|
|
|
payment_sessions = get_model().list_payment_sessions_for_account(session['account'])
|
|
|
|
for payment_session in payment_sessions:
|
|
|
|
if payment_session['type'] == 'btcpay':
|
|
|
|
poll_btcpay_session(payment_session['id'])
|
|
|
|
|
2020-05-12 01:34:12 +00:00
|
|
|
payments = get_payments()
|
2020-05-15 23:18:19 +00:00
|
|
|
vms = get_vms()
|
|
|
|
balance_1w = get_account_balance(vms, payments, datetime.utcnow() + timedelta(days=7))
|
|
|
|
balance_1d = get_account_balance(vms, payments, datetime.utcnow() + timedelta(days=1))
|
|
|
|
balance_now = get_account_balance(vms, payments, datetime.utcnow())
|
|
|
|
|
|
|
|
warning_index = -1
|
|
|
|
warning_text = ""
|
|
|
|
warnings = cli.get_warnings_list()
|
|
|
|
|
|
|
|
for i in range(0, len(warnings)):
|
|
|
|
if warnings[i]['get_active'](balance_1w, balance_1d, balance_now):
|
|
|
|
warning_index = i
|
|
|
|
if warning_index > -1:
|
|
|
|
pluralize_capsul = "s" if len(vms) > 1 else ""
|
|
|
|
warning_id = warnings[warning_index]['id']
|
|
|
|
warning_text = cli.get_warning_headline(warning_id, pluralize_capsul)
|
2020-05-12 01:34:12 +00:00
|
|
|
|
2020-05-12 05:45:37 +00:00
|
|
|
vms_billed = list()
|
|
|
|
|
|
|
|
for vm in get_vms():
|
2020-05-17 04:24:11 +00:00
|
|
|
vm_months = get_vm_months_float(vm, datetime.utcnow())
|
2020-05-12 05:45:37 +00:00
|
|
|
vms_billed.append(dict(
|
|
|
|
id=vm["id"],
|
|
|
|
dollars_per_month=vm["dollars_per_month"],
|
|
|
|
created=vm["created"].strftime("%b %d %Y"),
|
|
|
|
deleted=vm["deleted"].strftime("%b %d %Y") if vm["deleted"] else "N/A",
|
|
|
|
months=format(vm_months, '.3f'),
|
|
|
|
dollars=format(vm_months * float(vm["dollars_per_month"]), '.2f')
|
|
|
|
))
|
|
|
|
|
2020-05-12 03:59:36 +00:00
|
|
|
return render_template(
|
|
|
|
"account-balance.html",
|
2020-05-12 05:45:37 +00:00
|
|
|
has_vms=len(vms_billed)>0,
|
|
|
|
vms_billed=vms_billed,
|
2020-05-15 23:18:19 +00:00
|
|
|
warning_text=warning_text,
|
2020-05-14 23:03:00 +00:00
|
|
|
payments=list(map(
|
|
|
|
lambda x: dict(
|
|
|
|
dollars=x["dollars"],
|
|
|
|
class_name="invalidated" if x["invalidated"] else "",
|
|
|
|
created=x["created"].strftime("%b %d %Y")
|
|
|
|
),
|
|
|
|
payments
|
|
|
|
)),
|
2020-05-12 05:45:37 +00:00
|
|
|
has_payments=len(payments)>0,
|
2020-05-15 23:18:19 +00:00
|
|
|
account_balance=format(balance_now, '.2f')
|
2020-05-17 19:19:10 +00:00
|
|
|
)
|