desired_state column to be updated when the user starts/stops vm

This commit is contained in:
forest 2021-12-17 17:38:28 -06:00
parent 75b5b82c28
commit 5637da73ec
3 changed files with 10 additions and 8 deletions

View File

@ -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

View File

@ -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"

View File

@ -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("""