-
Notifications
You must be signed in to change notification settings - Fork 39
/
rexe
executable file
·104 lines (91 loc) · 3.03 KB
/
rexe
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
#!/bin/bash
set -e
HOST=""
LOCAL_PATH=""
REMOTE_PATH=""
REXE_DIR="rexe"
CMD=""
DOWNLOAD="true"
EXCLUDE=()
EXCLUDE_DOWNLOAD=()
function handle_argument()
{
if [[ "$HOST" == "" ]]; then
case "$1" in
*:*)
HOST="${1%:*}"
REMOTE_PATH="${1#*:}"
;;
*)
HOST="$1"
;;
esac
return
fi
if [[ "$CMD" == "" ]]; then
CMD="$1"
else
CMD="$CMD $1"
fi
}
while [ "$#" -gt 0 ]; do
if [[ "$CMD" == "" ]]; then
case "$1" in
--path=*) LOCAL_PATH="${1#*=}"; shift 1;;
-p|--path) LOCAL_PATH="$2"; shift 2;;
--no-download) DOWNLOAD="false"; shift 1;;
--exclude=*) EXCLUDE+=("--exclude" "${1#*=}"); shift 1;;
--exclude) EXCLUDE+=("--exclude" "$2"); shift 2;;
--exclude-download=*) EXCLUDE_DOWNLOAD+=("--exclude" "${1#*=}"); shift 1;;
--exclude-download) EXCLUDE_DOWNLOAD+=("--exclude" "$2"); shift 2;;
-*) echo "unknown option: $1" >&2; exit 1;;
*) handle_argument "$1"; shift 1;;
esac
else
handle_argument "$1"
shift 1
fi
done
if [[ "$HOST" == "" ]]; then
echo "error: remote host was not specified." >&2
exit 1
fi
if [[ "$CMD" == "" ]]; then
echo "error: remote command was not sepcified." >&2
exit 1
fi
# fill in defaults
if [[ "$LOCAL_PATH" == "" ]]; then
LOCAL_PATH=$(pwd)
fi
# NOTE: the tmpfs for $XDG_RUNTIME_DIR may be too small (e.g. only 10% of the available RAM)
#if [[ "$REMOTE_PATH" == "" ]]; then
# REMOTE_PATH=$(ssh "$HOST" echo '$XDG_RUNTIME_DIR')
#fi
if [[ "$REMOTE_PATH" == "" ]]; then
REMOTE_PATH="/tmp"
REXE_DIR="rexe_$(ssh "$HOST" whoami)"
fi
if [[ ! -d "$LOCAL_PATH" ]]; then
echo "error: local path '$LOCAL_PATH' is does not exist or is not a directory." >&2
exit 1
fi
# create remote main directory for rexe with restricted permissions
echo "Creating remote directory '$REMOTE_PATH/$REXE_DIR'..."
ssh "$HOST" mkdir -m 0700 -p "$REMOTE_PATH/$REXE_DIR"
# change remote path into full path
_basename=$(basename "$LOCAL_PATH")
REMOTE_PATH="$REMOTE_PATH/$REXE_DIR/$_basename"
echo "Uploading local directory '$LOCAL_PATH' to remote directory '$REMOTE_PATH'..."
rsync -rlptD "$LOCAL_PATH/" "$HOST:$REMOTE_PATH/" -e ssh -zz --info=progress2 --delete ${EXCLUDE[@]}
echo "Executing remote command '$CMD'..."
# ignore errors of the ssh command to always run rsync afterwards (even on keyboard interrupt)
set +e
ssh -t "$HOST" "cd ${REMOTE_PATH@Q}; bash --login -c -- ${CMD@Q}"
set -e
if [[ "$DOWNLOAD" != "false" ]]; then
echo "Synchronizing remote directory '$REMOTE_PATH' into the local directory..."
# FIXME: EXCLUDE_DOWNLOAD does not work correctly for wildcards
echo rsync -rlptD "$HOST:$REMOTE_PATH/" "$LOCAL_PATH/" -e ssh -zz --info=progress2 --delete ${EXCLUDE[@]} ${EXCLUDE_DOWNLOAD[@]} -v
rsync -rlptD "$HOST:$REMOTE_PATH/" "$LOCAL_PATH/" -e ssh -zz --info=progress2 --delete ${EXCLUDE[@]} ${EXCLUDE_DOWNLOAD[@]} -v
fi