2024-01-09 09:07:04 +01:00
|
|
|
#!/usr/bin/env python3
|
2023-02-16 06:06:42 +01:00
|
|
|
import argparse
|
2023-04-15 06:13:04 +02:00
|
|
|
import logging
|
2024-01-09 09:07:04 +01:00
|
|
|
from os import mkdir, path, access, W_OK
|
|
|
|
import yaml
|
|
|
|
import asyncio
|
|
|
|
|
2024-01-09 19:57:59 +01:00
|
|
|
from core.data import Data
|
|
|
|
from core.config import CoreConfig
|
2023-02-16 06:06:42 +01:00
|
|
|
|
2023-03-09 17:38:58 +01:00
|
|
|
if __name__ == "__main__":
|
2023-02-17 07:02:21 +01:00
|
|
|
parser = argparse.ArgumentParser(description="Database utilities")
|
2023-03-09 17:38:58 +01:00
|
|
|
parser.add_argument(
|
|
|
|
"--config", "-c", type=str, help="Config folder to use", default="config"
|
|
|
|
)
|
|
|
|
parser.add_argument(
|
|
|
|
"--version",
|
|
|
|
"-v",
|
|
|
|
type=str,
|
|
|
|
help="Version of the database to upgrade/rollback to",
|
|
|
|
)
|
2023-03-04 06:04:47 +01:00
|
|
|
parser.add_argument("--email", "-e", type=str, help="Email for the new user")
|
2024-01-09 09:07:04 +01:00
|
|
|
parser.add_argument("--access_code", "-a", type=str, help="Access code for new/transfer user", default="00000000000000000000")
|
2024-01-12 02:48:27 +01:00
|
|
|
parser.add_argument("--message", "-m", type=str, help="Revision message")
|
2024-01-12 03:01:51 +01:00
|
|
|
parser.add_argument("action", type=str, help="create, upgrade, create-owner, migrate, create-revision")
|
2023-02-17 07:02:21 +01:00
|
|
|
args = parser.parse_args()
|
|
|
|
|
|
|
|
cfg = CoreConfig()
|
2023-03-05 03:58:51 +01:00
|
|
|
if path.exists(f"{args.config}/core.yaml"):
|
2023-04-15 09:06:11 +02:00
|
|
|
cfg_dict = yaml.safe_load(open(f"{args.config}/core.yaml"))
|
2023-04-24 03:04:52 +02:00
|
|
|
cfg_dict.get("database", {})["loglevel"] = "info"
|
2023-04-15 09:06:11 +02:00
|
|
|
cfg.update(cfg_dict)
|
2023-04-24 03:04:52 +02:00
|
|
|
|
2023-03-17 02:36:42 +01:00
|
|
|
if not path.exists(cfg.server.log_dir):
|
|
|
|
mkdir(cfg.server.log_dir)
|
2023-04-24 03:04:52 +02:00
|
|
|
|
2023-03-17 02:36:42 +01:00
|
|
|
if not access(cfg.server.log_dir, W_OK):
|
|
|
|
print(
|
|
|
|
f"Log directory {cfg.server.log_dir} NOT writable, please check permissions"
|
|
|
|
)
|
|
|
|
exit(1)
|
|
|
|
|
2023-02-17 07:02:21 +01:00
|
|
|
data = Data(cfg)
|
|
|
|
|
|
|
|
if args.action == "create":
|
|
|
|
data.create_database()
|
2024-01-09 09:07:04 +01:00
|
|
|
|
|
|
|
elif args.action == "upgrade":
|
|
|
|
data.schema_upgrade(args.version)
|
2023-03-18 07:12:58 +01:00
|
|
|
|
2023-03-04 06:04:47 +01:00
|
|
|
elif args.action == "create-owner":
|
2024-01-09 09:07:04 +01:00
|
|
|
loop = asyncio.get_event_loop()
|
|
|
|
loop.run_until_complete(data.create_owner(args.email, args.access_code))
|
2024-01-09 19:57:59 +01:00
|
|
|
|
|
|
|
elif args.action == "migrate":
|
|
|
|
loop = asyncio.get_event_loop()
|
|
|
|
loop.run_until_complete(data.migrate())
|
2023-03-04 06:04:47 +01:00
|
|
|
|
2024-01-12 02:48:27 +01:00
|
|
|
elif args.action == "create-revision":
|
|
|
|
loop = asyncio.get_event_loop()
|
|
|
|
loop.run_until_complete(data.create_revision(args.message))
|
|
|
|
|
2024-01-09 09:07:04 +01:00
|
|
|
else:
|
|
|
|
logging.getLogger("database").info(f"Unknown action {args.action}")
|