-
Notifications
You must be signed in to change notification settings - Fork 0
/
_remote_deploy
executable file
·65 lines (50 loc) · 2.07 KB
/
_remote_deploy
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
#!/bin/bash
# This script is for copying dir of selected container to the remote server
# and run ./deploy_$container script remotely
#
# How to use:
# - for each deploy_$container script run 'ln -s _remote_deploy deploy_$container_remotely'
if [ -z "$1" ]; then
echo " " Runs Docker commands remotely.
echo " " Usage:
echo " " $0 [ -i private_key_path ] [user@]hostname.domain
echo
exit 255
fi
# SERVICE=$(echo $0 | grep -oE "deploy_[^_]+" | grep -oE "[^_]+$" )
SERVICE=$2
export container_name=$SERVICE;
# make unique socket file for ssh master connection (see https://en.wikibooks.org/wiki/OpenSSH/Cookbook/Multiplexing)
SSH_SOCK=/tmp/$RANDOM.sock
# dir will be copied to unique dir at server, for example /tmp/worker.234235
TMPDIR=/tmp/$SERVICE._deploy
quoted_args="$(printf " %q" "$@")"
# run prepare script (file is named ./deploy_project_remotely_prepare)
if [ -f "$0"_prepare ]; then
"$0"_prepare
fi
if [ "$4" ]; then
SSH_KEY="$1 $3"
SSH_HOST="$4"
else
SSH_HOST="$1"
fi
SSH_ARGS="$SSH_KEY $SSH_HOST"
# make ssh master connection socket. this line can request a password (but only 1 time per this script)
ssh -S $SSH_SOCK -M -N -f $SSH_ARGS
function finish {
# close ssh master connection
ssh -O exit -S $SSH_SOCK $SSH_ARGS
}
# close master connection and remove socket on script end or failure
trap finish EXIT
# if dir exist, copy contents to a remote server
if [ -d $SERVICE ]; then
#tar cvf - $SERVICE | ssh -S $SSH_SOCK $SSH_ARGS "mkdir -p $TMPDIR && cd $TMPDIR && tar xvf -"
rsync -rv -l -e "ssh -S $SSH_SOCK $SSH_KEY" --delete $SERVICE $SSH_HOST:$TMPDIR
fi
# runs ./deploy_$container script commands remotely
# subscribe to logs
# run bash console inside container (for debug)
# last 2 steps is temporary and used for manual debug. should be removed in future (before automation in CI)
ssh -S $SSH_SOCK -t $SSH_ARGS "cd $TMPDIR; container_name=$SERVICE; $(cat ../node_modules/@extremeprog-com/deploy-container/deploy_project) || exit; echo Streaming log...; (docker logs -f \$container_name & ); docker exec -it \$container_name bash || sleep infinity"