feat(user/bksum):Add wait option for timestamps
authorSteven Baltakatei Sandoval <baltakatei@gmail.com>
Tue, 20 Sep 2022 23:29:28 +0000 (23:29 +0000)
committerSteven Baltakatei Sandoval <baltakatei@gmail.com>
Tue, 20 Sep 2022 23:29:28 +0000 (23:29 +0000)
- Note: Equivalent to adding "--wait" to an `ots` command.
- feat(unitproc/bktemp-count_jobs):Assign version number to
    count_jobs() function.

unitproc/bktemp-count_jobs
user/bksum

index cf262341bdf1c4e1cf37aef7ed72235e8bcbd62e..9bde32d45519bde29e035e45ad6f4c242034aa36 100644 (file)
@@ -9,6 +9,14 @@ yell() { echo "$0: $*" >&2; } # print script path and all args to stderr
 die() { yell "$*"; exit 111; } # same as yell() but non-zero exit status
 try() { "$@" || die "cannot $*"; } # runs args as command, reports args if command fails
 count_jobs() {
+    # Desc: Count and return total number of jobs
+    # Usage: count_jobs
+    # Input: None.
+    # Output: stdout   integer number of jobs
+    # Depends: Bash 5.1.16
+    # Example: while [[$(count_jobs) -gt 0]]; do echo "Working..."; sleep 1; done;
+    # Version: 0.0.1
+    
     local job_count;
     job_count="$(jobs -r | wc -l | tr -d ' ' )";
     #yell "DEBUG:job_count:$job_count";
index ebf1f146ecfdb6f1c0c2e2b9016f302e4c807632..cb99cadb522a6d80bcfa07d64feab62f920f3135 100755 (executable)
@@ -5,7 +5,7 @@
 #        arg(s): file paths (IFS delimited)
 # Output: file containing sha256 hashes and file paths
 # Depends: bash v5.1.16, date (GNU Coreutils 8.32), gpg v2.2.27, ots v0.7.0
-# Version: 0.0.2
+# Version: 0.1.0
 
 declare -Ag appRollCall # Associative array for storing app status
 declare -Ag fileRollCall # Associative array for storing file status
@@ -65,6 +65,11 @@ showUsage() {
         -t, --timestamp
                 Timestamp with OpenTimestamps the checksum file
                 (and GnuPG signature if -s/--sign specified).
+        -T, --timestamp-wait
+                Same as '-t/--timestamp' except that
+                OpenTimestamps will run as a background process
+                until a calendar server returns a completed
+                timestamp.
         --
                 Indicate end of options.
 
@@ -272,6 +277,8 @@ processArgs() {
     #   pathDirOut1          Path to output directory.
     #   pathFileOut1         Path to output file.
     #   opFileOut1_overwrite Indicates whether file pathFileOut1 should be overwritten (ex: "true", "false").
+    #   opTs                 Indicates timestamp mode
+    #   opTsWait             Indicates timestamp mode with wait option
     #   arrPosArgs         Array of remaining positional argments
     # Depends:
     #   yell()           Displays messages to stderr.
@@ -319,7 +326,10 @@ processArgs() {
             -s | --sign) # sign with gpg
                 opSign="true"; vbm "DEBUG:Signing mode enabled.";;
             -t | --timestamp) # timestamp with ots
-                opTimestamp="true"; vbm "DEBUG:Timestamp mode enabled.";;
+                opTs="true"; vbm "DEBUG:Timestamp mode enabled.";;
+            -T | --timestamp-wait) # timestamp with ots with wait option
+                opTs="true"; vbm "DEBUG:Timestamp mode enabled.";
+                opTsWait="true"; vbm "DEBUG:Timestamp wait mode enabled.";;
             --) # End of all options. See [2].
                 shift;
                 for arg in "$@"; do
@@ -410,11 +420,26 @@ checkDepends() {
 
     checkapp date sha256sum;
     if [[ $opSign == "true" ]]; then checkapp gpg; fi;
-    if [[ $opTimestamp == "true" ]]; then checkapp ots; fi;
+    if [[ $opTs == "true" ]]; then checkapp ots; fi;
     
     # Return failure is displayMissing() reports apps missing.
     if ! displayMissing; then return 1; else return 0; fi;
 }; # Check dependencies
+count_jobs() {
+    # Desc: Count and return total number of jobs
+    # Usage: count_jobs
+    # Input: None.
+    # Output: stdout   integer number of jobs
+    # Depends: Bash 5.1.16
+    # Example: while [[$(count_jobs) -gt 0]]; do echo "Working..."; sleep 1; done;
+    # Version: 0.0.1
+    
+    local job_count;
+    job_count="$(jobs -r | wc -l | tr -d ' ' )";
+    #yell "DEBUG:job_count:$job_count";
+    if [[ -z $job_count ]]; then job_count="0"; fi;
+    echo "$job_count";
+}; # Return number of background jobs
 main() {
     # Input: pathFileOut1   option-specified output file path
     #        appRollCall    assoc-array for checkapp(), displayMissing()
@@ -458,17 +483,43 @@ main() {
     # Do optional work
     ## Sign checksum file.
     if [[ $opSign == "true" ]]; then
-        try gpg --detach-sign --armor --output "$pathSigOut" "$pathSumOut";
+        yell "STATUS:Signing checksum file...";
+        try gpg --detach-sign --armor --output "$pathSigOut" "$pathSumOut" && yell "STATUS:Checksum created.";
     fi;
     ## Timestamp checksum file.
-    if [[ $opTimestamp == "true" ]]; then
-        try ots s "$pathSumOut"; fi;
-
+    if [[ $opTs == "true" ]]; then
+        yell "STATUS:Timestamping checksum file...";
+        if [[ $opTsWait != "true" ]]; then
+            try ots s "$pathSumOut" &
+        elif [[ $opTsWait == "true" ]]; then
+            yell "NOTICE:Waiting for calendar server response in background. (This may take 8 to 24 hours)...";
+            yell "ADVICE:Do not close or suspend this terminal.";
+            try ots --wait s "$pathSumOut" &
+        fi;    
+    fi;
     ## Timestamp checksum signature file.
-    if [[ $opTimestamp == "true" && $opSign == "true" ]]; then
-        try ots s "$pathSigOut";
+    if [[ $opTs == "true" && $opSign == "true" ]]; then
+        yell "STATUS:Timestamping signature file...";
+        if [[ $opTsWait != "true" ]]; then
+            try ots s "$pathSigOut" && yell "STATUS:Timestamp of checksum signature file created.";
+        elif [[ $opTsWait == "true" ]]; then
+            yell "NOTICE:Waiting for calendar server response in background. (This may take 8 to 24 hours)...";
+            yell "ADVICE:Do not close or suspend this terminal.";
+            try ots --wait s "$pathSigOut" &
+        fi;
     fi;
 
+    ## Wait until background jobs (if any) completed
+    for (( n = 1; "$(count_jobs)" > 0; n++ )); do
+        if ! [[ $((n % 60)) -eq 0 ]]; then
+            printf ".";
+        else
+            printf ".%05ds passed.\n" "$SECONDS";
+        fi;
+        sleep 60;
+    done;
+    
+    yell "Done.";
     return 0;
 }; # main program