refactor: multiple

Signed-off-by: Dominik Willner th33xitus@gmail.com
This commit is contained in:
th33xitus
2022-04-18 10:53:11 +02:00
parent 0f43a4d61f
commit 6f85ad6815
15 changed files with 270 additions and 299 deletions

View File

@@ -132,7 +132,7 @@ function remove_fluidd(){
#================== UPDATE FLUIDD ==================#
#===================================================#
update_fluidd(){
function update_fluidd(){
bb4u "fluidd"
status_msg "Updating Fluidd ..."
fluidd_setup

View File

@@ -471,3 +471,55 @@ function compare_moonraker_versions(){
fi
echo "${versions}"
}
#==================================================#
#==================== HELPERS =====================#
#==================================================#
function update_log_paths(){
### update services to make use of moonrakers new log_path option
### https://github.com/Arksine/moonraker/commit/829b3a4ee80579af35dd64a37ccc092a1f67682a
shopt -s extglob # enable extended globbing
source_kiauh_ini
LPATH="${HOME}/klipper_logs"
[ ! -d "$LPATH" ] && mkdir -p "$LPATH"
FILE="$SYSTEMDDIR/$1?(-*([0-9])).service"
for file in $(ls $FILE); do
[ "$1" == "klipper" ] && LOG="klippy"
[ "$1" == "moonraker" ] && LOG="moonraker"
if [ ! "$(grep "\-l" $file)" ]; then
status_msg "Updating $file ..."
sudo sed -i -r "/ExecStart=/ s|$| -l $LPATH/$LOG.log|" $file
ok_msg "$file updated!"
elif [ "$(grep "\-l \/tmp\/$LOG" $file)" ]; then
status_msg "Updating $file ..."
sudo sed -i -r "/ExecStart=/ s|-l \/tmp\/$LOG|-l $LPATH/$LOG|" $file
ok_msg "$file updated!"
fi
done
sudo systemctl daemon-reload
# patch log_path entry if not found
dir1="$klipper_cfg_loc"
dir2="$klipper_cfg_loc/printer_*"
for conf in $(find $dir1 $dir2 -name "moonraker.conf" 2> /dev/null); do
if ! grep -q "log_path" $conf; then
status_msg "Patching $conf"
sed -i "/^config_path/a log_path: $LPATH" $conf
ok_msg "OK!"
fi
done
# create symlink for mainsail and fluidd nginx logs
symlink_webui_nginx_log "mainsail"
symlink_webui_nginx_log "fluidd"
# create symlink for webcamd log
if [ -f "/var/log/webcamd.log" ] && [ ! -L "$LPATH/webcamd.log" ]; then
status_msg "Creating symlink for '/var/log/webcamd.log' ..."
ln -s "/var/log/webcamd.log" "$LPATH"
ok_msg "OK!"
fi
shopt -u extglob # disable extended globbing
}

View File

@@ -1,177 +0,0 @@
#!/bin/bash
#=======================================================================#
# Copyright (C) 2020 - 2022 Dominik Willner <th33xitus@gmail.com> #
# #
# This file is part of KIAUH - Klipper Installation And Update Helper #
# https://github.com/th33xitus/kiauh #
# #
# This file may be distributed under the terms of the GNU GPLv3 license #
#=======================================================================#
set -e
function set_nginx_cfg(){
if [ "${SET_NGINX_CFG}" = "true" ]; then
local cfg="${SRCDIR}/kiauh/resources/${1}"
#check for dependencies
dep=(nginx)
dependency_check "${dep[@]}"
#execute operations
status_msg "Creating Nginx configuration for ${1} ..."
#copy content from resources to the respective nginx config file
cat "${SRCDIR}/kiauh/resources/klipper_webui_nginx.cfg" > "${cfg}"
##edit the nginx config file before moving it
sed -i "s/<<UI>>/${1}/g" "${cfg}"
if [ "${SET_LISTEN_PORT}" != "${DEFAULT_PORT}" ]; then
status_msg "Configuring port for $1 ..."
#set listen port ipv4
sed -i "s/listen\s[0-9]*;/listen ${SET_LISTEN_PORT};/" "${cfg}"
#set listen port ipv6
sed -i "s/listen\s\[\:*\]\:[0-9]*;/listen \[::\]\:${SET_LISTEN_PORT};/" "${cfg}"
fi
#set correct user
if [ "${1}" = "mainsail" ] || [ "${1}" = "fluidd" ]; then
sudo sed -i "/root/s/pi/${USER}/" "${cfg}"
fi
#moving the config file into correct directory
sudo mv "${cfg}" "/etc/nginx/sites-available/${1}"
ok_msg "Nginx configuration for $1 was set!"
if [ -n "${SET_LISTEN_PORT}" ]; then
ok_msg "${1} listening on port ${SET_LISTEN_PORT}!"
else
ok_msg "${1} listening on default port ${DEFAULT_PORT}!"
fi
#remove nginx default config
[ -e "/etc/nginx/sites-enabled/default" ] && sudo rm "/etc/nginx/sites-enabled/default"
#create symlink for own sites
[ ! -e "/etc/nginx/sites-enabled/${1}" ] && sudo ln -s "/etc/nginx/sites-available/${1}" "/etc/nginx/sites-enabled/"
restart_nginx
fi
}
function read_listen_port(){
LISTEN_PORT=$(grep listen "/etc/nginx/sites-enabled/${1}" | head -1 | sed 's/^\s*//' | cut -d" " -f2 | cut -d";" -f1)
}
function detect_enabled_sites(){
#check if there is another UI config already installed
#and reads the port they are listening on
if [ -e "/etc/nginx/sites-enabled/mainsail" ]; then
SITE_ENABLED="true" && MAINSAIL_ENABLED="true"
read_listen_port "mainsail"
MAINSAIL_PORT=${LISTEN_PORT}
#echo "debug: Mainsail listens on port: $MAINSAIL_PORT"
else
MAINSAIL_ENABLED="false"
fi
if [ -e /etc/nginx/sites-enabled/fluidd ]; then
SITE_ENABLED="true" && FLUIDD_ENABLED="true"
read_listen_port "fluidd"
FLUIDD_PORT=${LISTEN_PORT}
#echo "debug: Fluidd listens on port: $FLUIDD_PORT"
else
FLUIDD_ENABLED="false"
fi
if [ -e /etc/nginx/sites-enabled/octoprint ]; then
SITE_ENABLED="true" && OCTOPRINT_ENABLED="true"
read_listen_port "octoprint"
OCTOPRINT_PORT=${LISTEN_PORT}
#echo "debug: OctoPrint listens on port: $OCTOPRINT_PORT"
else
OCTOPRINT_ENABLED="false"
fi
}
function 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 "| |"
echo -e "| E.g.: If you set the hostname to 'my-printer' you |"
echo -e "| can open Mainsail / Fluidd / Octoprint by |"
echo -e "| browsing to: http://my-printer.local |"
bottom_border
while true; do
read -p "${cyan}###### Do you want to change the hostname? (y/N):${white} " yn
case "${yn}" in
Y|y|Yes|yes)
user_input_hostname
break;;
N|n|No|no|"") break;;
*)
print_unkown_cmd
print_msg && clear_msg;;
esac
done
}
function user_input_hostname(){
unset NEW_HOSTNAME
unset HOSTNAME_VALID
unset HOSTENAME_CONFIRM
echo
top_border
echo -e "| ${green}Allowed characters: a-z, 0-9 and single '-'${white} |"
echo -e "| ${red}No special characters allowed!${white} |"
echo -e "| ${red}No leading or trailing '-' allowed!${white} |"
bottom_border
while true; do
read -p "${cyan}###### Please set the new hostname:${white} " NEW_HOSTNAME
if [[ ${NEW_HOSTNAME} =~ ^[^\-\_]+([0-9a-z]\-{0,1})+[^\-\_]+$ ]]; then
ok_msg "'${NEW_HOSTNAME}' is a valid hostname!"
HOSTNAME_VALID="true"
while true; do
echo
read -p "${cyan}###### Do you want '${NEW_HOSTNAME}' to be the new hostname? (Y/n):${white} " yn
case "${yn}" in
Y|y|Yes|yes|"")
echo -e "###### > Yes"
HOSTENAME_CONFIRM="true"
break;;
N|n|No|no)
echo -e "###### > No"
echo -e "${red}Skip hostname change ...${white}"
HOSTENAME_CONFIRM="false"
break;;
*)
print_error "Invalid command!";;
esac
done
break
else
warn_msg "'${NEW_HOSTNAME}' is not a valid hostname!"
fi
done
}
function set_hostname(){
if [ "${HOSTNAME_VALID}" = "true" ] && [ "${HOSTENAME_CONFIRM}" = "true" ]; then
#check for dependencies
dep=(avahi-daemon)
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
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
#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 ..."
echo "127.0.0.1 ${NEW_HOSTNAME}" | sudo tee -a /etc/hosts &>/dev/null
ok_msg "New hostname successfully configured!"
ok_msg "Remember to reboot for the changes to take effect!"
fi
}

