From 900a1d1a55189531c82acf38057b15c8b51303fa Mon Sep 17 00:00:00 2001 From: Nicolas Boisselier Date: Mon, 18 Feb 2019 04:43:55 +0000 Subject: [PATCH] etc/profile.d/nb.sh --- bin/nb-install | 104 ---------------------------------------- etc/profile.d/functions | 103 +++++++++++++++++++++++++++++++++++++++ etc/profile.d/nb.sh | 37 ++++++++++++++ lib/sh/nb-update.sh | 36 -------------- 4 files changed, 140 insertions(+), 140 deletions(-) diff --git a/bin/nb-install b/bin/nb-install index 373f1eff..80beed0f 100755 --- a/bin/nb-install +++ b/bin/nb-install @@ -123,108 +123,4 @@ main() { } -cron_d_install() { - local 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 - #shell_help "$usage" "$@" && return 1 - [ -z "$*" ] && echo "$usage" && return 1 - if ! perl -e '' 2>/dev/null; then - echo "$FUNCNAME: please install perl" 1>&2 - return 1 - fi - - if [ "$#" -gt 0 ]; then - case "$1" in -t|--test) test=1; shift ;; esac - fi - - #echo "$#"; return - if [ $# -gt 4 -o $# = 0 ]; 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" -# NB 31.08.16 cat <<< "$1" > "$file"; shift - echo "$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 2>/dev/null)" != "$(cat /etc/cron.d/$fname 2>/dev/null)" ] && echo ': Changed' && return - echo ': Not changed' - return 1 - fi - - [ "$(cat $file 2>/dev/null)" != "$(cat /etc/cron.d/$fname 2>/dev/null)" ] && 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 - -# NB 31.08.16 [ "$cron" != "$(crontab -l)" ] && crontab <<< "$cron" - [ "$cron" != "$(crontab -l)" ] && echo "$cron" | crontab - -} - main diff --git a/etc/profile.d/functions b/etc/profile.d/functions index 39303c5c..58bcf33a 100755 --- a/etc/profile.d/functions +++ b/etc/profile.d/functions @@ -875,3 +875,106 @@ xml2csv() { # NB 16.01.18 ' $noheader # NB 16.01.18 } +cron_d_install() { + local 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 + #shell_help "$usage" "$@" && return 1 + [ -z "$*" ] && echo "$usage" && return 1 + if ! perl -e '' 2>/dev/null; then + echo "$FUNCNAME: please install perl" 1>&2 + return 1 + fi + + if [ "$#" -gt 0 ]; then + case "$1" in -t|--test) test=1; shift ;; esac + fi + + #echo "$#"; return + if [ $# -gt 4 -o $# = 0 ]; 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" +# NB 31.08.16 cat <<< "$1" > "$file"; shift + echo "$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 2>/dev/null)" != "$(cat /etc/cron.d/$fname 2>/dev/null)" ] && echo ': Changed' && return + echo ': Not changed' + return 1 + fi + + [ "$(cat $file 2>/dev/null)" != "$(cat /etc/cron.d/$fname 2>/dev/null)" ] && 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 + +# NB 31.08.16 [ "$cron" != "$(crontab -l)" ] && crontab <<< "$cron" + [ "$cron" != "$(crontab -l)" ] && echo "$cron" | crontab + +} diff --git a/etc/profile.d/nb.sh b/etc/profile.d/nb.sh index c01d7b72..f94b03fc 100644 --- a/etc/profile.d/nb.sh +++ b/etc/profile.d/nb.sh @@ -85,6 +85,7 @@ nb_api() { local uri="$1"; shift if [ -n "$NB_AUTH" -a x = x$(awk '/^machine api.nbdom.net / { print $4":"$6}' $HOME/.netrc 2> /dev/null) ]; then +#echo $NB_AUTH 1>&2 # NB 16.01.19 if [ -z "$NB_AUTH" -o x = x$(awk '/^machine api.nbdom.net / { print $4":"$6}' $HOME/.netrc 2> /dev/null) ]; then curl -sfS -u "$NB_AUTH" "https://api.nbdom.net$uri" "$@" else @@ -92,3 +93,39 @@ nb_api() { fi } +nb_api_table_post() { + local usage="Usage: nb_api_table_post TABLE field1=val1 field2=val2 ..." + local debug=0; [ "$1" = "-debug" ] && debug=1 && shift + local table="${1:?$usage}"; shift + + local curl="nb_api /data/$table/replace.sh" + + local args="$*" + while [ "$#" -gt "0" ]; do + curl="$curl -d \"$1\"" + shift + done + if [ "$debug" = "1" ]; then + echo "$curl"; return + fi + eval "$(eval "$curl")" #&& echo "$table: $rowCount: $args" +# NB 15.02.19 ( +# NB 15.02.19 eval "$(eval "$curl")" #&& echo "$table: $rowCount: $args" +# NB 15.02.19 #eval "$curl" #&& echo "$table: $rowCount: $args" +# NB 15.02.19 ) +} + +nb_api_post_sys_infos() { + # To be compatible with other sed that does not support \t + sys_infos | while read -ra i; do + nb_api_post_host_info "${i[0]}" "${i[1]}" + done +} + +nb_api_post_host_info() { + local host=$(hostname) + [ -z "$*" ] && echo "Usage: nb_api_post_host_info key=? val=?" && return 1 + [ -z "$host" ] && host=$(hostname -f) + nb_api_table_post host_info "host=$host" "key=$1" "val=$2" + #nb_api_table_post host_info "host=$host" "$@" +} diff --git a/lib/sh/nb-update.sh b/lib/sh/nb-update.sh index 3bd79771..e69de29b 100644 --- a/lib/sh/nb-update.sh +++ b/lib/sh/nb-update.sh @@ -1,36 +0,0 @@ -nb_api_table_post() { - local usage="Usage: nb_api_table_post TABLE field1=val1 field2=val2 ..." - local debug=0; [ "$1" = "-debug" ] && debug=1 && shift - local table="${1:?$usage}"; shift - - local curl="nb_api /data/$table/replace.sh" - - local args="$*" - while [ "$#" -gt "0" ]; do - curl="$curl -d \"$1\"" - shift - done - if [ "$debug" = "1" ]; then - echo "$curl"; return - fi - eval "$(eval "$curl")" #&& echo "$table: $rowCount: $args" -# NB 15.02.19 ( -# NB 15.02.19 eval "$(eval "$curl")" #&& echo "$table: $rowCount: $args" -# NB 15.02.19 #eval "$curl" #&& echo "$table: $rowCount: $args" -# NB 15.02.19 ) -} - -nb_api_post_sys_infos() { - # To be compatible with other sed that does not support \t - sys_infos | while read -ra i; do - nb_api_post_host_info "${i[0]}" "${i[1]}" - done -} - -nb_api_post_host_info() { - local host=$(hostname) - [ -z "$*" ] && echo "Usage: nb_api_post_host_info key=? val=?" && return 1 - [ -z "$host" ] && host=$(hostname -f) - nb_api_table_post host_info "host=$host" "key=$1" "val=$2" - #nb_api_table_post host_info "host=$host" "$@" -} -- 2.47.3