From 28782e910e70f6fd6e557d2be2b75acbd20d01d2 Mon Sep 17 00:00:00 2001 From: th33xitus Date: Mon, 18 Apr 2022 01:29:08 +0200 Subject: [PATCH] refactor(octoprint.sh): octoprint instance setup one dedicated env for each instance. allows independent installation of plugins. previously, all instances shared the same env and therefore all plugins. Signed-off-by: Dominik Willner th33xitus@gmail.com --- scripts/globals.sh | 3 - scripts/octoprint.sh | 254 ++++++++++++++++++++----------------------- 2 files changed, 116 insertions(+), 141 deletions(-) diff --git a/scripts/globals.sh b/scripts/globals.sh index b140a5b..5238b06 100644 --- a/scripts/globals.sh +++ b/scripts/globals.sh @@ -54,9 +54,6 @@ function set_globals(){ KLIPPERSCREEN_DIR="${HOME}/KlipperScreen" KLIPPERSCREEN_REPO=https://github.com/jordanruthe/KlipperScreen.git - #================ OCTOPRINT ===================# - OCTOPRINT_ENV="${HOME}/OctoPrint" - #========== MOONRAKER-TELEGRAM-BOT ============# MOONRAKER_TELEGRAM_BOT_ENV_DIR=${HOME}/moonraker-telegram-bot-env MOONRAKER_TELEGRAM_BOT_DIR=${HOME}/moonraker-telegram-bot diff --git a/scripts/octoprint.sh b/scripts/octoprint.sh index 6bed598..81489aa 100644 --- a/scripts/octoprint.sh +++ b/scripts/octoprint.sh @@ -22,10 +22,10 @@ function octoprint_systemd() { } function octoprint_setup_dialog(){ - status_msg "Initializing OctoPrint installation ..." - local klipper_count klipper_count=$(klipper_systemd | wc -w) + + status_msg "Initializing OctoPrint installation ..." top_border if [ -f "${INITD}/klipper" ] || [ -f "${SYSTEMD}/klipper.service" ]; then printf "|${green}%-55s${white}|\n" " 1 Klipper instance was found!" @@ -71,12 +71,12 @@ function octoprint_setup(){ dep=( git wget - python-pip - python-dev + python3-pip + python3-dev libyaml-dev build-essential - python-setuptools - python-virtualenv + python3-setuptools + python3-virtualenv ) dependency_check "${dep[@]}" @@ -85,14 +85,10 @@ function octoprint_setup(){ add_reboot_permission ### install octoprint - install_octoprint + install_octoprint "${instances}" - ### set up instances - if [ "${instances}" -eq 1 ]; then - create_single_octoprint_instance - else - create_multi_octoprint_instance "${instances}" - fi + ### set up service + create_octoprint_service "${instances}" ### step 6: enable and start all instances do_action_service "enable" "octoprint" @@ -106,24 +102,72 @@ function octoprint_setup(){ } function install_octoprint(){ - ### create and activate the virtualenv - [ ! -d "${OCTOPRINT_ENV}" ] && mkdir -p "${OCTOPRINT_ENV}" - status_msg "Installing python virtual environment..." - cd "${OCTOPRINT_ENV}" && virtualenv --python=python3 venv - ### activate virtualenv - source venv/bin/activate - status_msg "Installing OctoPrint ..." - pip install pip --upgrade - pip install --no-cache-dir octoprint - ok_msg "Download complete!" - ### leave virtualenv - deactivate + local i=1 instances=${1} octo_env + while (( i <= instances )); do + (( instances == 1 )) && octo_env="${HOME}/OctoPrint" + (( instances > 1 )) && octo_env="${HOME}/OctoPrint_${i}" + ### create and activate the virtualenv + status_msg "Installing python virtual environment..." + [ ! -d "${octo_env}" ] && mkdir -p "${octo_env}" + cd "${octo_env}" && virtualenv --python=python3 venv + ### activate virtualenv + source venv/bin/activate + (( instances == 1 )) && status_msg "Installing OctoPrint ..." + (( instances > 1 )) && status_msg "Installing OctoPrint instance ${i} ..." + pip install pip --upgrade + pip install --no-cache-dir octoprint + ok_msg "Ok!" + ### leave virtualenv + deactivate + i=$((i+1)) + done } -function create_config_yaml(){ - local basedir=${1} tmp_printer=${2} restart_cmd=${3} +function create_octoprint_service(){ + local i=1 instances=${1} port=5000 + local octo_env service basedir tmp_printer config_yaml restart_cmd - /bin/sh -c "cat > ${basedir}/config.yaml" << CONFIGYAML + while (( i <= instances )); do + if (( instances == 1 )); then + octo_env="${HOME}/OctoPrint" + service="${SYSTEMD}/octoprint.service" + basedir="${HOME}/.octoprint" + tmp_printer="/tmp/printer" + config_yaml="${basedir}/config.yaml" + restart_cmd="sudo service octoprint restart" + elif (( instances > 1 )); then + octo_env="${HOME}/OctoPrint_${i}" + service="${SYSTEMD}/octoprint-${i}.service" + basedir="${HOME}/.octoprint_${i}" + tmp_printer="/tmp/printer-${i}" + config_yaml="${basedir}/config.yaml" + restart_cmd="sudo service octoprint-${i} restart" + fi + (( instances == 1 )) && status_msg "Creating OctoPrint service ..." + (( instances > 1 )) && status_msg "Creating OctoPrint service ${i} ..." + sudo /bin/sh -c "cat > ${service}" << OCTOPRINT +[Unit] +Description=Starts OctoPrint on startup +After=network-online.target +Wants=network-online.target + +[Service] +Environment="LC_ALL=C.UTF-8" +Environment="LANG=C.UTF-8" +Type=simple +User=${USER} +ExecStart=${octo_env}/venv/bin/octoprint --basedir ${basedir} --config ${config_yaml} --port=${port} serve + +[Install] +WantedBy=multi-user.target +OCTOPRINT + ok_msg "Ok!" + + ### create config.yaml + if [ ! -f "${basedir}/config.yaml" ]; then + [ ! -d "${basedir}" ] && mkdir "${basedir}" + status_msg "Creating config.yaml ..." + /bin/sh -c "cat > ${basedir}/config.yaml" << CONFIGYAML serial: additionalPorts: - ${tmp_printer} @@ -135,88 +179,11 @@ server: systemRestartCommand: sudo shutdown -r now systemShutdownCommand: sudo shutdown -h now CONFIGYAML -} - -function create_single_octoprint_instance(){ - local port=5000 - local basedir="${HOME}/.octoprint" - local tmp_printer="/tmp/printer" - local config_yaml="${basedir}/config.yaml" - local restart_cmd="sudo service octoprint restart" - - status_msg "Creating OctoPrint instance ..." - sudo /bin/sh -c "cat > ${SYSTEMD}/octoprint.service" << OCTOPRINT -[Unit] -Description=Starts OctoPrint on startup -After=network-online.target -Wants=network-online.target - -[Service] -Environment="LC_ALL=C.UTF-8" -Environment="LANG=C.UTF-8" -Type=simple -User=${USER} -ExecStart=${OCTOPRINT_ENV}/venv/bin/octoprint --basedir ${basedir} --config ${config_yaml} --port=${port} serve - -[Install] -WantedBy=multi-user.target -OCTOPRINT - - ### create the config.yaml - if [ ! -f "${basedir}/config.yaml" ]; then - status_msg "Creating config.yaml ..." - [ ! -d "${basedir}" ] && mkdir "${basedir}" - create_config_yaml "${basedir}" "${tmp_printer}" "${restart_cmd}" - ok_msg "Config created!" + ok_msg "Ok!" fi -} -function create_multi_octoprint_instance(){ - local i=1 port=5000 instances=${1} - while [ "${i}" -le "${instances}" ]; do - ### multi instance variables - local basedir="${HOME}/.octoprint-${i}" - local tmp_printer="/tmp/printer-${i}" - local config_yaml="${basedir}/config.yaml" - local restart_cmd="sudo service octoprint-${i} restart" - - ### create instance - status_msg "Creating instance #${i} ..." - sudo /bin/sh -c "cat > ${SYSTEMD}/octoprint-${i}.service" << OCTOPRINT -[Unit] -Description=Starts OctoPrint instance ${instances} on startup -After=network-online.target -Wants=network-online.target - -[Service] -Environment="LC_ALL=C.UTF-8" -Environment="LANG=C.UTF-8" -Type=simple -User=${USER} -ExecStart=${OCTOPRINT_ENV}/venv/bin/octoprint --basedir ${basedir} --config ${config_yaml} --port=${port} serve - -[Install] -WantedBy=multi-user.target -OCTOPRINT - - ### create the config.yaml - if [ ! -f "${basedir}/config.yaml" ]; then - status_msg "Creating config.yaml for instance #${i}..." - [ ! -d "${basedir}" ] && mkdir "${basedir}" - create_config_yaml "${basedir}" "${tmp_printer}" "${restart_cmd}" - ok_msg "Config #${i} created!" - fi - - ### enable instance - sudo systemctl enable "octoprint-${i}.service" - ok_msg "OctoPrint instance ${i} created!" - - ### launching instance - status_msg "Launching OctoPrint instance ${i} ..." - sudo systemctl start "octoprint-${i}" - - i=$((i+1)) - port=$((port+1)) + port=$((port+1)) + i=$((i+1)) done } @@ -247,10 +214,10 @@ function print_op_ip_list(){ #=============== REMOVE OCTOPRINT ================# #=================================================# -function remove_octoprint(){ +function remove_octoprint_service(){ ###remove all octoprint services [ -z "$(octoprint_systemd)" ] && return - status_msg "Removing Moonraker Systemd Services ..." + status_msg "Removing OctoPrint Systemd Services ..." for service in $(octoprint_systemd | cut -d"/" -f5) do status_msg "Removing ${service} ..." @@ -262,30 +229,49 @@ function remove_octoprint(){ ### reloading units sudo systemctl daemon-reload sudo systemctl reset-failed +} +function remove_octoprint_sudoers(){ + [ ! -f /etc/sudoers.d/octoprint-shutdown ] && return ### remove sudoers file - if [ -f /etc/sudoers.d/octoprint-shutdown ]; then - sudo rm -rf /etc/sudoers.d/octoprint-shutdown - fi + sudo rm -f /etc/sudoers.d/octoprint-shutdown +} - ### remove OctoPrint directory - if [ -d "${HOME}/OctoPrint" ]; then - status_msg "Removing OctoPrint directory ..." - rm -rf "${HOME}/OctoPrint" && ok_msg "Directory removed!" - fi - - ###remove .octoprint directories - if ls -d "${HOME}"/.octoprint* 2>/dev/null 1>&2; then - for folder in $(ls -d ${HOME}/.octoprint*) - do - status_msg "Removing ${folder} ..." && rm -rf "${folder}" && ok_msg "Done!" +function remove_octoprint_env(){ + local files + files=$(find "${HOME}" -maxdepth 1 -regextype posix-extended -regex "${HOME}/OctoPrint(_[^0])?[0-9]*") + if [ -n "${files}" ]; then + for file in ${files}; do + status_msg "Removing ${file} ..." + rm -rf "${file}" + ok_msg "${file} removed!" done fi +} + +function remove_octoprint_dir(){ + local files + files=$(find "${HOME}" -maxdepth 1 -regextype posix-extended -regex "${HOME}/.octoprint(_[^0])?[0-9]*") + if [ -n "${files}" ]; then + for file in ${files}; do + status_msg "Removing ${file} ..." + rm -rf "${file}" + ok_msg "${file} removed!" + done + fi +} + +function remove_octoprint(){ + remove_octoprint_service + remove_octoprint_sudoers + remove_octoprint_env + remove_octoprint_dir ### remove octoprint_port from ~/.kiauh.ini sed -i "/^octoprint_port=/d" "${INI_FILE}" - print_confirm "OctoPrint successfully removed!" + local confirm="OctoPrint was successfully removed!" + print_confirm "${confirm}" && return } #=================================================# @@ -293,22 +279,14 @@ function remove_octoprint(){ #=================================================# function octoprint_status(){ - local sf_count status + local sf_count env_count dir_count status sf_count="$(octoprint_systemd | wc -w)" + env_count=$(find "${HOME}" -maxdepth 1 -regextype posix-extended -regex "${HOME}/OctoPrint(_[^0])?[0-9]*" | wc -w) + dir_count=$(find "${HOME}" -maxdepth 1 -regextype posix-extended -regex "${HOME}/.octoprint(_[^0])?[0-9]*" | wc -w) - ### remove the "SERVICE" entry from the data array if a moonraker service is installed - local data_arr=(SERVICE "${OCTOPRINT_DIR}") - [ "${sf_count}" -gt 0 ] && unset "data_arr[0]" - - ### count+1 for each found data-item from array - local filecount=0 - for data in "${data_arr[@]}"; do - [ -e "${data}" ] && filecount=$(("${filecount}" + 1)) - done - - if [ "${filecount}" == "${#data_arr[*]}" ]; then + if (( sf_count == env_count)) && (( sf_count == dir_count)); then status="$(printf "${green}Installed: %-5s${white}" "${sf_count}")" - elif [ "${filecount}" == 0 ]; then + elif (( sf_count == 0 )) && (( env_count == 0 )) && (( dir_count == 0 )); then status="${red}Not installed!${white} " else status="${yellow}Incomplete!${white} "