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
This commit is contained in:
@@ -54,9 +54,6 @@ function set_globals(){
|
|||||||
KLIPPERSCREEN_DIR="${HOME}/KlipperScreen"
|
KLIPPERSCREEN_DIR="${HOME}/KlipperScreen"
|
||||||
KLIPPERSCREEN_REPO=https://github.com/jordanruthe/KlipperScreen.git
|
KLIPPERSCREEN_REPO=https://github.com/jordanruthe/KlipperScreen.git
|
||||||
|
|
||||||
#================ OCTOPRINT ===================#
|
|
||||||
OCTOPRINT_ENV="${HOME}/OctoPrint"
|
|
||||||
|
|
||||||
#========== MOONRAKER-TELEGRAM-BOT ============#
|
#========== MOONRAKER-TELEGRAM-BOT ============#
|
||||||
MOONRAKER_TELEGRAM_BOT_ENV_DIR=${HOME}/moonraker-telegram-bot-env
|
MOONRAKER_TELEGRAM_BOT_ENV_DIR=${HOME}/moonraker-telegram-bot-env
|
||||||
MOONRAKER_TELEGRAM_BOT_DIR=${HOME}/moonraker-telegram-bot
|
MOONRAKER_TELEGRAM_BOT_DIR=${HOME}/moonraker-telegram-bot
|
||||||
|
|||||||
@@ -22,10 +22,10 @@ function octoprint_systemd() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
function octoprint_setup_dialog(){
|
function octoprint_setup_dialog(){
|
||||||
status_msg "Initializing OctoPrint installation ..."
|
|
||||||
|
|
||||||
local klipper_count
|
local klipper_count
|
||||||
klipper_count=$(klipper_systemd | wc -w)
|
klipper_count=$(klipper_systemd | wc -w)
|
||||||
|
|
||||||
|
status_msg "Initializing OctoPrint installation ..."
|
||||||
top_border
|
top_border
|
||||||
if [ -f "${INITD}/klipper" ] || [ -f "${SYSTEMD}/klipper.service" ]; then
|
if [ -f "${INITD}/klipper" ] || [ -f "${SYSTEMD}/klipper.service" ]; then
|
||||||
printf "|${green}%-55s${white}|\n" " 1 Klipper instance was found!"
|
printf "|${green}%-55s${white}|\n" " 1 Klipper instance was found!"
|
||||||
@@ -71,12 +71,12 @@ function octoprint_setup(){
|
|||||||
dep=(
|
dep=(
|
||||||
git
|
git
|
||||||
wget
|
wget
|
||||||
python-pip
|
python3-pip
|
||||||
python-dev
|
python3-dev
|
||||||
libyaml-dev
|
libyaml-dev
|
||||||
build-essential
|
build-essential
|
||||||
python-setuptools
|
python3-setuptools
|
||||||
python-virtualenv
|
python3-virtualenv
|
||||||
)
|
)
|
||||||
dependency_check "${dep[@]}"
|
dependency_check "${dep[@]}"
|
||||||
|
|
||||||
@@ -85,14 +85,10 @@ function octoprint_setup(){
|
|||||||
add_reboot_permission
|
add_reboot_permission
|
||||||
|
|
||||||
### install octoprint
|
### install octoprint
|
||||||
install_octoprint
|
install_octoprint "${instances}"
|
||||||
|
|
||||||
### set up instances
|
### set up service
|
||||||
if [ "${instances}" -eq 1 ]; then
|
create_octoprint_service "${instances}"
|
||||||
create_single_octoprint_instance
|
|
||||||
else
|
|
||||||
create_multi_octoprint_instance "${instances}"
|
|
||||||
fi
|
|
||||||
|
|
||||||
### step 6: enable and start all instances
|
### step 6: enable and start all instances
|
||||||
do_action_service "enable" "octoprint"
|
do_action_service "enable" "octoprint"
|
||||||
@@ -106,24 +102,72 @@ function octoprint_setup(){
|
|||||||
}
|
}
|
||||||
|
|
||||||
function install_octoprint(){
|
function install_octoprint(){
|
||||||
### create and activate the virtualenv
|
local i=1 instances=${1} octo_env
|
||||||
[ ! -d "${OCTOPRINT_ENV}" ] && mkdir -p "${OCTOPRINT_ENV}"
|
while (( i <= instances )); do
|
||||||
status_msg "Installing python virtual environment..."
|
(( instances == 1 )) && octo_env="${HOME}/OctoPrint"
|
||||||
cd "${OCTOPRINT_ENV}" && virtualenv --python=python3 venv
|
(( instances > 1 )) && octo_env="${HOME}/OctoPrint_${i}"
|
||||||
### activate virtualenv
|
### create and activate the virtualenv
|
||||||
source venv/bin/activate
|
status_msg "Installing python virtual environment..."
|
||||||
status_msg "Installing OctoPrint ..."
|
[ ! -d "${octo_env}" ] && mkdir -p "${octo_env}"
|
||||||
pip install pip --upgrade
|
cd "${octo_env}" && virtualenv --python=python3 venv
|
||||||
pip install --no-cache-dir octoprint
|
### activate virtualenv
|
||||||
ok_msg "Download complete!"
|
source venv/bin/activate
|
||||||
### leave virtualenv
|
(( instances == 1 )) && status_msg "Installing OctoPrint ..."
|
||||||
deactivate
|
(( 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(){
|
function create_octoprint_service(){
|
||||||
local basedir=${1} tmp_printer=${2} restart_cmd=${3}
|
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:
|
serial:
|
||||||
additionalPorts:
|
additionalPorts:
|
||||||
- ${tmp_printer}
|
- ${tmp_printer}
|
||||||
@@ -135,88 +179,11 @@ server:
|
|||||||
systemRestartCommand: sudo shutdown -r now
|
systemRestartCommand: sudo shutdown -r now
|
||||||
systemShutdownCommand: sudo shutdown -h now
|
systemShutdownCommand: sudo shutdown -h now
|
||||||
CONFIGYAML
|
CONFIGYAML
|
||||||
}
|
ok_msg "Ok!"
|
||||||
|
|
||||||
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!"
|
|
||||||
fi
|
fi
|
||||||
}
|
|
||||||
|
|
||||||
function create_multi_octoprint_instance(){
|
port=$((port+1))
|
||||||
local i=1 port=5000 instances=${1}
|
i=$((i+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))
|
|
||||||
done
|
done
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -247,10 +214,10 @@ function print_op_ip_list(){
|
|||||||
#=============== REMOVE OCTOPRINT ================#
|
#=============== REMOVE OCTOPRINT ================#
|
||||||
#=================================================#
|
#=================================================#
|
||||||
|
|
||||||
function remove_octoprint(){
|
function remove_octoprint_service(){
|
||||||
###remove all octoprint services
|
###remove all octoprint services
|
||||||
[ -z "$(octoprint_systemd)" ] && return
|
[ -z "$(octoprint_systemd)" ] && return
|
||||||
status_msg "Removing Moonraker Systemd Services ..."
|
status_msg "Removing OctoPrint Systemd Services ..."
|
||||||
for service in $(octoprint_systemd | cut -d"/" -f5)
|
for service in $(octoprint_systemd | cut -d"/" -f5)
|
||||||
do
|
do
|
||||||
status_msg "Removing ${service} ..."
|
status_msg "Removing ${service} ..."
|
||||||
@@ -262,30 +229,49 @@ function remove_octoprint(){
|
|||||||
### reloading units
|
### reloading units
|
||||||
sudo systemctl daemon-reload
|
sudo systemctl daemon-reload
|
||||||
sudo systemctl reset-failed
|
sudo systemctl reset-failed
|
||||||
|
}
|
||||||
|
|
||||||
|
function remove_octoprint_sudoers(){
|
||||||
|
[ ! -f /etc/sudoers.d/octoprint-shutdown ] && return
|
||||||
### remove sudoers file
|
### remove sudoers file
|
||||||
if [ -f /etc/sudoers.d/octoprint-shutdown ]; then
|
sudo rm -f /etc/sudoers.d/octoprint-shutdown
|
||||||
sudo rm -rf /etc/sudoers.d/octoprint-shutdown
|
}
|
||||||
fi
|
|
||||||
|
|
||||||
### remove OctoPrint directory
|
function remove_octoprint_env(){
|
||||||
if [ -d "${HOME}/OctoPrint" ]; then
|
local files
|
||||||
status_msg "Removing OctoPrint directory ..."
|
files=$(find "${HOME}" -maxdepth 1 -regextype posix-extended -regex "${HOME}/OctoPrint(_[^0])?[0-9]*")
|
||||||
rm -rf "${HOME}/OctoPrint" && ok_msg "Directory removed!"
|
if [ -n "${files}" ]; then
|
||||||
fi
|
for file in ${files}; do
|
||||||
|
status_msg "Removing ${file} ..."
|
||||||
###remove .octoprint directories
|
rm -rf "${file}"
|
||||||
if ls -d "${HOME}"/.octoprint* 2>/dev/null 1>&2; then
|
ok_msg "${file} removed!"
|
||||||
for folder in $(ls -d ${HOME}/.octoprint*)
|
|
||||||
do
|
|
||||||
status_msg "Removing ${folder} ..." && rm -rf "${folder}" && ok_msg "Done!"
|
|
||||||
done
|
done
|
||||||
fi
|
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
|
### remove octoprint_port from ~/.kiauh.ini
|
||||||
sed -i "/^octoprint_port=/d" "${INI_FILE}"
|
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(){
|
function octoprint_status(){
|
||||||
local sf_count status
|
local sf_count env_count dir_count status
|
||||||
sf_count="$(octoprint_systemd | wc -w)"
|
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
|
if (( sf_count == env_count)) && (( sf_count == dir_count)); then
|
||||||
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
|
|
||||||
status="$(printf "${green}Installed: %-5s${white}" "${sf_count}")"
|
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} "
|
status="${red}Not installed!${white} "
|
||||||
else
|
else
|
||||||
status="${yellow}Incomplete!${white} "
|
status="${yellow}Incomplete!${white} "
|
||||||
|
|||||||
Reference in New Issue
Block a user