PLUS4的moonraker

This commit is contained in:
whb0514
2024-09-02 13:31:06 +08:00
parent 1006bcb85e
commit f8fad844d7
101 changed files with 27357 additions and 7021 deletions

View File

@@ -0,0 +1,50 @@
#!/bin/bash
# LMDB Database backup utility
DATABASE_PATH="${HOME}/printer_data/database"
MOONRAKER_ENV="${HOME}/moonraker-env"
OUPUT_FILE="${HOME}/database.backup"
print_help()
{
echo "Moonraker Database Backup Utility"
echo
echo "usage: backup-database.sh [-h] [-e <python env path>] [-d <database path>] [-o <output file>]"
echo
echo "optional arguments:"
echo " -h show this message"
echo " -e <env path> Moonraker Python Environment"
echo " -d <database path> Moonraker LMDB database to backup"
echo " -o <output file> backup file to save to"
exit 0
}
# Parse command line arguments
while getopts "he:d:o:" arg; do
case $arg in
h) print_help;;
e) MOONRAKER_ENV=$OPTARG;;
d) DATABASE_PATH=$OPTARG;;
o) OUPUT_FILE=$OPTARG;;
esac
done
PYTHON_BIN="${MOONRAKER_ENV}/bin/python"
DB_TOOL="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )/dbtool.py"
if [ ! -f $PYTHON_BIN ]; then
echo "No Python binary found at '${PYTHON_BIN}'"
exit -1
fi
if [ ! -f "$DATABASE_PATH/data.mdb" ]; then
echo "No Moonraker database found at '${DATABASE_PATH}'"
exit -1
fi
if [ ! -f $DB_TOOL ]; then
echo "Unable to locate dbtool.py at '${DB_TOOL}'"
exit -1
fi
${PYTHON_BIN} ${DB_TOOL} backup ${DATABASE_PATH} ${OUPUT_FILE}

65
scripts/data-path-fix.sh Normal file
View File

@@ -0,0 +1,65 @@
#!/bin/bash
# Data Path Fix for legacy MainsailOS and FluiddPi installations running
# a single instance of Moonraker with a default configuration
DATA_PATH="${HOME}/printer_data"
DATA_PATH_BKP="${HOME}/.broken_printer_data"
DB_PATH="${HOME}/.moonraker_database"
CONFIG_PATH="${HOME}/klipper_config"
LOG_PATH="${HOME}/klipper_logs"
GCODE_PATH="${HOME}/gcode_files"
MOONRAKER_CONF="${CONFIG_PATH}/moonraker.conf"
MOONRAKER_LOG="${LOG_PATH}/moonraker.log"
ALIAS="moonraker"
# Parse command line arguments
while getopts "c:l:d:a:m:g:" arg; do
case $arg in
c)
MOONRAKER_CONF=$OPTARG
CONFIG_PATH="$( dirname $OPTARG )"
;;
l)
MOONRAKER_LOG=$OPTARG
LOG_PATH="$( dirname $OPTARG )"
;;
d)
DATA_PATH=$OPTARG
dpbase="$( basename $OPTARG )"
DATA_PATH_BKP="${HOME}/.broken_${dpbase}"
;;
a)
ALIAS=$OPTARG
;;
m)
DB_PATH=$OPTARG
[ ! -f "${DB_PATH}/data.mdb" ] && echo "No valid database found at ${DB_PATH}" && exit 1
;;
g)
GCODE_PATH=$OPTARG
[ ! -d "${GCODE_PATH}" ] && echo "No GCode Path found at ${GCODE_PATH}" && exit 1
;;
esac
done
[ ! -f "${MOONRAKER_CONF}" ] && echo "Error: unable to find config: ${MOONRAKER_CONF}" && exit 1
[ ! -d "${LOG_PATH}" ] && echo "Error: unable to find log path: ${LOG_PATH}" && exit 1
sudo systemctl stop ${ALIAS}
[ -d "${DATA_PATH_BKP}" ] && rm -rf ${DATA_PATH_BKP}
[ -d "${DATA_PATH}" ] && echo "Moving broken datapath to ${DATA_PATH_BKP}" && mv ${DATA_PATH} ${DATA_PATH_BKP}
mkdir ${DATA_PATH}
echo "Creating symbolic links..."
[ -f "${DB_PATH}/data.mdb" ] && ln -s ${DB_PATH} "$DATA_PATH/database"
[ -d "${GCODE_PATH}" ] && ln -s ${GCODE_PATH} "$DATA_PATH/gcodes"
ln -s ${LOG_PATH} "$DATA_PATH/logs"
ln -s ${CONFIG_PATH} "$DATA_PATH/config"
[ -f "${DB_PATH}/data.mdb" ] && ~/moonraker-env/bin/python -mlmdb -e ${DB_PATH} -d moonraker edit --delete=validate_install
echo "Running Moonraker install script..."
~/moonraker/scripts/install-moonraker.sh -f -a ${ALIAS} -d ${DATA_PATH} -c ${MOONRAKER_CONF} -l ${MOONRAKER_LOG}

