ny run-cosmos

This commit is contained in:
Leif Johansson 2017-02-02 15:37:32 +01:00
parent 5294261f2c
commit 4972eefdcc

View file

@ -1,22 +1,46 @@
#!/bin/sh #!/bin/bash
# #
# Simplify running cosmos, with serialization if flock is available. # Simplify running cosmos, with serialization if flock is available.
# #
set -e readonly PROGNAME=$(basename "$0")
readonly LOCKFILE_DIR=/tmp
readonly LOCK_FD=200
FLOCK=`which flock` lock() {
local prefix=$1
local fd=${2:-$LOCK_FD}
local lock_file=$LOCKFILE_DIR/$prefix.lock
if [ -x "$FLOCK" ]; then # create lock file
($FLOCK --exclusive --wait 60 9 || exit 1 eval "exec $fd>$lock_file"
cosmos $* update
cosmos $* apply # acquier the lock
)9>/var/lock/run-cosmos flock -n $fd \
else && return 0 \
cosmos $* update || return 1
cosmos $* apply }
eexit() {
local error_str="$@"
echo $error_str
exit 1
}
main () {
lock $PROGNAME || eexit "Only one instance of $PROGNAME can run at one time."
cosmos $* update
cosmos $* apply
touch /var/run/last-cosmos-ok.stamp
find /var/lib/puppet/reports/ -type f -mtime +10 | xargs rm -f
}
main $*
if [ -f /cosmos-reboot ]; then
rm -f /cosmos-reboot
reboot
fi fi
touch /var/run/last-cosmos-ok.stamp
find /var/lib/puppet/reports/ -type f -mtime +10 | xargs rm -f