Commit a8dd8c1af4adaa2ade6cd04c80786057a7536e03

Thomas de Grivel 2023-02-18T01:52:24

wip ci

diff --git a/bin/shpkg_ci b/bin/shpkg_ci
index 61fcee1..e643e5b 100755
--- a/bin/shpkg_ci
+++ b/bin/shpkg_ci
@@ -10,13 +10,27 @@ set -e
 
 . "${SHPKG_DIR}/share/shpkg/shpkg.subr"
 
+if [ "x$1" = "x-qqq" ]; then
+    export SHPKG_VERBOSE=0
+    shift
+elif [ "x$1" = "x-qq" ]; then
+    export SHPKG_VERBOSE=1
+    shift
+elif [ "x$1" = "x-q" ]; then
+    export SHPKG_VERBOSE=2
+    shift
+fi
+
 shpkg_config
 
 verbose 2 shpkg_lock shpkg_ci.lock
 
 verbose 2 shpkg_upgrade
 
-if [ "x$1" = "x--upgrade" ]; then
+if [ "x$1" = "x--clean" ]; then
+    verbose 1 rm -rf "${SHPKG_CI_DIR}"
+    exit
+elif [ "x$1" = "x--upgrade" ]; then
     exit
 fi
 
@@ -72,6 +86,7 @@ EOF
 EOF
     } > "$1.html.tmp"
     mv "$1.html.tmp" "$1.html"
