From 8837acd57f81b8d77d50c0cfac01bc224a5d6dfd Mon Sep 17 00:00:00 2001 From: Thomas Sileo Date: Sun, 21 Aug 2022 15:46:26 +0200 Subject: [PATCH] More tweals for Yunohost support --- app/utils/yunohost.py | 57 +++++++++++++++++++++++++++++++++++++++ misc/ynh-supervisord.conf | 24 +++++++++++++++++ 2 files changed, 81 insertions(+) create mode 100644 app/utils/yunohost.py create mode 100644 misc/ynh-supervisord.conf diff --git a/app/utils/yunohost.py b/app/utils/yunohost.py new file mode 100644 index 0000000..9dc4f01 --- /dev/null +++ b/app/utils/yunohost.py @@ -0,0 +1,57 @@ +"""Basic wizard for setting up microblog.pub configuration files.""" +import os +import sys +from pathlib import Path +from typing import Any + +import bcrypt +import tomli_w +from markdown import markdown # type: ignore + +from app.key import generate_key + +_ROOT_DIR = Path().parent.parent.resolve() +_KEY_PATH = _ROOT_DIR / "data" / "key.pem" +_CONFIG_PATH = _ROOT_DIR / "data" / "profile.toml" + + +def setup_config_file( + domain: str, + username: str, + name: str, + summary: str, + password: str, +) -> None: + print("Generating microblog.pub config\n") + if _KEY_PATH.exists(): + sys.exit(2) + + generate_key(_KEY_PATH) + + config_file = _CONFIG_PATH + + if config_file.exists(): + # Spit out the relative path for the "config artifacts" + rconfig_file = "data/profile.toml" + print( + f"Existing setup detected, please delete {rconfig_file} " + "before restarting the wizard" + ) + sys.exit(2) + + dat: dict[str, Any] = {} + dat["domain"] = domain + dat["username"] = username + dat["admin_password"] = bcrypt.hashpw(password.encode(), bcrypt.gensalt()).decode() + dat["name"] = name + dat["summary"] = markdown(summary) + dat["https"] = True + proto = "https" + dat["icon_url"] = f'{proto}://{dat["domain"]}/static/nopic.png' + dat["secret"] = os.urandom(16).hex() + + with config_file.open("w") as f: + f.write(tomli_w.dumps(dat)) + + print("Done") + sys.exit(0) diff --git a/misc/ynh-supervisord.conf b/misc/ynh-supervisord.conf new file mode 100644 index 0000000..7f12851 --- /dev/null +++ b/misc/ynh-supervisord.conf @@ -0,0 +1,24 @@ +[supervisord] + +[fcgi-program:uvicorn] +socket=tcp://localhost:%(ENV_UVICORN_PORT) +command=%(ENV_VENV_DIR)s/bin/uvicorn app.main:app --no-server-header --fd 0 +numprocs=2 +process_name=uvicorn-%(process_num)d +redirect_stderr=true +stdout_logfile=uvicorn.log +stdout_logfile_maxbytes=0 + +[program:incoming_worker] +command=%(ENV_VENV_DIR)s/bin/inv process-incoming-activities +numproc=1 +redirect_stderr=true +stdout_logfile=incoming_worker.log +stdout_logfile_maxbytes=0 + +[program:outgoing_worker] +command=%(ENV_VENV_DIR)s/bin/inv process-outgoing-activities +numproc=1 +redirect_stderr=true +stdout_logfile=outgoing_worker.log +stdout_logfile_maxbytes=0