mirror of https://github.com/MISP/misp-dashboard
chg: [updates] Improved database updates
parent
3a1fe9205d
commit
21af08a886
41
updates.py
41
updates.py
|
@ -3,6 +3,10 @@ import os
|
||||||
import configparser
|
import configparser
|
||||||
import logging
|
import logging
|
||||||
|
|
||||||
|
DATABASE_VERSION = [
|
||||||
|
1
|
||||||
|
]
|
||||||
|
|
||||||
|
|
||||||
configfile = os.path.join(os.path.dirname(os.path.realpath(__file__)), 'config/config.cfg')
|
configfile = os.path.join(os.path.dirname(os.path.realpath(__file__)), 'config/config.cfg')
|
||||||
cfg = configparser.ConfigParser()
|
cfg = configparser.ConfigParser()
|
||||||
|
@ -21,18 +25,6 @@ serv_list = redis.StrictRedis(
|
||||||
db=cfg.getint('RedisLIST', 'db'))
|
db=cfg.getint('RedisLIST', 'db'))
|
||||||
|
|
||||||
# logger
|
# logger
|
||||||
# logDir = cfg.get('Log', 'directory')
|
|
||||||
# logfilename = cfg.get('Log', 'update_filename')
|
|
||||||
# logPath = os.path.join(logDir, logfilename)
|
|
||||||
# if not os.path.exists(logDir):
|
|
||||||
# os.makedirs(logDir)
|
|
||||||
# try:
|
|
||||||
# logging.basicConfig(filename=logPath, filemode='a', level=logging.INFO)
|
|
||||||
# except PermissionError as error:
|
|
||||||
# print(error)
|
|
||||||
# print("Please fix the above and try again.")
|
|
||||||
# sys.exit(126)
|
|
||||||
# update_logger = logging.getLogger(__name__)
|
|
||||||
logDir = cfg.get('Log', 'directory')
|
logDir = cfg.get('Log', 'directory')
|
||||||
logfilename = cfg.get('Log', 'update_filename')
|
logfilename = cfg.get('Log', 'update_filename')
|
||||||
logPath = os.path.join(logDir, logfilename)
|
logPath = os.path.join(logDir, logfilename)
|
||||||
|
@ -48,17 +40,34 @@ update_logger.addHandler(handler)
|
||||||
|
|
||||||
def check_for_updates():
|
def check_for_updates():
|
||||||
db_version = serv_redis_db.get(cfg.get('RedisDB', 'dbVersion'))
|
db_version = serv_redis_db.get(cfg.get('RedisDB', 'dbVersion'))
|
||||||
db_version = db_version if db_version is not None else 0
|
db_version = int(db_version) if db_version is not None else 0
|
||||||
|
updates_to_be_done = find_updates(db_version)
|
||||||
|
if len(updates_to_be_done) == 0:
|
||||||
|
update_logger.info('database up-to-date')
|
||||||
|
else:
|
||||||
|
for i in updates_to_be_done:
|
||||||
|
exec_updates(i)
|
||||||
|
|
||||||
|
|
||||||
|
def find_updates(db_version):
|
||||||
|
updates_to_be_done = []
|
||||||
|
for i in DATABASE_VERSION:
|
||||||
|
if db_version < i:
|
||||||
|
updates_to_be_done.append(i)
|
||||||
|
return updates_to_be_done
|
||||||
|
|
||||||
|
|
||||||
|
def exec_updates(db_version):
|
||||||
result = False
|
result = False
|
||||||
|
|
||||||
if db_version == 0:
|
if db_version == 1:
|
||||||
result = apply_update_1()
|
result = apply_update_1()
|
||||||
|
|
||||||
if result:
|
if result:
|
||||||
serv_redis_db.set(cfg.get('RedisDB', 'dbVersion'), db_version+1)
|
serv_redis_db.set(cfg.get('RedisDB', 'dbVersion'), db_version+1)
|
||||||
update_logger.warning(f'Bumped dbVersion to {db_version+1}')
|
update_logger.warning(f'dbVersion sets to {db_version+1}')
|
||||||
else:
|
else:
|
||||||
update_logger.info('Database up-to-date')
|
update_logger.error(f'Something went wrong. {result}')
|
||||||
|
|
||||||
|
|
||||||
# Data format changed. Wipe the key.
|
# Data format changed. Wipe the key.
|
||||||
|
|
Loading…
Reference in New Issue