Now passes the arguments to the other side.
[xremote.git] / xremote.sh
index 9b753e0..cd6813c 100755 (executable)
@@ -18,7 +18,8 @@
 #########################################################################
 
 set -e
 #########################################################################
 
 set -e
-set -o pipefail
+
+# set -o pipefail
 
 ######################################################################
 
 
 ######################################################################
 
@@ -34,14 +35,14 @@ function check_remote_is_defined () {
 
 function help () {
     cat <<EOF
 
 function help () {
     cat <<EOF
-xremote.sh <executable>
+xremote.sh <script>
 
   This script takes a script as argument and executes it remotely in a
   temporary directory on a ssh-accessible server.
 
   It parses the script first to find embedded arguments which defines
 
   This script takes a script as argument and executes it remotely in a
   temporary directory on a ssh-accessible server.
 
   It parses the script first to find embedded arguments which defines
-  the hostname on which to run it, the files to send, the files to
-  get back when the execution is done, and commands to execute before
+  the hostname on which to run, the files to send, the files to get
+  back when the execution is over, and commands to execute before
   running the executable remotely.
 
   These arguments can appear multiple times, except the one that
   running the executable remotely.
 
   These arguments can appear multiple times, except the one that
@@ -50,11 +51,15 @@ xremote.sh <executable>
   Example:
 
     @XREMOTE_HOST: elk.fleuret.org
   Example:
 
     @XREMOTE_HOST: elk.fleuret.org
-    @XREMOTE_SEND: mnist.py
+    @XREMOTE_EXEC: python3
+    @XREMOTE_SEND: main.cf
     @XREMOTE_GET: *.dat
     @XREMOTE_PRE: ln -s /home/fleuret/data/pytorch ./data
 
     @XREMOTE_GET: *.dat
     @XREMOTE_PRE: ln -s /home/fleuret/data/pytorch ./data
 
- Contact <francois@fleuret.org> for comments.
+  If no argument is provided to @XREMOTE_HOST, the environment
+  variable $XREMOTE_HOST is used instead
+
+  Contact <francois@fleuret.org> for comments.
 
 EOF
     return 0
 
 EOF
     return 0
@@ -63,7 +68,7 @@ EOF
 function cleanup_remote_tmp () {
     if [[ "${REMOTE_HOST}" ]] && [[ "${REMOTE_DIR}" ]]
     then
 function cleanup_remote_tmp () {
     if [[ "${REMOTE_HOST}" ]] && [[ "${REMOTE_DIR}" ]]
     then
-        echo "Clean up remote workdir."
+        echo "xremote: Clean up remote workdir."
         ssh "${REMOTE_HOST}" "rm -rf \"${REMOTE_DIR}\""
     fi
 }
         ssh "${REMOTE_HOST}" "rm -rf \"${REMOTE_DIR}\""
     fi
 }
@@ -76,6 +81,8 @@ main="$(basename "$1")"
 
 cd "$(dirname "$1")"
 
 
 cd "$(dirname "$1")"
 
+shift
+
 trap cleanup_remote_tmp EXIT
 
 ######################################################################
 trap cleanup_remote_tmp EXIT
 
 ######################################################################
@@ -91,21 +98,31 @@ do
 
         case "${label}" in
 
 
         case "${label}" in
 
+            EXEC)
+                check_remote_is_defined
+                [[ "${REMOTE_EXEC}" ]] && (exit "Remote executable already defined!" >&2 && exit 1)
+                REMOTE_EXEC="${value}"
+                ;;
+
             PRE)
                 check_remote_is_defined
             PRE)
                 check_remote_is_defined
+                echo "xremote: ${value}"
                 ssh < /dev/null "${REMOTE_HOST}" "cd \"${REMOTE_DIR}\" && ${value}"
                 ;;
 
             SEND)
                 check_remote_is_defined
                 ssh < /dev/null "${REMOTE_HOST}" "cd \"${REMOTE_DIR}\" && ${value}"
                 ;;
 
             SEND)
                 check_remote_is_defined
