refactor: unlink moonraker install from klipper instances

chore: several shellcheck fixes
This commit is contained in:
th33xitus
2021-12-22 22:42:37 +01:00
parent 771191ab69
commit 4d834db5df

View File

@@ -9,79 +9,80 @@ system_check_moonraker(){
status_msg "Your Python 3 version is: $(python3 --version)"
major=$(python3 --version | cut -d" " -f2 | cut -d"." -f1)
minor=$(python3 --version | cut -d"." -f2)
if [ $major -ge 3 ] && [ $minor -ge 7 ]; then
ok_msg "Python version ok!"
if [ "$major" -ge 3 ] && [ "$minor" -ge 7 ]; then
py_chk_ok="true"
else
warn_msg "Python version not ok!"
py_chk_ok="false"
fi
}
moonraker_setup_dialog(){
status_msg "Initializing Moonraker installation ..."
### check for existing moonraker service installations
SERVICE_FILES=$(find "$SYSTEMDDIR" -regextype posix-extended -regex "$SYSTEMDDIR/moonraker(-[^0])?[0-9]*.service")
if [ -n "$SERVICE_FILES" ]; then
ERROR_MSG="At least one Moonraker service is already installed:"
for service in $SERVICE_FILES; do
ERROR_MSG="${ERROR_MSG}\n ➔ $service"
done && return
fi
status_msg "Initializing Moonraker installation ..."
### checking system for python3.7+
system_check_moonraker
### exit moonraker setup if python version is not ok
### exit moonraker setup if python versioncheck fails
if [ $py_chk_ok = "false" ]; then
ERROR_MSG="Python 3.7 or above required!\n Please upgrade your Python version first."
print_msg && clear_msg && return 0
ERROR_MSG="Versioncheck failed! Python 3.7 or newer required!\n"
ERROR_MSG="${ERROR_MSG} Please upgrade Python."
print_msg && clear_msg && return
fi
shopt -s extglob # enable extended globbing
### check for existing moonraker service installations
FILE="$SYSTEMDDIR/moonraker?(-*([0-9])).service"
if ls $FILE 2>/dev/null 1>&2; then
ERROR_MSG="At least one Moonraker service is already installed!" && return 0
fi
### check for existing klipper service installations
FILE="$SYSTEMDDIR/klipper?(-*([0-9])).service"
if ! ls $FILE 2>/dev/null 1>&2; then
ERROR_MSG="Klipper service not found, please install Klipper first!" && return 0
fi
shopt -u extglob # disable extended globbing
### count amount of klipper services
if ls /etc/systemd/system/klipper.service 2>/dev/null; then
INSTANCE_COUNT=1
else
INSTANCE_COUNT=$(ls /etc/systemd/system | grep -E "klipper-[[:digit:]]+.service" | wc -l)
SERVICE_FILES=$(find "$SYSTEMDDIR" -regextype posix-extended -regex "$SYSTEMDDIR/klipper(-[^0])+[0-9]*.service")
if [ -f /etc/init.d/klipper ] || [ -f /etc/systemd/system/klipper.service ]; then
KLIPPER_COUNT=1
elif [ -n "$SERVICE_FILES" ]; then
KLIPPER_COUNT=$(echo "$SERVICE_FILES" | wc -l)
fi
### initial moonraker.conf path check
check_klipper_cfg_path
### instance confirmation dialog
while true; do
echo
top_border
if [ $INSTANCE_COUNT -gt 1 ]; then
printf "|%-55s|\n" " $INSTANCE_COUNT Klipper instances were found!"
if [[ $KLIPPER_COUNT -eq 1 ]]; then
printf "|${green}%-55s${default}|\n" " 1 Klipper instance was found!"
elif [[ $KLIPPER_COUNT -gt 1 ]]; then
printf "|${green}%-55s${default}|\n" " $KLIPPER_COUNT Klipper instances were found!"
else
echo -e "| 1 Klipper instance was found! | "
echo -e "| ${yellow}INFO: No existing Klipper installation found!${default} |"
fi
echo -e "| You need one Moonraker instance per Klipper instance. | "
echo -e "| Usually you need one Moonraker instance per Klipper |"
echo -e "| instance. Though you can install as many as you wish. |"
bottom_border
echo
read -p "${cyan}###### Create $INSTANCE_COUNT Moonraker instances? (Y/n):${default} " yn
count=""
while [[ ! ($count =~ ^[1-9]+((0)+)?$) ]]; do
read -p "${cyan}###### Number of Moonraker instances to set up:${default} " count
if [[ ! ($count =~ ^[1-9]+((0)+)?$) ]]; then
warn_msg "Invalid Input!\n"
else
echo
INSTANCE_COUNT=$count
read -p "${cyan}###### Install $INSTANCE_COUNT instance(s)? (Y/n):${default} " yn
case "$yn" in
Y|y|Yes|yes|"")
echo -e "###### > Yes"
status_msg "Creating $INSTANCE_COUNT Moonraker instances ..."
status_msg "Installing Moonraker ...\n"
moonraker_setup
break;;
N|n|No|no)
echo -e "###### > No"
warn_msg "Exiting Moonraker setup ..."
echo
warn_msg "Exiting Moonraker setup ...\n"
break;;
*)
print_unkown_cmd
print_msg && clear_msg;;
esac
fi
done
}
@@ -95,8 +96,8 @@ moonraker_setup(){
### step 1: clone moonraker
status_msg "Downloading Moonraker ..."
### force remove existing moonraker dir and clone into fresh moonraker dir
[ -d $MOONRAKER_DIR ] && rm -rf $MOONRAKER_DIR
cd ${HOME} && git clone $MOONRAKER_REPO
[ -d "$MOONRAKER_DIR" ] && rm -rf "$MOONRAKER_DIR"
cd "${HOME}" && git clone $MOONRAKER_REPO
ok_msg "Download complete!"
### step 2: install moonraker dependencies and create python virtualenv
@@ -111,9 +112,11 @@ moonraker_setup(){
create_moonraker_service
### confirm message
if [[ $INSTANCE_COUNT -eq 1 ]]; then
CONFIRM_MSG="Moonraker has been set up!"
elif [[ $INSTANCE_COUNT -gt 1 ]]; then
CONFIRM_MSG="$INSTANCE_COUNT Moonraker instances have been set up!"
[ $INSTANCE_COUNT -eq 1 ] && CONFIRM_MSG="Moonraker has been set up!"
print_msg && clear_msg
fi && print_msg && clear_msg
### display moonraker ips to the user
print_mr_ip_list; echo
@@ -123,11 +126,11 @@ install_moonraker_packages(){
### read PKGLIST from official install script
status_msg "Reading dependencies..."
install_script="${HOME}/moonraker/scripts/install-moonraker.sh"
PKGLIST=$(grep "PKGLIST=" $install_script | sed 's/PKGLIST//g; s/[$={}\n"]//g')
### rewrite packages into new array
unset PKGARR
for PKG in $PKGLIST; do PKGARR+=($PKG); done
echo "${cyan}${PKGARR[@]}${default}"
PKGLIST=$(grep "PKGLIST=" "$install_script" | sed 's/PKGLIST//g; s/[$"{}=]//g; s/\s\s*/ /g' | tr -d '\n')
for pkg in $PKGLIST; do
echo "${cyan}$pkg${default}"
done
read -r -a PKGLIST <<< "$PKGLIST"
### Update system package info
status_msg "Running apt-get update..."
@@ -135,25 +138,24 @@ install_moonraker_packages(){
### Install desired packages
status_msg "Installing packages..."
sudo apt-get install --yes ${PKGARR[@]}
sudo apt-get install --yes "${PKGLIST[@]}"
}
create_moonraker_virtualenv(){
status_msg "Installing python virtual environment..."
### If venv exists and user prompts a rebuild, then do so
if [ -d ${MOONRAKER_ENV} ] && [ $REBUILD_ENV = "y" ]; then
if [ -d "$MOONRAKER_ENV" ] && [ "$REBUILD_ENV" = "y" ]; then
status_msg "Removing old virtualenv"
rm -rf ${MOONRAKER_ENV}
rm -rf "$MOONRAKER_ENV"
fi
if [ ! -d ${MOONRAKER_ENV} ]; then
virtualenv -p /usr/bin/python3 ${MOONRAKER_ENV}
ln -s /usr/lib/python3/dist-packages/gpiod* ${MOONRAKER_ENV}/lib/python*/site-packages
if [ ! -d "$MOONRAKER_ENV" ]; then
virtualenv -p /usr/bin/python3 "$MOONRAKER_ENV"
fi
### Install/update dependencies
${MOONRAKER_ENV}/bin/pip install -r ${MOONRAKER_DIR}/scripts/moonraker-requirements.txt
"$MOONRAKER_ENV"/bin/pip install -r "$MOONRAKER_DIR"/scripts/moonraker-requirements.txt
}
create_moonraker_service(){
@@ -173,7 +175,7 @@ create_moonraker_service(){
write_mr_service(){
if [ ! -f $MR_SERV_TARGET ]; then
status_msg "Creating Moonraker Service $i ..."
sudo cp $MR_SERV_SRC $MR_SERV_TARGET
sudo cp "$MR_SERV_SRC" $MR_SERV_TARGET
sudo sed -i "s|%INST%|$i|" $MR_SERV_TARGET
sudo sed -i "s|%USER%|${USER}|" $MR_SERV_TARGET
sudo sed -i "s|%MR_ENV%|$MR_ENV|" $MR_SERV_TARGET
@@ -183,7 +185,7 @@ create_moonraker_service(){
fi
}
if [ $SINGLE_INST -eq $INSTANCE_COUNT ]; then
if [[ $SINGLE_INST -eq $INSTANCE_COUNT ]]; then
### write single instance service
write_mr_service
### enable instance
@@ -193,7 +195,7 @@ create_moonraker_service(){
do_action_service "start" "moonraker"
else
i=1
while [ $i -le $INSTANCE_COUNT ]; do
while [[ $i -le $INSTANCE_COUNT ]]; do
### rewrite default variables for multi instance cases
CFG_PATH="$klipper_cfg_loc/printer_$i"
MR_SERV_TARGET="$SYSTEMDDIR/moonraker-$i.service"
@@ -212,7 +214,7 @@ create_moonraker_service(){
unset i
### enable mainsails remoteMode if mainsail is found
if [ -d $MAINSAIL_DIR ]; then
if [ -d "$MAINSAIL_DIR" ]; then
status_msg "Mainsail installation found!"
status_msg "Enabling Mainsail remoteMode ..."
enable_mainsail_remotemode
@@ -231,7 +233,7 @@ create_moonraker_conf(){
CFG_PATH="$klipper_cfg_loc"
LOG_PATH="${HOME}/klipper_logs"
MR_CONF="$CFG_PATH/moonraker.conf"
MR_DB="~/.moonraker_database"
MR_DB="${HOME}/.moonraker_database"
KLIPPY_UDS="/tmp/klippy_uds"
MR_CONF_SRC="${SRCDIR}/kiauh/resources/moonraker.conf"
mr_ip_list=()
@@ -239,23 +241,23 @@ create_moonraker_conf(){
LAN="$(hostname -I | cut -d" " -f1 | cut -d"." -f1-2).0.0/16"
write_mr_conf(){
[ ! -d $CFG_PATH ] && mkdir -p $CFG_PATH
if [ ! -f $MR_CONF ]; then
[ ! -d "$CFG_PATH" ] && mkdir -p "$CFG_PATH"
if [ ! -f "$MR_CONF" ]; then
status_msg "Creating moonraker.conf in $CFG_PATH ..."
cp $MR_CONF_SRC $MR_CONF
sed -i "s|%PORT%|$PORT|" $MR_CONF
sed -i "s|%CFG%|$CFG_PATH|" $MR_CONF
sed -i "s|%LOG%|$LOG_PATH|" $MR_CONF
sed -i "s|%MR_DB%|$MR_DB|" $MR_CONF
sed -i "s|%UDS%|$KLIPPY_UDS|" $MR_CONF
cp "$MR_CONF_SRC" "$MR_CONF"
sed -i "s|%PORT%|$PORT|" "$MR_CONF"
sed -i "s|%CFG%|$CFG_PATH|" "$MR_CONF"
sed -i "s|%LOG%|$LOG_PATH|" "$MR_CONF"
sed -i "s|%MR_DB%|$MR_DB|" "$MR_CONF"
sed -i "s|%UDS%|$KLIPPY_UDS|" "$MR_CONF"
# if host ip is not in the default ip ranges, replace placeholder
# otherwise remove placeholder from config
if ! grep -q $LAN $MR_CONF; then
sed -i "s|%LAN%|$LAN|" $MR_CONF
if ! grep -q "$LAN" "$MR_CONF"; then
sed -i "s|%LAN%|$LAN|" "$MR_CONF"
else
sed -i "/%LAN%/d" $MR_CONF
sed -i "/%LAN%/d" "$MR_CONF"
fi
sed -i "s|%USER%|${USER}|g" $MR_CONF
sed -i "s|%USER%|${USER}|g" "$MR_CONF"
ok_msg "moonraker.conf created!"
else
warn_msg "There is already a file called 'moonraker.conf'!"
@@ -263,17 +265,17 @@ create_moonraker_conf(){
fi
}
if [ $SINGLE_INST -eq $INSTANCE_COUNT ]; then
if [[ $SINGLE_INST -eq $INSTANCE_COUNT ]]; then
### write single instance config
write_mr_conf
mr_ip_list+=("$IP:$PORT")
else
i=1
while [ $i -le $INSTANCE_COUNT ]; do
while [[ $i -le $INSTANCE_COUNT ]]; do
### rewrite default variables for multi instance cases
CFG_PATH="$klipper_cfg_loc/printer_$i"
MR_CONF="$CFG_PATH/moonraker.conf"
MR_DB="~/.moonraker_database_$i"
MR_DB="${HOME}/.moonraker_database_$i"
KLIPPY_UDS="/tmp/klippy_uds-$i"
### write multi instance config
@@ -290,7 +292,7 @@ create_moonraker_conf(){
print_mr_ip_list(){
i=1
for ip in ${mr_ip_list[@]}; do
for ip in "${mr_ip_list[@]}"; do
echo -e " ${cyan}● Instance $i:${default} $ip"
i=$((i + 1))
done