View File

@@ -9,6 +9,7 @@ import pathlib
import base64
import tempfile
import re
import time
from typing import Any, Dict, Optional, TextIO, Tuple
import lmdb
@@ -16,7 +17,9 @@ MAX_NAMESPACES = 100
MAX_DB_SIZE = 200 * 2**20
HEADER_KEY = b"MOONRAKER_DATABASE_START"
LINE_MATCH = re.compile(r"\+(\d+),(\d+):(.+?)->(.+)")
LINE_MATCH = re.compile(
r"^\+(\d+),(\d+):([A-Za-z0-9+/]+={0,2})->([A-Za-z0-9+/]+={0,2})$"
)
class DBToolError(Exception):
pass
@@ -157,10 +160,13 @@ def restore(args: Dict[str, Any]):
print(f"Restoring backup from '{input_db}' to '{dest_path}'...")
bkp_dir: Optional[pathlib.Path] = None
if dest_path.joinpath("data.mdb").exists():
tmp_dir = pathlib.Path(tempfile.gettempdir())
bkp_dir = tmp_dir.joinpath("moonrakerdb_backup")
bkp_dir = dest_path.parent.joinpath("backup")
if not bkp_dir.exists():
bkp_dir = pathlib.Path(tempfile.gettempdir())
str_time = time.strftime("%Y%m%dT%H%M%SZ", time.gmtime())
bkp_dir = bkp_dir.joinpath(f"{str_time}/database")
if not bkp_dir.is_dir():
bkp_dir.mkdir()
bkp_dir.mkdir(parents=True)
print(f"Warning: database file at found in '{dest_path}', "
"all data will be overwritten. Copying existing DB "
f"to '{bkp_dir}'")

View File

@@ -1,6 +1,6 @@
#!/bin/bash
# Helper Script for fetching the API Key from a moonraker database
DATABASE_PATH="${HOME}/.moonraker_database"
DATABASE_PATH="${HOME}/printer_data/database"
MOONRAKER_ENV="${HOME}/moonraker-env"
DB_ARGS="--read=READ --db=authorized_users get _API_KEY_USER_"
API_REGEX='(?<="api_key": ")([^"]+)'

104
scripts/finish-upgrade.sh Normal file
View File