View File

@@ -20,7 +20,7 @@ NGINX_CONFD="/etc/nginx/conf.d"
#=================== REMOVE NGINX ==================#
#===================================================#
remove_nginx(){
function remove_nginx(){
if ls /lib/systemd/system/nginx.service 2>/dev/null 1>&2; then
status_msg "Stopping Nginx service ..."
sudo systemctl stop nginx && sudo systemctl disable nginx
@@ -238,4 +238,76 @@ function process_services_dialog(){
esac
done
fi
}
function set_nginx_cfg(){
if [ "${SET_NGINX_CFG}" = "true" ]; then
local cfg="${SRCDIR}/kiauh/resources/${1}"
#check for dependencies
dep=(nginx)
dependency_check "${dep[@]}"
#execute operations
status_msg "Creating Nginx configuration for ${1} ..."
#copy content from resources to the respective nginx config file
cat "${SRCDIR}/kiauh/resources/klipper_webui_nginx.cfg" > "${cfg}"
##edit the nginx config file before moving it
sed -i "s/<<UI>>/${1}/g" "${cfg}"
if [ "${SET_LISTEN_PORT}" != "${DEFAULT_PORT}" ]; then
status_msg "Configuring port for $1 ..."
#set listen port ipv4
sed -i "s/listen\s[0-9]*;/listen ${SET_LISTEN_PORT};/" "${cfg}"
#set listen port ipv6
sed -i "s/listen\s\[\:*\]\:[0-9]*;/listen \[::\]\:${SET_LISTEN_PORT};/" "${cfg}"
fi
#set correct user
if [ "${1}" = "mainsail" ] || [ "${1}" = "fluidd" ]; then
sudo sed -i "/root/s/pi/${USER}/" "${cfg}"
fi
#moving the config file into correct directory
sudo mv "${cfg}" "/etc/nginx/sites-available/${1}"
ok_msg "Nginx configuration for $1 was set!"
if [ -n "${SET_LISTEN_PORT}" ]; then
ok_msg "${1} listening on port ${SET_LISTEN_PORT}!"
else
ok_msg "${1} listening on default port ${DEFAULT_PORT}!"
fi
#remove nginx default config
[ -e "/etc/nginx/sites-enabled/default" ] && sudo rm "/etc/nginx/sites-enabled/default"
#create symlink for own sites
[ ! -e "/etc/nginx/sites-enabled/${1}" ] && sudo ln -s "/etc/nginx/sites-available/${1}" "/etc/nginx/sites-enabled/"
restart_nginx
fi
}
function read_listen_port(){
LISTEN_PORT=$(grep listen "/etc/nginx/sites-enabled/${1}" | head -1 | sed 's/^\s*//' | cut -d" " -f2 | cut -d";" -f1)
}
function detect_enabled_sites(){
#check if there is another UI config already installed
#and reads the port they are listening on
if [ -e "/etc/nginx/sites-enabled/mainsail" ]; then
SITE_ENABLED="true" && MAINSAIL_ENABLED="true"
read_listen_port "mainsail"
MAINSAIL_PORT=${LISTEN_PORT}
#echo "debug: Mainsail listens on port: $MAINSAIL_PORT"
else
MAINSAIL_ENABLED="false"
fi
if [ -e /etc/nginx/sites-enabled/fluidd ]; then
SITE_ENABLED="true" && FLUIDD_ENABLED="true"
read_listen_port "fluidd"
FLUIDD_PORT=${LISTEN_PORT}
#echo "debug: Fluidd listens on port: $FLUIDD_PORT"
else
FLUIDD_ENABLED="false"
fi
if [ -e /etc/nginx/sites-enabled/octoprint ]; then
SITE_ENABLED="true" && OCTOPRINT_ENABLED="true"
read_listen_port "octoprint"
OCTOPRINT_PORT=${LISTEN_PORT}
#echo "debug: OctoPrint listens on port: $OCTOPRINT_PORT"
else
OCTOPRINT_ENABLED="false"
fi
}

View File

@@ -11,7 +11,7 @@
set -e
save_klipper_state(){
function save_klipper_state(){
source_kiauh_ini
#read current klipper state
cd $KLIPPER_DIR
@@ -31,7 +31,7 @@ save_klipper_state(){
fi
}
load_klipper_state(){
function load_klipper_state(){
source_kiauh_ini
print_branch
cd $KLIPPER_DIR
@@ -63,7 +63,7 @@ load_klipper_state(){
rollback_klipper
}
rollback_ui(){
function rollback_ui(){
top_border
echo -e "| $(title_msg "~~~~~~~~~~~~~ [ Rollback Menu ] ~~~~~~~~~~~~~") | "
hr
@@ -82,7 +82,7 @@ rollback_ui(){
back_footer
}
rollback_klipper(){
function rollback_klipper(){
if [ "$PREVIOUS_COMMIT" != "0" ] && [ "$CURRENT_COMMIT" != "$PREVIOUS_COMMIT" ]; then
while true; do
echo -e "${cyan}"

View File

@@ -1,29 +0,0 @@
check_system_updates(){
SYS_UPDATE=$(apt list --upgradeable 2>/dev/null | sed "1d")
if [ ! -z "$SYS_UPDATE" ]; then
# add system updates to the update all array for the update all function in the updater
SYS_UPDATE_AVAIL="true" && update_arr+=(update_system)
DISPLAY_SYS_UPDATE="${yellow}System upgrade available!${white}"
else
SYS_UPDATE_AVAIL="false"
DISPLAY_SYS_UPDATE="${green}System up to date! ${white}"
fi
}
#############################################################
#############################################################
#display this as placeholder if no version/commit could be fetched
NONE="${red}$(printf "%-12s" "--------")${default}"
ui_print_versions(){
unset update_arr
check_system_updates
# compare_klipper_versions
# compare_moonraker_versions
# compare_mainsail_versions
# compare_fluidd_versions
# compare_klipperscreen_versions
# compare_MoonrakerTelegramBot_versions
# compare_pgc_versions
}

View File

@@ -11,7 +11,7 @@
set -e
advanced_ui(){
function advanced_ui(){
top_border
echo -e "| ${yellow}~~~~~~~~~~~~~ [ Advanced Menu ] ~~~~~~~~~~~~~${white} | "
hr
@@ -23,10 +23,10 @@ advanced_ui(){
echo -e "| 3) [Flash only] | | "
echo -e "| 4) [Build + Flash] | Extras: | "
echo -e "| 5) [Get MCU ID] | 8) [G-Code Shell Command] | "
back_footer
back_footer
}
advanced_menu(){
function advanced_menu(){
do_action "" "advanced_ui"
while true; do
read -p "${cyan}Perform action:${white} " action; echo

View File

@@ -11,7 +11,7 @@
set -e
backup_ui(){
function backup_ui(){
top_border
echo -e "| $(title_msg "~~~~~~~~~~~~~~ [ Backup Menu ] ~~~~~~~~~~~~~~") | "
hr
@@ -30,11 +30,11 @@ backup_ui(){
back_footer
}
backup_menu(){
function backup_menu(){
do_action "" "backup_ui"
while true; do
read -p "${cyan}Perform action:${white} " action; echo
case "$action" in
case "${action}" in
0)
do_action "backup_klipper_config_dir" "backup_ui";;
1)

View File

@@ -12,41 +12,41 @@
set -e
#ui total width = 57 chars
top_border(){
function top_border(){
echo -e "/=======================================================\\"
}
bottom_border(){
function bottom_border(){
echo -e "\=======================================================/"
}
blank_line(){
function blank_line(){
echo -e "| |"
}
hr(){
function hr(){
echo -e "|-------------------------------------------------------|"
}
quit_footer(){
function quit_footer(){
hr
echo -e "| ${red}Q) Quit${white} |"
bottom_border
}
back_footer(){
function back_footer(){
hr
echo -e "| ${green}B) « Back${white} |"
bottom_border
}
back_help_footer(){
function back_help_footer(){
hr
echo -e "| ${green}B) « Back${white} | ${yellow}H) Help [?]${white} |"
bottom_border
}
print_header(){
function print_header(){
top_border
echo -e "| $(title_msg "~~~~~~~~~~~~~~~~~ [ KIAUH ] ~~~~~~~~~~~~~~~~~") |"
echo -e "| $(title_msg " Klipper Installation And Update Helper ") |"
@@ -54,12 +54,7 @@ print_header(){
bottom_border
}
################################################################################
#******************************************************************************#
################################################################################
### TODO: rework other menus to make use of the following functions too and make them more readable
do_action(){
function do_action(){
clear && print_header
### $1 is the action the user wants to fire
$1
@@ -68,7 +63,7 @@ do_action(){
$2
}
deny_action(){
function deny_action(){
clear && print_header
print_error "Invalid command!"
$1

View File

@@ -11,7 +11,7 @@
set -e
main_ui(){
function main_ui(){
top_border
echo -e "| $(title_msg "~~~~~~~~~~~~~~~ [ Main Menu ] ~~~~~~~~~~~~~~~") |"
hr
@@ -29,14 +29,14 @@ main_ui(){
quit_footer
}
get_kiauh_version(){
function get_kiauh_version(){
local version
cd "${SRCDIR}/kiauh"
version="$(printf "%-20s" "$(git describe HEAD --always --tags | cut -d "-" -f 1,2)")"
echo "${cyan}${version}${white}"
}
kiauh_update_dialog(){
function kiauh_update_dialog(){
[ ! "$(kiauh_update_avail)" == "true" ] && return
top_border
echo -e "|${green} New KIAUH update available! ${white}| "
@@ -60,7 +60,7 @@ kiauh_update_dialog(){
done
}
main_menu(){
function main_menu(){
print_header
#prompt for KIAUH update if update available
kiauh_update_dialog

View File

@@ -11,7 +11,7 @@
set -e
remove_ui(){
function remove_ui(){
top_border
echo -e "| ${red}~~~~~~~~~~~~~~ [ Remove Menu ] ~~~~~~~~~~~~~~${white} | "
hr
@@ -30,7 +30,7 @@ remove_ui(){
back_footer
}
remove_menu(){
function remove_menu(){
do_action "" "remove_ui"
while true; do
read -p "${cyan}Perform action:${white} " action; echo

View File

@@ -117,7 +117,7 @@ function show_settings_help(){
done
}
settings_menu(){
function settings_menu(){
clear && print_header
settings_ui
while true; do
@@ -127,7 +127,7 @@ settings_menu(){
change_klipper_cfg_folder && settings_ui;;
2)
clear && print_header
change_klipper_repo_menu
change_klippefunction r_repo_menu
settings_ui;;
3)
switch_mainsail_releasetype && settings_menu;;

View File

@@ -11,8 +11,7 @@
set -e
update_ui(){
ui_print_versions
function update_ui(){
top_border
echo -e "| ${green}~~~~~~~~~~~~~~ [ Update Menu ] ~~~~~~~~~~~~~~${white} | "
hr
@@ -39,7 +38,8 @@ update_ui(){
back_footer
}
update_menu(){
function update_menu(){
unset update_arr
read_bb4u_stat
do_action "" "update_ui"
while true; do

View File

@@ -1,54 +0,0 @@
update_log_paths(){
### update services to make use of moonrakers new log_path option
### https://github.com/Arksine/moonraker/commit/829b3a4ee80579af35dd64a37ccc092a1f67682a
shopt -s extglob # enable extended globbing
source_kiauh_ini
LPATH="${HOME}/klipper_logs"
[ ! -d "$LPATH" ] && mkdir -p "$LPATH"
FILE="$SYSTEMDDIR/$1?(-*([0-9])).service"
for file in $(ls $FILE); do
[ "$1" == "klipper" ] && LOG="klippy"
[ "$1" == "moonraker" ] && LOG="moonraker"
if [ ! "$(grep "\-l" $file)" ]; then
status_msg "Updating $file ..."
sudo sed -i -r "/ExecStart=/ s|$| -l $LPATH/$LOG.log|" $file
ok_msg "$file updated!"
elif [ "$(grep "\-l \/tmp\/$LOG" $file)" ]; then
status_msg "Updating $file ..."
sudo sed -i -r "/ExecStart=/ s|-l \/tmp\/$LOG|-l $LPATH/$LOG|" $file
ok_msg "$file updated!"
fi
done
sudo systemctl daemon-reload
# patch log_path entry if not found
dir1="$klipper_cfg_loc"
dir2="$klipper_cfg_loc/printer_*"
for conf in $(find $dir1 $dir2 -name "moonraker.conf" 2> /dev/null); do
if ! grep -q "log_path" $conf; then
status_msg "Patching $conf"
sed -i "/^config_path/a log_path: $LPATH" $conf
ok_msg "OK!"
fi
done
# create symlink for mainsail and fluidd nginx logs
symlink_webui_nginx_log "mainsail"
symlink_webui_nginx_log "fluidd"
# create symlink for webcamd log
if [ -f "/var/log/webcamd.log" ] && [ ! -L "$LPATH/webcamd.log" ]; then
status_msg "Creating symlink for '/var/log/webcamd.log' ..."
ln -s "/var/log/webcamd.log" "$LPATH"
ok_msg "OK!"
fi
shopt -u extglob # disable extended globbing
}
update_system(){
status_msg "Updating System ..."
sudo apt-get update --allow-releaseinfo-change && sudo apt-get upgrade -y
CONFIRM_MSG="Update complete! Check the log above!\n ${yellow}KIAUH will not install any dist-upgrades or\n any packages which have been kept back!${green}"
print_msg && clear_msg
}

View File

@@ -565,9 +565,28 @@ function fetch_webui_ports(){
}
#================================================#
#================= USERGROUPS ===================#
#=================== SYSTEM =====================#
#================================================#
function check_system_updates(){
SYS_UPDATE=$(apt list --upgradeable 2>/dev/null | sed "1d")
if [ ! -z "$SYS_UPDATE" ]; then
# add system updates to the update all array for the update all function in the updater
SYS_UPDATE_AVAIL="true" && update_arr+=(update_system)
DISPLAY_SYS_UPDATE="${yellow}System upgrade available!${white}"
else
SYS_UPDATE_AVAIL="false"
DISPLAY_SYS_UPDATE="${green}System up to date! ${white}"
fi
}
function update_system(){
status_msg "Updating System ..."
sudo apt-get update --allow-releaseinfo-change && sudo apt-get upgrade -y
CONFIRM_MSG="Update complete! Check the log above!\n ${yellow}KIAUH will not install any dist-upgrades or\n any packages which have been kept back!${green}"
print_msg && clear_msg
}
function check_usergroups(){
local group_dialout group_tty
if grep -q "dialout" </etc/group && ! grep -q "dialout" <(groups "${USER}"); then
@@ -613,4 +632,97 @@ function check_usergroups(){
esac
done
fi
}
function 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 "| |"
echo -e "| E.g.: If you set the hostname to 'my-printer' you |"
echo -e "| can open Mainsail / Fluidd / Octoprint by |"
echo -e "| browsing to: http://my-printer.local |"
bottom_border
while true; do
read -p "${cyan}###### Do you want to change the hostname? (y/N):${white} " yn
case "${yn}" in
Y|y|Yes|yes)
user_input_hostname
break;;
N|n|No|no|"") break;;
*)
print_unkown_cmd
print_msg && clear_msg;;
esac
done
}
function user_input_hostname(){
unset NEW_HOSTNAME
unset HOSTNAME_VALID
unset HOSTENAME_CONFIRM
echo
top_border
echo -e "| ${green}Allowed characters: a-z, 0-9 and single '-'${white} |"
echo -e "| ${red}No special characters allowed!${white} |"
echo -e "| ${red}No leading or trailing '-' allowed!${white} |"
bottom_border
while true; do
read -p "${cyan}###### Please set the new hostname:${white} " NEW_HOSTNAME
if [[ ${NEW_HOSTNAME} =~ ^[^\-\_]+([0-9a-z]\-{0,1})+[^\-\_]+$ ]]; then
ok_msg "'${NEW_HOSTNAME}' is a valid hostname!"
HOSTNAME_VALID="true"
while true; do
echo
read -p "${cyan}###### Do you want '${NEW_HOSTNAME}' to be the new hostname? (Y/n):${white} " yn
case "${yn}" in
Y|y|Yes|yes|"")
echo -e "###### > Yes"
HOSTENAME_CONFIRM="true"
break;;
N|n|No|no)
echo -e "###### > No"
echo -e "${red}Skip hostname change ...${white}"
HOSTENAME_CONFIRM="false"
break;;
*)
print_error "Invalid command!";;
esac
done
break
else
warn_msg "'${NEW_HOSTNAME}' is not a valid hostname!"
fi
done
}
function set_hostname(){
if [ "${HOSTNAME_VALID}" = "true" ] && [ "${HOSTENAME_CONFIRM}" = "true" ]; then
#check for dependencies
dep=(avahi-daemon)
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
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
#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 ..."
echo "127.0.0.1 ${NEW_HOSTNAME}" | sudo tee -a /etc/hosts &>/dev/null
ok_msg "New hostname successfully configured!"
ok_msg "Remember to reboot for the changes to take effect!"
fi
}