Source code for reemote.operations.builtin.hostname
# Copyright (c) 2025 Kim Jarvis TPF Software Services S.A. kim.jarvis@tpfsystems.com # This software is licensed under the MIT License. See the LICENSE file for details.#fromreemote.commandimportCommand
[docs]classHostname:""" A class to manage the system's hostname on Unix-like operating systems. This class allows setting the hostname and supports different strategies for updating the hostname based on the underlying OS/distribution. Attributes: name (str): The desired hostname to set. use (str): Strategy to use for updating hostname. guard (bool): If `False` the commands will not be executed. Supported strategies: - "alpine" - "debian" - "freebsd" - "generic" - "macos" / "macosx" / "darwin" - "openbsd" - "openrc" - "redhat" - "sles" - "solaris" - "systemd" **Examples:** .. code:: python # Set a hostname r = yield Hostname(name="web01") print(r.cp.stdout) # Set a hostname specifying strategy r = yield Hostname(name="web01", use="systemd") print(r.cp.stdout) Usage: This class is designed to be used in a generator-based workflow where commands are yielded for execution. Notes: - This module does NOT modify /etc/hosts. You need to modify it yourself using other modules. - On macOS, this module uses scutil to set HostName, ComputerName, and LocalHostName. - Windows, HP-UX, and AIX are not currently supported. """def__init__(self,name:str,use:str=None,guard:bool=True):self.name=nameself.use=useself.guard=guarddef__repr__(self):return(f"Hostname(name={self.name!r}, "f"use={self.use!r}, "f"guard={self.guard!r})")defexecute(self):# Build the command based on the strategyifself.use=="systemd":cmd=f"hostnamectl set-hostname {self.name}"elifself.use=="redhat":cmd=f"echo '{self.name}' > /etc/hostname && hostname {self.name}"elifself.use=="debian":cmd=f"echo '{self.name}' > /etc/hostname && hostname --file /etc/hostname"elifself.use=="macos"orself.use=="macosx"orself.use=="darwin":# Replace special characters for LocalHostNamelocal_name=''.join(cifc.isalnum()orcin['-','.']else'-'forcinself.name)cmd=(f"scutil --set HostName {self.name} && "f"scutil --set ComputerName {self.name} && "f"scutil --set LocalHostName {local_name}")elifself.use=="generic":cmd=f"hostname {self.name}"else:# Auto-detect or fallback to generic approachcmd=f"hostnamectl set-hostname {self.name} || hostname {self.name}"r=yieldCommand(cmd,guard=self.guard)r.changed=True