image allocation

This commit is contained in:
Mateusz779 2023-04-14 11:24:32 +02:00
parent 06e66b0bbc
commit 3e6e22e108
2 changed files with 81 additions and 6 deletions

34
app.py
View File

@ -1,3 +1,4 @@
import datetime
from time import sleep from time import sleep
from flask import Flask, flash, make_response, redirect, send_file, jsonify, request, render_template, url_for from flask import Flask, flash, make_response, redirect, send_file, jsonify, request, render_template, url_for
import db import db
@ -19,21 +20,30 @@ ssh_thread = threading.Thread(target=ssh_thread_function)
ssh_thread.start() ssh_thread.start()
class PingThread(threading.Thread): class PingThread(threading.Thread):
def __init__(self, ip): def __init__(self, ip, id):
super(PingThread, self).__init__() super(PingThread, self).__init__()
self.Ip = ip self.Ip = ip
self.Id = id
def run(self): def run(self):
utils.ping_client(self.ip) if utils.ping_client(self.Ip) == False:
date = db.get_image_allocation_time_id(self.Id)
if date is None:
return
delta = datetime.datetime.now() - date
if delta.total_seconds() > 30:
db.del_image_allocation_id(self.Id)
else:
db.update_image_allocation_time(self.Id)
def check_allocation_thread_function(): def check_allocation_thread_function():
while True: while True:
ids = db.get_image_allocation_all() ids = db.get_image_allocation_all()
for x in ids: for x in ids:
ip = db.get_image_allocation_clientip_id(x) ip = db.get_image_allocation_clientip_id(x)
ping_thread = PingThread(ip) ping_thread = PingThread(ip, x)
ping_thread.start() ping_thread.start()
sleep(15) sleep(10)
allocation_thread = threading.Thread(target=check_allocation_thread_function) allocation_thread = threading.Thread(target=check_allocation_thread_function)
allocation_thread.start() allocation_thread.start()
@ -156,7 +166,21 @@ def add_image():
@app.route("/api/getconf") @app.route("/api/getconf")
def get_image(): def get_image():
filename = db.get_conf_image(request.headers['token']) try:
filename = db.get_conf_image(request.headers['token'])
except:
pass
try:
date = db.get_image_allocation_time(request.headers['token'])
if date is not None:
delta = datetime.datetime.now() - date
if delta.total_seconds() > 30:
db.del_image_allocation_token(request.headers['token'])
else:
filename = None
except:
pass
if filename is None or filename == "": if filename is None or filename == "":
filename = "default.squashfs" filename = "default.squashfs"

51
db.py
View File

@ -183,6 +183,17 @@ def get_image_allocation_time_imageid(image_id):
except: except:
return None return None
def get_image_allocation_time_id(id):
connect()
with get_cur() as cur:
cur.execute("""
SELECT last_access_time FROM image_allocation WHERE id = %s
""",(id,))
try:
return cur.fetchone()[0]
except:
return None
def get_image_allocation_clientip(token): def get_image_allocation_clientip(token):
id_image = get_conf_id(token) id_image = get_conf_id(token)
if id_image is None: if id_image is None:
@ -222,3 +233,43 @@ def set_image_allocation(token, client_ip):
""",(id_image,client_ip,)) """,(id_image,client_ip,))
conn.commit() conn.commit()
return token return token
def del_image_allocation_token(token):
id_image = get_conf_id(token)
if id_image is None:
return None
connect()
with get_cur() as cur:
cur.execute("""
DELETE FROM image_allocation WHERE id_image = %s
""",(id_image,))
try:
conn.commit()
return True
except:
return None
def del_image_allocation_id(id):
connect()
with get_cur() as cur:
cur.execute("""
DELETE FROM image_allocation WHERE id = %s
""",(id,))
try:
conn.commit()
return True
except:
return None
def update_image_allocation_time(id):
connect()
with get_cur() as cur:
cur.execute("""
UPDATE SET last_access_time = NOW() FROM image_allocation WHERE id = %s
""",(id,))
try:
conn.commit()
return True
except:
return None