create capsul is working

This commit is contained in:
2021-01-04 15:02:56 -06:00
parent 44e918a974
commit 4833c6250b
6 changed files with 95 additions and 55 deletions

View File

@ -13,6 +13,7 @@ class DBModel:
def __init__(self, connection, cursor):
self.connection = connection
self.cursor = cursor
self.cursor.execute("SET TRANSACTION ISOLATION LEVEL SERIALIZABLE;")
# ------ LOGIN ---------
@ -300,18 +301,29 @@ class DBModel:
self.connection.commit()
return operation_id
def update_host_operation(self, host_id: str, operation_id: int, assignment_status: str):
self.cursor.execute(
"UPDATE host_operation SET assignment_status = %s WHERE host = %s AND operation = %s",
(assignment_status, host_id, operation_id)
)
def update_host_operation(self, host_id: str, operation_id: int, assignment_status: str, result: str):
if assignment_status and not result:
self.cursor.execute(
"UPDATE host_operation SET assignment_status = %s, assigned = NOW() WHERE host = %s AND operation = %s",
(assignment_status, host_id, operation_id)
)
elif not assignment_status and result:
self.cursor.execute(
"UPDATE host_operation SET results = %s, completed = NOW() WHERE host = %s AND operation = %s",
(result, host_id, operation_id)
)
elif assignment_status and result:
self.cursor.execute(
"UPDATE host_operation SET assignment_status = %s, assigned = NOW(), results = %s, completed = NOW() WHERE host = %s AND operation = %s",
(assignment_status, result, host_id, operation_id)
)
self.connection.commit()
def host_of_capsul(self, capsul_id: str):
self.cursor.execute("SELECT host from vms where id = %s", (capsul_id,))
def host_of_capsul(self, capsul_id: str) -> OnlineHost:
self.cursor.execute("SELECT hosts.id, hosts.https_url from vms JOIN hosts on hosts.id = vms.host where vms.id = %s", (capsul_id,))
row = self.cursor.fetchone()
if row:
return row[0]
return OnlineHost(row[0], row[1])
else:
return None
@ -320,8 +332,9 @@ class DBModel:
return len(self.cursor.fetchall()) != 0
def claim_operation(self, operation_id: int, host_id: str) -> bool:
# have to make a new cursor to set isolation level
# cursor = self.connection.cursor()
self.cursor.execute("""
SET TRANSACTION ISOLATION LEVEL SERIALIZABLE;
BEGIN TRANSACTION;
UPDATE host_operation SET assignment_status = 'assigned'
WHERE host = %s AND operation = %s AND operation != (
@ -336,6 +349,7 @@ class DBModel:
to_return = self.cursor.rowcount != 0
self.connection.commit()
#cursor.close()
return to_return