summaryrefslogtreecommitdiff
path: root/global/overlay/usr/local/bin/run-cosmos
blob: 5f2cbc1e980f2d0ce13fafba6e1396c74eba2264 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
#!/bin/bash
#
# Simplify running cosmos, with serialization if flock is available.
#

readonly PROGNAME=$(basename "$0")
readonly LOCKFILE_DIR=/tmp
readonly LOCK_FD=200

lock() {
    local prefix=$1
    local fd=${2:-$LOCK_FD}
    local lock_file=$LOCKFILE_DIR/$prefix.lock

    # 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
}

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