forked from 3wordchant/capsul-flask
fixing capsul creation after I broke it with the pre-allocated IP
address changes
This commit is contained in:
@ -1,4 +1,6 @@
|
||||
|
||||
import re
|
||||
|
||||
# I was never able to get this type hinting to work correctly
|
||||
# from psycopg2.extensions import connection as Psycopg2Connection, cursor as Psycopg2Cursor
|
||||
from nanoid import generate
|
||||
@ -61,7 +63,13 @@ class DBModel:
|
||||
if host_id is None:
|
||||
self.cursor.execute(query)
|
||||
else:
|
||||
self.cursor.execute(f"{query} AND host = %s", (host_id))
|
||||
if not re.match(r"^[a-zA-Z0-9_-]+$", host_id):
|
||||
raise ValueError(f"host_id \"{host_id}\" must match \"^[a-zA-Z0-9_-]+\"")
|
||||
|
||||
# I kept getting "TypeError: not all arguments converted during string formatting"
|
||||
# when I was trying to mix python string templating with psycopg2 safe parameter passing.
|
||||
# so i just did all of it in python and check the user-provided data for safety myself (no sql injection).
|
||||
self.cursor.execute(f"{query} AND host = '{host_id}'")
|
||||
|
||||
hosts = dict()
|
||||
for row in self.cursor.fetchall():
|
||||
@ -150,12 +158,12 @@ class DBModel:
|
||||
)
|
||||
self.connection.commit()
|
||||
|
||||
def create_vm(self, email, id, size, os, ssh_authorized_keys):
|
||||
def create_vm(self, email, id, size, os, host, network_name, public_ipv4, ssh_authorized_keys):
|
||||
self.cursor.execute("""
|
||||
INSERT INTO vms (email, id, size, os)
|
||||
VALUES (%s, %s, %s, %s)
|
||||
INSERT INTO vms (email, id, size, os, host, network_name, public_ipv4)
|
||||
VALUES (%s, %s, %s, %s, %s, %s, %s)
|
||||
""",
|
||||
(email, id, size, os)
|
||||
(email, id, size, os, host, network_name, public_ipv4)
|
||||
)
|
||||
|
||||
for ssh_authorized_key in ssh_authorized_keys:
|
||||
@ -330,7 +338,13 @@ class DBModel:
|
||||
if host_id is None:
|
||||
self.cursor.execute(query)
|
||||
else:
|
||||
self.cursor.execute(f"{query} WHERE hosts.id = %s", (host_id))
|
||||
if not re.match(r"^[a-zA-Z0-9_-]+$", host_id):
|
||||
raise ValueError(f"host_id \"{host_id}\" must match \"^[a-zA-Z0-9_-]+\"")
|
||||
|
||||
# I kept getting "TypeError: not all arguments converted during string formatting"
|
||||
# when I was trying to mix python query string templating with psycopg2 safe parameter passing.
|
||||
# so i just did all of it in python and check the user-provided data for safety myself (no sql injection).
|
||||
self.cursor.execute(f"{query} WHERE hosts.id = '{host_id}'")
|
||||
|
||||
hosts = dict()
|
||||
for row in self.cursor.fetchall():
|
||||
|
Reference in New Issue
Block a user