2017-02-02 14:37:32 +00:00
|
|
|
#!/bin/bash
|
2015-02-23 15:02:43 +00:00
|
|
|
#
|
|
|
|
# Simplify running cosmos, with serialization if flock is available.
|
|
|
|
#
|
|
|
|
|
2017-02-02 14:37:32 +00:00
|
|
|
readonly PROGNAME=$(basename "$0")
|
|
|
|
readonly LOCKFILE_DIR=/tmp
|
|
|
|
readonly LOCK_FD=200
|
2015-02-23 15:02:43 +00:00
|
|
|
|
2017-02-02 14:37:32 +00:00
|
|
|
lock() {
|
|
|
|
local prefix=$1
|
|
|
|
local fd=${2:-$LOCK_FD}
|
|
|
|
local lock_file=$LOCKFILE_DIR/$prefix.lock
|
2015-02-23 15:02:43 +00:00
|
|
|
|
2017-02-02 14:37:32 +00:00
|
|
|
# create lock file
|
|
|
|
eval "exec $fd>$lock_file"
|
|
|
|
|
|
|
|
# acquier the lock
|
|
|
|
flock -n $fd \
|
|
|
|
&& return 0 \
|
|
|
|
|| return 1
|
|
|
|
}
|
|
|
|
|
|
|
|
eexit() {
|
|
|
|
local error_str="$@"
|
|
|
|
|
|
|
|
echo $error_str
|
|
|
|
exit 1
|
|
|
|
}
|
2015-02-23 15:02:43 +00:00
|
|
|
|
2017-02-02 14:37:32 +00:00
|
|
|
main () {
|
|
|
|
lock $PROGNAME || eexit "Only one instance of $PROGNAME can run at one time."
|
|
|
|
cosmos $* update
|
|
|
|
cosmos $* apply
|
2015-02-23 15:02:43 +00:00
|
|
|
|
2017-02-02 14:37:32 +00:00
|
|
|
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
|