From 3d7306075ad93f163a4d5e7ca603637f6e00526b Mon Sep 17 00:00:00 2001 From: Nicolas Boisselier Date: Fri, 15 Feb 2019 03:33:27 +0000 Subject: [PATCH] bin/nb-update --- bin/nb-update | 191 +++++++++++++++++++----------------------- etc/profile.d/ldap.sh | 6 +- etc/profile.d/nb.sh | 34 -------- lib/sh/nb-update.sh | 36 ++++++++ 4 files changed, 128 insertions(+), 139 deletions(-) create mode 100644 lib/sh/nb-update.sh diff --git a/bin/nb-update b/bin/nb-update index cb689ee3..40baa058 100755 --- a/bin/nb-update +++ b/bin/nb-update @@ -12,34 +12,15 @@ CLEANUP=0 [ "$1" = "-cleanup" ] && CLEANUP=1 ##################################################################### -# -# Functions -# -##################################################################### -run_parts_minutes() { - local usage="run_parts_minutes MINUTES DIR" \ - local min=${1:?$usage}; min=$(($min-1)) - local dir=${2:?$usage} - local lock="/run/run_parts_minutes.$min" - #echo $min $dir - find "$lock" -mmin +$min -exec rm {} \; >/dev/null 2>&1 - [ -e "$lock" ] && return - #touch "$lock" || return - run-parts -v --test --report "$dir" - echo BYE -} -if [ "$1" = "-test" ]; then - run_parts_minutes 1 /tmp/run/ - exit 0 -fi - # # Requirments # +##################################################################### . $(dirname "$0")/../etc/profile || exit 1; [ -z "$NB_ROOT" ] && exit +! [ -e "$NB_ROOT/lib/sh/$NAME.sh" ] || . "$NB_ROOT/lib/sh/$NAME.sh" || exit if ! which git > /dev/null; then - echo "$NAME: can't find git!" 1>&2 - exit 1 + echo "$NAME: can't find git!" 1>&2 + exit 1 fi ex=0 @@ -53,102 +34,104 @@ export GIT_SSH="$TMP" filter_name="$@" for repo in $(nb_repos); do - [ -d "$repo/.git" ] || continue - [ ! -w "$repo/.git" ] && echo ">No Permission $repo" && echo && continue - cd "$repo" || continue + [ -d "$repo/.git" ] || continue + [ ! -w "$repo/.git" ] && echo ">No Permission $repo" && echo && continue + cd "$repo" || continue - # name from git not from path - name=$(git_name) || continue - [ -z "$name" ] && continue + # name from git not from path + name=$(git_name) || continue + [ -z "$name" ] && continue - # args filter on name - if [ -n "$filter_name" ]; then - match=0 - for i in $@; do - case "$name" in + # args filter on name + if [ -n "$filter_name" ]; then + match=0 + for i in $@; do + case "$name" in $i) match=1; break ;; #*) echo ">$i!=$name" ;; - esac - done - [ "$match" = "1" ] || continue - fi - - branch=$(git_branch) || continue - [ -z "$branch" ] && continue - - # - # Get user - # - user=`ls -dl "$repo/.git" |awk '{print $3}'` - - cmd="" - - # - # Check ssh agent sh file - # - agent=$(eval realpath ~$user/.ssh/agent.sh) - [ -r "$agent" ] && cmd=". $agent && " - #[ -r "$agent" ] && echo "Found agent $agent" - - # - # Pull - # - echo ">Pull $name branch=$branch user=$user path=$repo" - - cmd="${cmd}GIT_SSH=$GIT_SSH cd \"$repo\" && git pull" - [ "$CLEANUP" = "1" ] && cmd="$cmd && git gc" - - if [ 1 = 1 ] && ! git_conn_check; then - echo - elif [ "$USER" = "$user" ]; then - sh -c "$cmd" - elif [ "$USER" = "root" ]; then - su $user -c "$cmd" - else - echo "Wrong user current user=$USER repo user=$user" - false - fi - ret=$? - - # - # *-install - # - if [ "$ret" != "0" ]; then - ex=$ret && echo ">err $ret" - - else - cmd_root="true" - # NB 23.04.18: TODEL + esac + done + [ "$match" = "1" ] || continue + fi + + branch=$(git_branch) || continue + [ -z "$branch" ] && continue + + # + # Get user + # + user=`ls -dl "$repo/.git" |awk '{print $3}'` + + cmd="" + + # + # Check ssh agent sh file + # + agent=$(eval realpath ~$user/.ssh/agent.sh) + [ -r "$agent" ] && cmd=". $agent && " + #[ -r "$agent" ] && echo "Found agent $agent" + + # + # Pull + # + echo ">Pull $name branch=$branch user=$user path=$repo" + + cmd="${cmd}GIT_SSH=$GIT_SSH cd \"$repo\" && git pull" + [ "$CLEANUP" = "1" ] && cmd="$cmd && git gc" + + if [ 1 = 1 ] && ! git_conn_check; then + echo + elif [ "$USER" = "$user" ]; then + sh -c "$cmd" + elif [ "$USER" = "root" ]; then + su $user -c "$cmd" + else + echo "Wrong user current user=$USER repo user=$user" + false + fi + ret=$? + + # + # *-install + # + if [ "$ret" != "0" ]; then + ex=$ret && echo ">err $ret" + + else + cmd_root="true" + # NB 23.04.18: TODEL # NB 24.07.18 [ "$(git config --get remote.origin.url)" = "git@git.nbdom.net:root.git" ] && git remote set-url origin git.nbdom.net:/home/git/root.git # NB 24.07.18 [ "$(git config --get remote.origin.url)" = "git@git.nbdom.net:nbdom.git" ] && git remote set-url origin git.nbdom.net:/home/git/nbdom.git # NB 24.07.18 [ "$(git config --get remote.origin.url)" = "git@git.nbdom.net:pi.git" ] && git remote set-url origin git.nbdom.net:/home/git/pi.git - [ -x bin/$name-install ] && cmd_root="$cmd_root && echo ' >$name-install' && bin/$name-install|sed 's/^/ /'" - if [ "$cmd_root" != "true" ]; then - eval "$cmd_root" - ex=$? - [ "$ex" -gt "0" ] && echo ">err" - fi + [ -x bin/$name-install ] && cmd_root="$cmd_root && echo ' >$name-install' && bin/$name-install|sed 's/^/ /'" + if [ "$cmd_root" != "true" ]; then + eval "$cmd_root" + ex=$? + [ "$ex" -gt "0" ] && echo ">err" + fi - fi + fi - echo - nb_api_post_host_info "key=${NAME}.$repo" "val=name:$name branch:$branch" -# NB 06.02.18 key="repo.$name.$branch" -# NB 06.02.18 nb_api_post_host_info "key=${key}.path" "val=$repo" -# NB 06.02.18 nb_api_post_host_info "key=${key}.exit_code" "val=$ex" + echo + ( + nb_api_post_host_info "${NAME}.$repo" "name:$name branch:$branch" + # NB 06.02.18 key="repo.$name.$branch" + # NB 06.02.18 nb_api_post_host_info "${key}.path" "$repo" + # NB 06.02.18 nb_api_post_host_info "${key}.exit_code" "$ex" + ) 2>/dev/null done ##################################################################### # Update infos # -rm -f /tmp/$NAME.infos /tmp/$NAME.check -find /run/$NAME-nb_post_sys_infos.lock -mmin +60 -exec rm {} \; >/dev/null 2>&1 -if [ ! -e /run/$NAME-nb_post_sys_infos.lock ]; then - touch /run/$NAME-nb_post_sys_infos.lock - nb_post_sys_infos +rm -f /tmp/$NAME.infos /tmp/$NAME.check /run/$NAME-nb_post_sys_infos.lock +find /run/$NAME-nb_api_post_sys_infos.lock -mmin +60 -exec rm {} \; >/dev/null 2>&1 +if [ ! -e /run/$NAME-nb_api_post_sys_infos.lock ]; then + touch /run/$NAME-nb_api_post_sys_infos.lock + nb_api_post_sys_infos 2>/dev/null fi ##################################################################### @@ -156,8 +139,8 @@ fi # rm -f "$TMP" if [ "$ex" = "0" ]; then - printf ">OK\n" + printf ">OK\n" else - printf ">ERR\n" + printf ">ERR\n" fi exit $ex diff --git a/etc/profile.d/ldap.sh b/etc/profile.d/ldap.sh index f4f99cab..bf391fa1 100644 --- a/etc/profile.d/ldap.sh +++ b/etc/profile.d/ldap.sh @@ -160,6 +160,10 @@ ldap_backup() { return 1 fi + ( + echo "ldap_backup: $*" + set + ) 2>&1 | mail -s ldap_backup root slapcat -n 0 -l $dir/$preff-config.ldif slapcat -n 1 -l $dir/$preff-data.ldif gzip $dir/$preff-*.ldif @@ -168,4 +172,4 @@ ldap_backup() { find $dir/ -type f -mtime +$keep_days -delete fi - } +} diff --git a/etc/profile.d/nb.sh b/etc/profile.d/nb.sh index d2f12fba..c01d7b72 100644 --- a/etc/profile.d/nb.sh +++ b/etc/profile.d/nb.sh @@ -92,37 +92,3 @@ nb_api() { fi } -nb_api_post() { - local usage="Usage: nb_api_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" - #eval "$curl" #&& echo "$table: $rowCount: $args" - ) -} - -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_post host_info "host=$host" "$@" -} - -nb_post_sys_infos() { - # To be compatible with other sed that does not support \t - sys_infos | tr '\t' '!' | sed -e 's/"/\\"/g' -e 's/^/key="sys./' -e 's/!/" val="/' -e 's/$/"/' | while read -r i; do - eval nb_api_post_host_info $i - done -} diff --git a/lib/sh/nb-update.sh b/lib/sh/nb-update.sh new file mode 100644 index 00000000..3bd79771 --- /dev/null +++ b/lib/sh/nb-update.sh @@ -0,0 +1,36 @@ +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