@@ -0,0 +1,104 @@
#!/bin/bash
# Helper script for completing service upgrades via ssh
ADDRESS="localhost"
PORT="7125"
API_KEY=""
# Python Helper Scripts
check_sudo_request=$( cat << EOF
import sys
import json
try:
ret = json.load(sys.stdin)
except Exception:
exit(0)
entries = ret.get('result', {}).get('entries', [])
for item in entries:
if item['dismissed'] is False and item['title'] == 'Sudo Password Required':
sys.stdout.write('true')
exit(0)
sys.stdout.write('false')
EOF
)
check_pw_response=$( cat << EOF
import sys
import json
try:
ret = json.load(sys.stdin)
except Exception:
exit(0)
responses = ret.get('result', {}).get('sudo_responses', [])
if responses:
sys.stdout.write('\n'.join(responses))
EOF
)
print_help_message()
{
echo "Utility to complete privileged upgrades for Moonraker"
echo
echo "usage: finish-upgrade.sh [-h] [-a <address>] [-p <port>] [-k <api_key>]"
echo
echo "optional arguments:"
echo " -h show this message"
echo " -a <address> address for Moonraker instance"
echo " -p <port> port for Moonraker instance"
echo " -k <api_key> API Key for authorization"
}
while getopts "a:p:k:h" arg; do
case $arg in
a) ADDRESS=${OPTARG};;
b) PORT=${OPTARG};;
k) API_KEY=${OPTARG};;
h)
print_help_message
exit 0
;;
esac
done
base_url="http://${ADDRESS}:${PORT}"
echo "Completing Upgrade for Moonraker at ${base_url}"
echo "Requesting Announcements..."
ann_url="${base_url}/server/announcements/list"
curl_cmd=(curl -f -s -S "${ann_url}")
[ -n "${API_KEY}" ] && curl_cmd+=(-H "X-Api-Key: ${API_KEY}")
result="$( "${curl_cmd[@]}" 2>&1 )"
if [ $? -ne 0 ]; then
echo "Moonraker announcement request failed with error: ${result}"
echo "Make sure the address and port are correct. If authorization"
echo "is required supply the API Key with the -k option."
exit -1
fi
has_req="$( echo "$result" | python3 -c "${check_sudo_request}" )"
if [ "$has_req" != "true" ]; then
echo "No sudo request detected, aborting"
exit -1
fi
# Request Password, send to Moonraker
echo "Sudo request announcement found, please enter your password"
read -sp "Password: " passvar
echo -e "\n"
sudo_url="${base_url}/machine/sudo/password"
curl_cmd=(curl -f -s -S -X POST "${sudo_url}")
curl_cmd+=(-d "{\"password\": \"${passvar}\"}")
curl_cmd+=(-H "Content-Type: application/json")
[ -n "$API_KEY" ] && curl_cmd+=(-H "X-Api-Key: ${API_KEY}")
result="$( "${curl_cmd[@]}" 2>&1)"
if [ $? -ne 0 ]; then
echo "Moonraker password request failed with error: ${result}"
echo "Make sure you entered the correct password."
exit -1
fi
response="$( echo "$result" | python3 -c "${check_pw_response}" )"
if [ -n "${response}" ]; then
echo "${response}"
else
echo "Invalid response received from Moonraker. Raw result: ${result}"
fi

View File

