diff --git a/wo/cli/plugins/stack_pref.py b/wo/cli/plugins/stack_pref.py index 5cd7987..e0de983 100644 --- a/wo/cli/plugins/stack_pref.py +++ b/wo/cli/plugins/stack_pref.py @@ -1468,4 +1468,12 @@ def pre_stack(self): '* soft nofile 500000\n' 'root hard nofile 500000\n' 'root soft nofile 500000\n') - Log.valide(self, 'Applying Linux tweaks') + # custom motd-news + data = dict() + if os.path.isdir('/etc/update-motd.d/'): + if not os.path.isfile('/etc/update-motd.d/98-wo-update'): + WOTemplate.deploy( + self, '/etc/update-motd.d/98-wo-update', + 'wo-update.mustache', data) + WOFileUtils.chmod( + self, "/etc/update-motd.d/98-wo-update", 0o755) diff --git a/wo/cli/templates/wo-update.mustache b/wo/cli/templates/wo-update.mustache new file mode 100644 index 0000000..be5091b --- /dev/null +++ b/wo/cli/templates/wo-update.mustache @@ -0,0 +1,37 @@ +#!/bin/sh +# script to update motd when a new WordOps release is available on Debian/Ubuntu +# the script is added in /etc/update-motd.d + +safe_print() { + cat "$1" | head -n 10 | tr -d '\000-\011\013\014\016-\037' | cut -c -80 +} + +# Ensure sane defaults +[ -n "$URL" ] || URL="https://github.com/WordOps/WordOps/releases/latest" +[ -n "$WAIT" ] || WAIT=5 +[ -n "$CACHE" ] || CACHE="/var/cache/motd-wo" + +# Generate our temp files, clean up when done +NEWS=$(mktemp) || exit 1 +ERR=$(mktemp) || exit 1 +CLOUD=$(mktemp) || exit 1 +trap "rm -f $NEWS $ERR $CLOUD" HUP INT QUIT ILL TRAP BUS TERM + +if [ -n "$(command -v curl > /dev/null 2>&1)" ]; then + LATEST_RELEASE=$(curl -m 5 --retry 3 -sI "$URL" | grep tag | awk -F "/" '{print $8}' 2>&1) +fi +if [ -n "$(command -v wo > /dev/null 2>&1)" ]; then + CURRENT_RELEASE=$(wo -v 2>&1 | grep v | awk -F " " '{print $2}') +fi +if [ -n "$CURRENT_RELEASE" ] && [ -n "$LATEST_RELEASE" ]; then + if [ "$CURRENT_RELEASE" != "$LATEST_RELEASE" ]; then + # display message with motd-news on Ubuntu + echo '*** A new WordOps release is available ***' > "$NEWS" 2> "$ERR" + + echo + # At most, 10 lines of text, remove control characters, print at most 80 characters per line + safe_print "$NEWS" + # Try to update the cache + safe_print "$NEWS" 2> /dev/null > $CACHE || true + fi +fi diff --git a/wo/core/fileutils.py b/wo/core/fileutils.py index 87a3da6..5f9ed35 100644 --- a/wo/core/fileutils.py +++ b/wo/core/fileutils.py @@ -343,3 +343,16 @@ class WOFileUtils(): # If it's not a symlink we're not interested. continue return True + + def writein(self, path, content): + """ + Write content in path + """ + Log.debug(self, "Writing content in {0}".format(path)) + try: + with open("{0}".format(path), + encoding='utf-8', mode='w') as final_file: + final_file.write('{0}'.format(content)) + except IOError as e: + Log.debug(self, "{0}".format(e)) + Log.error(self, "Unable to write content in {0}".format(path))