code cleanup

This commit is contained in:
Mateusz779 2023-04-14 12:37:37 +02:00
parent 5788f1e1b5
commit 2a829396c2
3 changed files with 54 additions and 39 deletions

43
app.py
View File

@ -5,49 +5,15 @@ import db
import os
from werkzeug.utils import secure_filename
import subprocess
import threading
import utils
import shutil
import config
app = Flask(__name__)
app.config['UPLOAD_FOLDER'] = "squash"
app.config['MAX_CONTENT_LENGTH'] = 1024 * 1024 * 512 #512MB
def ssh_thread_function():
subprocess.run(['wssh','--fbidhttp=False'])
ssh_thread = threading.Thread(target=ssh_thread_function)
ssh_thread.start()
class PingThread(threading.Thread):
def __init__(self, ip, id):
super(PingThread, self).__init__()
self.Ip = ip
self.Id = id
def run(self):
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[0])
ping_thread = PingThread(ip, x[0])
ping_thread.start()
sleep(10)
allocation_thread = threading.Thread(target=check_allocation_thread_function)
allocation_thread.start()
utils.init_threads()
@app.route('/')
def main():
@ -184,6 +150,9 @@ def get_image():
pass
if filename is None or filename == "":
filename = "default.squashfs"
filename = config.default_file
return send_file(os.path.join(app.config['UPLOAD_FOLDER'], filename))
if __name__ == '__main__':
app.run(host="0.0.0.0")

View File

@ -3,3 +3,4 @@ host="localhost"
user="praktyki"
password="2a7driUITXFy73tO"
port="5432"
default_file = "default.squashfs"

View File

@ -1,8 +1,13 @@
import datetime
import hashlib
import secrets
import string
import random
import os
import subprocess
import threading
from time import sleep
import db
def generate_random_string(length):
letters = string.ascii_letters
@ -22,3 +27,43 @@ def ping_client(ip):
return True
else:
return False
def ssh_thread_function():
subprocess.run(['wssh','--fbidhttp=False'])
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[0])
ping_thread = PingThread(ip, x[0])
ping_thread.start()
sleep(10)
class PingThread(threading.Thread):
def __init__(self, ip, id):
super(PingThread, self).__init__()
self.Ip = ip
self.Id = id
def run(self):
if self.Ip is None:
return
if 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 init_threads():
ssh_thread = threading.Thread(target=ssh_thread_function)
ssh_thread.start()
allocation_thread = threading.Thread(target=check_allocation_thread_function)
allocation_thread.start()