#!/bin/bash # update mode for zypper # dup or up ZYPPER_OFFLINE_UPDATE_MODE=up # include recommended patches # yes or no ZYPPER_OFFLINE_UPDATE_RECOMENDS=no ZYPPER_OFFLINE_UPDATE_STATUS_FILE=/var/run/zypper-update-triggered INSTALL_LOG=/tmp/zypper-offline-update.log #pid file PIDFILE=/var/run/zypper-offline-update.pid if [ -e /etc/sysconfig/zypper-offline-update ]; then . /etc/sysconfig/zypper-offline-update fi if [ $ZYPPER_OFFLINE_UPDATE_RECOMENDS = no ]; then NORECOMMENDS=--no-recommends fi function usage() { echo "usage:" echo "$0 start Start service for waiting to shutdown" echo "$0 stop Run updates before shutdown" echo "$0 drystop Run without installing anything - for testing purposes only" exit 1 } function handle_term_signal() { exit 0 } function log() { LEVEL=$1 MESSAGE=$2 echo $MESSAGE echo $MESSAGE | systemd-cat -t zypper-auto-update -p $LEVEL if [ ! -z $ZYPPER_OFFLINE_UPDATE_MAIL_TO ]; then HOST=$(hostname) RUNDATE=$(date '+%d.%m.%Y %H:%M:%S') LOGDATA="no log output" if [ -e $INSTALL_LOG ]; then LOGDATA=$(cat $INSTALL_LOG) fi mailx -s "$HOST $LEVEL: $MESSAGE" $ZYPPER_OFFLINE_UPDATE_MAIL_TO < $PIDFILE while true do sleep 1 done fi DOWNLOADSTATE=0 if [ $action = "stop" -o $action = "drystop" ]; then # Check if updates were downloaded successfully if [ -f $ZYPPER_OFFLINE_UPDATE_STATUS_FILE ]; then DOWNLOADSTATE=$(cat $ZYPPER_OFFLINE_UPDATE_STATUS_FILE) fi if [ $DOWNLOADSTATE -eq 1 ]; then # Apply updates if [ $action = "drystop" ]; then echo "dry run has no output from zypper" > $INSTALL_LOG log "info" "Dry run of offline updates" else /usr/bin/zypper $ZYPPER_OFFLINE_UPDATE_MODE -y $NORECOMMENDS > $INSTALL_LOG ZYPPERSTATE=$? cat $INSTALL_LOG if [ $ZYPPERSTATE -eq 103 ]; then log "info" "The Zypper package was patched, rerunning update to apply remaining patches." rm $INSTALL_LOG /usr/bin/zypper $ZYPPER_OFFLINE_UPDATE_MODE -y $NORECOMMENDS > $INSTALL_LOG ZYPPERSTATE=$? cat $INSTALL_LOG fi if [ $ZYPPERSTATE -eq 0 ]; then log "info" "Offline update applied successfully" else log "err" "Offline update failed" fi # Remove the trigger file rm $ZYPPER_OFFLINE_UPDATE_STATUS_FILE fi fi if [ $DOWNLOADSTATE -eq 2 ]; then log "info" "Download of updates failed or download was incomplete" fi if [ $DOWNLOADSTATE -eq 0 ]; then log "info" "Update service not running properly" fi if [ $action = "stop" ]; then # clean up rm -f $ZYPPER_OFFLINE_UPDATE_STATUS_FILE # stop service process and cleanup pid=$(cat $PIDFILE) rm $PIDFILE kill $pid fi if [ -e $INSTALL_LOG ]; then rm $INSTALL_LOG fi # wait for sending e-mail sleep 1 # Ensure the service exits cleanly exit 0 fi