-                tar c "${value}" | ssh "${REMOTE_HOST}" "cd \"${REMOTE_DIR}\" && tar mx"
+                echo "xremote: -- sending files --------------------------------------------"
+                tar ch ${value} | ssh "${REMOTE_HOST}" "cd \"${REMOTE_DIR}\" && tar mxv"
                 ;;
 
             HOST)
                 [[ "${REMOTE_DIR}" ]] && (exit "Remote host already defined!" >&2 && exit 1)
                 ;;
 
             HOST)
                 [[ "${REMOTE_DIR}" ]] && (exit "Remote host already defined!" >&2 && exit 1)
-                cleanup_remote_tmp
-                REMOTE_HOST=${value}
-                REMOTE_DIR="$(ssh </dev/null "${REMOTE_HOST}" mktemp -d /tmp/xremote.from_"$(hostname)_$(date +%Y%m%d)".XXXXXX)"
+                REMOTE_HOST="${value}"
+                [[ "${REMOTE_HOST}" ]] || REMOTE_HOST="${XREMOTE_HOST}"
+                [[ "${REMOTE_HOST}" ]] || (echo "xremote: No remote host specified." >&2 && exit 1)
+                REMOTE_DIR="$(ssh </dev/null "${REMOTE_HOST}" mktemp -d /tmp/xremote.from_"$(hostname)_$(date +%Y%m%d-%H%M%S)".XXXXXX)"
+                echo "xremote: target is ${REMOTE_HOST}"
                 ;;
         esac
     fi
                 ;;
         esac
     fi
@@ -118,16 +135,31 @@ check_remote_is_defined
 
 tar c "${main}" | ssh "${REMOTE_HOST}" "cd \"${REMOTE_DIR}\" && tar mx"
 
 
 tar c "${main}" | ssh "${REMOTE_HOST}" "cd \"${REMOTE_DIR}\" && tar mx"
 
-echo "----------------------------------------------------------------------"
-echo "-- On ${REMOTE_HOST}"
-echo "----------------------------------------------------------------------"
-ssh </dev/null "${REMOTE_HOST}" "cd \"${REMOTE_DIR}\" && ./$(basename "${main}")"
-echo "----------------------------------------------------------------------"
+echo "xremote: -- running the executable -----------------------------------"
+
+if [[ "${REMOTE_EXEC}" ]]
+then
+    REMOTE_COMMAND="${REMOTE_EXEC} ${main}"
+else
+    REMOTE_COMMAND="./${main}"
+fi
+
+######################################################################
+
+# I find this slightly ugly ...
+for s in "$@"
+do
+  quoted_args="${quoted_args} \"${s}\""
+done
+
+ssh </dev/null "${REMOTE_HOST}" "cd \"${REMOTE_DIR}\" && ${REMOTE_COMMAND} ${quoted_args}"
 
 ######################################################################
 
 # Disable globbing to keep wildcards for the remote side
 
 
 ######################################################################
 
 # Disable globbing to keep wildcards for the remote side
 
+echo "xremote: -- retrieving files -----------------------------------------"
+
 set -f
 
 while read line
 set -f
 
 while read line
@@ -139,7 +171,7 @@ do
         case "${label}" in
             GET)
                 check_remote_is_defined
         case "${label}" in
             GET)
                 check_remote_is_defined
-                ssh </dev/null "${REMOTE_HOST}" "cd \"${REMOTE_DIR}\" && tar c ${value}" | tar mxv
+                ssh </dev/null "${REMOTE_HOST}" "cd \"${REMOTE_DIR}\" && tar 2>/dev/null c ${value}" | tar mxv
                 ;;
         esac
     fi
                 ;;
         esac
     fi
@@ -147,4 +179,6 @@ done < "${main}"
 
 set +f
 
 
 set +f
 
+echo "xremote: -- finished -------------------------------------------------"
+
 ######################################################################
 ######################################################################