3 #########################################################################
4 # This program is free software: you can redistribute it and/or modify #
5 # it under the terms of the version 3 of the GNU General Public License #
6 # as published by the Free Software Foundation. #
8 # This program is distributed in the hope that it will be useful, but #
9 # WITHOUT ANY WARRANTY; without even the implied warranty of #
10 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU #
11 # General Public License for more details. #
13 # You should have received a copy of the GNU General Public License #
14 # along with this program. If not, see <http://www.gnu.org/licenses/>. #
16 # Written by and Copyright (C) Francois Fleuret #
17 # Contact <francois@fleuret.org> for comments & bug reports #
18 #########################################################################
23 ######################################################################
25 function check_remote_is_defined () {
26 if [[ "${REMOTE_HOST}" ]] && [[ "${REMOTE_DIR}" ]]
30 echo "@XREMOTE_HOST should come first." >&2
37 xremote.sh <executable>
39 This script takes a script as argument and executes it remotely in a
40 temporary directory on a ssh-accessible server.
42 It parses the script first to find embedded arguments which defines
43 the hostname on which to run it, the files to send, the files to
44 get back when the execution is done, and commands to execute before
45 running the executable remotely.
47 These arguments can appear multiple times, except the one that
48 specifies the remote host.
52 @XREMOTE_HOST: elk.fleuret.org
53 @XREMOTE_SEND: mnist.py
55 @XREMOTE_PRE: ln -s /home/fleuret/data/pytorch ./data
57 If no argument is provided to @XREMOTE_HOST, the environment
58 variable $XREMOTE_HOST is used instead
60 Contact <francois@fleuret.org> for comments.
66 function cleanup_remote_tmp () {
67 if [[ "${REMOTE_HOST}" ]] && [[ "${REMOTE_DIR}" ]]
69 echo "Clean up remote workdir."
70 ssh "${REMOTE_HOST}" "rm -rf \"${REMOTE_DIR}\""
74 ######################################################################
76 [[ -x "$1" ]] || (help && exit 1)
78 main="$(basename "$1")"
82 trap cleanup_remote_tmp EXIT
84 ######################################################################
89 if [[ "${line}" =~ '@XREMOTE' ]]
92 label=$(echo "${line}" | sed -e 's/^.*@XREMOTE_\([^:]*\):.*$/\1/')
93 value=$(echo "${line}" | sed -e 's/^.*@XREMOTE_[^:]*: *\(.*\)$/\1/')
98 check_remote_is_defined
99 ssh < /dev/null "${REMOTE_HOST}" "cd \"${REMOTE_DIR}\" && ${value}"
103 check_remote_is_defined
104 tar c "${value}" | ssh "${REMOTE_HOST}" "cd \"${REMOTE_DIR}\" && tar mx"
108 [[ "${REMOTE_DIR}" ]] && (exit "Remote host already defined!" >&2 && exit 1)
109 REMOTE_HOST="${value}"
110 [[ "${REMOTE_HOST}" ]] || REMOTE_HOST="${XREMOTE_HOST}"
111 [[ "${REMOTE_HOST}" ]] || (echo "No remote host specified." >&2 && exit 1)
112 REMOTE_DIR="$(ssh </dev/null "${REMOTE_HOST}" mktemp -d /tmp/xremote.from_"$(hostname)_$(date +%Y%m%d)".XXXXXX)"
119 ######################################################################
121 check_remote_is_defined
123 tar c "${main}" | ssh "${REMOTE_HOST}" "cd \"${REMOTE_DIR}\" && tar mx"
125 echo "----------------------------------------------------------------------"
126 echo "-- On ${REMOTE_HOST}"
127 echo "----------------------------------------------------------------------"
128 ssh </dev/null "${REMOTE_HOST}" "cd \"${REMOTE_DIR}\" && ./$(basename "${main}")"
129 echo "----------------------------------------------------------------------"
131 ######################################################################
133 # Disable globbing to keep wildcards for the remote side
139 if [[ "${line}" =~ '@XREMOTE' ]]
141 label=$(echo "${line}" | sed -e 's/^.*@XREMOTE_\([^:]*\):.*$/\1/')
142 value=$(echo "${line}" | sed -e 's/^.*@XREMOTE_[^:]*: *\(.*\)$/\1/')
145 check_remote_is_defined
146 ssh </dev/null "${REMOTE_HOST}" "cd \"${REMOTE_DIR}\" && tar c ${value}" | tar mxv
154 ######################################################################