This repository has been archived by the owner on Dec 4, 2017. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
docksend.sh
executable file
·135 lines (116 loc) · 2.97 KB
/
docksend.sh
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
#!/bin/bash
if ! which rsync > /dev/null; then
echo "cannot use docksend without rsync installed!"
echo "make sure you have rsync installed and in your path"
exit 2
fi
# colorful echo only if verbose env var is set
print() {
if [ "$VERBOSE" ]; then
tput setaf 2
echo "$1"
tput sgr0
fi
}
printusage() {
echo "usage: ./docksend.sh [-v docker_volume] [-i ssh_identity_file] [user@]hostname docker_image [command]"
}
# parse arguments
while getopts ":i:v:d:w:" opt; do
case $opt in
i)
IDENTITY_FILE="$OPTARG";;
d)
REMOTE_DIR="$OPTARG";;
w)
WORKING_DIR="$OPTARG";;
p)
PULL_FIRST=true;;
v)
# split by colon into array
IFS=':' read -a DOCKER_VOLUME <<< "$OPTARG";;
:)
echo "option -$OPTARG requires an argument." >&2
exit 3;;
\?)
echo "invalid option: -$OPTARG" >&2
exit 4;;
esac
done
# remove (shift) option arguments until they are all gone
n=1
while [ $# -gt 0 ]; do
if [ $n -lt $OPTIND ]; then
let n=$n+1
shift
else
break
fi
done
# check non option parameters
if [ "$#" -lt 3 ]; then
printusage
exit 1
fi
SSH_HOSTNAME="$1"
DOCKER_IMAGE="$2"
DOCKER_COMMAND="$3"
DOCKER_ARGS="${*:3}"
if [ -z "${DOCKER_VOLUME+x}" ]; then
LOCAL_DIR="$(pwd)"
DOCKER_DIR="/root"
else
LOCAL_DIR="${DOCKER_VOLUME[0]}"
DOCKER_DIR="${DOCKER_VOLUME[1]}"
fi
if [ -z "${IDENTITY_FILE+x}" ]; then
SSH_ARGS=""
else
SSH_ARGS="-i $IDENTITY_FILE"
fi
# more verbose rsync output if user wants it
if [ "$VERBOSE" ]; then
RSYNC_FLAGS="-avz"
else
RSYNC_FLAGS="-az"
fi
# create tempdir for remote location if tempdir was not specified
if [ -z "${REMOTE_DIR+x}" ]; then
USED_TEMPDIR=true
REMOTE_DIR=$(ssh $SSH_ARGS "$SSH_HOSTNAME" "mktemp -d")
print "created tempdir $SSH_HOSTNAME:$REMOTE_DIR for syncing"
fi
if [ -z "${WORKING_DIR+x}" ]; then
WORKING_DIR="$REMOTE_DIR"
fi
# make sure we never have trailing slashes for rsync dirs
# because we append them later
LOCAL_DIR=$(echo "$LOCAL_DIR"|sed 's/\/$//g')
REMOTE_DIR=$(echo "$REMOTE_DIR"|sed 's/\/$//g')
# ensure tempdir gets always deleted
function deltempdir {
if $USED_TEMPDIR; then
ssh $SSH_ARGS "$SSH_HOSTNAME" "rm -r $REMOTE_DIR" > /dev/null
print "deleted tempdir $SSH_HOSTNAME:$REMOTE_DIR"
fi
}
# sync directory up to server
function syncup {
print "syncing $LOCAL_DIR up to $SSH_HOSTNAME:$REMOTE_DIR"
rsync "$RSYNC_FLAGS" --exclude='.git' -e "ssh $SSH_ARGS" "$LOCAL_DIR/" "$SSH_HOSTNAME:$REMOTE_DIR/"
}
function rundocker {
if [ "$PULL_FIRST" ]; then
ssh $SSH_ARGS "$SSH_HOSTNAME" "docker pull $DOCKER_IMAGE" > /dev/null
fi
ssh $SSH_ARGS "$SSH_HOSTNAME" "docker run --rm -w $WORKING_DIR -v $REMOTE_DIR:$DOCKER_DIR $DOCKER_IMAGE $DOCKER_COMMAND $DOCKER_ARGS"
}
# sync changes down
function syncdown {
print "syncing $SSH_HOSTNAME:$REMOTE_DIR down to $LOCAL_DIR"
rsync "$RSYNC_FLAGS" --exclude='.git' -e "ssh $SSH_ARGS" "$SSH_HOSTNAME:$REMOTE_DIR/" "$LOCAL_DIR/"
}
trap deltempdir EXIT
syncup
rundocker
syncdown