forked from checkpoint-restore/p.haul
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathp.haul-ssh
executable file
·150 lines (132 loc) · 3.8 KB
/
p.haul-ssh
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
139
140
141
142
143
144
145
146
147
148
149
150
#!/bin/bash
set -e
# This script is a p.haul wrapper that allows to migrate process
# using ssh tunnel for transferring data and without having to
# start p.haul-service in advance. Script use p.haul-wrap helper script
# to establish required connections and specify these connections via
# command line arguments.
#
# This script performs folowing actions:
# 1) Launch p.haul-service on the remote and bind it to localhost:REM_PORT
# 2) Parse cmdline options to extract ssh* opts and change
# p.haul opts appropriately, so p.haul will connect to
# localhost:LOC_PORT, that will be followed to the remote
# through the ssh tunnel.
# 3) Establish ssh tunnel localhost:LOC_PORT->SSH->localhost:REM_PORT.
# 4) Launch p.haul to start migration.
TUNNEL_FLAGS="-o ExitOnForwardFailure=yes -fN "
SERVER_FLAGS="-ttfM "
LOC_PORT=54321
REM_PORT=12345
PORT=22
USER="$(whoami)"
REMOTE=""
LOG="/tmp/phs.log"
PH_EXEC="p.haul"
PH_WRAP_EXEC="p.haul-wrap"
PH_WRAP_OPTS=()
PHS_EXEC="p.haul-service"
PHS_WRAP_EXEC="p.haul-wrap"
PHS_WRAP_OPTS=()
usage=\
"SSH wrapper for p.haul\n\
Usage: p.haul-ssh [SSH_OPTIONS] [PHAUL_WRAP_OPTIONS]
\n\
SSH options:\n\
--ssh-compression Use compression in ssh tunnel\n\
--ssh-username Remote username (current user by default)\n\
--ssh-loc-port Local port to use for forwarding (${LOC_PORT} by default)\n\
--ssh-port SSH port (${PORT} by default)\n\
--ssh-log Logfile for service log\n\
--ssh-ph-exec Path to p.haul executable\n\
--ssh-ph-wrap-exec Path to p.haul-wrap executable\n\
--ssh-phs-exec Path to p.haul-service executable on the remote machine\n\
--ssh-phs-wrap-exec Path to p.haul-wrap executable on the remote machine\n\
--help Print this messange\n\
\n\
See p.haul-wrap --help and p.haul --help for PHAUL_WRAP_OPTIONS\n"
while [ "$1" != "" ]; do
case $1 in
"--ssh-compression")
SERVER_FLAGS="${SERVER_FLAGS} -C"
TUNNEL_FLAGS="${TUNNEL_FLAGS} -C"
;;
"--ssh-username")
shift
USER=$1
;;
"--ssh-loc-port")
shift
LOC_PORT=$1
;;
"--port")
# Extract --port option(if present).
# --port LOC_PORT is added below.
shift
REM_PORT=$1
;;
"--ssh-ph-exec")
shift
PH_EXEC=$1
;;
"--ssh-ph-wrap-exec")
shift
PH_WRAP_EXEC=$1
;;
"--ssh-phs-exec")
shift
PHS_EXEC=$1
;;
"--ssh-phs-wrap-exec")
shift
PHS_WRAP_EXEC=$1
;;
"--ssh-port")
shift
PORT=$1
;;
"--ssh-log")
shift
LOG=$1
;;
"--help")
printf "${usage}"
exit 0
;;
*)
PH_WRAP_OPTS+=($1)
;;
esac
shift
done
# Replace p.haul positional "to" option with 127.0.0.1
REMOTE=${PH_WRAP_OPTS[0]}
PH_WRAP_OPTS[0]="127.0.0.1"
# Tell p.haul to connect to the local port
PH_WRAP_OPTS+=("--port ${LOC_PORT}")
# Specify path to p.haul executable
PH_WRAP_OPTS+=("--path ${PH_EXEC}")
# Tell p.haul-service to bind on localhost, so it is not directly accesible
# from the outer web.
PHS_OPTS+=("--bind-port ${REM_PORT}")
PHS_OPTS+=("--bind-addr \"127.0.0.1\"")
# Specify path to p.haul-service executable
PHS_WRAP_OPTS+=("--path ${PHS_EXEC}")
# Use ssh multiplexing to speedup establishing additional ssh sessions and
# to get control over all of them.
CTL_SK="~/ph_ssh_${USER}_${REMOTE}_${PORT}"
SSH_BASE="ssh -p ${PORT} ${USER}@${REMOTE} -S ${CTL_SK}"
# Trap EXIT to stop ssh connection(i.e. all ssh sessions) on both normal
# and error(see "set -e" above) exits.
trap '${SSH_BASE} -S ${CTL_SK} -O exit &> /dev/null' EXIT
echo "Start p.haul-service"
${SSH_BASE} ${SERVER_FLAGS} ${PHS_WRAP_EXEC} service ${PHS_WRAP_OPTS[@]} &> ${LOG} < /dev/null
echo "Done"
echo "Checking that p.haul-service is started"
${SSH_BASE} "while netstat -lnt | awk '\$4 ~ /:${REM_PORT}$/ {exit 1}'; do echo 'Waiting...'; sleep 1; done"
echo "Start ssh tunnel"
${SSH_BASE} ${TUNNEL_FLAGS} -L 127.0.0.1:${LOC_PORT}:127.0.0.1:${REM_PORT}
echo "Done"
echo "Launch p.haul"
${PH_WRAP_EXEC} client ${PH_WRAP_OPTS[@]}
echo "Done"