fix(install): a fresh install creates ntfy accounts without prompts (database created first; phone uses the web password)
This commit is contained in:
+44
-7
@@ -20,6 +20,7 @@ import platform
|
||||
import getpass
|
||||
import plistlib
|
||||
import secrets
|
||||
import time
|
||||
import shlex
|
||||
import shutil
|
||||
import subprocess
|
||||
@@ -61,10 +62,12 @@ DEFAULT_BIND_ADDRESS = "127.0.0.1"
|
||||
|
||||
|
||||
class Setup:
|
||||
def __init__(self, directory, prefix=None, runner=subprocess.run, resolver=None, environ=None):
|
||||
def __init__(self, directory, prefix=None, runner=subprocess.run, resolver=None, environ=None,
|
||||
popen=subprocess.Popen):
|
||||
self.directory = Path(directory)
|
||||
self.prefix_argument = prefix
|
||||
self.runner = runner
|
||||
self.popen = popen
|
||||
self.resolver = resolver or release_lookup.resolve_ntfy
|
||||
self.environ = environ if environ is not None else os.environ
|
||||
|
||||
@@ -543,22 +546,54 @@ class Setup:
|
||||
|
||||
# --- ntfy users and publishing token --------------------------------
|
||||
|
||||
def ntfy(self, prefix, config_path, args, capture=False):
|
||||
def ntfy(self, prefix, config_path, args, capture=False, password=None):
|
||||
# The account subcommands locate the server configuration through
|
||||
# NTFY_CONFIG_FILE; --config is a `serve`-only flag.
|
||||
env = dict(self.environ)
|
||||
env["NTFY_CONFIG_FILE"] = str(config_path)
|
||||
if password is not None:
|
||||
# `ntfy user add` takes the password from here instead of a prompt.
|
||||
env["NTFY_PASSWORD"] = password
|
||||
result = self.runner([str(prefix / "bin/ntfy"), *args], capture_output=capture, text=True, env=env)
|
||||
if result.returncode:
|
||||
step = " ".join(args[:2])
|
||||
raise ValueError(f"ntfy {step} failed; the native install is incomplete")
|
||||
return result
|
||||
|
||||
def bootstrap_ntfy(self, prefix, config_path):
|
||||
def run_ntfy(args, capture=False, interactive=False):
|
||||
return self.ntfy(prefix, config_path, list(args), capture=capture)
|
||||
def ensure_ntfy_auth_db(self, prefix, config_path):
|
||||
"""ntfy creates its account database the first time its server runs;
|
||||
the account commands refuse to run before that, so a fresh install
|
||||
runs it once."""
|
||||
auth = config_path.parent / "auth.db"
|
||||
if auth.exists():
|
||||
return
|
||||
env = dict(self.environ)
|
||||
process = self.popen([str(prefix / "bin/ntfy"), "serve", "--config", str(config_path)], env=env)
|
||||
try:
|
||||
deadline = time.monotonic() + 20
|
||||
while not auth.exists():
|
||||
if process.poll() is not None or time.monotonic() > deadline:
|
||||
raise ValueError("ntfy did not create its account database; is its port free?")
|
||||
time.sleep(0.2)
|
||||
finally:
|
||||
if process.poll() is None:
|
||||
process.terminate()
|
||||
try:
|
||||
process.wait(timeout=10)
|
||||
except subprocess.TimeoutExpired:
|
||||
process.kill()
|
||||
process.wait()
|
||||
|
||||
tokens.bootstrap_accounts(run_ntfy, prefix / "secrets/publisher-token", 0o600)
|
||||
def bootstrap_ntfy(self, prefix, config_path, reader_password=None):
|
||||
def run_ntfy(args, capture=False, interactive=False, password=None):
|
||||
return self.ntfy(prefix, config_path, list(args), capture=capture, password=password)
|
||||
|
||||
self.ensure_ntfy_auth_db(prefix, config_path)
|
||||
# The publisher authenticates with its token; its password is never
|
||||
# needed, so it is random. The phone signs in with the web password.
|
||||
tokens.bootstrap_accounts(run_ntfy, prefix / "secrets/publisher-token", 0o600,
|
||||
publisher_password=secrets.token_urlsafe(24),
|
||||
reader_password=reader_password)
|
||||
|
||||
# --- Docker migration ------------------------------------------------
|
||||
|
||||
@@ -726,7 +761,8 @@ class Setup:
|
||||
print("Starting empty (--fresh); existing Docker volumes are left untouched.")
|
||||
else:
|
||||
self.migrate_docker(prefix, args.replace_data)
|
||||
self.bootstrap_ntfy(prefix, config_path)
|
||||
self.bootstrap_ntfy(prefix, config_path,
|
||||
reader_password=json.loads(Path(web["auth_file"]).read_text())["password"])
|
||||
timezone = args.timezone
|
||||
if not timezone:
|
||||
try:
|
||||
@@ -793,6 +829,7 @@ class Setup:
|
||||
"Check your login service manager and that ports "
|
||||
f"{args.port}, {args.ntfy_port} and {args.web_port} are free (a running Docker installation may use them).")
|
||||
print(f"ntfy is configured for {base_url}; server, web proxy and ntfy have login autostart ({service_path}).")
|
||||
print("Phone notifications: subscribe to the 'pipelines' topic as phone-reader with the web password.")
|
||||
print(f"Web: http://{lan_ip if args.web_host == '0.0.0.0' else args.web_host}:{args.web_port}/ "
|
||||
f"(sign-in password in {web['auth_file']}; existing credentials retained).")
|
||||
print("Login autostart is already enabled. To enable/start it again, run:\n "
|
||||
|
||||
Reference in New Issue
Block a user