gencode-remote 3.33 KB
Newer Older
Darko Poljak's avatar
Darko Poljak committed
1
#!/bin/sh -e
2 3
#
# 2011 Steven Armstrong (steven-cdist at armstrong.cc)
4
# 2013      Nico Schottelius (nico-cdist at schottelius.org)
5
# 2013      Thomas Oettli (otho at sfs.biz)
6
# 2017      Daniel Heule (hda at sfs.biz)
7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
#
# This file is part of cdist.
#
# cdist is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# cdist is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with cdist. If not, see <http://www.gnu.org/licenses/>.
#

24
name="$__object_name"
25
user="$(cat "$__object/parameter/user")"
26 27 28 29
command="$(cat "$__object/parameter/command")"

if [ -f "$__object/parameter/raw" ]; then
   raw="$(cat "$__object/parameter/raw")"
Daniel Heule's avatar
Daniel Heule committed
30
   entry="$raw $command # $name"
Daniel Heule's avatar
Daniel Heule committed
31 32
elif [ -f "$__object/parameter/raw_command" ]; then
   entry="$command"
33 34 35 36 37 38
else
   minute="$(cat "$__object/parameter/minute" 2>/dev/null || echo "*")"
   hour="$(cat "$__object/parameter/hour" 2>/dev/null || echo "*")"
   day_of_month="$(cat "$__object/parameter/day_of_month" 2>/dev/null || echo "*")"
   month="$(cat "$__object/parameter/month" 2>/dev/null || echo "*")"
   day_of_week="$(cat "$__object/parameter/day_of_week" 2>/dev/null || echo "*")"
Daniel Heule's avatar
Daniel Heule committed
39
   entry="$minute $hour $day_of_month $month $day_of_week $command # $name"
40 41 42 43
fi

mkdir "$__object/files"
echo "$entry" > "$__object/files/entry"
44

45 46 47 48 49 50
if [ -s "$__object/explorer/entry" ]; then
    if diff -q "$__object/files/entry" "$__object/explorer/entry" >/dev/null; then
        state_is=present
    else
        state_is=modified
    fi
51 52
else
    state_is=absent
53
fi
54 55 56 57 58

state_should="$(cat "$__object/parameter/state" 2>/dev/null || echo "present")"

[ "$state_is" = "$state_should" ] && exit 0

59 60 61 62 63 64
# If anything is going to change, ensure the old entries are
# not present anymore

# These are the old markers
prefix="#cdist:__cron/$__object_id"
suffix="#/cdist:__cron/$__object_id"
Darko Poljak's avatar
Darko Poljak committed
65
filter='^# DO NOT EDIT THIS FILE|^# \(.* installed on |^# \(Cron version V|^# \(Cronie version .\..\)$'
66
cat << DONE
67
crontab -u $user -l 2>/dev/null | grep -v -E "$filter" | awk -v prefix="$prefix" -v suffix="$suffix" '
68 69 70 71 72 73 74 75 76 77 78 79 80 81 82
{
   if (index(\$0,prefix)) {
      triggered=1
   }
   if (triggered) {
      if (index(\$0,suffix)) {
            triggered=0
      }
   } else {
      print
   }
}
' | crontab -u $user -
DONE

83 84
case "$state_should" in
    present)
85
        # if we insert new entry, filter also all entrys out with the same id 
86
        echo "("
87
        echo "crontab -u $user -l 2>/dev/null | grep -v -E \"$filter\" | grep -v \"# $name\\$\" 2>/dev/null || true"
88 89 90 91
        echo "echo '$entry'"
        echo ") | crontab -u $user -"
    ;;
    absent)
Daniel Heule's avatar
Daniel Heule committed
92 93 94 95 96 97 98
        if [ -f "$__object/parameter/raw_command" ]; then
            echo "( crontab -u $user -l 2>/dev/null | grep -v -E \"$filter\" 2>/dev/null || true ) | \\"
            echo "grep -v \"^$entry\\$\" | crontab -u $user -"
        else
            echo "( crontab -u $user -l 2>/dev/null | grep -v -E \"$filter\" 2>/dev/null || true ) | \\"
            echo "grep -v \"# $name\\$\" | crontab -u $user -"
        fi
99 100
    ;;
esac