@@ -7,8 +7,24 @@ SYSTEMDDIR="/etc/systemd/system"
REBUILD_ENV="${MOONRAKER_REBUILD_ENV:-n}"
FORCE_DEFAULTS="${MOONRAKER_FORCE_DEFAULTS:-n}"
DISABLE_SYSTEMCTL="${MOONRAKER_DISABLE_SYSTEMCTL:-n}"
CONFIG_PATH="${MOONRAKER_CONFIG_PATH:-${HOME}/moonraker.conf}"
LOG_PATH="${MOONRAKER_LOG_PATH:-/tmp/moonraker.log}"
SKIP_POLKIT="${MOONRAKER_SKIP_POLKIT:-n}"
CONFIG_PATH="${MOONRAKER_CONFIG_PATH}"
LOG_PATH="${MOONRAKER_LOG_PATH}"
DATA_PATH="${MOONRAKER_DATA_PATH}"
INSTANCE_ALIAS="${MOONRAKER_ALIAS:-moonraker}"
SPEEDUPS="${MOONRAKER_SPEEDUPS:-n}"
SERVICE_VERSION="1"
package_decode_script=$( cat << EOF
import sys
import json
try:
ret = json.load(sys.stdin)
except Exception:
exit(0)
sys.stdout.write(' '.join(ret['debian']))
EOF
)
# Step 2: Clean up legacy installation
cleanup_legacy() {
@@ -25,17 +41,30 @@ cleanup_legacy() {
# Step 3: Install packages
install_packages()
{
PKGLIST="python3-virtualenv python3-dev libopenjp2-7 python3-libgpiod"
PKGLIST="${PKGLIST} curl libcurl4-openssl-dev libssl-dev liblmdb-dev"
PKGLIST="${PKGLIST} libsodium-dev zlib1g-dev libjpeg-dev packagekit"
# Update system package info
report_status "Running apt-get update..."
sudo apt-get update --allow-releaseinfo-change
system_deps="${SRCDIR}/scripts/system-dependencies.json"
if [ -f "${system_deps}" ]; then
if [ ! -x "$(command -v python3)" ]; then
report_status "Installing python3 base package..."
sudo apt-get install --yes python3
fi
PKGS="$( cat ${system_deps} | python3 -c "${package_decode_script}" )"
else
echo "Error: system-dependencies.json not found, falling back to legacy pacakge list"
PKGLIST="${PKGLIST} python3-virtualenv python3-dev"
PKGLIST="${PKGLIST} libopenjp2-7 libsodium-dev zlib1g-dev libjpeg-dev"
PKGLIST="${PKGLIST} packagekit wireless-tools curl"
PKGS=${PKGLIST}
fi
# Install desired packages
report_status "Installing packages..."
sudo apt-get install --yes ${PKGLIST}
report_status "Installing Moonraker Dependencies:"
report_status "${PKGS}"
sudo apt-get install --yes ${PKGS}
}
# Step 4: Create python virtual environment
@@ -50,29 +79,84 @@ create_virtualenv()
fi
if [ ! -d ${PYTHONDIR} ]; then
GET_PIP="${HOME}/get-pip.py"
virtualenv --no-pip -p /usr/bin/python3 ${PYTHONDIR}
curl https://bootstrap.pypa.io/pip/3.6/get-pip.py -o ${GET_PIP}
${PYTHONDIR}/bin/python ${GET_PIP}
rm ${GET_PIP}
virtualenv -p /usr/bin/python3 ${PYTHONDIR}
#GET_PIP="${HOME}/get-pip.py"
#curl https://bootstrap.pypa.io/pip/3.6/get-pip.py -o ${GET_PIP}
#${PYTHONDIR}/bin/python ${GET_PIP}
#rm ${GET_PIP}
fi
# Install/update dependencies
export SKIP_CYTHON=1
${PYTHONDIR}/bin/pip install -r ${SRCDIR}/scripts/moonraker-requirements.txt
if [ ${SPEEDUPS} = "y" ]; then
report_status "Installing Speedups..."
${PYTHONDIR}/bin/pip install -r ${SRCDIR}/scripts/moonraker-speedups.txt
fi
}
# Step 5: Install startup script
# Step 5: Initialize data folder
init_data_path()
{
report_status "Initializing Moonraker Data Path at ${DATA_PATH}"
config_dir="${DATA_PATH}/config"
logs_dir="${DATA_PATH}/logs"
env_dir="${DATA_PATH}/systemd"
config_file="${DATA_PATH}/config/moonraker.conf"
[ ! -e "${DATA_PATH}" ] && mkdir ${DATA_PATH}
[ ! -e "${config_dir}" ] && mkdir ${config_dir}
[ ! -e "${logs_dir}" ] && mkdir ${logs_dir}
[ ! -e "${env_dir}" ] && mkdir ${env_dir}
[ -n "${CONFIG_PATH}" ] && config_file=${CONFIG_PATH}
# Write initial configuration for first time installs
if [ ! -f $SERVICE_FILE ] && [ ! -e "${config_file}" ]; then
# detect machine provider
if [ "$( systemctl is-active dbus )" = "active" ]; then
provider="systemd_dbus"
else
provider="systemd_cli"
fi
report_status "Writing Config File ${config_file}:\n"
/bin/sh -c "cat > ${config_file}" << EOF
# Moonraker Configuration File
[server]
host: 0.0.0.0
port: 7125
# Make sure the klippy_uds_address is correct. It is initialized
# to the default address.
klippy_uds_address: /tmp/klippy_uds
[machine]
provider: ${provider}
EOF
cat ${config_file}
fi
}
# Step 6: Install startup script
install_script()
{
# Create systemd service file
SERVICE_FILE="${SYSTEMDDIR}/moonraker.service"
ENV_FILE="${DATA_PATH}/systemd/moonraker.env"
if [ ! -f $ENV_FILE ] || [ $FORCE_DEFAULTS = "y" ]; then
rm -f $ENV_FILE
env_vars="MOONRAKER_DATA_PATH=\"${DATA_PATH}\""
[ -n "${CONFIG_PATH}" ] && env_vars="${env_vars}\nMOONRAKER_CONFIG_PATH=\"${CONFIG_PATH}\""
[ -n "${LOG_PATH}" ] && env_vars="${env_vars}\nMOONRAKER_LOG_PATH=\"${LOG_PATH}\""
env_vars="${env_vars}\nMOONRAKER_ARGS=\"-m moonraker\""
env_vars="${env_vars}\nPYTHONPATH=\"${SRCDIR}\"\n"
echo -e $env_vars > $ENV_FILE
fi
[ -f $SERVICE_FILE ] && [ $FORCE_DEFAULTS = "n" ] && return
report_status "Installing system start script..."
sudo groupadd -f moonraker-admin
sudo /bin/sh -c "cat > ${SERVICE_FILE}" << EOF
#Systemd service file for moonraker
# systemd service file for moonraker
[Unit]
Description=API Server for Klipper
Description=API Server for Klipper SV${SERVICE_VERSION}
Requires=network-online.target
After=network-online.target
@@ -84,50 +168,57 @@ Type=simple
User=$USER
SupplementaryGroups=moonraker-admin
RemainAfterExit=yes
WorkingDirectory=${SRCDIR}
ExecStart=${LAUNCH_CMD} -c ${CONFIG_PATH} -l ${LOG_PATH}
EnvironmentFile=${ENV_FILE}
ExecStart=${PYTHONDIR}/bin/python \$MOONRAKER_ARGS
Restart=always
RestartSec=10
EOF
# Use systemctl to enable the klipper systemd service script
if [ $DISABLE_SYSTEMCTL = "n" ]; then
sudo systemctl enable moonraker.service
sudo systemctl enable "${INSTANCE_ALIAS}.service"
sudo systemctl daemon-reload
fi
}
# Step 7: Validate/Install polkit rules
check_polkit_rules()
{
if [ ! -x "$(command -v pkaction)" ]; then
if [ ! -x "$(command -v pkaction || true)" ]; then
return
fi
POLKIT_VERSION="$( pkaction --version | grep -Po "(\d?\.\d+)" )"
POLKIT_VERSION="$( pkaction --version | grep -Po "(\d+\.?\d*)" )"
NEED_POLKIT_INSTALL="n"
if [ "$POLKIT_VERSION" = "0.105" ]; then
POLKIT_LEGACY_FILE="/etc/polkit-1/localauthority/50-local.d/10-moonraker.pkla"
# legacy policykit rules don't give users other than root read access
if sudo [ ! -f $POLKIT_LEGACY_FILE ]; then
echo -e "\n*** No PolicyKit Rules detected, run 'set-policykit-rules.sh'"
echo "*** if you wish to grant Moonraker authorization to manage"
echo "*** system services, reboot/shutdown the system, and update"
echo "*** packages."
NEED_POLKIT_INSTALL="y"
fi
else
POLKIT_FILE="/etc/polkit-1/rules.d/moonraker.rules"
POLKIT_USR_FILE="/usr/share/polkit-1/rules.d/moonraker.rules"
if [ ! -f $POLKIT_FILE ] && [ ! -f $POLKIT_USR_FILE ]; then
NEED_POLKIT_INSTALL="y"
fi
fi
if [ "${NEED_POLKIT_INSTALL}" = "y" ]; then
if [ "${SKIP_POLKIT}" = "y" ]; then
echo -e "\n*** No PolicyKit Rules detected, run 'set-policykit-rules.sh'"
echo "*** if you wish to grant Moonraker authorization to manage"
echo "*** system services, reboot/shutdown the system, and update"
echo "*** packages."
else
report_status "Installing PolKit Rules"
${SRCDIR}/scripts/set-policykit-rules.sh -z
fi
fi
}
# Step 6: Start server
# Step 8: Start server
start_software()
{
report_status "Launching Moonraker API Server..."
sudo systemctl restart moonraker
sudo systemctl restart ${INSTANCE_ALIAS}
}
# Helper functions
@@ -149,24 +240,43 @@ set -e
# Find SRCDIR from the pathname of this script
SRCDIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )"/.. && pwd )"
LAUNCH_CMD="${PYTHONDIR}/bin/python ${SRCDIR}/moonraker/moonraker.py"
# Parse command line arguments
while getopts "rfzc:l:" arg; do
while getopts "rfzxsc:l:d:a:" arg; do
case $arg in
r) REBUILD_ENV="y";;
f) FORCE_DEFAULTS="y";;
z) DISABLE_SYSTEMCTL="y";;
x) SKIP_POLKIT="y";;
s) SPEEDUPS="y";;
c) CONFIG_PATH=$OPTARG;;
l) LOG_PATH=$OPTARG;;
d) DATA_PATH=$OPTARG;;
a) INSTANCE_ALIAS=$OPTARG;;
esac
done
if [ -z "${DATA_PATH}" ]; then
if [ "${INSTANCE_ALIAS}" = "moonraker" ]; then
DATA_PATH="${HOME}/printer_data"
else
num="$( echo ${INSTANCE_ALIAS} | grep -Po "moonraker[-_]?\K\d+" || true )"
if [ -n "${num}" ]; then
DATA_PATH="${HOME}/printer_${num}_data"
else
DATA_PATH="${HOME}/${INSTANCE_ALIAS}_data"
fi
fi
fi
SERVICE_FILE="${SYSTEMDDIR}/${INSTANCE_ALIAS}.service"
# Run installation steps defined above
verify_ready
cleanup_legacy
install_packages
create_virtualenv
init_data_path
install_script
check_polkit_rules
if [ $DISABLE_SYSTEMCTL = "n" ]; then