+    verbose 2 shpkg_ci_changed_file "$1.html"
 }
 
 logs2html () {
@@ -87,6 +102,7 @@ logs2html () {
 }
 
 main () {
+    echo -n > "${SHPKG_CI_CHANGED_FILES}"
     verbose 2 shpkg_ci_status "shpkg_ci.status" running
     for SHPKG_REPO; do
         SHPKG_REPO_DIR="$(shpkg_dir "${SHPKG_REPO}")"
@@ -115,14 +131,11 @@ main () {
             # run shpkg upgrade
             ( . "${SHPKG_REPO_BUILD_DIR}/etc/shpkg/profile"
               verbose 2 shpkg_ci_status "${SHPKG_REPO}.upgrade.status" running
-              if verbose 2 shpkg upgrade "${SHPKG_REPO}"; then
+              verbose -n 2 shpkg upgrade "${SHPKG_REPO}"
+              if shpkg upgrade "${SHPKG_REPO}"; then
                   verbose 2 shpkg_ci_status "${SHPKG_REPO}.upgrade.status" ok
               else
-                  verbose 2 shpkg_ci_status "${SHPKG_REPO}.upgrade.status" ko
-                  verbose 2 shpkg_ci_status "${SHPKG_REPO}.ci.status" ko
-                  verbose 2 shpkg_ci_status "${SHPKG_REPO}.ci.branch_${SHPKG_REPO_GIT_BRANCH}.status" ko
-                  verbose 2 shpkg_ci_status "${SHPKG_REPO}.ci.commit_${SHPKG_REPO_HASH}.status" ko
-                  verbose 2 shpkg_ci_status "shpkg_ci.status" ko
+                  exit 1
               fi; )
             if verbose 1 cp -p "${SHPKG_REPO_BUILD_DIR}/var/log/shpkg/tags/${SHPKG_REPO}".*.log "${SHPKG_CI_LOG_DIR}"; then
                 verbose 2 logs2html
@@ -132,14 +145,11 @@ main () {
         ( verbose 2 shpkg_bootstrap -f "${SHPKG_REPO_BUILD_DIR}"; )
         ( . "${SHPKG_REPO_BUILD_DIR}/etc/shpkg/profile"
           verbose 2 shpkg_ci_status "${SHPKG_REPO}.install.status" running
-          if verbose 2 shpkg install "${SHPKG_REPO}"; then
+          verbose -n 2 shpkg install "${SHPKG_REPO}"
+          if shpkg install "${SHPKG_REPO}"; then
               verbose 2 shpkg_ci_status "${SHPKG_REPO}.install.status" ok
           else
-              verbose 2 shpkg_ci_status "${SHPKG_REPO}.install.status" ko
-              verbose 2 shpkg_ci_status "${SHPKG_REPO}.ci.status" ko
-              verbose 2 shpkg_ci_status "${SHPKG_REPO}.ci.branch_${SHPKG_REPO_GIT_BRANCH}.status" ko
-              verbose 2 shpkg_ci_status "${SHPKG_REPO}.ci.commit_${SHPKG_REPO_HASH}.status" ko
-              verbose 2 shpkg_ci_status "shpkg_ci.status" ko
+              exit 1
           fi; )
         verbose 2 shpkg_ci_status "${SHPKG_REPO}.ci.status" ok
         verbose 2 shpkg_ci_status "${SHPKG_REPO}.ci.branch_${SHPKG_REPO_GIT_BRANCH}.status" ok
@@ -161,9 +171,4 @@ main () {
     verbose 2 shpkg_ci_status2png
 }
 
-if [ "x$1" = "x--clean" ]; then
-    rm -rf "${SHPKG_CI_DIR}"
-    exit
-fi
-
 shpkg_log shpkg_ci main "$@"
diff --git a/bin/shpkg_ci_mux b/bin/shpkg_ci_mux
index 20a67ef..1de6330 100755
--- a/bin/shpkg_ci_mux
+++ b/bin/shpkg_ci_mux
@@ -19,12 +19,23 @@ verbose 2 shpkg_lock shpkg_ci_mux.lock
 : ${SHPKG_CI_DIR:=${SHPKG_DIR}/ci}
 
 shpkg_ci_remote () {
-    verbose 1 ssh "$1" sh -c "\"shpkg/bin/shpkg_ci '$2' >/dev/null\""
     if [ "x$2" == "x--clean" ]; then
+        verbose 1 ssh "$1" shpkg/bin/shpkg_ci -qqq "$2"
         rm -rf "${SHPKG_CI_DIR}"
+    elif [ "x$2" != "x--upgrade" ]; then
+        verbose 1 ssh "$1" shpkg/bin/shpkg_ci -qqq "$2"
     else
-        if [ "x$2" != "x--upgrade" ]; then
-            verbose 1 rsync -a "$1":shpkg/ci/. "${SHPKG_CI_DIR}/"
+        ( verbose 1 ssh "$1" shpkg/bin/shpkg_ci -qqq "$2" &
+          { ssh "$1" tail -n 10000 -f "${SHPKG_CI_CHANGED_FILES}" |
+                while read F; do
+                    D="${SHPKG_CI_DIR}/$(dirname "$F")"
+                    mkdir -p "$D"
+                    verbose 1 rsync "$1":shpkg/ci/"$F" "$D" &
+                done
+          } &
+          wait %1
+          sleep 10
+          kill %2
         fi
     fi
 }
@@ -57,11 +68,9 @@ main () {
     shift
     if [ "x${SHPKG_REPO}" != "x--no-remote" ]; then
         for SHPKG_CI_MUX_HOST; do
-            shpkg_ci_remote "${SHPKG_CI_MUX_HOST}" "${SHPKG_REPO}" &
-        done
-        for SHPKG_CI_MUX_HOST; do
-            wait
+            verbose 2 shpkg_ci_remote "${SHPKG_CI_MUX_HOST}" "${SHPKG_REPO}" &
         done
+        wait
         if [ "x${SHPKG_REPO}" == "x--upgrade" ]; then
             verbose 2 shpkg_upgrade
             exit
@@ -104,26 +113,28 @@ main () {
     <div class="ci-header">
       <a href="./"><i class="fas fa-asterisk"></i> All logs</a>
     </div>
-    <h1>
-      <img src="${IMG}" class="status-${STATUS}"/>
-      ${TITLE}
-    </h1>
-    <ul class="list-group">
+    <p>
+      <h1>
+        <img src="${IMG}" class="status-${STATUS}"/>
+        ${TITLE}
+      </h1>
+      <ul class="list-group">
 EOF
                   for F in */*/"$I"; do
                       F_STATUS="$(cat "${SHPKG_CI_DIR}/status/$(echo "$F" | sed -e 's/[.]log$/.status/')" || echo unknown)"
                       F_IMG="$(echo "$F" | sed -e 's/[.]log$/.png/')"
                       cat <<EOF
-      <li class="list-group-item">
-        <a href="$F.html">
-          <img src="${F_IMG}" class="status-${F_STATUS}"/>
-          $F.html
-        </a>
-      </li>
+        <li class="list-group-item">
+          <a href="$F.html">
+            <img src="${F_IMG}" class="status-${F_STATUS}"/>
+            $F.html
+          </a>
+        </li>
 EOF
                   done
                   cat <<EOF
-    </ul>
+      </ul>
+    </p>
   </body>
 </html>
 EOF
diff --git a/share/shpkg/shpkg.subr b/share/shpkg/shpkg.subr
index f35c7f9..733744d 100644
--- a/share/shpkg/shpkg.subr
+++ b/share/shpkg/shpkg.subr
@@ -19,14 +19,22 @@ SHPKG_PREFIX="${SHPKG_DIR}"
 
 # Console output
 
+: ${SHPKG_VERBOSE:=3}
+
 SHPKG_COLOR_BLUE="$(printf '\33[0;34m')"
 SHPKG_COLOR_PURPLE="$(printf '\33[0;35m')"
 SHPKG_COLOR_RESET="$(printf '\33[0m\n')"
 
 verbose__puts () {
     if [ "x$1" = "x1" ]; then
+        if [ "$SHPKG_VERBOSE" -lt 2 ]; then
+            return
+        fi
         echo -n "${SHPKG_COLOR_BLUE}"
     elif [ "x$1" = "x2" ]; then
+        if [ "$SHPKG_VERBOSE" -lt 1 ]; then
+            return
+        fi
         echo -n "${SHPKG_COLOR_PURPLE}"
     else
         err 1 "bad verbose level"
@@ -49,10 +57,18 @@ verbose () {
     else
         verbose__puts "$@"
         shift
-        if [ "x${VERBOSE_KEEP_GOING}" = "xYes" ]; then
-            "$@" || true
+        if [ "${SHPKG_VERBOSE}" -lt 3 ]; then
+            if [ "x${VERBOSE_KEEP_GOING}" = "xYes" ]; then
+                "$@" >/dev/null || true
+            else
+                "$@" >/dev/null
+            fi
         else
-            "$@"
+            if [ "x${VERBOSE_KEEP_GOING}" = "xYes" ]; then
+                "$@" || true
+            else
+                "$@"
+            fi
         fi
     fi
 }
@@ -191,6 +207,7 @@ shpkg_config () {
     SHPKG_CI_BUILD_DIR="${SHPKG_CI_DIR}/build"
     SHPKG_CI_LOG_DIR="${SHPKG_CI_DIR}/log/${SHPKG_OS_}/${SHPKG_TARGET}"
     SHPKG_CI_STATUS_DIR="${SHPKG_CI_DIR}/status/${SHPKG_OS_}/${SHPKG_TARGET}"
+    SHPKG_CI_CHANGED_FILES="${SHPKG_CI_DIR}/changed_files"
     # Directories
     if ! [ -d "${SHPKG_CI_LOG_DIR}" ]; then
         verbose 1 mkdir -p "${SHPKG_CI_LOG_DIR}"
@@ -815,33 +832,54 @@ shpkg_upgrade_shpkg () {
 
 # CI
 
+cut_prefix () {
+    LEN=$(echo -n "$1" | wc -c | tr -d ' ')
+    if [ "x$(echo "$2" | cut -c 1-${LEN})" = "$1" ]; then
+        echo "$2" | cut -c $((${LEN} + 1))-
+    fi
+}
+        
+shpkg_ci_changed_file () {
+    if ! cut_prefix "${SHPKG_CI_DIR}" "$1"; then
+        err 1 "shpkg_ci_changed_file: file not in ci dir: $1"
+    fi
+    cut_prefix "${SHPKG_CI_DIR}" "$1" >> "${SHPKG_CI_CHANGED_FILES}"
+}
+
 # state machine :
 #  any -> running
 #  running -> ko, ok
 # additionaly status will be changed to ko if aborted while running
 shpkg_ci_status () {
-    if [ -f "${SHPKG_CI_STATUS_DIR}/$1" ]; then
-        SHPKG_CI_STATUS="$(cat "${SHPKG_CI_STATUS_DIR}/$1")"
+    SHPKG_CI_STATUS_FILE="${SHPKG_CI_STATUS_DIR}/$1"
+    if [ -f "${SHPKG_CI_STATUS_FILE}" ]; then
+        SHPKG_CI_STATUS="$(cat "${SHPKG_CI_STATUS_FILE}")"
     else
         SHPKG_CI_STATUS=unknown
     fi
     if [ "x$2" = "xrunning" ]; then
         verbose -n 2 "Status change: $1: ${SHPKG_CI_STATUS} -> $2"
-        verbose -n 1 "echo $2 > ${SHPKG_CI_STATUS_DIR}/$1"
-        echo "$2" > "${SHPKG_CI_STATUS_DIR}/$1"
+        verbose -n 1 "echo $2 > ${SHPKG_CI_STATUS_FILE}"
+        echo "$2" > "${SHPKG_CI_STATUS_FILE}"
+        verbose 2 shpkg_ci_changed_file "${SHPKG_CI_STATUS_FILE}"
         trap "shpkg_ci_status_trap $1" EXIT
     elif [ "x${SHPKG_CI_STATUS}" = "xrunning" ]; then
         verbose -n 2 "Status change: $1: ${SHPKG_CI_STATUS} -> $2"
-        verbose -n 1 "echo $2 > ${SHPKG_CI_STATUS_DIR}/$1"
-        echo "$2" > "${SHPKG_CI_STATUS_DIR}/$1"
+        verbose -n 1 "echo $2 > ${SHPKG_CI_STATUS_FILE}"
+        echo "$2" > "${SHPKG_CI_STATUS_FILE}"
+        verbose 2 shpkg_ci_changed_file "${SHPKG_CI_STATUS_FILE}"
     else
         verbose -n 2 "Status change blocked: $1: ${SHPKG_CI_STATUS} -> $2"
     fi
 }
 
 shpkg_ci_status_trap () {
-    if [ "x$(< "${SHPKG_CI_STATUS_DIR}/$1")" = "xrunning" ]; then
-        echo ko > "${SHPKG_CI_STATUS_DIR}/$1"
+    SHPKG_CI_STATUS_FILE="${SHPKG_CI_STATUS_DIR}/$1"
+    SHPKG_CI_STATUS="$(cat "${SHPKG_CI_STATUS_FILE}")"
+    if [ "x${SHPKG_CI_STATUS}" = "xrunning" ]; then
+        verbose -n 1 "echo ko > '${SHPKG_CI_STATUS_FILE}'"
+        echo ko > "${SHPKG_CI_STATUS_FILE}"
+        verbose -n 2 shpkg_ci_changed_file "${SHPKG_CI_STATUS_FILE}"
     fi
 }
 
@@ -852,6 +890,7 @@ shpkg_ci_status2png () {
         if ! [ -f "$I" ] ||
                 [ "$F" -nt "$I" ]; then
             verbose 1 cp "${SHPKG_DIR}/share/shpkg/img/status_${STATUS}.64.png" "$I"
+            verbose 2 shpkg_ci_changed_file "$I"
         fi
     done
 }