This repository has been archived on 2025-03-16. You can view files and clone it, but cannot push or open issues or pull requests.
Files
puppet/templates/puppetd_run.sh.erb
2015-01-13 17:26:17 +01:00

73 lines
2.0 KiB
Plaintext
Executable File

#!/usr/bin/env bash
# a wrapper script, to run puppetd after a random time
# this will be called by cron at the same time on each client,
# but should not call the master at the same time as the others.
<% if @norunifloggedin == true -%>
# not running if users are logged in
[[ $(who|wc -l) -gt 0 ]] && exit 0
<% end -%>
#export FACTERLIB="<%= scope.lookupvar("factpath") %>"
PATH="/opt/local/bin:/opt/local/sbin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin"
PUPPET=$(which puppet)
[[ -e ${PUPPET} ]] || exit 1
PUPPET="${PUPPET}"
<% if @run_if_ipmatch != "" -%>
ISUNIIP=$(ifconfig |grep -c -e "<%= @run_if_ipmatch %>")
<% else -%>
ISUNIIP=1
<% end -%>
PIDF="/tmp/puppetd_run.pid"
function cleanup {
[[ -e ${PIDF} ]] && rm -f ${PIDF}
exit 0
}
if [ -e ${PIDF} ]; then
PID=`cat ${PIDF}`
if [ $(ps -eo pid |grep -c ${PID}) -gt 0 ]; then
exit 0
fi
fi
# write our pid
echo -n $$ >${PIDF}
if [ ${ISUNIIP} -gt 0 ]; then
if [ -e ${PUPPET} ]; then
#mac os X laptop check
if [ -e /usr/bin/pmset ]; then
if [ $(/usr/bin/pmset -g batt | grep -c "Battery Power") -eq 1 ]; then
#we are running on battery and do not want to do anything
cleanup
fi
fi
#Linux laptop check
if [ -e /proc/acpi/ac_adapter/AC/state ]; then
if [ $(grep -c "on-line" /proc/acpi/ac_adapter/AC/state) -eq 0 ]; then
#we are running on battery and do not want to do anything
cleanup
fi
fi
#MAXSLEEP=<%= @maxsleep %> #How many seconds to wait maximally
SLEEP=${RANDOM}
I=${RANDOM}
# $RANDOM must be called several times, to ensure it really
# is something like random
for x in 1 2 3 4 5 6 7 8 9 10 11 12; do
SLEEP=${RANDOM}
done
# wait maximally $MAXSLEEP seconds
#sleep $(expr ${SLEEP} % ${MAXSLEEP})
# run puppetd
nice -n 12 $PUPPET agent -t 2&>>/var/log/puppet/puppetd_run.log
fi
fi
# cleanup the pidfile
cleanup