Skip to content

Commit

Permalink
fix: Update handling of the MOTD file
Browse files Browse the repository at this point in the history
1/ Improve logging; be more explicit about what is being done
2/ Remove the file instead of symlinking to /dev/null
3/ Handle situations in which the motd symlink is broken

(Updates 0ee6971)
  • Loading branch information
m-horky committed Mar 8, 2024
1 parent 3276dfd commit f2a3d84
Showing 1 changed file with 22 additions and 12 deletions.
34 changes: 22 additions & 12 deletions src/insights_client/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -257,25 +257,35 @@ def update_motd_message():

if motd_should_exist:
# .registered & .unregistered do not exist, MOTD should be displayed
if not os.path.exists(MOTD_FILE):
if not os.path.lexists(MOTD_FILE):
logger.debug(
".registered and .unregistered do not exist; "
"pointing the MOTD file '{source}' to '{motd}'".format(source=MOTD_SRC, motd=MOTD_FILE)
)
try:
os.symlink(MOTD_SRC, MOTD_FILE)
logger.debug("pointed the MOTD file '{source}' to '{motd}'".format(
source=MOTD_SRC, motd=MOTD_FILE,
))
except OSError:
logger.exception("could not point the MOTD file '{source}' to '{motd}'".format(
source=MOTD_SRC, motd=MOTD_FILE,
except OSError as exc:
logger.debug("could not point the MOTD file '{source}' to '{motd}': {exc}".format(
source=MOTD_SRC, motd=MOTD_FILE, exc=exc
))
else:
logger.debug(
".registered and .unregistered do not exist; "
"file '{source}' correctly points to '{motd}'".format(source=MOTD_SRC, motd=MOTD_FILE)
)

else:
# .registered or .unregistered exist, MOTD should not be displayed
if os.path.exists(MOTD_FILE):
if os.path.lexists(MOTD_FILE):
logger.debug(".registered or .unregistered exist; removing the MOTD file '{path}'".format(path=MOTD_FILE))
try:
os.symlink(os.devnull, MOTD_FILE)
logger.debug("pointed the MOTD file '{path}' to '/dev/null'".format(path=MOTD_FILE))
except OSError:
logger.exception("could not remove the MOTD file '{path}'".format(path=MOTD_FILE))
os.remove(MOTD_FILE)
except OSError as exc:
logger.debug("could not remove the MOTD file '{path}': {exc}".format(path=MOTD_FILE, exc=exc))
else:
logger.debug(
".registered or .unregistered exist; file '{motd}' correctly does not exist".format(motd=MOTD_FILE)
)


def _main():
Expand Down

0 comments on commit f2a3d84

Please sign in to comment.