-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.py
More file actions
117 lines (91 loc) · 4.05 KB
/
app.py
File metadata and controls
117 lines (91 loc) · 4.05 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
import os
import shutil
import signal
import sqlite3
import sys
from typing import Optional
from flask import Flask
import job_manager
from job_manager import JobStatus
from queue_executor import QueueExecutor
from routes import route_app
# Static variables
JOBS_FOLDER_PATH = os.getenv("CSR_JOBS", os.getcwd() + "/jobs/")
SQLITE_DB_PATH = os.path.join(os.getenv("CSR_DB", os.getcwd()), "sqlite.db")
# Set to none for scope, will get set in #startup_tasks
queue_thread: Optional[QueueExecutor] = None
def cleanup():
""" Cleanup the database and jobs folder by removing invalid jobs
If a job is in the database but some/all of its files are missing, remove from DB and delete files, if any
If a job is in the jobs folder but not in the DB, don't delete the folder but log that we found a bad folder
"""
print("Running cleanup operations.")
with sqlite3.connect(SQLITE_DB_PATH) as conn:
db = conn.cursor()
# folder names to DB - No deletion
folder_names = os.listdir(JOBS_FOLDER_PATH)
for job_id in folder_names:
# If folder is empty, delete it (since no data will be lost)
folder_path = os.path.join(JOBS_FOLDER_PATH, job_id)
if len(os.listdir(folder_path)) == 0:
os.rmdir(folder_path)
print("An empty folder named {} was found in the jobs folder and was deleted.".format(job_id))
continue
db.execute("SELECT 1 FROM jobs WHERE id=?", (job_id,))
rows = db.fetchall()
if len(rows) == 0:
# No jobs found in db with that id
print("A folder named {} was found in the jobs folder, but no job with that id could be found in the "
"database.".format(job_id))
# DB to folder - delete if missing file(s)
db.execute("SELECT id,status FROM jobs")
job_ids = db.fetchall()
for job_id, status in job_ids:
status = JobStatus(status)
valid = True
folder_path = os.path.join(JOBS_FOLDER_PATH, job_id)
if not os.path.isdir(folder_path):
valid = False
conf_path = os.path.join(folder_path, "{}.conf".format(job_id))
if not os.path.isfile(conf_path):
valid = False
if status == JobStatus.GENERATED:
key_path = os.path.join(folder_path, "{}.key".format(job_id))
if not os.path.isfile(key_path):
valid = False
csr_path = os.path.join(folder_path, "{}.csr".format(job_id))
if not os.path.isfile(csr_path):
valid = False
if not valid:
# Missing at least one file, delete from DB and remove folder if present
db.execute("DELETE FROM jobs WHERE id=?", (job_id,))
conn.commit()
shutil.rmtree(folder_path, ignore_errors=True) # Remove folder and sub-files
print("Job {} removed from database due to missing files.".format(job_id))
print("Completed cleanup operations.")
def stop_app(_, __):
print("Stopping the app...")
if queue_thread: # Check if queue_thread exists, sigint could be received before thread is created
queue_thread.stop()
sys.exit(0)
def startup_tasks():
# Create the database if it doesn't exist
job_manager.initialize_db()
# Start the queue executor thread
global queue_thread
queue_thread = QueueExecutor()
queue_thread.start()
# Run the cleanup ops
cleanup()
def create_app():
app = Flask(__name__)
app.register_blueprint(route_app) # Register the routes from the routes.py file
if app.env == "development":
# If app is in development, call startup_tasks manually as there is no gunicorn to call it for us
startup_tasks()
# Also register the SIGINT signal to stopping the queue thread (only needed when not running w/ gunicorn)
signal.signal(signal.SIGINT, stop_app)
return app
if __name__ == '__main__':
app = create_app()
app.run()