streams-ops/global/overlay/usr/local/bin/run-cosmos

93 lines
2.4 KiB
Plaintext
Raw Normal View History

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
readonly FLEETLOCK_CONFIG=/etc/run-cosmos-fleetlock-conf
readonly FLEETLOCK_DISABLE_FILE=/etc/run-cosmos-fleetlock-disable
readonly FLEETLOCK_TOOL=/usr/local/bin/sunet-fleetlock
readonly HEALTHCHECK_TOOL=/usr/local/bin/sunet-machine-healthy
readonly HEALTHCHECK_DISABLE_FILE=/etc/run-cosmos-healthcheck-disable
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
2023-02-06 15:41:04 +00:00
flock -n "$fd" \
2017-02-02 14:37:32 +00:00
&& return 0 \
|| return 1
}
eexit() {
2023-02-06 15:41:04 +00:00
local error_str="$*"
2017-02-02 14:37:32 +00:00
2023-02-06 15:41:04 +00:00
echo "$error_str"
2017-02-02 14:37:32 +00:00
exit 1
}
2015-02-23 15:02:43 +00:00
fleetlock_lock() {
if [ ! -f $FLEETLOCK_DISABLE_FILE ] && [ -f $FLEETLOCK_CONFIG ] && [ -x $FLEETLOCK_TOOL ]; then
local fleetlock_group=""
# shellcheck source=/dev/null
. $FLEETLOCK_CONFIG || return 1
if [ -z "$fleetlock_group" ]; then
echo "Unable to set fleetlock_group"
return 1
fi
echo "Getting fleetlock lock"
$FLEETLOCK_TOOL --lock-group "$fleetlock_group" --lock || return 1
fi
return 0
}
fleetlock_unlock() {
if [ ! -f $FLEETLOCK_DISABLE_FILE ] && [ -f $FLEETLOCK_CONFIG ] && [ -x $FLEETLOCK_TOOL ]; then
local fleetlock_group=""
# shellcheck source=/dev/null
. $FLEETLOCK_CONFIG || return 1
if [ -z "$fleetlock_group" ]; then
echo "Unable to set fleetlock_group"
return 1
fi
machine_is_healthy || return 1
echo "Releasing fleetlock lock"
$FLEETLOCK_TOOL --lock-group "$fleetlock_group" --unlock || return 1
fi
return 0
}
machine_is_healthy() {
if [ ! -f $HEALTHCHECK_DISABLE_FILE ] && [ -x $HEALTHCHECK_TOOL ]; then
echo "Running any health checks"
$HEALTHCHECK_TOOL || return 1
fi
return 0
}
2017-02-02 14:37:32 +00:00
main () {
2023-02-06 15:41:04 +00:00
lock "$PROGNAME" || eexit "Only one instance of $PROGNAME can run at one time."
fleetlock_lock || eexit "Unable to acquire fleetlock lock."
2023-02-06 15:41:04 +00:00
cosmos "$@" update
cosmos "$@" apply
fleetlock_unlock || eexit "Unable to release fleetlock lock."
2015-02-23 15:02:43 +00:00
2017-02-02 14:37:32 +00:00
touch /var/run/last-cosmos-ok.stamp
2023-02-06 15:41:04 +00:00
find /var/lib/puppet/reports/ -type f -mtime +10 -print0 | xargs -0 rm -f
2017-02-02 14:37:32 +00:00
}
2023-02-06 15:41:04 +00:00
main "$@"
2017-02-02 14:37:32 +00:00
if [ -f /cosmos-reboot ]; then
rm -f /cosmos-reboot
reboot
fi