Repository navigation
Expand file tree
/
Copy pathdeploy.sh
More file actions
executable file
·200 lines (161 loc) · 4.79 KB
/
Copy pathdeploy.sh
File metadata and controls
executable file
·200 lines (161 loc) · 4.79 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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
#!/bin/bash
# ----------------
# methods
# ログ出力の先頭に付加するラベル
log_label="[DEPLOY] "
log()
{
echo "$log_label$1"
}
do_sync()
{
# ----------------
# Before Sync
if type before_sync 1>/dev/null 2>/dev/null; then
# detect before_sync method
echo -n "$log_label- before_sync -> Processing..."
before_sync
log " -> Done."
fi
# ----------------
# Sync
log "- sync -> Start with $DEP_COMMAND. This could take a while..."
# download defaults if ignore file isn't exists
if [ ${DEP_IGNORE_FILE:-isnil} = "isnil" -o ! -f "$DEP_IGNORE_FILE" ]; then
log "| Downloading default ignore file..."
sudo wget -O .depignore https://raw.githubusercontent.com/karappo/drone-deploy/master/.depignore
DEP_IGNORE_FILE=$PWD/.depignore
fi
log "- ignore file -> $DEP_IGNORE_FILE"
if [ "$DEP_COMMAND" = "rsync" ]; then
# ------ install -------
if [ `which ssh-askpass` ]; then
log "- ssh-askpass -> installed"
else
log "- ssh-askpass -> install"
sudo apt-get install ssh-askpass
fi
if [ "${DEP_PASSWORD:+isexists}" = "isexists" ]; then
if [ `which sshpass` ]; then
log "- sshpass -> installed"
else
log "- sshpass -> install"
sudo apt-get install sshpass
fi
fi
# ------ /install -------
opt_exclude=''
if [ -f $DEP_IGNORE_FILE ]; then
opt_exclude="--exclude-from=$DEP_IGNORE_FILE"
fi
if [ "${DEP_PASSWORD:+isexists}" = "isexists" ]; then
log 'rsync with password'
if sshpass -p $DEP_PASSWORD rsync -aIzhv --stats --delete -e ssh $opt_exclude . $DEP_USER@$DEP_HOST:$DEP_HOST_DIR; then
log "- sync -> done."
else
log "- sync -> [ERROR]"
exit 1
fi
else
log 'rsync without password'
if rsync -aIzhv --stats --delete -e ssh $opt_exclude . $DEP_USER@$DEP_HOST:$DEP_HOST_DIR; then
log "- sync -> done."
else
log "- sync -> [ERROR]"
exit 1
fi
fi
else
# ------ install -------
if [ `which lftp` ]; then
log "- lftp -> installed"
else
log "- lftp -> install"
sudo apt-get install lftp
fi
# ------ /install -------
opt_exclude=""
while read line; do
# TODO: allow commentout in the middle of line
if [ "${line:0:1}" = "/" ]; then
# /xxx/yyy -> xxx/yyy
opt_exclude="$opt_exclude -X ${line:1}"
elif [ "${line:0:1}" != "#" -a "$line" != "" ]; then
opt_exclude="$opt_exclude -X $line"
fi
done<$DEP_IGNORE_FILE
opt_setting=""
if [ "$DEP_FTPS" = "no" ]; then
log "- sync -> via FTP"
opt_setting="set ftp:ssl-allow off;"
else
# TODO: chanto FTPS ni natteruka kakunin
log "- sync -> via FTPS"
opt_setting="set ftp:ssl-auth TLS;set ftp:ssl-force true;set ftp:ssl-allow yes;set ftp:ssl-protect-list yes;set ftp:ssl-protect-data yes;set ftp:ssl-protect-fxp yes;"
fi
if lftp -u $DEP_USER,$DEP_PASSWORD -e "$opt_setting;pwd;mirror -evR --parallel=10 $opt_exclude ./ $DEP_HOST_DIR;exit" $DEP_HOST; then
log "- sync -> done."
else
log "- sync -> [ERROR]"
exit 1
fi
fi
# ----------------
# After Sync
if type after_sync 1>/dev/null 2>/dev/null; then
# detect after_sync method
echo -n "$log_label- after_sync -> Processing... "
after_sync
log " -> Done."
fi
}
# ----------------
# check parameters
ALL_PARAMS=(COMMAND FTPS HOST USER PASSWORD HOST_DIR INCLUDE_FILE IGNORE_FILE)
NECESSARY_PARAMS=(HOST USER HOST_DIR)
for param in ${NECESSARY_PARAMS[@]}; do
branch_param='DEP_'${DRONE_BRANCH^^}'_'$param
eval 'val=${'$branch_param'}'
if [ ! $val ]; then
log '- ERROR -> Not defined necessary parameter: '$branch_param
exit 1
fi
done
# ----------------
# casting all parameters
# e.g. DEP_COMMAND=${DEP_MASTER_COMMAND}
for param in ${ALL_PARAMS[@]}; do
branch_param='DEP_'${DRONE_BRANCH^^}'_'$param
eval 'val=${'$branch_param'}'
if [ $val ]; then
eval 'DEP_'$param'='$val
fi
done
# ----------------
# default value
if [ "${DEP_COMMAND:-isnil}" = "isnil" ]; then
DEP_COMMAND=lftp
fi
# ----------------
# main
if [ "$DEP_COMMAND" = "rsync" -a "${DEP_HOST_DIR:0:1}" != "/" ]; then
log "- ERROR -> DEP_HOST_DIR must be absolute path: $DEP_HOST_DIR"
exit 1
fi
# include file
# from web
if [ "${DEP_INCLUDE_FILE:+isexists}" = "isexists" ]; then
if [ "${DEP_INCLUDE_FILE:0:7}" = "http://" -o "${DEP_INCLUDE_FILE:0:8}" = "https://" ]; then
log "| Downloading include file..."
sudo wget -O .depinc.sh $DEP_INCLUDE_FILE
DEP_INCLUDE_FILE=$PWD/.depinc.sh
fi
fi
if [ "${DEP_INCLUDE_FILE:-isnil}" = "isnil" -o ! -f "$DEP_INCLUDE_FILE" ]; then
log "- include file -> Detect failed..."
else
log "- include file -> Detect : $DEP_INCLUDE_FILE"
source $DEP_INCLUDE_FILE
fi
do_sync
exit 0