forked from RhinoSecurityLabs/pacu
-
Notifications
You must be signed in to change notification settings - Fork 0
/
setup_database.py
48 lines (35 loc) · 1.6 KB
/
setup_database.py
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
import os
from core.base import Base, engine
from utils import set_sigint_handler
def setup_database_if_not_present(database_file_path, auto_proceed=True):
if os.path.exists(database_file_path):
return True
else:
print('No database found at {}'.format(database_file_path))
return attempt_to_create_database(database_file_path, auto_proceed)
def attempt_to_create_database(database_file_path, auto_proceed=True):
if auto_proceed:
proceed = 'y'
else:
print('A database will be created at the following location:\n {}'.format(database_file_path))
if os.path.exists(database_file_path):
question = 'A file already exists at this location.\nAre you sure you want to delete and recreate it? [y/n]\n> '
else:
question = 'Create a new database file? [y/n]\n> '
proceed = input(question)
if proceed.strip().lower() == 'y':
if os.path.exists(database_file_path):
os.remove(database_file_path)
# Base.metadata.create_all requires all models to be loaded before
# tables can be created. It's is placed here for emphasis.
from core.models import AWSKey, PacuSession
Base.metadata.create_all(engine)
print('Database created at {}\n'.format(database_file_path))
return True
else:
print('Database creation cancelled.\n')
return False
if __name__ == '__main__':
from settings import DATABASE_FILE_PATH
set_sigint_handler(exit_text='\nDatabase creation cancelled.')
attempt_to_create_database(DATABASE_FILE_PATH, auto_proceed=False)