Friday, April 25, 2008

Emerge Automation with email notification, makes gentoo server farms happy! Just add to the cron task level of your choice. I got tired of updating multiple machines at once. If you have a local rsync server (which there's no reason why you shouldn't ; ] ) the script will make sure it runs a proper sync off the defined SYNC in /etc/make.conf . If it's not defined use the faster delta-webrsync.

Requires: net-mail/mailx && sys-apps/emerge-delta-webrsync && (sys-apps/mktemp || >=sys-apps/coreutils-6.10)

/etc/cron.*/emerge-update-notification.sh



#!/bin/bash

# Auto emerge update notification - v0.3
#
# This script was developed to help automate Gentoo server farms. Released under GNU GPL V2
#
# Requires: net-mail/mailx && sys-apps/emerge-delta-webrsync && (sys-apps/mktemp || >=sys-apps/coreutils-6.10)
#
# Questions/Concerns/Bugs: email c4blem0nkey AT gmail.com

. /etc/conf.d/emerge-update-notification

hostname=`hostname`

syncResponse=`mktemp`
if [ ${1}='' ]; then
# Lets see if we have any custom rsync servers defined and use them instead...
if [ `grep "SYNC=" /etc/make.conf` ]; then emerge --sync > ${syncResponse}
else
# Nope, roll'em them updates a faster way
emerge-delta-webrsync > ${syncResponse}
fi;
fi;

[ `cat ${syncResponse} | grep "rsync error"` ] && rsyncError=1;
emerge -pv portage > /dev/null && portageUpdate=1;

syncTime=`cat /usr/portage/metadata/timestamp`

if [ -n $rsyncError ]; then
# Check to see if everything is already up-to-date
emergeTotal=`emerge -pv world | tail -1`
packageCount=`echo ${emergeTotal} | awk '{print $2}'`
# if it is, bail out happy
[ ${packageCount} -lt 1 ] && exit 0;

#Otherwise...lets roll
emergeResponse=`mktemp`
emerge -p world > ${emergeResponse}

# Check to see if we have any problems to report
[ `grep "MASKED PACKAGES" ${emergeResponse}` ] && packageMaskedError=1;
[ `grep "circular dependencies" ${emergeResponse}` ] && packageCircDepError=1;
[ `grep "^\[blocks" ${emergeResponse}` ] && blockingPackages=1;

#blockers?
blockingDetails=`mktemp`
[ ! -n $blockingPackages ] && grep "^\[blocks " ${emergeResponse} > ${blockingDetails}

#packages?
packageDetails=`mktemp`
[ ${packageCount} -gt 0 ] && grep "^\[ebuild" ${emergeResponse} > ${packageDetails}

#mktemp the email container
messageBody=`mktemp`
echo -e "Last portage sync at: ${syncTime}" >> ${messageBody}

#everything is kosher lets build the message
if [ -n $packagesUpToDate ] && [ -n $packageMaskedError ] && [ -n $packageCircDepError ]; then
#No errors seen on the emerge -p
subject="[emerge] ${hostname}: ${packageCount}"
if [ ${packageCount} -gt 1 ]; then subject="${subject} packages need"
else subject="${subject} package needs "
fi;
subject="${subject} updating"
echo -e "${emergeTotal}\n" >> ${messageBody}
cat ${packageDetails} >> ${messageBody}

if [ ! -n ${blockingPackages}]; then
echo -e "Detected Issues:" >> ${messageBody}
cat blockingDetails >> ${messageBody}
fi;
else
if [ -n $packagesUpToDate ] && [ -n $packageMaskedError ]; then
#looks like there was a circular dependency. oops!
subject="[emerge] ${hostname}: circular dependency detected"
echo "Oops, there looks to be a circular dependency in the next update." >> ${messageBody}
fi;
if [ -n $packagesUpToDate ] && [ -n $packageCircDepError ]; then
#looks like a package to be updated is masked for some reason
subject="[emerge] ${hostname}: required package masked"
echo "Looks like for some reason a package to be updated has been masked, to determine why this is you may wish to check /etc/portage/package.mask for comments." >> ${messageBody}
fi;


echo -e "The emerge command reported the following:\n" >> ${messageBody}
cat ${emergeResponse} >> ${messageBody}
fi;
else
#There was a problem rsyncing...fire off an email reporting a failure
subject="[emerge] ${hostname}: failed to sync portage"
echo -e "The last portage sync failed to complete, and returned the following messages:\n" >> ${messageBody}
echo ${syncResponse} >> ${messageBody}
fi;

mail -s "${subject}" ${toAddress} < ${messageBody} for i in ${syncResponse} ${emergeResponse} ${blockingDetails} ${packageDetails} ${messageBody}; do rm ${i}; done


/etc/conf.d/emerge-update-notification


# Auto emerge update notification settings
#
# This script was developed to help automate Gentoo server farms. Released under GNU GPL V2
#
# toAddress: This variable assigns where you would like update notifications to arrive.
#
# Required, no defaults

toAddress="replace-this-email-address@here.com"

0 Comments:

Post a Comment

<< Home