machine: update install validation to use package

If the "moonraker" entry point is available use it as an executable.  Otherwise
launch the module through the "-m" switch.

Signed-off-by:  Eric Callahan <arksine.code@gmail.com>
This commit is contained in:
Eric Callahan 2023-02-09 20:32:35 -05:00
parent 79467e6484
commit 5ffd18d132
No known key found for this signature in database
GPG Key ID: 5A1EB336DFB4C71B
1 changed files with 19 additions and 5 deletions

View File

@ -1474,14 +1474,13 @@ Type=simple
User=%s
SupplementaryGroups=moonraker-admin
RemainAfterExit=yes
WorkingDirectory=%s
EnvironmentFile=%s
ExecStart=%s $MOONRAKER_ARGS
Restart=always
RestartSec=10
""" # noqa: E122
ENVIRONMENT = "MOONRAKER_ARGS=\"%s/moonraker/moonraker.py %s\""
ENVIRONMENT = "MOONRAKER_ARGS=\"%s%s\"%s"
TEMPLATE_NAME = "password_request.html"
class ValidationError(Exception):
@ -1656,7 +1655,6 @@ class InstallValidator:
tmp_svc = pathlib.Path(
tempfile.gettempdir()
).joinpath(f"{unit}-tmp.svc")
src_path = source_info.source_path()
# Create local environment file
sysd_data = self.data_path.joinpath("systemd")
if not sysd_data.exists():
@ -1694,13 +1692,29 @@ class InstallValidator:
service_bkp = svc_bkp_path.joinpath(svc_dest.name)
shutil.copy2(str(svc_dest), str(service_bkp))
# write temporary service file
src_path = source_info.source_path()
exec_path = pathlib.Path(sys.executable)
py_exec = exec_path.parent.joinpath("python")
pythonpath = ""
src_arg = ""
if exec_path.name == "python" or py_exec.is_file():
# Default to loading via the python executable. This
# makes it possible to switch between git repos, pip
# releases and git releases without reinstalling the
# service.
exec_path = py_exec
src_arg = "-m moonraker "
if not source_info.is_dist_package():
# This module isn't in site/dist packages,
# add PYTHONPATH env variable
pythonpath = f"\nPYTHONPATH=\"{src_path}\""
tmp_svc.write_text(
SYSTEMD_UNIT
% (SERVICE_VERSION, user, src_path, env_file, sys.executable)
% (SERVICE_VERSION, user, env_file, exec_path)
)
try:
# write new environment
env_file.write_text(ENVIRONMENT % (src_path, cmd_args))
env_file.write_text(ENVIRONMENT % (src_arg, cmd_args, pythonpath))
await machine.exec_sudo_command(
f"cp -f {tmp_svc} {svc_dest}", tries=5, timeout=60.)
await machine.exec_sudo_command(