57
scripts/make_sysdeps.py Normal file
View File

@@ -0,0 +1,57 @@
#! /usr/bin/python3
# Create system dependencies json file from the install script
#
# Copyright (C) 2023 Eric Callahan <arksine.code@gmail.com>
#
# This file may be distributed under the terms of the GNU GPLv3 license
from __future__ import annotations
import argparse
import pathlib
import json
import re
from typing import List, Dict
def make_sysdeps(input: str, output: str, distro: str, truncate: bool) -> None:
sysdeps: Dict[str, List[str]] = {}
outpath = pathlib.Path(output).expanduser().resolve()
if outpath.is_file() and not truncate:
sysdeps = json.loads(outpath.read_bytes())
inst_path: pathlib.Path = pathlib.Path(input).expanduser().resolve()
if not inst_path.is_file():
raise Exception(f"Unable to locate install script: {inst_path}")
data = inst_path.read_text()
plines: List[str] = re.findall(r'PKGLIST="(.*)"', data)
plines = [p.lstrip("${PKGLIST}").strip() for p in plines]
packages: List[str] = []
for line in plines:
packages.extend(line.split())
sysdeps[distro] = packages
outpath.write_text(json.dumps(sysdeps, indent=4))
if __name__ == "__main__":
def_path = pathlib.Path(__file__).parent
desc = (
"make_sysdeps - generate system dependency json file from an install script"
)
parser = argparse.ArgumentParser(description=desc)
parser.add_argument(
"-i", "--input", metavar="<install script>",
help="path of the install script to read",
default=f"{def_path}/install-moonraker.sh"
)
parser.add_argument(
"-o", "--output", metavar="<output file>",
help="path of the system dependency file to write",
default=f"{def_path}/system-dependencies.json"
)
parser.add_argument(
"-d", "--distro", metavar="<linux distro>",
help="linux distro for dependencies", default="debian"
)
parser.add_argument(
"-t", "--truncate", action="store_true",
help="truncate output file"
)
args = parser.parse_args()
make_sysdeps(args.input, args.output, args.distro, args.truncate)

