Преглед на файлове

Refactored + updated documentation.

Tom Flucke преди 6 години
родител
ревизия
b792b7589a
променени са 1 файла, в които са добавени 40 реда и са изтрити 17 реда
  1. 40 17
      src/distributer/distribute.sh

+ 40 - 17
src/distributer/distribute.sh

@@ -25,10 +25,12 @@ help() {
     printf "    out_file_fmt: File name format to write the output of each\n" >&2
     printf "    command to (default: %s).\n\n" "$DEFAULT_OUT_FMT" >&2
     printf "Options:.\n" >&2
-    printf "    -h -?:        Print this help mesage and exit. \n" >&2
-    printf "    -c processor: If present, instead of directly executing cmd_list,\n" >&2
-    printf "    will launch a processor on each server and distribute cmds to the\n" >&2
-    printf "    processors.\n\n" >&2
+    printf "    -h -?:          Print this help mesage and exit. \n" >&2
+    printf "    -d destination: Move files to destination as space becomes\n" >&2
+    printf "      unavailable.  (Requires quota to be available)\n" >&2
+    printf "    -c processor:   If present, instead of directly executing\n\n" >&2
+    printf "      cmd_list, will launch a processor on each server and \n" >&2
+    printf "      distribute cmds to the processors.\n\n" >&2
     printf "All commands will be allocated to the first available server.\n" >&2
     printf "Each command must be valid on every server.\n" >&2
     printf "The output will be saved to a text file on the remote systems.\n" >&2
@@ -55,7 +57,9 @@ run_processor() {
         while read -t $TIMEOUT line < "$loop" > /dev/null && \
             cmd=$("$READER" $MAX_CMD_SIZE)
         do
-            cmd=$(printf "%s" "$cmd" | sed -e 's/^[[:space:]]*//' -e 's/[[:space:]]*$//')
+            cmd=$(printf "%s" "$cmd" |
+                sed -e 's/^[[:space:]]*//' \
+                    -e 's/[[:space:]]*$//')
             printf "%s\n" "$cmd" >&2
             printf "%s\n" "$cmd"
         done
@@ -73,7 +77,9 @@ run_cmd_list() {
         while read -t $TIMEOUT line < "$loop" > /dev/null && \
             cmd=$("$READER" $MAX_CMD_SIZE)
         do
-            cmd=$(printf "%s" "$cmd" | sed -e 's/^[[:space:]]*//' -e 's/[[:space:]]*$//')
+            cmd=$(printf "%s" "$cmd" | 
+                sed -e 's/^[[:space:]]*//' \
+                    -e 's/[[:space:]]*$//')
             printf "%s\n" "$cmd" >&2
             printf "printf \"%s: \" >> %s\n" "$cmd"  "$out_file"
             printf "%s >> %s\n" "$cmd"  "$out_file"
@@ -99,9 +105,7 @@ run_server() {
 }
 
 clean_up() {
-    for pid in $(pgrep -P $$); do
-        pkill -P $pid
-    done
+    pkill -P $$
     rm "$CMD_FEED"
     exit 2
 }
@@ -112,30 +116,48 @@ prepend() {
     done
 }
 
+promote_subordinate_master() {
+    server="$(head -1 -)"
+    until server="$(head -1 -)";\
+        ssh -oBatchMode=yes -oStrictHostKeyChecking=no "$server" \
+        "$(realpath $0) -t"; do
+        sleep 1
+    done
+}
+
+launch_children() {
+    for server in $(head -n$MAX_PROCS -); do
+        run_server "$server" < "$CMD_FEED" 2>&1 | prepend "$server" &
+    done
+    # promote_subordinate_master &
+    while pgrep -P $$ > /dev/null; do
+        wait
+    done
+}
+
 main() {
     mkfifo "$CMD_FEED"
     trap clean_up 2 15
+    trap "" 10
     cat "$CONF_LIST" | sed '/^[[:space:]]*$/d' | \
         xargs -d'\n' printf "%-$MAX_CMD_SIZE.${MAX_CMD_SIZE}s" > "$CMD_FEED" &
-    for server in $(head -n$MAX_PROCS "$SERVER_LIST"); do
-        run_server "$server" < "$CMD_FEED" 2>&1 | prepend "$server" &
-    done
-    for pid in $(pgrep -P $$); do
-        wait $pid
-    done
+    cat "$SERVER_LIST" | launch_children
     clean_up
     echo "All jobs finished!"
 }
 
 OPTIND=1
-while getopts "h?c:" opt; do
+while getopts "h?c:t" opt; do
     case "$opt" in
         h|\?)
             help 0
             ;;
         c)  readonly PROCESSOR="$OPTARG"
             ;;
-        *)  help 1
+        t)  launch_children
+            ;;
+        *)  echo "Unknown option '$opt'" >&2
+            help 1
             ;;
     esac
 done
@@ -146,4 +168,5 @@ fi
 readonly CONF_LIST="$1"
 readonly SERVER_LIST="$2"
 readonly OUT_FMT="${3:-$DEFAULT_OUT_FMT}"
+
 main