From: Nicolas Boisselier Date: Sat, 27 Feb 2016 16:53:37 +0000 (+0000) Subject: cron_d_install X-Git-Url: https://git.nbdom.net/?a=commitdiff_plain;h=f3b38d66c6b8bfe455bcde7e7464d4b97384552b;p=nb.git cron_d_install --- diff --git a/bin/nb-install b/bin/nb-install index 13269aac..95c29835 100755 --- a/bin/nb-install +++ b/bin/nb-install @@ -7,18 +7,27 @@ . "${BASH_SOURCE%/*}/../etc/profile" || exit declare -r NAME="$(basename "${0}")" declare TMP=/tmp/$NAME.$$ -declare INSTALL="rsync -au --force" +declare INSTALL="rsync --archive --checksum --force" +if [ "$UID" != "0" ] ; then + echo "$NAME: please login as root." 1>&2 + exit 1 +fi + +############################################################################## # # Args # +############################################################################## case "$@" in *-h*|*-help*) exec echo "Usage: $NAME [-v] [-n]";; esac case "$@" in *-v*) INSTALL="$INSTALL -v";; esac case "$@" in *-n*) INSTALL="$INSTALL -n";; esac +############################################################################## # # Functions # +############################################################################## function verbose() { [ -z "$@" ] && return printf '> %s\n' "$@" @@ -31,31 +40,12 @@ function fdiff() { return 1 } +############################################################################## # # Cron # -if [ -d /etc/cron.d ]; then - - if fdiff "$NB_ROOT/etc/cron/nb" "/etc/cron.d/nb"; then - verbose "Install /etc/cron.d/nb" - $INSTALL "$NB_ROOT/etc/cron/nb" /etc/cron.d/nb - fi - -else - - crontab -l > $TMP.crontab.orig.orig - ( - # Delete user - crontab -l | grep -vE 'nb-update|^(PATH|NB_LOG|SHELL|#[<>]nb)' - perl -ane '/^(NB_LOG|SHELL|PATH)/ and print; /^[\*\d]/ or next; print join(" ",@F[0..4,6..$#F])."\n"' $NB_ROOT/etc/cron/nb - ) > $TMP.crontab.orig.new - - if fdiff $TMP.crontab.orig.orig $TMP.crontab.orig.new; then - verbose "Install /etc/cron.d/nb" - crontab $TMP.crontab.orig.new - fi - -fi +cron_d_install "$NB_ROOT/etc/cron/nb" "nb-update|$NB_ROOT|NB_LOG" && verbose "Cron: $NB_ROOT/etc/cron/nb" +#cron_d_install "test" "" "nico.*true" && verbose "Cron: nico" # # Profile @@ -63,8 +53,6 @@ fi echo "[ -r $NB_ROOT/etc/profile ] && . $NB_ROOT/etc/profile" > $TMP.profile if [ -d /etc/profile.d ]; then - rm -fv /etc/profile.d/nb-profile.sh - if fdiff $TMP.profile /etc/profile.d/nb.sh 1>/dev/null 2>&1; then verbose "Install /etc/profile.d/nb.sh" $INSTALL -c $TMP.profile /etc/profile.d/nb.sh @@ -97,7 +85,7 @@ chmod 755 $NB_ROOT/etc/vim/vim* $NB_ROOT/etc/vim/*.sh $NB_ROOT/etc/vim/*.pl $NB_ # # Log # -NB_USER=`(ls -dl $NB_ROOT 2>/dev/null || printf '1 2 root') | awk '{print $3}'` +NB_USER=`(ls -dl $NB_ROOT 2>/dev/null || printf '- 0 root') | awk '{print $3}'` verbose $(chown "$NB_USER" /var/log/nb.log 2> /dev/null) # diff --git a/bin/nb-update b/bin/nb-update index c946717c..b2dcb101 100755 --- a/bin/nb-update +++ b/bin/nb-update @@ -50,7 +50,7 @@ for repo in $(nb-repo-dirs); do else cmd_root="true" - [ -x bin/$name-install ] && cmd_root="$cmd_root && (echo '>$name-install' && bin/$name-install)|sed 's/^/ /'" + [ -x bin/$name-install ] && cmd_root="$cmd_root && echo ' >$name-install' && bin/$name-install|sed 's/^/ /'" [ "$cmd_root" == "true" ] || eval "$cmd_root" || echo ">err" fi diff --git a/etc/profile.d/functions b/etc/profile.d/functions index e5ff228f..76467c30 100644 --- a/etc/profile.d/functions +++ b/etc/profile.d/functions @@ -58,7 +58,6 @@ shell_ssh() { ( alias eval type $(shell_functions|perl -pe 's/\s+/ /') | grep -v 'is a function$' - #cat $NB_ROOT/etc/profile echo "[ -r /etc/profile ] && . /etc/profile" ) | ssh "$server" "cat > $tmp || rm -vf $tmp" || return @@ -337,3 +336,98 @@ nb_repo_dirs() { (ls /{opt,home,home/www}/*/.nb-install | xargs -n1 dirname) 2>/dev/null ) | perl -ne '/^\s*$/ and next; s/^\s+//; print unless $h{$_}++' } + +cron_d_install() { + declare usage fname file exp cron tmp test + usage="Usage: $FUNCNAME [-t|--test] [CRON_D_FILE] [IGNORE_REGEXP] or [NAME] [CRON_D_CONTENT] [IGNORE_REGEXP]" + + shell_help_noarg "$usage" "$@" && return 1 + case "$1" in -t|--test) test=1; shift ;; esac + + #echo "$#"; return + if [ $# -gt 3 ]; then + shell_help_noarg "$usage" "--help" && return 1 + + #elif [ $# -eq 3 ]; then + elif [ ! -e "$1" ]; then + fname=$1; shift + tmp="/tmp/$FUNCNAME.$$" + trap "rm -f $tmp*" EXIT + file="$tmp.cron" + cat <<< "$1" > "$file"; shift + exp=$1; shift + + else + file=$1; shift + exp=$1; shift + fname=$(basename "$file") + + fi + + # + # Case 1 - /etc/cron.d + # + if [ -d /etc/cron.d ]; then + + if [ "$test" = "1" ]; then + printf '%s' "$FUNCNAME: /etc/cron.d: $fname" + [ "$(cat $file)" != "$(cat /etc/cron.d/$fname)" ] && echo ': Changed' && return + echo ': Not changed' + return 1 + fi + + [ "$(cat $file)" != "$(cat /etc/cron.d/$fname)" ] && cp "$file" "/etc/cron.d/$fname" + return + fi + + # + # Case 2 - crontab + # + + cron=$( + perl -e ' +$diff = 0; +$e = shift @ARGV; + +@f = map {chomp; $_} grep {!/^(#|\s*$)/} <>; +for ($i=0;$i<@f;$i++) { + @F = split(" ",$f[$i]); + next if $f[$i] !~ /^[\*\d]/ or @F<6; + if ($F[5] ne "root"){ + $_ = join(" ",@F[6..$#F]); + s/"/\\"/g; + @_=qq|su -l "$F[5]" -c "$_"|; + @F = (@F[0..4],"",@_); + }; + $f[$i] = join(" ",@F[0..4,6..$#F]); +} +%f = map {$_=>1} @f; + +@ARGV = ("crontab -l|"); +@c = map {chomp; $_} <>; +%c = map {$_=>1} @c; + +for (@c) { + 0 and print STDERR "-$_\n" if $e and !$f{$_} and /$e/; + $diff=1 and next if $e and !$f{$_} and /$e/; + print "$_\n"; +} + +for (@f) { + $diff=2 and print "$_\n" if !$c{$_}; +} + +exit $diff ? 0 : 1; +' "$exp" "$file" +) + + if [ "$test" = "1" ]; then + printf '%s' "$FUNCNAME: crontab: $fname" + #echo -e "\n<\n$(crontab -l)\n>\n<\n$cron\n>\n" + [ "$cron" != "$(crontab -l)" ] && echo ': Changed' && return + echo ": Not changed; return 1" + fi + + [ "$cron" != "$(crontab -l)" ] && crontab <<< "$cron" + +}