View File

@@ -1,18 +1,21 @@
# Python dependencies for Moonraker
tornado==6.1.0
--find-links=python_wheels
tornado==6.2.0 ; python_version=='3.7'
tornado==6.4.0 ; python_version>='3.8'
pyserial==3.4
pyserial-asyncio==0.6
pillow==9.0.1
lmdb==1.2.1
streaming-form-data==1.8.1
distro==1.5.0
pillow==9.5.0 ; python_version=='3.7'
pillow==10.3.0 ; python_version>='3.8'
streaming-form-data==1.11.0 ; python_version=='3.7'
streaming-form-data==1.15.0 ; python_version>='3.8'
distro==1.9.0
inotify-simple==1.3.5
libnacl==1.7.2
paho-mqtt==1.5.1
pycurl==7.44.1
zeroconf==0.37.0
preprocess-cancellation==0.2.0
jinja2==3.0.3
libnacl==2.1.0
paho-mqtt==1.6.1
zeroconf==0.131.0
preprocess-cancellation==0.2.1
jinja2==3.1.4
dbus-next==0.2.3
apprise==0.9.7
apprise==1.8.0
ldap3==2.9.1
python-periphery==2.4.1

View File

@@ -0,0 +1,2 @@
msgspec>=0.18.4 ; python_version>='3.8'
uvloop>=0.17.0

