Change device hostname through web UI
See original GitHub issueAs more users are deploying multiple TinyPilots to the same network, we should make it easier to change their hostnames.
Which changes the hostname to the desired
Ansible
Add a script called /opt/tinypilot-privileged/change-hostname
that the tinypilot
user can execute as sudo to change the hostname in /etc/hosts
and /etc/hostname
.
The script can be written in bash or as a standalone Python script. See this PR as an example of adding a privileged script.
Updating /etc/hostname
is simple because it’s a file with just the hostname, but /etc/hosts
requires some care. We should ensure that:
-
We perform exactly one string replacement(see update, below) -
The string is in the position we expect(see update, below) -
The hostname contains only valid characters
-
The new hostname is not
localhost
-
Update: Per discussion, rather than updating the existing
/etc/hosts
entry,change-hostname
should add a block to the top of /etc/hosts like this:
# AUTOGENERATED BY TINYPILOT <<<
127.0.1.1 my-custom-hostname.local
# >>> AUTOGENERATED BY TINYPILOT
The OS processes /etc/hosts
from top to bottom, so our block will take precedence over future blocks.
Backend
Add a new REST endpoint:
/api/hostname
(PUT)
The API should accept a JSON message like this:
{
"hostname": "some-new-name"
}
The handler should call a module that wraps the call to /opt/tinypilot-privileged/change-hostname
script using subprocess
. See debug_logs.py
for a similar example.
The endpoint should respond with a message like this:
{
"success": true,
"error": null
}
/api/hostname
(GET)
Retrieves the current hostname like this:
{
"success": true,
"error": null,
"hostname": "tinypilot"
}
It should retrieve the hostname by executing the system’s calling hostname
command and piping through the resultplatform.node
.
Frontend
We should add an option under “Commands” that’s “Change Hostname”. It pops up a dialog box that says “Change hostname” and offers a text input showing the current hostname (from /api/hostname
) and allows the user to change it.
Added (2021-02-15): The dialog should include a warning at the bottom that says, “Your device will restart to apply this change.”
If the user changes it, the dialog should say: “Restart now to apply changes?”
If the user chooses to reboot, The dialog should call /api/shutdown
to reboot and then poll in the background for a response at the new hostname URL. When the new URL is serving, the dialog should redirect the browser to the new URL.
Issue Analytics
- State:
- Created 3 years ago
- Comments:6 (3 by maintainers)
Top GitHub Comments
I believe this is done, isn’t it? 🥳
Oh right. Fixed!