2011-11-02 15:30:35 +00:00
|
|
|
#!/bin/sh
|
|
|
|
#
|
|
|
|
# 2011 Steven Armstrong (steven-cdist at armstrong.cc)
|
2013-06-19 08:55:07 +00:00
|
|
|
# 2013 Nico Schottelius (nico-cdist at schottelius.org)
|
2013-12-09 10:39:44 +00:00
|
|
|
# 2013 Thomas Oettli (otho at sfs.biz)
|
2011-11-02 15:30:35 +00:00
|
|
|
#
|
|
|
|
# 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/>.
|
|
|
|
#
|
|
|
|
|
2013-06-19 08:55:07 +00:00
|
|
|
name="$__object_name"
|
2011-11-02 15:30:35 +00:00
|
|
|
user="$(cat "$__object/parameter/user")"
|
2013-06-19 08:55:07 +00:00
|
|
|
command="$(cat "$__object/parameter/command")"
|
|
|
|
|
|
|
|
if [ -f "$__object/parameter/raw" ]; then
|
|
|
|
raw="$(cat "$__object/parameter/raw")"
|
2014-07-23 07:22:03 +00:00
|
|
|
entry="$raw $command # $name"
|
2013-12-09 08:44:40 +00:00
|
|
|
elif [ -f "$__object/parameter/raw_command" ]; then
|
|
|
|
entry="$command"
|
2013-06-19 08:55:07 +00:00
|
|
|
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 "*")"
|
2014-07-23 07:22:03 +00:00
|
|
|
entry="$minute $hour $day_of_month $month $day_of_week $command # $name"
|
2013-06-19 08:55:07 +00:00
|
|
|
fi
|
|
|
|
|
|
|
|
mkdir "$__object/files"
|
|
|
|
echo "$entry" > "$__object/files/entry"
|
2011-11-02 15:30:35 +00:00
|
|
|
|
2013-06-19 08:55:07 +00:00
|
|
|
if diff -q "$__object/files/entry" "$__object/explorer/entry" >/dev/null; then
|
|
|
|
state_is=present
|
|
|
|
else
|
|
|
|
state_is=absent
|
2011-11-02 15:30:35 +00:00
|
|
|
fi
|
2013-06-19 08:55:07 +00:00
|
|
|
|
|
|
|
state_should="$(cat "$__object/parameter/state" 2>/dev/null || echo "present")"
|
|
|
|
|
|
|
|
[ "$state_is" = "$state_should" ] && exit 0
|
|
|
|
|
2013-06-20 07:20:14 +00:00
|
|
|
# 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"
|
2013-12-09 08:44:40 +00:00
|
|
|
filter="^# DO NOT EDIT THIS FILE|^# \(.* installed on |^# \(Cron version V"
|
2013-06-20 07:20:14 +00:00
|
|
|
cat << DONE
|
2013-12-19 13:45:55 +00:00
|
|
|
crontab -u $user -l 2>/dev/null | grep -v -E "$filter" | awk -v prefix="$prefix" -v suffix="$suffix" '
|
2013-06-20 07:20:14 +00:00
|
|
|
{
|
|
|
|
if (index(\$0,prefix)) {
|
|
|
|
triggered=1
|
|
|
|
}
|
|
|
|
if (triggered) {
|
|
|
|
if (index(\$0,suffix)) {
|
|
|
|
triggered=0
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
print
|
|
|
|
}
|
|
|
|
}
|
|
|
|
' | crontab -u $user -
|
|
|
|
DONE
|
|
|
|
|
2013-06-19 08:55:07 +00:00
|
|
|
case "$state_should" in
|
|
|
|
present)
|
2014-01-14 09:11:33 +00:00
|
|
|
# if we insert new entry, filter also all entrys out with the same id
|
2013-06-19 08:55:07 +00:00
|
|
|
echo "("
|
2014-01-14 09:11:33 +00:00
|
|
|
echo "crontab -u $user -l 2>/dev/null | grep -v -E \"$filter\" | grep -v \"# $name\\$\" 2>/dev/null || true"
|
2013-06-19 08:55:07 +00:00
|
|
|
echo "echo '$entry'"
|
|
|
|
echo ") | crontab -u $user -"
|
|
|
|
;;
|
|
|
|
absent)
|
2014-07-23 07:22:03 +00:00
|
|
|
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
|
2013-06-19 08:55:07 +00:00
|
|
|
;;
|
|
|
|
esac
|