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 #########################################################################
24 ######################################################################
26 function check_remote_is_defined () {
27 if [[ "${REMOTE_HOST}" ]] && [[ "${REMOTE_DIR}" ]]
31 echo "@XREMOTE_HOST should come first." >&2
38 xremote.sh [--help] [-h <remote_host>] [-d <remote_dir>] [-i] <script> [script arguments]
40 This script takes a script as argument and executes it remotely in a
41 temporary directory on a ssh-accessible server.
43 It parses the script first to find embedded arguments which define
44 the hostname on which to run, the files to send, the files to get
45 back when the execution is over, and commands to execute before
46 running the executable remotely.
48 These arguments can appear multiple times, except the one that
49 specifies the remote host.
53 @XREMOTE_HOST: elk.fleuret.org
54 @XREMOTE_EXEC: python3
55 @XREMOTE_SEND: main.cf
57 @XREMOTE_PRE: ln -s /home/fleuret/data/pytorch ./data
59 If a file with the same name as the script with the .xremote
60 extension appended to it exists, arguments will be read from it by
63 If the -h option is provided @XREMOTE_HOST is ignored.
65 If the -d option is provided, the provided directory is used and
66 kept, instead of a temporary one
68 If the -i option is provided, all the files are installed and
69 scripts run in the specified directory on the remote host, but the
70 main executable and post-run commands are ignored
72 If no argument is provided to @XREMOTE_HOST, and the -h option is
73 not specified, the environment variable \$XREMOTE_HOST is used
76 Contact <francois@fleuret.org> for comments.
82 function cleanup_remote_tmp () {
83 if [[ "${REMOTE_HOST}" ]] && [[ "${REMOTE_DIR}" ]]
87 echo "xremote: Keeping remote workdir."
89 echo "xremote: Cleaning up temporary remote workdir."
90 ssh "${REMOTE_HOST}" rm -rf "${REMOTE_DIR}"
95 ######################################################################
97 while [[ "$1" =~ ^- ]]
104 [[ ${ARG_HOST} ]] || (echo "xremote: Hostname missing." && exit 1)
105 echo "xremote: remote forced to ${ARG_HOST}"
111 [[ ${ARG_DIR} ]] || (echo "xremote: Directory missing." && exit 1)
112 echo "xremote: remote dir set to ${ARG_DIR}"
117 echo "xremote: no run"
126 echo "xremote: Unknown option $1"
133 ######################################################################
135 [[ "$1" ]] || (echo "xremote: Script name missing" && exit 1)
137 [[ -a "$1" ]] || (help && echo >&2 "xremote: Cannot find script \`$1'" && exit 1)
141 main="$(basename "$1")"
142 main_config="${main}.xremote"
144 if [[ -f "${main_config}" ]]
146 echo "xremote: found ${main_config}"
148 main_config="${main}"
153 trap cleanup_remote_tmp EXIT
155 ######################################################################
160 if [[ "${line}" =~ '@XREMOTE' ]]
163 label=$(echo "${line}" | sed -e 's/^.*@XREMOTE_\([^:]*\):.*$/\1/')
164 value=$(echo "${line}" | sed -e 's/^.*@XREMOTE_[^:]*: *\(.*\)$/\1/')
169 check_remote_is_defined
170 [[ "${REMOTE_EXEC}" ]] && (exit "Remote executable already defined!" >&2 && exit 1)
171 REMOTE_EXEC="${value}"
175 check_remote_is_defined
176 echo "xremote: ${value}"
177 ssh < /dev/null "${REMOTE_HOST}" "cd \"${REMOTE_DIR}\" && ${value}"
181 check_remote_is_defined
182 echo "xremote: -- sending files --------------------------------------------"
183 tar ch ${value} | ssh "${REMOTE_HOST}" "cd \"${REMOTE_DIR}\" && tar mxv"
187 [[ "${REMOTE_DIR}" ]] && (exit "Remote host already defined!" >&2 && exit 1)
188 REMOTE_HOST="${ARG_HOST}" # Host given in argument has priority
189 [[ "${REMOTE_HOST}" ]] || REMOTE_HOST="${value}"
190 [[ "${REMOTE_HOST}" ]] || REMOTE_HOST="${XREMOTE_HOST}"
191 [[ "${REMOTE_HOST}" ]] || (echo "xremote: No remote host specified." >&2 && exit 1)
192 if [[ "${ARG_DIR}" ]]
194 ssh </dev/null "${REMOTE_HOST}" "mkdir -p \"${ARG_DIR}\""
195 REMOTE_DIR="${ARG_DIR}"
197 REMOTE_DIR="$(ssh </dev/null "${REMOTE_HOST}" mktemp -d /tmp/xremote_\$\(whoami\)_from_"$(hostname)_$(date +%Y%m%d_%H%M%S)".XXXXXX)"
199 echo "xremote: target is ${REMOTE_HOST}"
204 done < "${main_config}"
206 ######################################################################
208 check_remote_is_defined
210 tar c "${main}" | ssh "${REMOTE_HOST}" "cd \"${REMOTE_DIR}\" && tar mx"
214 echo "xremote: everything has been set up in ${REMOTE_HOST}:${ARG_DIR}"
218 echo "xremote: -- running the executable -----------------------------------"
220 if [[ "${REMOTE_EXEC}" ]]
222 REMOTE_COMMAND="${REMOTE_EXEC} ${main}"
224 REMOTE_COMMAND="./${main}"
227 ######################################################################
229 # I find this slightly ugly ...
233 quoted_args="${quoted_args} \"${s}\""
236 ssh </dev/null "${REMOTE_HOST}" "cd \"${REMOTE_DIR}\" && ${REMOTE_COMMAND} ${quoted_args}"
238 ######################################################################
240 # Disable globbing to keep wildcards for the remote side
242 echo "xremote: -- retrieving files -----------------------------------------"
248 if [[ "${line}" =~ '@XREMOTE' ]]
250 label=$(echo "${line}" | sed -e 's/^.*@XREMOTE_\([^:]*\):.*$/\1/')
251 value=$(echo "${line}" | sed -e 's/^.*@XREMOTE_[^:]*: *\(.*\)$/\1/')
254 check_remote_is_defined
255 echo "xremote: ${value}"
256 ssh < /dev/null "${REMOTE_HOST}" "cd \"${REMOTE_DIR}\" && ${value}"
260 check_remote_is_defined
261 ssh </dev/null "${REMOTE_HOST}" "cd \"${REMOTE_DIR}\" && tar 2>/dev/null c ${value}" | tar mxv
266 done < "${main_config}"
270 echo "xremote: -- finished -------------------------------------------------"
272 ######################################################################