feat(fluidd): use fluidd-config repo for downloading fluidd macros (#305)

This commit is contained in:
Pedro Lamas
2023-02-28 15:33:32 +00:00
committed by GitHub
parent e3ed223b5c
commit afe6f7499a
2 changed files with 119 additions and 75 deletions

View File

@@ -39,30 +39,6 @@ function install_fluidd() {
### check if another site already listens to port 80
fluidd_port_check
# ### ask user to install mjpg-streamer
# local install_mjpg_streamer
# if [[ ! -f "${SYSTEMD}/webcamd.service" ]]; then
# while true; do
# echo
# top_border
# echo -e "| Install MJPG-Streamer for webcam support? |"
# bottom_border
# read -p "${cyan}###### Please select (y/N):${white} " yn
# case "${yn}" in
# Y|y|Yes|yes)
# select_msg "Yes"
# install_mjpg_streamer="true"
# break;;
# N|n|No|no|"")
# select_msg "No"
# install_mjpg_streamer="false"
# break;;
# *)
# error_msg "Invalid command!";;
# esac
# done
# fi
### download fluidd
download_fluidd
@@ -82,9 +58,6 @@ function install_fluidd() {
### add fluidd to the update manager in moonraker.conf
patch_fluidd_update_manager
### install mjpg-streamer
# [[ ${install_mjpg_streamer} == "true" ]] && install_mjpg-streamer
fetch_webui_ports #WIP
### confirm message
@@ -92,22 +65,21 @@ function install_fluidd() {
}
function install_fluidd_macros() {
local yn
while true; do
echo
top_border
echo -e "| It is recommended to have some important macros in |"
echo -e "| your printer configuration to have Fluidd fully |"
echo -e "| functional and working. |"
echo -e "| It is recommended to use special macros in order to |"
echo -e "| have Fluidd fully functional and working. |"
blank_line
echo -e "| The recommended macros for Fluidd can be found here: |"
echo -e "| https://docs.fluidd.xyz/configuration/initial_setup |"
echo -e "| https://github.com/fluidd-core/fluidd-config |"
blank_line
echo -e "| If you already have these macros in your config file, |"
echo -e "| skip this step and answer with 'no'. |"
echo -e "| If you already use these macros skip this step. |"
echo -e "| Otherwise you should consider to answer with 'yes' to |"
echo -e "| add the recommended example macros to your config. |"
echo -e "| download the recommended macros. |"
bottom_border
read -p "${cyan}###### Add the recommended macros? (Y/n):${white} " yn
read -p "${cyan}###### Download the recommended macros? (Y/n):${white} " yn
case "${yn}" in
Y|y|Yes|yes|"")
select_msg "Yes"
@@ -124,36 +96,64 @@ function install_fluidd_macros() {
}
function download_fluidd_macros() {
local fluidd_cfg path configs regex
local ms_cfg_repo path configs regex line gcode_dir
fluidd_cfg="https://raw.githubusercontent.com/fluidd-core/FluiddPI/master/src/modules/fluidd/filesystem/home/pi/klipper_config/fluidd.cfg"
ms_cfg_repo="https://github.com/fluidd-core/fluidd-config.git"
regex="${HOME//\//\\/}\/([A-Za-z0-9_]+)\/config\/printer\.cfg"
configs=$(find "${HOME}" -maxdepth 3 -regextype posix-extended -regex "${regex}" | sort)
if [[ -n ${configs} ]]; then
for config in ${configs}; do
path=$(echo "${config}" | rev | cut -d"/" -f2- | rev)
if [[ ! -f "${path}/fluidd.cfg" ]]; then
status_msg "Downloading fluidd.cfg to ${path} ..."
log_info "downloading fluidd.cfg to: ${path}"
wget "${fluidd_cfg}" -O "${path}/fluidd.cfg"
### replace user 'pi' with current username to prevent issues in cases where the user is not called 'pi'
log_info "modify fluidd.cfg"
sed -i "/^path: \/home\/pi\/gcode_files/ s/\/home\/pi/${HOME//\//\\/}/" "${path}/fluidd.cfg"
### write include to the very first line of the printer.cfg
if ! grep -Eq "^[include fluidd.cfg]$" "${path}/printer.cfg"; then
log_info "modify printer.cfg"
sed -i "1 i [include fluidd.cfg]" "${path}/printer.cfg"
fi
ok_msg "Done!"
fi
done
else
if [[ -z ${configs} ]]; then
print_error "No printer.cfg found! Installation of Macros will be skipped ..."
log_error "execution stopped! reason: no printer.cfg found"
return
fi
status_msg "Cloning fluidd-config ..."
[[ -d "${HOME}/fluidd-config" ]] && rm -rf "${HOME}/fluidd-config"
if git clone --recurse-submodules "${ms_cfg_repo}" "${HOME}/fluidd-config"; then
for config in ${configs}; do
path=$(echo "${config}" | rev | cut -d"/" -f2- | rev)
if [[ -e "${path}/fluidd.cfg" && ! -h "${path}/fluidd.cfg" ]]; then
warn_msg "Attention! Existing fluidd.cfg detected!"
warn_msg "The file will be renamed to 'fluidd.bak.cfg' to be able to continue with the installation."
if ! mv "${path}/fluidd.cfg" "${path}/fluidd.bak.cfg"; then
error_msg "Renaming fluidd.cfg failed! Aborting installation ..."
return
fi
fi
if [[ -h "${path}/fluidd.cfg" ]]; then
warn_msg "Recreating symlink in ${path} ..."
rm -rf "${path}/fluidd.cfg"
fi
if ! ln -sf "${HOME}/fluidd-config/client.cfg" "${path}/fluidd.cfg"; then
error_msg "Creating symlink failed! Aborting installation ..."
return
fi
if ! grep -Eq "^\[include fluidd.cfg\]$" "${path}/printer.cfg"; then
log_info "${path}/printer.cfg"
sed -i "1 i [include fluidd.cfg]" "${path}/printer.cfg"
fi
line=$(($(grep -n "\[include fluidd.cfg\]" "${path}/printer.cfg" | tail -1 | cut -d: -f1) + 1))
gcode_dir=${path/config/gcodes}
if ! grep -Eq "^\[virtual_sdcard\]$" "${path}/printer.cfg"; then
log_info "${path}/printer.cfg"
sed -i "${line} i \[virtual_sdcard]\npath: ${gcode_dir}\non_error_gcode: CANCEL_PRINT\n" "${path}/printer.cfg"
fi
done
else
print_error "Cloning failed! Aborting installation ..."
log_error "execution stopped! reason: cloning failed"
return
fi
patch_fluidd_config_update_manager
ok_msg "Done!"
}
function download_fluidd() {
@@ -191,7 +191,7 @@ function remove_fluidd_dir() {
rm -rf "${FLUIDD_DIR}" && ok_msg "Directory removed!"
}
function remove_fluidd_config() {
function remove_fluidd_nginx_config() {
if [[ -e "/etc/nginx/sites-available/fluidd" ]]; then
status_msg "Removing Fluidd configuration for Nginx ..."
sudo rm "/etc/nginx/sites-available/fluidd" && ok_msg "File removed!"
@@ -243,9 +243,18 @@ function remove_legacy_fluidd_log_symlinks() {
fi
}
function remove_fluidd_config() {
if [[ -d "${HOME}/fluidd-config" ]]; then
status_msg "Removing ${HOME}/fluidd-config ..."
rm -rf "${HOME}/fluidd-config"
ok_msg "${HOME}/fluidd-config removed!"
print_confirm "Fluidd-Config successfully removed!"
fi
}
function remove_fluidd() {
remove_fluidd_dir
remove_fluidd_config
remove_fluidd_nginx_config
remove_fluidd_logs
remove_fluidd_log_symlinks
remove_legacy_fluidd_log_symlinks
@@ -389,7 +398,7 @@ function select_fluidd_port() {
blank_line
[[ ${MAINSAIL_PORT} == "80" ]] && echo "| ● Mainsail |"
blank_line
echo -e "| Make sure you don't choose a port which is already |"
echo -e "| Make sure you don't choose a port which was already |"
echo -e "| assigned to another webinterface! |"
blank_line
echo -e "| Be aware: there is ${red}NO${white} sanity check for the following |"
@@ -445,3 +454,36 @@ MOONRAKER_CONF
do_action_service "restart" "moonraker"
fi
}
function patch_fluidd_config_update_manager() {
local patched moonraker_configs regex
regex="${HOME//\//\\/}\/([A-Za-z0-9_]+)\/config\/moonraker\.conf"
moonraker_configs=$(find "${HOME}" -maxdepth 3 -type f -regextype posix-extended -regex "${regex}" | sort)
patched="false"
for conf in ${moonraker_configs}; do
if ! grep -Eq "^\[update_manager fluidd-config\]\s*$" "${conf}"; then
### add new line to conf if it doesn't end with one
[[ $(tail -c1 "${conf}" | wc -l) -eq 0 ]] && echo "" >> "${conf}"
### add Fluidds update manager section to moonraker.conf
status_msg "Adding Fluidd-Config to update manager in file:\n ${conf}"
/bin/sh -c "cat >> ${conf}" << MOONRAKER_CONF
[update_manager fluidd-config]
type: git_repo
primary_branch: master
path: ~/fluidd-config
origin: https://github.com/fluidd-core/fluidd-config.git
managed_services: klipper
MOONRAKER_CONF
fi
patched="true"
done
if [[ ${patched} == "true" ]]; then
do_action_service "restart" "moonraker"
fi
}