-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdeploy
411 lines (349 loc) · 12.3 KB
/
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
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
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
#!/bin/bash
###############################################################################
# Project: hi-cli
# Description: The cross platform development toolkit - hi-cli
# Author: John Deng ([email protected])
#
# Copyright (c) 2014-2017 John Deng ([email protected])
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http:#www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
# Author: John Deng ([email protected])
# Updated: Mon Mar 27 23:24:29 CST 2017
# Module: oc
# Submodule:
# Command: deploy
###############################################################################
# TODO:
# 1. add app_postfix to start new pod for blue-green update
source "${HI_CLI_HOME}/bin/colors"
source "${HI_CLI_HOME}/bin/logging"
source "${module_dir}/utils/common"
source "${module_dir}/version"
parser="${HI_CLI_HOME}/utils/parser.py"
# temporary solution
if [ ! -f "${parser}" ]; then
hi update
fi
###############################################################################
function get_dc_postfixes()
{
oc get dc | grep ${app} | awk '{print $1}' | sed "s/${app}-//g" | xargs -I {} sh -c """
echo {}
"""
}
###############################################################################
function deploy_with_tag()
{
l_app_name=${app}
if [ "$1" != "" ]; then
l_app_name=${app}-$1
fi
# tag for stage or prod
log_debug "tag image stream"
image_tag="${ver}"
log_debug "image_tag: ${image_tag}, ver: ${ver}"
if [ $(oc get is ${app} -n ${project}-dev 2>/dev/null | grep latest | wc -l) != 0 ]; then
log_debug "found image steam ${app} from ${project}-dev"
images_profile="dev"
elif [ $(oc get is ${app} -n ${project}-stage 2>/dev/null | grep latest | wc -l) != 0 ]; then
log_debug "found image steam ${app} from ${project}-stage"
images_profile="stage"
else
log_error "no image stream found!"
return -3
fi
log_debug "oc tag ${project}-${images_profile}/${l_app_name}:latest ${project_profile}/${l_app_name}:latest"
src_app_name="${l_app_name}"
if [ "${origin}" != "" ] || [ $(oc get is -n ${project}-${images_profile} -l app=${l_app_name} | grep ${l_app_name} | wc -l) == 0 ]; then
src_app_name="${app}"
fi
# tag new image stream
oc tag ${project}-${images_profile}/${src_app_name}:latest ${project_profile}/${l_app_name}:${image_tag}
oc tag ${project}-${images_profile}/${src_app_name}:latest ${project_profile}/${l_app_name}:latest
# switch to new
oc project ${project_profile}
}
###############################################################################
function run_plugins()
{
# run plugin
result=0
for p in "${plugins[@]}"
do
log_debug "found plugin: ${p}"
plugin_file="${module_dir}/plugins/${p}"
if [ -f "${plugin_file}" ]; then
log_debug "run plugin: ${p}"
source "${plugin_file}"
run_plugin_${p}
result=$?
fi
done
return ${result}
}
###############################################################################
function deploy() {
# deploy(project, app, ver, "dev", "Asia/Shanghai")
# ensure hi init
if [ ! -f hi.conf ]; then
log_warn "Please run 'hi init' first!"
return -1
fi
ensure_oc_login
[ "${profile}" == "" ] && project_profile=${project} || project_profile="${project}-${profile}"
# ensure project is switched
oc project ${project_profile}
image_tag="latest"
env_profile=""
if [ -f ./package.json ]; then
log_debug "found nodejs app"
ver=$(get_nodejs_app_version)
from_dir="dist"
image_stream="${is_s2i_nodejs}"
if [ ! -f dist/package.json ] || [ "${build}" == "build" ]; then
rm -rf dist # clean up dist
npm install
[ -f gulpfile.js ] && gulp build --env=${profile} || npm run build:${profile}
create_package_json "dist/package.json"
fi
elif [ -f ./SUMMARY.md ] && [ -f ./README.md ]; then
log_debug "found gitbook app"
[[ "${ver}" == "" ]] && ver="1.0.0"
from_dir="dist"
image_stream="${is_s2i_gitbook}"
if [ ! -f dist/package.json ] || [ "${build}" == "build" ]; then
rm -rf dist # clean up dist
gitbook install && gitbook build . dist
create_package_json "dist/package.json"
fi
elif [ -d ./dist ]; then
log_debug "found web app"
if [ ! -f ./dist/package.json ]; then
if [ "${ver}" == "" ]; then
ver="1.0.0"
fi
elif [ -f ./package.json ]; then
ver=$(get_nodejs_app_version)
fi
create_package_json "dist/package.json"
from_dir="dist"
image_stream="${is_s2i_nodejs}"
elif [ -f pom.xml ]; then
log_debug "found java app"
if [ "${new}" != "" ]; then
if [ "${profile}" == "prod" ]; then
log_warn "Please deploy to stage first!"
return -4
elif [ "${profile}" == "test" ] || [ "${profile}" == "load" ]; then
log_warn "Please deploy to dev first!"
return -4
fi
fi
# parse cluster name from application.yml
if [ "${cluster}" == "" ]; then
cluster=$(python "${parser}" file "$PWD/src/main/resources/application.yml" app cluster-name)
cluster_active=$(python "${parser}" file "$PWD/src/main/resources/application-${profile}.yml" app cluster-name)
if [ "${cluster_active}" != "" ]; then
log_debug "parsed cluster_active: ${cluster_active}"
cluster="${cluster_active}"
fi
log_debug "parsed cluster: ${cluster}"
fi
if [ "${app_options}" == "" ]; then
app_options="-Xmx512m -Xss512k"
fi
# all applications will need -Djava.net.preferIPv4Stack=true -Dfile.encoding=utf-8
app_options="${app_options} -Djava.net.preferIPv4Stack=true -Dfile.encoding=utf-8"
env_profile=${profile}
from_dir="oc-build"
image_stream="${is_s2i_java}"
ver=$(get_java_app_version)
log_debug "ver: ${ver}"
if [ ! -f target/${app}-${ver}.jar ] && [ ! -f target/${app}.war ]; then
build="build"
fi
if [ "${build}" == "build" ]; then
log_debug "build app ..."
mvn clean install -U
fi
fi
if [ "${need_deploy}" == "false" ]; then
log_info "done"
return 0
fi
# change app name for
app_name=${app}
if [ "${postfix}" != "" ]; then
app_name="${app}-${postfix}"
fi
log_info "deploy ${project}/${app_name}, ver: ${ver}, project: ${project_profile}, timezone: ${timezone} port: ${port}"
deployer="$(oc whoami) ($(whoami)) (cicd-${cicd_version})"
log_debug "deployer: ${deployer}"
rev="$(git show --summary --pretty='%H' | grep -v create )"
log_debug "revsion: ${rev}"
# set labels_options
if [ "${cluster}" != "" ]; then
labels="cluster=${cluster}-${profile}"
labels_option="--labels=${labels}"
fi
labels_option="--labels=app=${app_name} ${labels_option}"
# set env_options
master_url="$(oc project | awk '{print $NF}' | sed -e s/\"//g | sed 's/.$//')"
if [ "${app_options}" != "" ]; then
log_debug "set app options"
if [ "${namespaces_constraint}" != "" ]; then
app_options="${app_options} -DNAMESPACE=${project_profile}"
fi
if [ "${labels}" != "" ]; then
app_options="${app_options} -DLABELS=${labels}"
fi
if [ "${cluster}" != "" ]; then
app_options="${app_options} -DMASTER_URL=${master_url}"
app_options="${app_options} -Dapp.cluster-name=${cluster}"
fi
fi
log_debug "app_options: ${app_options}"
is_dc_exist=$(is_exist "dc/${app_name}" "${project_profile}")
log_debug "is_dc_exist: ${is_dc_exist}"
if [ ${is_dc_exist} == "yes" ]; then
# remove config triggers
log_debug "check config trigger"
if [ "$(oc set triggers dc/${app_name} -a | grep "config " | awk '{print $3}')" == "true" ]; then
log_debug "set config trigger"
oc set triggers dc/${app_name} --from-config --remove
fi
# only set env according user's need
log_debug "set env for dc"
if [ "${app_options}" != "" ]; then
oc env dc ${app_name} APP_OPTIONS="${app_options}" DEPLOYER="${deployer}" REVISION="${rev}"
else
oc env dc ${app_name} DEPLOYER="${deployer}" REVISION="${rev}"
fi
if [ "${cluster}" != "" ]; then
oc label dc ${app_name} cluster=${cluster}-${profile} --overwrite
fi
fi
if [ "" != "${new}" ]; then
if [ -f target/${app}-${ver}.war ]; then
mkdir -p oc-build/deployments
rm -rf oc-build && mkdir -p oc-build/deployments
log_debug "cp target/${app}-${ver}.war oc-build/deployments/ROOT.war"
cp target/${app}-${ver}.war oc-build/deployments/ROOT.war
image_stream="${is_s2i_java_war}"
elif [ -f target/${app}-${ver}.jar ]; then
ver=$(get_java_app_version)
rm -rf oc-build && mkdir -p oc-build
cp target/${app}-${ver}.jar oc-build/app.jar
elif [ -f dist/package.json ]; then
log_debug "deploy web app"
else
log_error "No app target found!"
exit
fi
# check deploy target type is war or jar
# create build. override the exit code since it complains about exising imagestream
if [ $(is_exist "bc/${app_name}" "${project_profile}") == "no" ]; then
log_debug "create new image stream"
oc new-build --name=${app_name} --image-stream=${image_stream} --binary=true --labels=app=${app_name} -n ${project_profile} || true
fi
# build image
log_debug "start build, from_dir: ${from_dir}"
oc start-build ${app_name} --from-dir="${from_dir}" --wait=true -n ${project_profile}
log_debug "build result: $?"
else
if [ "${postfix}" != "" ]; then
deploy_with_tag ${postfix}
else
deploy_with_tag
fi
fi
if [ $? != 0 ]; then
log_error "failed to create image stream"
return $?
fi
# deploy image
if [ $(is_exist "dc/${app_name}" "${project_profile}" ) == "no" ]; then
log_debug "create new app"
if [ "${app_options}" != "" ]; then
log_debug "set app options"
oc new-app ${app_name}:latest \
--env SPRING_PROFILES_ACTIVE="${profile}" \
--env APP_OPTIONS="${app_options}" \
--env DEPLOYER="${deployer}" \
--env TZ="${timezone}" \
--env REVISION="${rev}" \
${labels_option} \
-n ${project_profile}
else
log_debug "oc new-app ${app_name}:latest --env DEPLOYER="${deployer}" --env TZ="${timezone}" --env REVISION="${rev}" ${labels_option} -n ${project_profile}"
oc new-app ${app_name}:latest --env DEPLOYER="${deployer}" --env TZ="${timezone}" --env REVISION="${rev}" ${labels_option} -n ${project_profile}
fi
# if [ "${env_profile}" != "" ] && [ "${app_options}" != "" ]; then
# log_debug "add app options"
# oc env dc ${app_name} SPRING_PROFILES_ACTIVE="${profile}"
# fi
fi
# set Automatically start a new deployment when the image changes
log_debug "check image trigger"
if [ "$(oc set triggers dc/${app_name} -a | grep image | awk '{print $5}')" == "false" ]; then
log_debug "set image trigger"
oc set triggers dc/${app_name} --auto=true
fi
# expose service
if [ $(is_exist "route/${app_name}" "${project_profile}") == "no" ]; then
log_debug "create router"
oc expose svc/${app_name} --port=${port} -n ${project_profile}
fi
# run plugins
if [ $? == 0 ]; then
run_plugins
fi
# done
return $?
}
function create_package_json() {
log_debug "create_package_json"
cat > "$1" <<_EOF_
{
"name": "${app}",
"version": "${ver}",
"description": "nodejs http-server",
"scripts": {
"start": "http-server . -d false -p 8080"
},
"author": "John Deng",
"license": "MIT"
}
_EOF_
}
# postfix = version
function run() {
cli="hi cicd deploy"
if [ "${all}" != "" ]; then
l_app_options=${app_options}
oc get dc | grep ${app} | awk '{print $1}' | while read p; do
app_options=${l_app_options}
postfix=${p/${app}/}
postfix=${postfix/-/}
deploy
done
else
deploy
fi
result=$?
log_debug "result: $result"
eval $1="\${result}"
eval $2="\${cli}"
}
###############################################################################