-
Notifications
You must be signed in to change notification settings - Fork 0
/
machine_neo4j
executable file
·74 lines (59 loc) · 1.67 KB
/
machine_neo4j
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
#!/bin/bash -e
if [ $# -lt 1 ]; then
echo "usage: $(basename $0) <machine> [<cmd>]"
exit 1
fi
machine=$1
cmd=${@:2}
local_port="7474"
remote_port="7474"
remote_backup_cmd="/neo4j-extension/neo4j-s3-backup.sh"
neo4j_container="wust_neo4j_1"
ssh_pid=""
cleanup() {
[ -n "$ssh_pid" ] && pkill -P "$ssh_pid"
}
exit_handler() {
echo "exiting!"
cleanup
exit $?
}
backup_neo4j() {
echo "backing up neo4j in container '$neo4j_container' '$machine' using '$remote_backup_cmd'"
(
eval "$(docker-machine env $machine)"
docker exec "$neo4j_container" "$remote_backup_cmd"
)
}
trap exit_handler SIGINT
trap exit_handler EXIT
machine_status=$(docker-machine status "$machine")
if [ "$machine_status" != "Running" ]; then
echo "machine '$machine' is not running"
exit 1
fi
if [ -z "$cmd" ]; then
backup_neo4j
else
local_port_on=$(netstat -lnt | awk "\$6 == \"LISTEN\" && \$4 ~ \":$local_port\"")
if [ -n "$local_port_on" ]; then
echo "local port '$local_port' already in use"
exit 1
fi
remote_port_on=$(docker-machine ssh "$machine" netstat -lnt | awk "\$6 == \"LISTEN\" && \$4 ~ \":$remote_port\"")
if [ -z "$remote_port_on" ]; then
echo "remote port '$remote_port' on machine '$machine' not listening"
exit 1
fi
backup_neo4j
read -p "Do you really want to execute the command '$cmd'? (y/n)" answer
if [ "$answer" != "y" ]; then
exit 1
fi
echo "forward remote port '$remote_port' to local port '$local_port'"
docker-machine ssh "$machine" -NL $local_port:localhost:$remote_port &
ssh_pid=$!
echo "executing command '$cmd'"
bash -c "$cmd"
fi
echo "done"