Merge branch '__cron-simplify'
This commit is contained in:
		
				commit
				
					
						d78dc7af8b
					
				
			
		
					 3 changed files with 43 additions and 101 deletions
				
			
		| 
						 | 
					@ -1,6 +1,7 @@
 | 
				
			||||||
#!/bin/sh
 | 
					#!/bin/sh
 | 
				
			||||||
#
 | 
					#
 | 
				
			||||||
# 2011-2013 Steven Armstrong (steven-cdist at armstrong.cc)
 | 
					# 2011-2013 Steven Armstrong (steven-cdist at armstrong.cc)
 | 
				
			||||||
 | 
					# 2013      Nico Schottelius (nico-cdist at schottelius.org)
 | 
				
			||||||
#
 | 
					#
 | 
				
			||||||
# This file is part of cdist.
 | 
					# This file is part of cdist.
 | 
				
			||||||
#
 | 
					#
 | 
				
			||||||
| 
						 | 
					@ -18,22 +19,7 @@
 | 
				
			||||||
# along with cdist. If not, see <http://www.gnu.org/licenses/>.
 | 
					# along with cdist. If not, see <http://www.gnu.org/licenses/>.
 | 
				
			||||||
#
 | 
					#
 | 
				
			||||||
 | 
					
 | 
				
			||||||
name="$__object_id"
 | 
					name="$__object_name"
 | 
				
			||||||
user="$(cat "$__object/parameter/user")"
 | 
					user="$(cat "$__object/parameter/user")"
 | 
				
			||||||
 | 
					
 | 
				
			||||||
prefix="#cdist:__cron/$name"
 | 
					crontab -u $user -l 2>/dev/null | grep "# $name\$" || true
 | 
				
			||||||
suffix="#/cdist:__cron/$name"
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
crontab -u $user -l 2>/dev/null | awk -v prefix="$prefix" -v suffix="$suffix" '
 | 
					 | 
				
			||||||
{
 | 
					 | 
				
			||||||
   if (index($0,prefix)) {
 | 
					 | 
				
			||||||
      triggered=1
 | 
					 | 
				
			||||||
   }
 | 
					 | 
				
			||||||
   if (triggered) {
 | 
					 | 
				
			||||||
      if (index($0,suffix)) {
 | 
					 | 
				
			||||||
            triggered=0
 | 
					 | 
				
			||||||
      }
 | 
					 | 
				
			||||||
      print
 | 
					 | 
				
			||||||
   }
 | 
					 | 
				
			||||||
}
 | 
					 | 
				
			||||||
'
 | 
					 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -1,6 +1,7 @@
 | 
				
			||||||
#!/bin/sh
 | 
					#!/bin/sh
 | 
				
			||||||
#
 | 
					#
 | 
				
			||||||
# 2011 Steven Armstrong (steven-cdist at armstrong.cc)
 | 
					# 2011 Steven Armstrong (steven-cdist at armstrong.cc)
 | 
				
			||||||
 | 
					# 2013      Nico Schottelius (nico-cdist at schottelius.org)
 | 
				
			||||||
#
 | 
					#
 | 
				
			||||||
# This file is part of cdist.
 | 
					# This file is part of cdist.
 | 
				
			||||||
#
 | 
					#
 | 
				
			||||||
| 
						 | 
					@ -18,45 +19,45 @@
 | 
				
			||||||
# along with cdist. If not, see <http://www.gnu.org/licenses/>.
 | 
					# along with cdist. If not, see <http://www.gnu.org/licenses/>.
 | 
				
			||||||
#
 | 
					#
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					name="$__object_name"
 | 
				
			||||||
user="$(cat "$__object/parameter/user")"
 | 
					user="$(cat "$__object/parameter/user")"
 | 
				
			||||||
state_should="$(cat "$__object/parameter/state" 2>/dev/null || echo "present")"
 | 
					command="$(cat "$__object/parameter/command")"
 | 
				
			||||||
state_is=$(diff -q "$__object/parameter/entry" "$__object/explorer/entry" \
 | 
					 | 
				
			||||||
   && echo present \
 | 
					 | 
				
			||||||
   || echo absent
 | 
					 | 
				
			||||||
)
 | 
					 | 
				
			||||||
 | 
					
 | 
				
			||||||
