From 3e6d3d901541d828b16d4d89c7e1853fd9d552f8 Mon Sep 17 00:00:00 2001 From: dw-0 Date: Sun, 11 Aug 2024 12:55:13 +0200 Subject: [PATCH] feat: add change hostename procedure Signed-off-by: Dominik Willner --- kiauh/core/menus/advanced_menu.py | 8 ++- kiauh/procedures/__init__.py | 0 kiauh/procedures/system.py | 107 ++++++++++++++++++++++++++++++ 3 files changed, 114 insertions(+), 1 deletion(-) create mode 100644 kiauh/procedures/__init__.py create mode 100644 kiauh/procedures/system.py diff --git a/kiauh/core/menus/advanced_menu.py b/kiauh/core/menus/advanced_menu.py index eb00d01..9df5e83 100644 --- a/kiauh/core/menus/advanced_menu.py +++ b/kiauh/core/menus/advanced_menu.py @@ -25,6 +25,7 @@ from components.moonraker.moonraker import Moonraker from core.constants import COLOR_YELLOW, RESET_FORMAT from core.menus import Option from core.menus.base_menu import BaseMenu +from procedures.system import change_system_hostname from utils.git_utils import rollback_repository @@ -48,6 +49,7 @@ class AdvancedMenu(BaseMenu): "4": Option(method=self.get_id, menu=False), "5": Option(method=self.klipper_rollback, menu=True), "6": Option(method=self.moonraker_rollback, menu=True), + "7": Option(method=self.change_hostname, menu=True), } def print_menu(self) -> None: @@ -63,7 +65,8 @@ class AdvancedMenu(BaseMenu): ║ 1) [Build] │ 5) [Klipper] ║ ║ 2) [Flash] │ 6) [Moonraker] ║ ║ 3) [Build + Flash] │ ║ - ║ 4) [Get MCU ID] │ ║ + ║ 4) [Get MCU ID] │ System: ║ + ║ │ 7) [Change hostname] ║ ╟───────────────────────────┴───────────────────────────╢ """ )[1:] @@ -90,3 +93,6 @@ class AdvancedMenu(BaseMenu): previous_menu=self.__class__, standalone=True, ).run() + + def change_hostname(self, **kwargs) -> None: + change_system_hostname() diff --git a/kiauh/procedures/__init__.py b/kiauh/procedures/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/kiauh/procedures/system.py b/kiauh/procedures/system.py new file mode 100644 index 0000000..e1c37a1 --- /dev/null +++ b/kiauh/procedures/system.py @@ -0,0 +1,107 @@ +# ======================================================================= # +# Copyright (C) 2020 - 2024 Dominik Willner # +# # +# This file is part of KIAUH - Klipper Installation And Update Helper # +# https://github.com/dw-0/kiauh # +# # +# This file may be distributed under the terms of the GNU GPLv3 license # +# ======================================================================= # + +from pathlib import Path +from subprocess import PIPE, CalledProcessError, run + +from utils.common import check_install_dependencies, get_current_date +from utils.fs_utils import check_file_exist +from utils.input_utils import get_confirm, get_string_input +from utils.logger import DialogType, Logger + + +def change_system_hostname() -> None: + """ + Procedure to change the system hostname. + :return: + """ + + Logger.print_dialog( + DialogType.CUSTOM, + [ + "Changing the hostname of this system allows you to access an installed " + "webinterface by simply typing the hostname like this in the browser:", + "\n\n", + "http://.local", + "\n\n", + "Example: If you set your hostname to 'my-printer', you can access an " + "installed webinterface by tyoing 'http://my-printer.local' in the " + "browser.", + ], + custom_title="CHANGE SYSTEM HOSTNAME", + padding_top=0, + padding_bottom=0, + ) + if not get_confirm("Do you want to change the hostname?", default_choice=False): + return + + Logger.print_dialog( + DialogType.CUSTOM, + [ + "Allowed characters: a-z, 0-9 and '-'", + "The name must not contain the following:", + "\n\n", + "● Any special characters", + "● No leading or trailing '-'", + ], + padding_top=0, + padding_bottom=0, + ) + hostname = get_string_input( + "Enter the new hostname", + regex="^[a-z0-9]+([a-z0-9-]*[a-z0-9])?$", + ) + if not get_confirm(f"Change the hostname to '{hostname}'?", default_choice=False): + Logger.print_info("Aborting hostname change ...") + return + + try: + Logger.print_status("Changing hostname ...") + + Logger.print_status("Checking for dependencies ...") + check_install_dependencies({"avahi-daemon"}, include_global=False) + + # create or backup hosts file + Logger.print_status("Creating backup of hosts file ...") + hosts_file = Path("/etc/hosts") + if not check_file_exist(hosts_file, True): + cmd = ["sudo", "touch", hosts_file.as_posix()] + run(cmd, stderr=PIPE, check=True) + else: + date_time = get_current_date() + name = f"hosts.{date_time.get('date')}-{date_time.get('time')}.bak" + hosts_file_backup = Path(f"/etc/{name}") + cmd = [ + "sudo", + "cp", + hosts_file.as_posix(), + hosts_file_backup.as_posix(), + ] + run(cmd, stderr=PIPE, check=True) + Logger.print_ok() + + # call hostnamectl set-hostname + Logger.print_status(f"Setting hostname to '{hostname}' ...") + cmd = ["sudo", "hostnamectl", "set-hostname", hostname] + run(cmd, stderr=PIPE, check=True) + Logger.print_ok() + + # add hostname to hosts file at the end of the file + Logger.print_status("Writing new hostname to /etc/hosts ...") + stdin = f"127.0.0.1 {hostname}\n" + cmd = ["sudo", "tee", "-a", hosts_file.as_posix()] + run(cmd, input=stdin.encode(), stderr=PIPE, stdout=PIPE, check=True) + Logger.print_ok() + + Logger.print_ok("New hostname successfully configured!") + Logger.print_ok("Remember to reboot for the changes to take effect!\n") + + except CalledProcessError as e: + Logger.print_error(f"Error during change hostname procedure: {e}") + return