80
scripts/pdm_build_dist.py Normal file
View File

@@ -0,0 +1,80 @@
# Wheel Setup Script for generating metadata
#
# Copyright (C) 2023 Eric Callahan <arksine.code@gmail.com>
#
# This file may be distributed under the terms of the GNU GPLv3 license
from __future__ import annotations
import pathlib
import subprocess
import shlex
import json
import shutil
from datetime import datetime, timezone
from typing import Dict, Any, TYPE_CHECKING
if TYPE_CHECKING:
from pdm.backend.hooks.base import Context
__package_name__ = "moonraker"
__dependencies__ = "scripts/system-dependencies.json"
def _run_git_command(cmd: str) -> str:
prog = shlex.split(cmd)
process = subprocess.Popen(
prog, stdout=subprocess.PIPE, stderr=subprocess.PIPE
)
ret, err = process.communicate()
retcode = process.wait()
if retcode == 0:
return ret.strip().decode()
return ""
def get_commit_sha(source_path: pathlib.Path) -> str:
cmd = f"git -C {source_path} rev-parse HEAD"
return _run_git_command(cmd)
def retrieve_git_version(source_path: pathlib.Path) -> str:
cmd = f"git -C {source_path} describe --always --tags --long --dirty"
return _run_git_command(cmd)
def pdm_build_initialize(context: Context) -> None:
context.ensure_build_dir()
build_ver: str = context.config.metadata['version']
proj_name: str = context.config.metadata['name']
urls: Dict[str, str] = context.config.metadata['urls']
build_dir = pathlib.Path(context.build_dir)
rel_dpath = f"{__package_name__}-{build_ver}.data/data/share/{proj_name}"
data_path = build_dir.joinpath(rel_dpath)
pkg_path = build_dir.joinpath(__package_name__)
build_time = datetime.now(timezone.utc)
release_info: Dict[str, Any] = {
"project_name": proj_name,
"package_name": __package_name__,
"urls": {key.lower(): val for key, val in urls.items()},
"package_version": build_ver,
"git_version": retrieve_git_version(context.root),
"commit_sha": get_commit_sha(context.root),
"build_time": datetime.isoformat(build_time, timespec="seconds")
}
if __dependencies__:
deps = pathlib.Path(context.root).joinpath(__dependencies__)
if deps.is_file():
dep_info: Dict[str, Any] = json.loads(deps.read_bytes())
release_info["system_dependencies"] = dep_info
# Write the release info to both the package and the data path
rinfo_data = json.dumps(release_info, indent=4)
data_path.mkdir(parents=True, exist_ok=True)
pkg_path.mkdir(parents=True, exist_ok=True)
data_path.joinpath("release_info").write_text(rinfo_data)
pkg_path.joinpath("release_info").write_text(rinfo_data)
scripts_path = context.root.joinpath("scripts")
scripts_dest = data_path.joinpath("scripts")
scripts_dest.mkdir()
for item in scripts_path.iterdir():
if item.name == "__pycache__":
continue
if item.is_dir():
shutil.copytree(str(item), str(scripts_dest.joinpath(item.name)))
else:
shutil.copy2(str(item), str(scripts_dest))

