microblog.pub/setup_wizard/wizard.py

86 lines
2.2 KiB
Python
Raw Normal View History

2019-04-13 03:03:43 -05:00
"""Basic wizard for setting up microblog.pub configuration files."""
import binascii
2019-04-22 02:58:11 -05:00
import os
import sys
2019-04-13 03:03:43 -05:00
from pathlib import Path
import bcrypt
from markdown import markdown
2019-04-22 02:58:11 -05:00
from prompt_toolkit import prompt
2019-04-13 03:03:43 -05:00
2019-04-22 15:41:58 -05:00
def main() -> None:
2019-04-13 03:03:43 -05:00
print("Welcome to microblog.pub setup wizard\n")
2019-04-22 03:52:32 -05:00
config_file = Path("/app/out/config/me.yml")
env_file = Path("/app/out/.env")
if config_file.exists() or env_file.exists():
# Spit out the relative path for the "config artifacts"
2019-04-22 15:41:58 -05:00
rconfig_file = "config/me.yml"
renv_file = ".env"
2019-04-22 04:06:54 -05:00
print(
2019-04-22 15:41:58 -05:00
f"Existing setup detected, please delete {rconfig_file} and/or {renv_file} before restarting the wizard"
2019-04-22 04:06:54 -05:00
)
2019-04-22 03:52:32 -05:00
sys.exit(2)
2019-04-13 03:03:43 -05:00
dat = {}
print("Your identity will be @{username}@{domain}")
dat["domain"] = prompt("domain: ")
dat["username"] = prompt("username: ")
2019-05-17 11:54:03 -05:00
dat["pass"] = bcrypt.hashpw(
2019-04-13 03:03:43 -05:00
prompt("password: ", is_password=True).encode(), bcrypt.gensalt()
).decode()
dat["name"] = prompt("name (e.g. John Doe): ")
dat["summary"] = markdown(
prompt(
"summary (short description, in markdown, press [ESC] then [ENTER] to submit):\n",
multiline=True,
)
)
dat["https"] = True
proto = "https"
yn = ""
while yn not in ["y", "n"]:
yn = prompt("will the site be served via https? (y/n): ", default="y").lower()
if yn == "n":
dat["https"] = False
proto = "http"
dat["icon_url"] = prompt(
"icon URL: ", default=f'{proto}://{dat["domain"]}/static/nopic.png'
)
out = ""
for k, v in dat.items():
out += f"{k}: {v!r}\n"
2019-04-22 03:52:32 -05:00
with config_file.open("w") as f:
f.write(out)
env = {
2019-04-13 03:03:43 -05:00
"WEB_PORT": 5005,
"CONFIG_DIR": "./config",
"DATA_DIR": "./data",
"POUSSETACHES_AUTH_KEY": binascii.hexlify(os.urandom(32)).decode(),
"COMPOSE_PROJECT_NAME": Path.cwd().name.replace(".", ""),
}
out2 = ""
2019-04-22 03:52:32 -05:00
for k, v in env.items():
2019-04-13 03:03:43 -05:00
out2 += f"{k}={v}\n"
2019-04-22 03:52:32 -05:00
with env_file.open("w") as f:
f.write(out2)
print("Done")
sys.exit(0)
2019-04-13 03:03:43 -05:00
if __name__ == "__main__":
try:
main()
except KeyboardInterrupt:
2019-04-22 02:58:11 -05:00
print("Aborted")
sys.exit(1)