From 5637da73ec74a705fc4a449f0dbd9e5e0f41d60c Mon Sep 17 00:00:00 2001 From: forest Date: Fri, 17 Dec 2021 17:38:28 -0600 Subject: [PATCH] desired_state column to be updated when the user starts/stops vm --- capsulflask/btcpay/client.py | 3 ++- capsulflask/console.py | 5 ++--- capsulflask/db_model.py | 10 ++++++---- 3 files changed, 10 insertions(+), 8 deletions(-) diff --git a/capsulflask/btcpay/client.py b/capsulflask/btcpay/client.py index 5b0f999..2fea8fa 100644 --- a/capsulflask/btcpay/client.py +++ b/capsulflask/btcpay/client.py @@ -5,7 +5,8 @@ import json import re class Client: - def __init__(self, api_uri="https://bitpay.com", insecure=False, pem=key_utils.generate_pem(), tokens={}): + # this api_url used to default to bitpay.com, I changed it because why not ;P + def __init__(self, api_uri="https://btcpay.cyberia.club", insecure=False, pem=key_utils.generate_pem(), tokens={}): self.uri = api_uri self.verify = not(insecure) self.pem = pem diff --git a/capsulflask/console.py b/capsulflask/console.py index f15907a..7b5feb8 100644 --- a/capsulflask/console.py +++ b/capsulflask/console.py @@ -30,9 +30,6 @@ def make_capsul_id(): def double_check_capsul_address(id, get_ssh_host_keys): try: result = current_app.config["HUB_MODEL"].get(id, get_ssh_host_keys) - # if result != None and result.ipv4 != None and result.ipv4 != ipv4: - # ipv4 = result.ipv4 - # get_model().update_vm_ip(email=session["account"], id=id, ipv4=result.ipv4) if result != None and result.ssh_host_keys != None and get_ssh_host_keys: get_model().update_vm_ssh_host_keys(email=session["account"], id=id, ssh_host_keys=result.ssh_host_keys) @@ -122,6 +119,7 @@ def detail(id): return abort(400, "action is required") if request.form['action'] == "start": + get_model().set_desired_state(email=session["account"], vm_id=id, desired_state="running") current_app.config["HUB_MODEL"].vm_state_command(email=session['account'], id=id, command="start") vm["state"] = "starting" @@ -150,6 +148,7 @@ def detail(id): ) else: current_app.logger.info(f"force stopping {vm['id']} per user request ({session['account']})") + get_model().set_desired_state(email=session["account"], vm_id=id, desired_state="shut off") current_app.config["HUB_MODEL"].vm_state_command(email=session['account'], id=id, command="force-stop") vm["state"] = "stopped" diff --git a/capsulflask/db_model.py b/capsulflask/db_model.py index 9fd0fe9..213cc78 100644 --- a/capsulflask/db_model.py +++ b/capsulflask/db_model.py @@ -86,6 +86,12 @@ class DBModel: return hosts + + def set_desired_state(self, email, vm_id, desired_state): + self.cursor.execute("UPDATE vms SET desired_state = %s WHERE email = %s AND id = %s", (desired_state, email, vm_id)) + self.connection.commit() + + def all_accounts_with_active_vms(self): self.cursor.execute("SELECT DISTINCT email FROM vms WHERE deleted IS NULL") return list(map(lambda x: x[0], self.cursor.fetchall())) @@ -144,10 +150,6 @@ class DBModel: self.cursor.fetchall() )) - def update_vm_ip(self, email, id, ipv4): - self.cursor.execute("UPDATE vms SET public_ipv4 = %s WHERE email = %s AND id = %s", (ipv4, email, id)) - self.connection.commit() - def update_vm_ssh_host_keys(self, email, id, ssh_host_keys): for key in ssh_host_keys: self.cursor.execute("""