new type __cron: installs and manages cron jobs

Signed-off-by: Steven Armstrong <steven@icarus.ethz.ch>
This commit is contained in:
Steven Armstrong 2011-11-02 16:30:35 +01:00
parent b8435b2fd2
commit e195eb46c4
6 changed files with 237 additions and 0 deletions

39
conf/type/__cron/explorer/entry Executable file
View file

@ -0,0 +1,39 @@
#!/bin/sh
#
# 2011 Steven Armstrong (steven-cdist at armstrong.cc)
#
# 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/>.
#
name="$__object_id"
user="$(cat "$__object/parameter/user")"
prefix="#cdist:__cron/$name"
suffix="#/cdist:__cron/$name"
crontab -u $user -l | awk -v prefix="$prefix" -v suffix="$suffix" '
{
if (index($0,prefix)) {
triggered=1
}
if (triggered) {
if (index($0,suffix)) {
triggered=0
}
print
}
}
'

63
conf/type/__cron/gencode-remote Executable file
View file

@ -0,0 +1,63 @@
#!/bin/sh
#
# 2011 Steven Armstrong (steven-cdist at armstrong.cc)
#
# 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/>.
#
user="$(cat "$__object/parameter/user")"
state_should="$(cat "$__object/parameter/state")"
state_is=$(cmp --quiet "$__object/parameter/entry" "$__object/explorer/entry" \
&& echo present \
|| echo absent
)
if [ "$state_is" != "$state_should" ]; then
case "$state_should" in
present)
cat << DONE
tmp=\$(mktemp)
crontab -u $user -l > \$tmp
cat >> \$tmp << EOC
$(cat "$__object/parameter/entry")"
EOC
crontab -u $user \$tmp
rm \$tmp
DONE
;;
absent)
# defined in type manifest
prefix="$(cat "$__object/parameter/prefix")"
suffix="$(cat "$__object/parameter/suffix")"
cat << DONE
crontab -u $user -l | awk -v prefix="$prefix" -v suffix="$suffix" '
{
if (index(\$0,prefix)) {
triggered=1
}
if (triggered) {
if (index(\$0,suffix)) {
triggered=0
}
} else {
print
}
}
' | crontab -u $user -
DONE
;;
esac
fi

61
conf/type/__cron/man.text Normal file
View file

@ -0,0 +1,61 @@
cdist-type__cron(7)
===================
Steven Armstrong <steven-cdist--@--armstrong.cc>
NAME
----
cdist-type__cron - installs and manages cron jobs
DESCRIPTION
-----------
This cdist type allows you to manage entries in a users crontab.
REQUIRED PARAMETERS
-------------------
user::
The user who's crontab is edited
command::
The command to run.
OPTIONAL PARAMETERS
-------------------
state::
Either present or absent. Defaults to present.
minute::
See crontab(5). Defaults to *
hour::
See crontab(5). Defaults to *
day_of_month::
See crontab(5). Defaults to *
month::
See crontab(5). Defaults to *
day_of_week::
See crontab(5). Defaults to *
EXAMPLES
--------
--------------------------------------------------------------------------------
# add cronjob
__cron some-id --user root --command "/path/to/script"
# remove cronjob
__cron some-id --user root --command "/path/to/script" --state absent
--------------------------------------------------------------------------------
SEE ALSO
--------
- cdist-type(7)
- crontab(5)
COPYING
-------
Copyright \(C) 2011 Steven Armstrong. Free use of this software is
granted under the terms of the GNU General Public License version 3 (GPLv3).

66
conf/type/__cron/manifest Executable file
View file

@ -0,0 +1,66 @@
#!/bin/sh
#
# 2011 Steven Armstrong (steven-cdist at armstrong.cc)
#
# 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/>.
#
name="$__object_id"
user="$(cat "$__object/parameter/user")"
command="$(cat "$__object/parameter/command")"
# set defaults
if [ ! -f "$__object/parameter/state" ]; then
echo "present" > "$__object/parameter/state"
fi
if [ -f "$__object/parameter/minute" ]; then
minute="$(cat "$__object/parameter/minute")"
else
minute="*"
echo "$minute" > "$__object/parameter/minute"
fi
if [ -f "$__object/parameter/hour" ]; then
hour="$(cat "$__object/parameter/hour")"
else
hour="*"
echo "$hour" > "$__object/parameter/hour"
fi
if [ -f "$__object/parameter/day_of_month" ]; then
day_of_month="$(cat "$__object/parameter/day_of_month")"
else
day_of_month="*"
echo "$day_of_month" > "$__object/parameter/day_of_month"
fi
if [ -f "$__object/parameter/month" ]; then
month="$(cat "$__object/parameter/month")"
else
month="*"
echo "$month" > "$__object/parameter/month"
fi
if [ -f "$__object/parameter/day_of_week" ]; then
day_of_week="$(cat "$__object/parameter/day_of_week")"
else
day_of_week="*"
echo "$day_of_week" > "$__object/parameter/day_of_week"
fi
# NOTE: if changed, also change in explorers
prefix="#cdist:__cron/$name"
suffix="#/cdist:__cron/$name"
echo "$prefix" | tee "$__object/parameter/prefix" > "$__object/parameter/entry"
echo "$minute $hour $day_of_month $month $day_of_week $command" >> "$__object/parameter/entry"
echo "$suffix" | tee "$__object/parameter/suffix" >> "$__object/parameter/entry"

View file

@ -0,0 +1,6 @@
state
minute
hour
day_of_month
month
day_of_week

View file

@ -0,0 +1,2 @@
user
command