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 import os
from werkzeug.utils import secure_filename from werkzeug.utils import secure_filename
import subprocess import subprocess
import threading
import utils import utils
import shutil import shutil
import config
app = Flask(__name__) app = Flask(__name__)
app.config['UPLOAD_FOLDER'] = "squash" app.config['UPLOAD_FOLDER'] = "squash"
app.config['MAX_CONTENT_LENGTH'] = 1024 * 1024 * 512 #512MB app.config['MAX_CONTENT_LENGTH'] = 1024 * 1024 * 512 #512MB
def ssh_thread_function(): utils.init_threads()
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()
@app.route('/') @app.route('/')
def main(): def main():
@ -184,6 +150,9 @@ def get_image():
pass pass
if filename is None or filename == "": if filename is None or filename == "":
filename = "default.squashfs" filename = config.default_file
return send_file(os.path.join(app.config['UPLOAD_FOLDER'], filename)) return send_file(os.path.join(app.config['UPLOAD_FOLDER'], filename))
if __name__ == '__main__':
app.run(host="0.0.0.0")

View File

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

View File

@ -1,8 +1,13 @@
import datetime
import hashlib import hashlib
import secrets import secrets
import string import string
import random import random
import os import os
import subprocess
import threading
from time import sleep
import db
def generate_random_string(length): def generate_random_string(length):
letters = string.ascii_letters letters = string.ascii_letters
@ -21,4 +26,44 @@ def ping_client(ip):
if response == 0: if response == 0:
return True return True
else: else:
return False 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()