-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path,
executable file
·138 lines (125 loc) · 3.42 KB
/
,
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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
#!/bin/sh
# check if is sourced
if [ "$0" = "," ]; then
if ! which , >/dev/null; then
echo 'remote-exec: File , not found in PATH or is hidden' >&2
return
fi
if [ "$#" -eq 1 ]; then
if SOURCED=MOUNT command , "$1"; then
cd "$1"
echo 'remote-exec: Mount succeed, current pwd:' >&2
pwd >&2
fi
elif [ "$#" -ge 2 ]; then
echo 'remote-exec: Too many arguments' >&2
return
else
OWD="$PWD"
cd /
if RMT_RESULT="$(SOURCED=UMOUNT command , "$OWD")"; then
cd "$RMT_RESULT"
echo 'remote-exec: Unmount succeed, current pwd:' >&2
pwd >&2
else
cd "$OWD"
fi
fi
unset RMT_RESULT
return
fi
set -e
if [ -n "$SOURCED" ]; then
cd "$1"
fi
if [ ! -t 0 ] || [ ! -t 1 ] || [ ! -t 2 ]; then
echo 'remote-exec: WHAT ARE YOU DOING??? YOU SHOULD NEVER REDIRECT , OR ,, !!!' >&2
exit 114
fi
REL=""
WD="$PWD"
while true; do
OWD="$PWD"
if [ -f .rmt-config ]; then
. "$PWD/.rmt-config"
break
else
REL="$(basename "$OWD")/$REL"
cd ..
if [ "$PWD" = "$OWD" ]; then
echo 'remote-exec: .rmt-config not found' >&2
exit 3
fi
fi
done
OWD="$(realpath "$OWD")"
set +e
if [ -n "$RMT_LOCALHOST" ]; then
echo 'remote-exec: WHAT ARE YOU DOING??? YOU SHOULD USE ,, INSTEAD OF , !!!' >&2
exit 114
fi
set -e
cd "$WD"
if [ -z "$RMT_HOST" ]; then
echo 'remote-exec: RMT_HOST not found' >&2
exit 2
fi
if [ -z "$RMT_SSH" ]; then
RMT_SSH="ssh -Y -t"
fi
if [ -z "$RMT_SSHFS" ]; then
RMT_SSHFS="sshfs"
fi
if [ -z "$RMT_RDIR" ]; then
RMT_RDIR=".rmt"
fi
# check if already mounted
RD="$RMT_RDIR/$(printf '%s' "$OWD" | sha1sum | cut -d ' ' -f 1)"
if mount | grep -qF " on $OWD type fuse.sshfs"; then
if [ "$SOURCED" = "MOUNT" ]; then
echo "remote-exec: WARNING: $OWD is already mounted" >&2
exit 4
elif [ "$SOURCED" = "UMOUNT" ]; then
cd /
echo "remote-exec: Unmounting $OWD" >&2
umount "$OWD"
echo "$OWD/.."
exit 0
fi
else
if [ "$SOURCED" = "UMOUNT" ]; then
echo "remote-exec: WARNING: $OWD is already unmounted" >&2
exit 4
fi
echo "remote-exec: Creating remote dir $RD" >&2
ssh "$RMT_HOST" mkdir -p "$RD"
echo "remote-exec: Mount remote dir to $OWD by sshfs" >&2
T="$(realpath "$(mktemp)")"
finish() {
rm -f "$T"
}
trap finish EXIT
cat "$OWD/.rmt-config" >"$T"
$RMT_SSHFS "$RMT_HOST:$RD" "$OWD"
echo "remote-exec: Restore $OWD/.rmt-config" >&2
echo "### WARNING: DO NOT EDIT THIS FILE ###" >"$OWD/.rmt-config"
if [ -z "$RMT_RSHELL" ]; then
printf "remote-exec: Detecting login shell ... " >&2
RMT_RSHELL="$(ssh "$RMT_HOST" echo '$SHELL')"
echo "$RMT_RSHELL" >&2
echo "RMT_RSHELL=$RMT_RSHELL" >>"$OWD/.rmt-config"
fi
cat "$T" >>"$OWD/.rmt-config"
if [ "$SOURCED" = "MOUNT" ]; then
exit 0
fi
echo 'remote-exec: WARNING: You may need to execute "cd ."' >&2
fi
CMD="/usr/bin/env -C $(/usr/bin/printf '%q' "$RD/$REL") $RMT_RENV"
if [ "$#" -eq 0 ]; then
CMD="$CMD $(/usr/bin/printf '%q' "$RMT_RSHELL")"
else
CMD="$CMD$(/usr/bin/printf ' %q' "$@")"
fi
echo "remote-exec: $RMT_SSH -Y $RMT_HOST /bin/sh -c $(/usr/bin/printf '%q' "$CMD")" >&2
exec $RMT_SSH "$RMT_HOST" /bin/sh -c "$(/usr/bin/printf '%q' "$CMD")"