View File

@@ -0,0 +1,55 @@
#!/bin/bash
# LMDB Database restore utility
DATABASE_PATH="${HOME}/printer_data/database"
MOONRAKER_ENV="${HOME}/moonraker-env"
INPUT_FILE="${HOME}/database.backup"
print_help()
{
echo "Moonraker Database Restore Utility"
echo
echo "usage: restore-database.sh [-h] [-e <python env path>] [-d <database path>] [-i <input file>]"
echo
echo "optional arguments:"
echo " -h show this message"
echo " -e <env path> Moonraker Python Environment"
echo " -d <database path> Moonraker LMDB database path to restore to"
echo " -i <input file> backup file to restore from"
exit 0
}
# Parse command line arguments
while getopts "he:d:i:" arg; do
case $arg in
h) print_help;;
e) MOONRAKER_ENV=$OPTARG;;
d) DATABASE_PATH=$OPTARG;;
i) INPUT_FILE=$OPTARG;;
esac
done
PYTHON_BIN="${MOONRAKER_ENV}/bin/python"
DB_TOOL="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )/dbtool.py"
if [ ! -f $PYTHON_BIN ]; then
echo "No Python binary found at '${PYTHON_BIN}'"
exit -1
fi
if [ ! -d $DATABASE_PATH ]; then
echo "No database folder found at '${DATABASE_PATH}'"
exit -1
fi
if [ ! -f $INPUT_FILE ]; then
echo "No Database Backup File found at '${INPUT_FILE}'"
exit -1
fi
if [ ! -f $DB_TOOL ]; then
echo "Unable to locate dbtool.py at '${DB_TOOL}'"
exit -1
fi
${PYTHON_BIN} ${DB_TOOL} restore ${DATABASE_PATH} ${INPUT_FILE}

View File

@@ -30,6 +30,8 @@ add_polkit_legacy_rules()
ACTIONS="${ACTIONS};org.freedesktop.login1.power-off-multiple-sessions"
ACTIONS="${ACTIONS};org.freedesktop.login1.reboot"
ACTIONS="${ACTIONS};org.freedesktop.login1.reboot-multiple-sessions"
ACTIONS="${ACTIONS};org.freedesktop.login1.halt"
ACTIONS="${ACTIONS};org.freedesktop.login1.halt-multiple-sessions"
ACTIONS="${ACTIONS};org.freedesktop.packagekit.*"
sudo /bin/sh -c "cat > ${RULE_FILE}" << EOF
[moonraker permissions]
@@ -72,6 +74,8 @@ polkit.addRule(function(action, subject) {
action.id == "org.freedesktop.login1.power-off-multiple-sessions" ||
action.id == "org.freedesktop.login1.reboot" ||
action.id == "org.freedesktop.login1.reboot-multiple-sessions" ||
action.id == "org.freedesktop.login1.halt" ||
action.id == "org.freedesktop.login1.halt-multiple-sessions" ||
action.id.startsWith("org.freedesktop.packagekit.")) &&
subject.user == "$USER") {
// Only allow processes with the "moonraker-admin" supplementary group

View File

@@ -0,0 +1,13 @@
{
"debian": [
"python3-virtualenv",
"python3-dev",
"libopenjp2-7",
"libsodium-dev",
"zlib1g-dev",
"libjpeg-dev",
"packagekit",
"wireless-tools",
"curl"
]
}