mirror of
https://github.com/dw-0/kiauh.git
synced 2025-12-26 01:03:35 +05:00
feat: add change hostename procedure
Signed-off-by: Dominik Willner <th33xitus@gmail.com>
This commit is contained in:
@@ -25,6 +25,7 @@ from components.moonraker.moonraker import Moonraker
|
|||||||
from core.constants import COLOR_YELLOW, RESET_FORMAT
|
from core.constants import COLOR_YELLOW, RESET_FORMAT
|
||||||
from core.menus import Option
|
from core.menus import Option
|
||||||
from core.menus.base_menu import BaseMenu
|
from core.menus.base_menu import BaseMenu
|
||||||
|
from procedures.system import change_system_hostname
|
||||||
from utils.git_utils import rollback_repository
|
from utils.git_utils import rollback_repository
|
||||||
|
|
||||||
|
|
||||||
@@ -48,6 +49,7 @@ class AdvancedMenu(BaseMenu):
|
|||||||
"4": Option(method=self.get_id, menu=False),
|
"4": Option(method=self.get_id, menu=False),
|
||||||
"5": Option(method=self.klipper_rollback, menu=True),
|
"5": Option(method=self.klipper_rollback, menu=True),
|
||||||
"6": Option(method=self.moonraker_rollback, menu=True),
|
"6": Option(method=self.moonraker_rollback, menu=True),
|
||||||
|
"7": Option(method=self.change_hostname, menu=True),
|
||||||
}
|
}
|
||||||
|
|
||||||
def print_menu(self) -> None:
|
def print_menu(self) -> None:
|
||||||
@@ -63,7 +65,8 @@ class AdvancedMenu(BaseMenu):
|
|||||||
║ 1) [Build] │ 5) [Klipper] ║
|
║ 1) [Build] │ 5) [Klipper] ║
|
||||||
║ 2) [Flash] │ 6) [Moonraker] ║
|
║ 2) [Flash] │ 6) [Moonraker] ║
|
||||||
║ 3) [Build + Flash] │ ║
|
║ 3) [Build + Flash] │ ║
|
||||||
║ 4) [Get MCU ID] │ ║
|
║ 4) [Get MCU ID] │ System: ║
|
||||||
|
║ │ 7) [Change hostname] ║
|
||||||
╟───────────────────────────┴───────────────────────────╢
|
╟───────────────────────────┴───────────────────────────╢
|
||||||
"""
|
"""
|
||||||
)[1:]
|
)[1:]
|
||||||
@@ -90,3 +93,6 @@ class AdvancedMenu(BaseMenu):
|
|||||||
previous_menu=self.__class__,
|
previous_menu=self.__class__,
|
||||||
standalone=True,
|
standalone=True,
|
||||||
).run()
|
).run()
|
||||||
|
|
||||||
|
def change_hostname(self, **kwargs) -> None:
|
||||||
|
change_system_hostname()
|
||||||
|
|||||||
0
kiauh/procedures/__init__.py
Normal file
0
kiauh/procedures/__init__.py
Normal file
107
kiauh/procedures/system.py
Normal file
107
kiauh/procedures/system.py
Normal file
@@ -0,0 +1,107 @@
|
|||||||
|
# ======================================================================= #
|
||||||
|
# Copyright (C) 2020 - 2024 Dominik Willner <th33xitus@gmail.com> #
|
||||||
|
# #
|
||||||
|
# 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://<hostname>.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 <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
|
||||||
Reference in New Issue
Block a user