diff options
author | Boris Kolpackov <boris@codesynthesis.com> | 2017-03-25 15:03:59 +0200 |
---|---|---|
committer | Boris Kolpackov <boris@codesynthesis.com> | 2017-03-25 15:03:59 +0200 |
commit | 864fc1c2e0597606ec8e2e04ae3150144aa3ed39 (patch) | |
tree | 640a3ad90260881937fad6a3ca675522b2fa0879 /buildos | |
parent | 0ba253ef6926d2f8bf403f9b96ca9120d0022861 (diff) |
Add buildos monitor
Diffstat (limited to 'buildos')
-rwxr-xr-x | buildos | 76 |
1 files changed, 76 insertions, 0 deletions
@@ -0,0 +1,76 @@ +#!/bin/bash + +# Build OS monitor. It starts as a systemd service and performs the following +# steps: +# +# 1. Bootstrap the build2 toolchain. +# 2. Build and start bbot. +# 3. Build and start bslave. +# 4. Monitor for OS and toolchain changes and reboot if detected. +# + +# @@ What do we do in case or error, trap? Send email? Reboot? Probably +# reboot bad idea since someone may need to login to examine what's +# going on. +# +# @@ What will systemd do if we fail? Perhaps configure it to restart +# us? Or not since we may hose the logs. +# +trap "exit 1" ERR +set -o errtrace # Trap in functions. + +# Note: diagnostics goes to stdout. +# +function info () { echo "$*" 1>&2; } +function error () +{ + if [ "$#" -gt 0 ]; then + info "$*"; + fi + + exit 1 +} + +info "starting buildos monitor..." + +# Parse the kernel command line. This is complicated by the fact that the +# values can be quoted, for example: +# +# foo='foo fox' +# bar="bar 'box'" +# +# First we separete quoted variables and arguments with newlines (giving +# priority to assignments). Then we replace whitespaces with newline on +# lines that don't contain quites. Finally, clean up by removing blank +# lines. +# +# Note: the same code as in init. +# +readarray -t cmdline < <(cat /proc/cmdline | \ + sed -r -e "s/([^ ]+=)?('[^']*'|\"[^\"]*\")/\n\1\2\n/g" | \ + sed -r -e "/['\"]/!s/ /\n/g" | + sed -r -e '/^\s*$/d') + +# Enter all buildos variables as bash variables. +# +for v in "${cmdline[@]}"; do + var="$(sed -r -n -e 's/^buildos\.([^=]+)=.*$/\1/p' <<<"$v")" # Extract name. + + if [ -n "$var" ]; then + val="$(sed -r -e 's/^[^=]+=(.*)$/\1/' <<<"$v")" # Extract value. + val="$(sed -r -e "s/^('(.*)'|\"(.*)\")$/\2\3/" <<<"$val")" # Strip quoted. + declare "$var=$val" + fi +done + +function email () # <subject> < <body> +{ + (echo -e "Subject: $1\n"; cat -) | sendmail -i "$admin_email" +} + +email "starting buildos monitor on $(hostname)" <<<"" + +while true; do + info "monitoring..." + sleep 2 +done |