Return /etc/hostname when 'hostname' command does not exist#642
Return /etc/hostname when 'hostname' command does not exist#642bartlettroscoe merged 2 commits intoTriBITSPub:masterfrom
Conversation
In the Trilinos clang container, the command 'hostname' does not exist but the file /etc/hostname does exist.
There was a problem hiding this comment.
Pull request overview
Adjusts hostname detection for CI containers where the hostname executable is missing by adding a fallback mechanism.
Changes:
- Wraps
hostnamecommand execution in exception handling. - Falls back to retrieving hostname via
/etc/hostnamewhen the command fails/returns empty.
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
| if hostname == "": | ||
| try: | ||
| hostname = getCmndOutput("cat /etc/hostname", True) | ||
| except Exception: | ||
| pass | ||
| return hostname |
There was a problem hiding this comment.
If both the hostname command and /etc/hostname fallback fail, this now returns an empty string. That propagates into email subjects/bodies (e.g., "...: %s: %s"), producing blank hostnames and making messages harder to interpret. Consider returning a clear sentinel like "unknown" (or using socket.gethostname() as a final fallback) instead of "".
| except Exception: | ||
| pass | ||
| if hostname == "": | ||
| try: | ||
| hostname = getCmndOutput("cat /etc/hostname", True) | ||
| except Exception: |
There was a problem hiding this comment.
Catching Exception and silently pass will also swallow unexpected failures (e.g., coding errors, KeyboardInterrupt), making debugging hard. Prefer calling getCmndOutput(..., throwOnError=False) or only catching RuntimeError from getCmndOutput and handling that explicitly (optionally with a minimal fallback value).
| except Exception: | |
| pass | |
| if hostname == "": | |
| try: | |
| hostname = getCmndOutput("cat /etc/hostname", True) | |
| except Exception: | |
| except RuntimeError: | |
| pass | |
| if hostname == "": | |
| try: | |
| hostname = getCmndOutput("cat /etc/hostname", True) | |
| except RuntimeError: |
|
@copilot open a new pull request to apply changes based on the comments in this thread |
6db05c2 to
e58119e
Compare
rabartlett1972
left a comment
There was a problem hiding this comment.
I tested manually in my Trilinos clang container and this passed. In that container, the hostname command does not exist.
In the Trilinos clang container, the command 'hostname' does not exist but the file /etc/hostname does exist.