}
-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
# 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
+
+}
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
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" "$@"
+}
-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" "$@"
-}