From e4cfad41624040c2f32ee66bdfbb71e1f216bf6d Mon Sep 17 00:00:00 2001 From: th33xitus <> Date: Sat, 18 Jul 2020 18:33:55 +0200 Subject: [PATCH] new function: allow hostname to be set --- scripts/functions.sh | 75 +++++++++++++++++++++++++++++++++++++ scripts/install_mainsail.sh | 16 +++++--- 2 files changed, 86 insertions(+), 5 deletions(-) diff --git a/scripts/functions.sh b/scripts/functions.sh index 3886f3f..ac8ad21 100644 --- a/scripts/functions.sh +++ b/scripts/functions.sh @@ -254,4 +254,79 @@ read_octoprint_service_status(){ else OPRINT_SERVICE_STATUS="${red}[Disable]${default} OctoPrint Service " fi +} + +create_custom_hostname(){ + echo + top_border + echo -e "| You can change the hostname of this machine to use |" + echo -e "| that name to open the Interface in your browser. |" + echo -e "| This option is completely optional and can also be |" + echo -e "| done at a later point in time if you are unsure. |" + echo -e "| |" + echo -e "| Example: If you set the hostname to 'my-printer' |" + echo -e "| you can open Mainsail by browsing to: |" + echo -e "| http://my-printer.local |" + bottom_border + while true; do + echo -e "${cyan}" + read -p "###### Do you want to change the hostname? (Y/n): " yn + echo -e "${default}" + case "$yn" in + Y|y|Yes|yes|"") set_hostname; break;; + N|n|No|no) break;; + esac + done +} + +set_hostname(){ + #check for existing avahi installation + status_msg "Checking for necessary Avahi installation ..." + if ! [[ $(dpkg-query -f'${Status}' --show avahi-daemon 2>/dev/null) = *\ installed ]]; then + status_msg "Installing Avahi ..." + sudo apt-get install avahi-daemon -y && ok_msg "Avahi successfully installed!" + fi + ok_msg "Avahi found!" + #get current hostname and write to variable + HOSTNAME=$(hostname) + #create host file if missing or create backup of existing one with current date&time + if [ -f /etc/hosts ]; then + status_msg "Creating backup of hosts file ..." + get_date + sudo cp /etc/hosts /etc/hosts."$current_date".bak + ok_msg "Backup done!" + ok_msg "File:'/etc/hosts."$current_date".bak'" + else + sudo touch /etc/hosts + fi + echo + top_border + echo -e "| ${green}Allowed characters: a-z, 0-9 and single '-'${default} |" + echo -e "| ${red}No special characters allowed!${default} |" + echo -e "| ${red}No leading or trailing '-' allowed!${default} |" + bottom_border + while true; do + echo -e "${cyan}" + read -p "###### Please set the new hostname: " NEW_HOSTNAME + echo -e "${default}" + if [[ $NEW_HOSTNAME =~ ^[^\-]+([0-9a-z]\-{0,1})+[^\-]+$ ]]; then + ok_msg "'$NEW_HOSTNAME' is a valid hostname!" + #set hostname in /etc/hostname + status_msg "Setting hostname to '$NEW_HOSTNAME' ..." + status_msg "Please wait ..." + sudo hostnamectl set-hostname $NEW_HOSTNAME + #write new hostname to /etc/hosts + status_msg "Writing new hostname to /etc/hosts ..." + if cat /etc/hosts | grep "###set by kiauh" &>/dev/null; then + sudo sed -i "/###set by kiauh/s/\<$HOSTNAME\>/$NEW_HOSTNAME/" /etc/hosts + else + echo "127.0.0.1 $NEW_HOSTNAME ###set by kiauh" | sudo tee -a /etc/hosts &>/dev/null + fi + ok_msg "New hostname successfully configured!" + ok_msg "You need to reboot your machine for changes to take effect!" + break + else + warn_msg "'$NEW_HOSTNAME' is not a valid hostname!" + fi + done } \ No newline at end of file diff --git a/scripts/install_mainsail.sh b/scripts/install_mainsail.sh index e59233c..66ebbe5 100644 --- a/scripts/install_mainsail.sh +++ b/scripts/install_mainsail.sh @@ -6,8 +6,8 @@ mainsail_install_routine(){ #execute operation #disable octoprint service if installed disable_octoprint_service - disable_wrong_webserver - remove_wrong_webserver + disable_haproxy_lighttpd + remove_haproxy_lighttpd install_moonraker if [ "$ERROR" != 1 ]; then check_printer_cfg @@ -16,7 +16,9 @@ mainsail_install_routine(){ config_nginx_mainsail test_api test_nginx - install_mainsail && ok_msg "Mainsail installation complete!"; echo + install_mainsail + create_custom_hostname + ok_msg "Mainsail installation complete!"; echo fi else ERROR_MSG=" Please install Klipper first!\n Skipping..." @@ -168,7 +170,7 @@ gcode: DEFAULT_CFG } -disable_wrong_webserver(){ +disable_haproxy_lighttpd(){ if systemctl is-active haproxy -q; then status_msg "Stopping haproxy service ..." sudo /etc/init.d/haproxy stop && ok_msg "Service stopped!" @@ -179,7 +181,7 @@ disable_wrong_webserver(){ fi } -remove_wrong_webserver(){ +remove_haproxy_lighttpd(){ rem=(haproxy lighttpd) for remove in "${rem[@]}" do @@ -193,6 +195,7 @@ remove_wrong_webserver(){ } config_nginx_mainsail(){ + USER=$(whoami) if ! [[ $(dpkg-query -f'${Status}' --show nginx 2>/dev/null) = *\ installed ]]; then status_msg "Installing Nginx ..." sudo apt-get install nginx -y && ok_msg "Nginx successfully installed!" @@ -203,6 +206,8 @@ config_nginx_mainsail(){ status_msg "Create Nginx configuration ..." cat ${HOME}/kiauh/resources/mainsail_nginx.cfg > ${HOME}/kiauh/resources/mainsail sudo mv ${HOME}/kiauh/resources/mainsail /etc/nginx/sites-available/mainsail + #make sure the config is for the correct user + sudo sed -i "/root/s/pi/$USER/" /etc/nginx/sites-available/mainsail if [ -e /etc/nginx/sites-enabled/default ]; then sudo rm /etc/nginx/sites-enabled/default fi @@ -258,4 +263,5 @@ install_mainsail(){ wget -q -O mainsail.zip $MAINSAIL_URL && status_msg "Extracting archive ..." && unzip -o mainsail.zip && rm mainsail.zip ### write mainsail version to file for update check reasons echo "$MAINSAIL_VERSION" > $MAINSAIL_DIR/version + echo } \ No newline at end of file