if [ "$state_is" != "$state_should" ]; then
 | 
					if [ -f "$__object/parameter/raw" ]; then
 | 
				
			||||||
   case "$state_should" in
 | 
					   raw="$(cat "$__object/parameter/raw")"
 | 
				
			||||||
      present)
 | 
					   entry="$raw $command"
 | 
				
			||||||
         cat << DONE
 | 
					else
 | 
				
			||||||
(
 | 
					   minute="$(cat "$__object/parameter/minute" 2>/dev/null || echo "*")"
 | 
				
			||||||
crontab -u $user -l || true
 | 
					   hour="$(cat "$__object/parameter/hour" 2>/dev/null || echo "*")"
 | 
				
			||||||
cat << EOC
 | 
					   day_of_month="$(cat "$__object/parameter/day_of_month" 2>/dev/null || echo "*")"
 | 
				
			||||||
$(cat "$__object/parameter/entry")
 | 
					   month="$(cat "$__object/parameter/month" 2>/dev/null || echo "*")"
 | 
				
			||||||
EOC
 | 
					   day_of_week="$(cat "$__object/parameter/day_of_week" 2>/dev/null || echo "*")"
 | 
				
			||||||
) | crontab -u $user -
 | 
					   entry="$minute $hour $day_of_month $month $day_of_week $command"
 | 
				
			||||||
DONE
 | 
					 | 
				
			||||||
      ;;
 | 
					 | 
				
			||||||
      absent)
 | 
					 | 
				
			||||||
         # NOTE: keep variables in sync in manifest/explorer/gencode-*
 | 
					 | 
				
			||||||
         prefix="#cdist:__cron/$name"
 | 
					 | 
				
			||||||
         suffix="#/cdist:__cron/$name"
 | 
					 | 
				
			||||||
         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
 | 
					fi
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					entry="$entry # $name"
 | 
				
			||||||
 | 
					mkdir "$__object/files"
 | 
				
			||||||
 | 
					echo "$entry" > "$__object/files/entry"
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					if diff -q "$__object/files/entry" "$__object/explorer/entry" >/dev/null; then
 | 
				
			||||||
 | 
					    state_is=present
 | 
				
			||||||
 | 
					else
 | 
				
			||||||
 | 
					    state_is=absent
 | 
				
			||||||
 | 
					fi
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					state_should="$(cat "$__object/parameter/state" 2>/dev/null || echo "present")"
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					[ "$state_is" = "$state_should" ] && exit 0
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					case "$state_should" in
 | 
				
			||||||
 | 
					    present)
 | 
				
			||||||
 | 
					        echo "("
 | 
				
			||||||
 | 
					        echo "crontab -u $user -l 2>/dev/null || true"
 | 
				
			||||||
 | 
					        echo "echo '$entry'"
 | 
				
			||||||
 | 
					        echo ") | crontab -u $user -"
 | 
				
			||||||
 | 
					    ;;
 | 
				
			||||||
 | 
					    absent)
 | 
				
			||||||
 | 
					        echo "( crontab -u $user -l 2>/dev/null || true ) | \\"
 | 
				
			||||||
 | 
					        echo "grep -v \"# $name\\$\" | crontab -u $user -"
 | 
				
			||||||
 | 
					    ;;
 | 
				
			||||||
 | 
					esac
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -1,45 +0,0 @@
 | 
				
			||||||
#!/bin/sh
 | 
					 | 
				
			||||||
#
 | 
					 | 
				
			||||||
# 2011-2013 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")"
 | 
					 | 
				
			||||||
state="$(cat "$__object/parameter/state" 2>/dev/null || echo "present")"
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
if [ -f "$__object/parameter/raw" ]; then
 | 
					 | 
				
			||||||
   raw="$(cat "$__object/parameter/raw")"
 | 
					 | 
				
			||||||
   entry="$raw $command"
 | 
					 | 
				
			||||||
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 "*")"
 | 
					 | 
				
			||||||
   entry="$minute $hour $day_of_month $month $day_of_week $command"
 | 
					 | 
				
			||||||
fi
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
# NOTE: keep variables in sync in manifest/explorer/gencode-*
 | 
					 | 
				
			||||||
prefix="#cdist:__cron/$name"
 | 
					 | 
				
			||||||
suffix="#/cdist:__cron/$name"
 | 
					 | 
				
			||||||
cat >> "$__object/parameter/entry" << DONE
 | 
					 | 
				
			||||||
"$prefix"
 | 
					 | 
				
			||||||
"$entry"
 | 
					 | 
				
			||||||
"$suffix"
 | 
					 | 
				
			||||||
DONE
 | 
					 | 
				
			||||||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue