refactor(utilities.sh): refactor change hostname functions
Signed-off-by: Dominik Willner th33xitus@gmail.com
This commit is contained in:
@@ -53,7 +53,7 @@ function advanced_menu(){
|
|||||||
7)
|
7)
|
||||||
clear
|
clear
|
||||||
print_header
|
print_header
|
||||||
create_custom_hostname && set_hostname
|
set_custom_hostname
|
||||||
advanced_ui;;
|
advanced_ui;;
|
||||||
8)
|
8)
|
||||||
do_action "setup_gcode_shell_command" "advanced_ui";;
|
do_action "setup_gcode_shell_command" "advanced_ui";;
|
||||||
|
|||||||
@@ -604,11 +604,12 @@ function check_usergroups(){
|
|||||||
fi
|
fi
|
||||||
}
|
}
|
||||||
|
|
||||||
function create_custom_hostname(){
|
function set_custom_hostname(){
|
||||||
echo
|
echo
|
||||||
top_border
|
top_border
|
||||||
echo -e "| You can change the hostname of this machine to use |"
|
echo -e "| Changing the hostname of this machine allows you to |"
|
||||||
echo -e "| that name to open the Interface in your browser. |"
|
echo -e "| access a webinterface that is configured for port 80 |"
|
||||||
|
echo -e "| by simply typing '<hostname>.local' in the browser. |"
|
||||||
echo -e "| |"
|
echo -e "| |"
|
||||||
echo -e "| E.g.: If you set the hostname to 'my-printer' you |"
|
echo -e "| E.g.: If you set the hostname to 'my-printer' you |"
|
||||||
echo -e "| can open Mainsail / Fluidd / Octoprint by |"
|
echo -e "| can open Mainsail / Fluidd / Octoprint by |"
|
||||||
@@ -618,20 +619,20 @@ function create_custom_hostname(){
|
|||||||
read -p "${cyan}###### Do you want to change the hostname? (y/N):${white} " yn
|
read -p "${cyan}###### Do you want to change the hostname? (y/N):${white} " yn
|
||||||
case "${yn}" in
|
case "${yn}" in
|
||||||
Y|y|Yes|yes)
|
Y|y|Yes|yes)
|
||||||
user_input_hostname
|
select_msg "Yes"
|
||||||
|
change_hostname
|
||||||
|
break;;
|
||||||
|
N|n|No|no|"")
|
||||||
|
select_msg "No"
|
||||||
break;;
|
break;;
|
||||||
N|n|No|no|"") break;;
|
|
||||||
*)
|
*)
|
||||||
print_unkown_cmd
|
error_msg "Invalid command!";;
|
||||||
print_msg && clear_msg;;
|
|
||||||
esac
|
esac
|
||||||
done
|
done
|
||||||
}
|
}
|
||||||
|
|
||||||
function user_input_hostname(){
|
function change_hostname(){
|
||||||
unset NEW_HOSTNAME
|
local new_hostname
|
||||||
unset HOSTNAME_VALID
|
|
||||||
unset HOSTENAME_CONFIRM
|
|
||||||
echo
|
echo
|
||||||
top_border
|
top_border
|
||||||
echo -e "| ${green}Allowed characters: a-z, 0-9 and single '-'${white} |"
|
echo -e "| ${green}Allowed characters: a-z, 0-9 and single '-'${white} |"
|
||||||
@@ -639,60 +640,56 @@ function user_input_hostname(){
|
|||||||
echo -e "| ${red}No leading or trailing '-' allowed!${white} |"
|
echo -e "| ${red}No leading or trailing '-' allowed!${white} |"
|
||||||
bottom_border
|
bottom_border
|
||||||
while true; do
|
while true; do
|
||||||
read -p "${cyan}###### Please set the new hostname:${white} " NEW_HOSTNAME
|
read -p "${cyan}###### Please set the new hostname:${white} " new_hostname
|
||||||
if [[ ${NEW_HOSTNAME} =~ ^[^\-\_]+([0-9a-z]\-{0,1})+[^\-\_]+$ ]]; then
|
if [[ ${new_hostname} =~ ^[^\-\_]+([0-9a-z]\-{0,1})+[^\-\_]+$ ]]; then
|
||||||
ok_msg "'${NEW_HOSTNAME}' is a valid hostname!"
|
|
||||||
HOSTNAME_VALID="true"
|
|
||||||
while true; do
|
while true; do
|
||||||
echo
|
echo
|
||||||
read -p "${cyan}###### Do you want '${NEW_HOSTNAME}' to be the new hostname? (Y/n):${white} " yn
|
read -p "${cyan}###### Do you want '${new_hostname}' to be the new hostname? (Y/n):${white} " yn
|
||||||
case "${yn}" in
|
case "${yn}" in
|
||||||
Y|y|Yes|yes|"")
|
Y|y|Yes|yes|"")
|
||||||
echo -e "###### > Yes"
|
select_msg "Yes"
|
||||||
HOSTENAME_CONFIRM="true"
|
set_hostname "${new_hostname}"
|
||||||
break;;
|
break;;
|
||||||
N|n|No|no)
|
N|n|No|no)
|
||||||
echo -e "###### > No"
|
select_msg "No"
|
||||||
echo -e "${red}Skip hostname change ...${white}"
|
abort_msg "Skip hostname change ..."
|
||||||
HOSTENAME_CONFIRM="false"
|
|
||||||
break;;
|
break;;
|
||||||
*)
|
*)
|
||||||
print_error "Invalid command!";;
|
print_error "Invalid command!";;
|
||||||
esac
|
esac
|
||||||
done
|
done
|
||||||
break
|
|
||||||
else
|
else
|
||||||
warn_msg "'${NEW_HOSTNAME}' is not a valid hostname!"
|
warn_msg "'${new_hostname}' is not a valid hostname!"
|
||||||
fi
|
fi
|
||||||
|
break
|
||||||
done
|
done
|
||||||
}
|
}
|
||||||
|
|
||||||
function set_hostname(){
|
function set_hostname(){
|
||||||
if [ "${HOSTNAME_VALID}" = "true" ] && [ "${HOSTENAME_CONFIRM}" = "true" ]; then
|
local new_hostname=${1} current_date
|
||||||
#check for dependencies
|
#check for dependencies
|
||||||
dep=(avahi-daemon)
|
dep=(avahi-daemon)
|
||||||
dependency_check "${dep[@]}"
|
dependency_check "${dep[@]}"
|
||||||
#execute operations
|
|
||||||
#get current hostname and write to variable
|
|
||||||
HOSTNAME=$(hostname)
|
|
||||||
#create host file if missing or create backup of existing one with current date&time
|
#create host file if missing or create backup of existing one with current date&time
|
||||||
if [ -f /etc/hosts ]; then
|
if [ -f /etc/hosts ]; then
|
||||||
|
current_date=$(get_date)
|
||||||
status_msg "Creating backup of hosts file ..."
|
status_msg "Creating backup of hosts file ..."
|
||||||
get_date
|
|
||||||
sudo cp "/etc/hosts /etc/hosts.${current_date}.bak"
|
sudo cp "/etc/hosts /etc/hosts.${current_date}.bak"
|
||||||
ok_msg "Backup done!"
|
ok_msg "Backup done!"
|
||||||
ok_msg "File:'/etc/hosts.${current_date}.bak'"
|
ok_msg "File:'/etc/hosts.${current_date}.bak'"
|
||||||
else
|
else
|
||||||
sudo touch /etc/hosts
|
sudo touch /etc/hosts
|
||||||
fi
|
fi
|
||||||
#set hostname in /etc/hostname
|
|
||||||
status_msg "Setting hostname to '${NEW_HOSTNAME}' ..."
|
#set new hostname in /etc/hostname
|
||||||
|
status_msg "Setting hostname to '${new_hostname}' ..."
|
||||||
status_msg "Please wait ..."
|
status_msg "Please wait ..."
|
||||||
sudo hostnamectl set-hostname "${NEW_HOSTNAME}"
|
sudo hostnamectl set-hostname "${new_hostname}"
|
||||||
|
|
||||||
#write new hostname to /etc/hosts
|
#write new hostname to /etc/hosts
|
||||||
status_msg "Writing new hostname to /etc/hosts ..."
|
status_msg "Writing new hostname to /etc/hosts ..."
|
||||||
echo "127.0.0.1 ${NEW_HOSTNAME}" | sudo tee -a /etc/hosts &>/dev/null
|
echo "127.0.0.1 ${new_hostname}" | sudo tee -a /etc/hosts &>/dev/null
|
||||||
ok_msg "New hostname successfully configured!"
|
ok_msg "New hostname successfully configured!"
|
||||||
ok_msg "Remember to reboot for the changes to take effect!"
|
ok_msg "Remember to reboot for the changes to take effect!"
|
||||||
fi
|
|
||||||
}
|
}
|
||||||
Reference in New Issue
Block a user