image allocation
This commit is contained in:
parent
06e66b0bbc
commit
3e6e22e108
32
app.py
32
app.py
@ -1,3 +1,4 @@
|
||||
import datetime
|
||||
from time import sleep
|
||||
from flask import Flask, flash, make_response, redirect, send_file, jsonify, request, render_template, url_for
|
||||
import db
|
||||
@ -19,21 +20,30 @@ ssh_thread = threading.Thread(target=ssh_thread_function)
|
||||
ssh_thread.start()
|
||||
|
||||
class PingThread(threading.Thread):
|
||||
def __init__(self, ip):
|
||||
def __init__(self, ip, id):
|
||||
super(PingThread, self).__init__()
|
||||
self.Ip = ip
|
||||
self.Id = id
|
||||
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():
|
||||
while True:
|
||||
ids = db.get_image_allocation_all()
|
||||
for x in ids:
|
||||
ip = db.get_image_allocation_clientip_id(x)
|
||||
ping_thread = PingThread(ip)
|
||||
ping_thread = PingThread(ip, x)
|
||||
ping_thread.start()
|
||||
|
||||
sleep(15)
|
||||
sleep(10)
|
||||
|
||||
allocation_thread = threading.Thread(target=check_allocation_thread_function)
|
||||
allocation_thread.start()
|
||||
@ -156,7 +166,21 @@ def add_image():
|
||||
|
||||
@app.route("/api/getconf")
|
||||
def get_image():
|
||||
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 == "":
|
||||
filename = "default.squashfs"
|
||||
|
||||
|
51
db.py
51
db.py
@ -183,6 +183,17 @@ def get_image_allocation_time_imageid(image_id):
|
||||
except:
|
||||
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):
|
||||
id_image = get_conf_id(token)
|
||||
if id_image is None:
|
||||
@ -222,3 +233,43 @@ def set_image_allocation(token, client_ip):
|
||||
""",(id_image,client_ip,))
|
||||
conn.commit()
|
||||
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
|
Loading…
Reference in New Issue
Block a user