forked from rjminsha/pipeline_service_docker_deployer_ice
-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy path_init.sh
executable file
·397 lines (366 loc) · 12.8 KB
/
_init.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
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
#!/bin/bash
#********************************************************************************
# Copyright 2014 IBM
#
# 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
#********************************************************************************
#############
# Colors #
#############
export green='\e[0;32m'
export red='\e[0;31m'
export label_color='\e[0;33m'
export no_color='\e[0m' # No Color
########################################
# default values to build server names #
########################################
# beta servers
BETA_API_PREFIX="api-ice"
BETA_REG_PREFIX="registry-ice"
# default servers
DEF_API_PREFIX="containers-api"
DEF_REG_PREFIX="registry"
export MODULE_NAME="container-deployer"
##################################################
# Simple function to only run command if DEBUG=1 #
### ###############################################
debugme() {
[[ $DEBUG = 1 ]] && "$@" || :
}
installwithpython27() {
echo "Installing Python 2.7"
sudo apt-get update &> /dev/null
sudo apt-get -y install python2.7 &> /dev/null
python --version
wget --no-check-certificate https://bootstrap.pypa.io/get-pip.py &> /dev/null
python get-pip.py --user &> /dev/null
export PATH=$PATH:~/.local/bin
if [ -f icecli-3.0.zip ]; then
debugme echo "there was an existing icecli.zip"
debugme ls -la
rm -f icecli-3.0.zip
fi
wget https://static-ice.ng.bluemix.net/icecli-3.0.zip &> /dev/null
pip install --user icecli-3.0.zip > cli_install.log 2>&1
debugme cat cli_install.log
}
installwithpython34() {
curl -kL http://xrl.us/pythonbrewinstall | bash
source $HOME/.pythonbrew/etc/bashrc
sudo apt-get install zlib1g-dev libexpat1-dev libdb4.8-dev libncurses5-dev libreadline6-dev
sudo apt-get update &> /dev/null
debugme pythonbrew list -k
echo "Installing Python 3.4.1"
pythonbrew install 3.4.1 &> /dev/null
debugme cat /home/jenkins/.pythonbrew/log/build.log
pythonbrew switch 3.4.1
python --version
echo "Installing pip"
wget --no-check-certificate https://bootstrap.pypa.io/get-pip.py &> /dev/null
python get-pip.py --user
export PATH=$PATH:~/.local/bin
which pip
echo "Installing ice cli"
wget https://static-ice.ng.bluemix.net/icecli-3.0.zip &> /dev/null
wget https://static-ice.ng.bluemix.net/icecli-3.0.zip
pip install --user icecli-3.0.zip > cli_install.log 2>&1
debugme cat cli_install.log
}
installwithpython277() {
pushd $EXT_DIR >/dev/null
echo "Installing Python 2.7.7"
curl -kL http://xrl.us/pythonbrewinstall | bash
source $HOME/.pythonbrew/etc/bashrc
sudo apt-get update &> /dev/null
sudo apt-get build-dep python2.7
sudo apt-get install zlib1g-dev
debugme pythonbrew list -k
echo "Installing Python 2.7.7"
pythonbrew install 2.7.7 --no-setuptools &> /dev/null
debugme cat /home/jenkins/.pythonbrew/log/build.log
pythonbrew switch 2.7.7
python --version
echo "Installing pip"
wget --no-check-certificate https://bootstrap.pypa.io/get-pip.py &> /dev/null
python get-pip.py --user &> /dev/null
debugme pwd
debugme ls
popd >/dev/null
pip remove requests
pip install --user -U requests
pip install --user -U pip
export PATH=$PATH:~/.local/bin
which pip
echo "Installing ice cli"
wget https://static-ice.ng.bluemix.net/icecli-3.0.zip &> /dev/null
pip install --user icecli-3.0.zip > cli_install.log 2>&1
debugme cat cli_install.log
}
installwithpython3() {
sudo apt-get update &> /dev/null
sudo apt-get upgrade &> /dev/null
sudo apt-get -y install python3 &> /dev/null
python3 --version
echo "installing pip"
wget --no-check-certificate https://bootstrap.pypa.io/get-pip.py
python3 get-pip.py --user &> /dev/null
export PATH=$PATH:~/.local/bin
which pip
echo "installing ice cli"
wget https://static-ice.ng.bluemix.net/icecli-3.0.zip
pip install --user icecli-3.0.zip > cli_install.log 2>&1
debugme cat cli_install.log
}
if [[ $DEBUG = 1 ]]; then
export ICE_ARGS="--verbose"
else
export ICE_ARGS=""
fi
set +e
set +x
###############################
# Configure extension PATH #
###############################
if [ -n $EXT_DIR ]; then
export PATH=$EXT_DIR:$PATH
else
export EXT_DIR=`pwd`
fi
#########################################
# Configure log file to store errors #
#########################################
if [ -z "$ERROR_LOG_FILE" ]; then
ERROR_LOG_FILE="${EXT_DIR}/errors.log"
export ERROR_LOG_FILE
fi
#################################
# Source git_util sh file #
#################################
source ${EXT_DIR}/git_util.sh
################################
# get the extensions utilities #
################################
pushd . >/dev/null
cd $EXT_DIR
if [ -n "${OVERRIDE_UTILITIES_BRANCH}" ]; then
echo "Pulling utilities from branch ${OVERRIDE_UTILITIES_BRANCH}"
git_retry clone https://github.com/Osthanes/utilities.git -b "${OVERRIDE_UTILITIES_BRANCH}" utilities
else
git_retry clone https://github.com/Osthanes/utilities.git utilities
fi
popd >/dev/null
################################
# Source utilities sh files #
################################
source ${EXT_DIR}/utilities/ice_utils.sh
source ${EXT_DIR}/utilities/logging_utils.sh
#################################
# Show deprecated message #
#################################
log_and_echo "$ERROR" "ATTENTION: This job type has been DEPRECATED. Please use the 'Kubernetes' deploy type instead."
########################
# setup deploy property file #
########################
# create deploy property file.
if [ -z ${DEPLOY_PROPERTY_FILE} ]; then
export DEPLOY_PROPERTY_FILE="${EXT_DIR}/deploy-property.sh"
fi
if [ -f "${DEPLOY_PROPERTY_FILE}" ]; then
/bin/rm -f ${DEPLOY_PROPERTY_FILE}
fi
/bin/touch ${DEPLOY_PROPERTY_FILE}
chmod +x $DEPLOY_PROPERTY_FILE
echo '#!/bin/bash' >> "${DEPLOY_PROPERTY_FILE}"
#############################
# Install Cloud Foundry CLI #
#############################
CF_VER=$(cf -v)
log_and_echo "$INFO" "Existing Cloud Foundry CLI ${CF_VER}"
log_and_echo "$INFO" "Installing Cloud Foundry CLI"
pushd $EXT_DIR >/dev/null
gunzip cf-linux-amd64.tgz &> /dev/null
tar -xvf cf-linux-amd64.tar &> /dev/null
cf help &> /dev/null
RESULT=$?
if [ $RESULT -ne 0 ]; then
log_and_echo "$ERROR" "Could not install the Cloud Foundry CLI"
${EXT_DIR}/print_help.sh
${EXT_DIR}/utilities/sendMessage.sh -l bad -m "Failed to install Cloud Foundry CLI. $(get_error_info)"
exit $RESULT
fi
CF_VER=$(cf -v)
popd >/dev/null
log_and_echo "$LABEL" "Successfully installed Cloud Foundry CLI ${CF_VER}"
#####################################
# Install IBM Container Service CLI #
#####################################
# Install ICE CLI
log_and_echo "$INFO" "Installing IBM Container Service CLI"
ice help &> /dev/null
RESULT=$?
if [ $RESULT -ne 0 ]; then
# installwithpython3
installwithpython27
# installwithpython277
# installwithpython34
ice help &> /dev/null
RESULT=$?
if [ $RESULT -ne 0 ]; then
log_and_echo "$ERROR" "Failed to install IBM Container Service CLI"
debugme python --version
if [ "$USE_ICE_CLI" = "1" ]; then
${EXT_DIR}/print_help.sh
${EXT_DIR}/utilities/sendMessage.sh -l bad -m "Failed to install IBM Container Service CLI. $(get_error_info)"
exit $RESULT
fi
else
log_and_echo "$LABEL" "Successfully installed IBM Container Service CLI"
fi
fi
#############################################
# Install the IBM Containers plug-in (cf ic) #
#############################################
if [ "$USE_ICE_CLI" != "1" ]; then
export IC_COMMAND="${EXT_DIR}/cf ic"
install_cf_ic
RESULT=$?
if [ $RESULT -ne 0 ]; then
exit $RESULT
fi
else
export IC_COMMAND="ice"
fi
##########################################
# setup bluemix env
##########################################
# attempt to target env automatically
if [ -n "$BLUEMIX_TARGET" ]; then
# cf not setup yet, try manual setup
if [ "$BLUEMIX_TARGET" == "staging" ]; then
echo -e "Targetting staging Bluemix"
export BLUEMIX_API_HOST="api.stage1.ng.bluemix.net"
elif [ "$BLUEMIX_TARGET" == "prod" ]; then
echo -e "Targetting production Bluemix"
export BLUEMIX_API_HOST="api.ng.bluemix.net"
else
echo -e "${red}Unknown Bluemix environment specified: ${BLUEMIX_TARGET}${no_color}" | tee -a "$ERROR_LOG_FILE"
echo -e "Targetting production Bluemix"
export BLUEMIX_TARGET="prod"
export BLUEMIX_API_HOST="api.ng.bluemix.net"
fi
else
CF_API=$(${EXT_DIR}/cf api)
RESULT=$?
debugme echo "CF_API: ${CF_API}"
if [ $RESULT -eq 0 ]; then
# find the bluemix api host
export BLUEMIX_API_HOST=`echo $CF_API | awk '{print $3}' | sed '0,/.*\/\//s///'`
echo $BLUEMIX_API_HOST | grep 'stage1'
if [ $? -eq 0 ]; then
# on staging, make sure bm target is set for staging
export BLUEMIX_TARGET="staging"
else
# on prod, make sure bm target is set for prod
export BLUEMIX_TARGET="prod"
fi
else
echo -e "Targetting production Bluemix"
export BLUEMIX_TARGET="prod"
export BLUEMIX_API_HOST="api.ng.bluemix.net"
fi
fi
echo -e "Bluemix host is '${BLUEMIX_API_HOST}'"
echo -e "Bluemix target is '${BLUEMIX_TARGET}'"
# strip off the hostname to get full domain
CF_TARGET=`echo $BLUEMIX_API_HOST | sed 's/[^\.]*//'`
if [ -z "$API_PREFIX" ]; then
API_PREFIX=$DEF_API_PREFIX
fi
if [ -z "$REG_PREFIX" ]; then
REG_PREFIX=$DEF_REG_PREFIX
fi
# build api server hostname
export CCS_API_HOST="${API_PREFIX}${CF_TARGET}"
# build registry server hostname
export CCS_REGISTRY_HOST="${REG_PREFIX}${CF_TARGET}"
# set up the ice cfg
sed -i "s/ccs_host =.*/ccs_host = $CCS_API_HOST/g" $EXT_DIR/ice-cfg.ini
sed -i "s/reg_host =.*/reg_host = $CCS_REGISTRY_HOST/g" $EXT_DIR/ice-cfg.ini
sed -i "s/cf_api_url =.*/cf_api_url = $BLUEMIX_API_HOST/g" $EXT_DIR/ice-cfg.ini
export ICE_CFG="ice-cfg.ini"
################################
# Login to Container Service #
################################
login_to_container_service
RESULT=$?
if [ $RESULT -ne 0 ] && [ "$USE_ICE_CLI" = "1" ]; then
exit $RESULT
fi
############################
# enable logging to logmet #
############################
setup_met_logging "${BLUEMIX_USER}" "${BLUEMIX_PASSWORD}"
RESULT=$?
if [ $RESULT -ne 0 ]; then
log_and_echo "$WARN" "LOGMET setup failed with return code ${RESULT}"
fi
################################
# Get the namespace #
################################
get_name_space
RESULT=$?
if [ $RESULT -ne 0 ]; then
exit $RESULT
fi
##############################
# Identify the Image to use #
##############################
# If the IMAGE_NAME is set in the environment then use that.
# Else assume the input is coming from the build.properties created and archived by the Docker builder job
if [ -z $IMAGE_NAME ]; then
debugme echo "finding build.properties"
debugme pwd
debugme ls
if [ -f build.properties ]; then
. build.properties
export IMAGE_NAME
debugme cat build.properties
echo "IMAGE_NAME: $IMAGE_NAME"
fi
if [ -z $IMAGE_NAME ]; then
log_and_echo "$ERROR" "IMAGE_NAME not set. Set the IMAGE_NAME in the environment or provide a Docker build job as input to this deploy job."
log_and_echo "$ERROR" "If there was a recent change to the pipeline, such as deleting or moving a job or stage, check that the input to this and other later stages is still set to the correct build stage and job."
${EXT_DIR}/print_help.sh
${EXT_DIR}/utilities/sendMessage.sh -l bad -m "Failed to get image name. $(get_error_info)"
exit 1
fi
else
log_and_echo "$LABEL" "Image being overridden by the environment. Using ${IMAGE_NAME}"
fi
########################
# Current Limitations #
########################
if [ -z $IP_LIMIT ]; then
export IP_LIMIT=8
fi
if [ -z $CONTAINER_LIMIT ]; then
export CONTAINER_LIMIT=8
fi
########################
# Adjust Build Number #
########################
if [ -n "$BUILD_OFFSET" ]; then
log_and_echo "$INFO" "Using BUILD_OFFSET of $BUILD_OFFSET"
export APPLICATION_VERSION=$((APPLICATION_VERSION + BUILD_OFFSET))
export BUILD_NUMBER=$((BUILD_NUMBER + BUILD_OFFSET))
fi
log_and_echo "$LABEL" "Initialization complete"