forked from khs1994-docker/ci
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ci.sh
executable file
·283 lines (201 loc) · 7.22 KB
/
ci.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
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
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
#!/usr/bin/env bash
print_help_info(){
exec echo "
CI-CLI v18.06
Official WebSite https://ci.khs1994.com
Usage: ./ci.sh COMMAND
Debug: [env] DEBUG=1 ./ci.sh COMMAND options
Options:
--port-port 开放 MySQL Redis 端口
-d 后台运行
--use-external-nginx 使用外部 NGINX = 后边值为配置文件路径 (TLS Only)
Commands:
change-mysql-root-password
up [-d] [--open-port]
config [--open-port]
up-tls [-d] [--open-port] [--use-external-nginx=/etc/nginx/conf.d]
tls-config [--open-port]
down
swarm-deploy
swarm-remove
k8s-deploy
k8s-remove
reset 恢复原始状态
Read './docs/*.md' for more information about CLI commands.
You can open issue in [ https://github.com/khs1994-docker/ci/issues ] when you meet problems.
You must Update .env file when update this project.
Donate https://zan.khs1994.com
"
}
_cp(){
if [ ! -f $1 ];then cp $2 $1; fi
}
_init(){
_cp .env .env.example
_cp docker-compose.override.yml docker-compose.override.demo.yml
cd config
_cp gogs/app.ini gogs/app.example.ini
_cp registry/config.yml registry/config.example.yml
cd ../webhooks
_cp .env .env.example
_cp update.js update.example.js
_cp update.sh update.example.sh
cd ../
cd config/nginx
_cp docker-registry.conf demo-docker-registry.config
_cp drone.conf demo-drone.config
_cp gogs.conf demo-gogs.config
cd ../../
command -v docker-compose > /dev/null 2>&1
if [ $? -ne 0 ];then exec echo "docker-compose not install" ; fi
}
_reset_(){
rm -rf config/gogs/app.ini \
config/registry/config.yml \
webhooks/update.js \
webhooks/update.sh \
config/nginx/*.conf
}
_reset(){
_reset_
rm -rf .env webhooks/.env docker-compose.override.yml
}
_change_mysql_root_password(){
echo 1
}
_sed_common(){
cd config
sed -i "s#{{ DB_TYPE }}#${CI_DB_TYPE:-mysql}#g" gogs/app.ini
sed -i "s#{{ DB_HOST }}#${CI_EXTERNAL_MYSQL_HOST:-mysql}:${CI_EXTERNAL_MYSQL_PORT:-3306}#g" gogs/app.ini
sed -i "s#{{ DB_DATABASE }}#${CI_EXTERNAL_MYSQL_DATABASE:-$MYSQL_DATABASE}#g" gogs/app.ini
sed -i "s#{{ DB_USERNAME }}#${CI_EXTERNAL_MYSQL_USERNAME:-root}#g" gogs/app.ini
sed -i "s#{{ DB_PASSWORD }}#${CI_EXTERNAL_MYSQL_PASSWORD:-$MYSQL_ROOT_PASSWORD}#g" gogs/app.ini
sed -i "s#{{ SSH_PORT }}#${CI_GOGS_SSH_PORT:-8022}#g" gogs/app.ini
sed -i "s#{{ MAIL_HOST }}#${CI_MAIL_HOST}#g" gogs/app.ini
sed -i "s#{{ MAIL_FROM }}#${CI_MAIL_FROM}#g" gogs/app.ini
sed -i "s#{{ MAIL_USERNAME }}#${CI_MAIL_USERNAME}#g" gogs/app.ini
sed -i "s#{{ MAIL_PASSWORD }}#${CI_MAIL_PASSWORD}#g" gogs/app.ini
cd -
}
_up(){
_reset_ ; _init
_sed_common
cd config
sed -i "s#{{ CI_DOMAIN }}#${CI_HOST:-192.168.199.100}#g" gogs/app.ini
sed -i "s#{{ CI_DOMAIN_FULL }}#${CI_HOST:-192.168.199.100}#g" gogs/app.ini
sed -i "s#{{ CI_GOGS_PORT }}#${CI_GOGS_PORT:-3000}#g" gogs/app.ini
sed -i "s#{{ PROTOCOL }}#http#g" gogs/app.ini
sed -i "s!^CERT_FILE.*!#CERT_FILE!g" gogs/app.ini
sed -i "s!^KEY_FILE.*!#KEY_FILE!g" gogs/app.ini
sed -i "s!^TLS_MIN_VERSION.*!#TLS_MIN_VERSION!g" gogs/app.ini
sed -i "s#{{ REDIS_HOST }}#${CI_EXTERNAL_REDIS_HOST:-$REDIS_HOST}#g" registry/config.yml
sed -i "s#{{ WEBHOOKS_HOST }}#${WEBHOOKS_HOST:-http://192.168.199.100}#g" registry/config.yml
cd -
sed -i "s#{{ DRONE_HOST }}#http://${CI_HOST:-192.168.199.100}:${CI_DRONE_PORT:-8000}#g" docker-compose.override.yml
sed -i "s#{{ DRONE_GOGS_URL }}#http://${CI_HOST:-192.168.199.100}:${CI_GOGS_PORT:-3000}#g" docker-compose.override.yml
CI_INCLUDE=${CI_INCLUDE:-drone-server drone-agent gogs registry}
# based posrt don't up nginx
for soft in $CI_INCLUDE
do
if [ $soft = 'nginx' ];then
continue
fi
CI_INCLUDE_TARGET+="$soft "
done
docker-compose ${COMPOSE_FILE:-} up ${opt:-} ${CI_INCLUDE_TARGET}
}
_config(){
docker-compose ${COMPOSE_FILE:-} config
}
_up-tls(){
_reset_ ; _init
_sed_common
cd config
sed -i "s#{{ CI_DOMAIN }}#${CI_DOMAIN:-t.khs1994.com}#g" gogs/app.ini
sed -i "s#{{ CI_DOMAIN_FULL }}#git.${CI_DOMAIN:-t.khs1994.com}#g" gogs/app.ini
sed -i "s#{{ CI_GOGS_PORT }}#${CI_GOGS_PORT:-443}#g" gogs/app.ini
sed -i "s#{{ PROTOCOL }}#https#g" gogs/app.ini
sed -i "s#{{ REDIS_HOST }}#${CI_EXTERNAL_REDIS_HOST:-$REDIS_HOST}#g" registry/config.yml
sed -i "s#{{ WEBHOOKS_HOST }}#${WEBHOOKS_HOST:-https://ci.t.khs1994.com/docker/webhooks}#g" registry/config.yml
cd -
sed -i "s#{{ DRONE_HOST }}#https://drone.${CI_DOMAIN:-t.khs1994.com}#g" docker-compose.override.yml
sed -i "s#{{ DRONE_GOGS_URL }}#https://git.${CI_DOMAIN:-t.khs1994.com}#g" docker-compose.override.yml
sed -i "s#{{ CI_DOMAIN }}#${CI_DOMAIN:-t.khs1994.com}#g" config/nginx/docker-registry.conf
sed -i "s#{{ CI_DOMAIN }}#${CI_DOMAIN:-t.khs1994.com}#g" config/nginx/drone.conf
sed -i "s#{{ CI_DOMAIN }}#${CI_DOMAIN:-t.khs1994.com}#g" config/nginx/gogs.conf
_sed_external_nginx(){
# 使用外部 NGINX
cd config/nginx
for file in $( ls *.conf )
do
sed -i "s#{{ CI_DOMAIN }}#${CI_DOMAIN:-t.khs1994.com}#g" $file
sed -i "s#{{ REGISTRY_UPSTREAM }}#${CI_HOST:-192.168.199.100}#g" $file
sed -i "s#{{ DRONE_UPSTREAM }}#${CI_HOST:-192.168.199.100}#g" $file
sed -i "s#{{ GOGS_UPSTREAM }}#${CI_HOST:-192.168.199.100}#g" $file
done
cp -a $PWD/*.conf $NGINX_CONF
mkdir -p $NGINX_CONF/ssl || echo
if ! [ -f ssl/$CI_DOMAIN.crt ];then
cp -a $PWD/ssl/* $NGINX_CONF/ssl
fi
cd -
}
_sed_nginx(){
# 使用内部 NGINX
cd config/nginx
for file in `ls *.conf`
do
sed -i "s#{{ CI_DOMAIN }}#${CI_DOMAIN:-t.khs1994.com}#g" $file
sed -i "s#{{ REGISTRY_UPSTREAM }}#registry#g" $file
sed -i "s#{{ DRONE_UPSTREAM }}#drone-server#g" $file
sed -i "s#{{ GOGS_UPSTREAM }}#gogs#g" $file
done
cd -
}
set +e
test "$ENABLE_NGINX" = 'FALSE' && _sed_external_nginx
set -e
test "$ENABLE_NGINX" = 'FALSE' || _sed_nginx
docker-compose ${COMPOSE_FILE:-} up ${opt:-} ${CI_INCLUDE:-drone-server drone-agent gogs registry} \
$( test "$ENABLE_NGINX" = 'FALSE' || echo 'nginx' )
}
_tls-config(){
docker-compose ${COMPOSE_FILE:-} config
}
_down(){
docker-compose down --remove-orphans
}
_logs(){
if ! [ -f logs/nginx/access.log ];then
mkdir -p logs/nginx
touch logs/nginx/access.log
touch logs/nginx/error.log
fi
}
set -e
_logs
if [ "$DEBUG" = 'true' ];then set -x; fi
OS=`uname -s`
test $OS = 'Darwin' && export PATH=/usr/local/opt/gnu-sed/libexec/gnubin:$PATH
_init
. $PWD/.env
. $PWD/secrets/mysql.env
if [ -f $HOME/.bash_profile ];then
. ~/.bash_profile
fi
test ! -z "$1" || print_help_info
command=$1
shift
if [ -z "$CI_HOST" -a "$command" = 'up' ];then
exec echo "CI_HOST not set"
fi
if [ -z "$CI_DOMAIN" -a "$command" = 'up-tls' ];then
exec echo "CI_DOMAIN not set"
fi
for arg in "$@"
do
test $arg = '--open-port' && COMPOSE_FILE='-f docker-compose.yml -f docker-compose.override.yml -f docker-compose.port.yml'
test $arg = '-d' && opt='-d'
[[ $arg =~ --use-external-nginx=* ]] && ENABLE_NGINX=FALSE && NGINX_CONF=$( echo $arg | cut -d '=' -f 2 )
done
_$command "$@"