-
Notifications
You must be signed in to change notification settings - Fork 7
/
_init.sh
executable file
·333 lines (303 loc) · 11.7 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
#!/bin/bash
#********************************************************************************
# Copyright 2015 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
##################################################
# Simple function to only run command if DEBUG=1 #
##################################################
debugme() {
[[ $DEBUG = 1 ]] && "$@" || :
}
export -f debugme
set +e
set +x
###############################
# Configure extension PATH #
###############################
if [ -n $EXT_DIR ]; then
export PATH=$EXT_DIR:$PATH
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 file #
#################################
source ${EXT_DIR}/git_util.sh
################################
# get the extensions utilities #
################################
pushd . >/dev/null
cd $EXT_DIR
git_retry clone https://github.com/Osthanes/utilities.git utilities
export PYTHONPATH=$EXT_DIR/utilities:$PYTHONPATH
popd >/dev/null
#################################
# Source utilities sh files #
#################################
source ${EXT_DIR}/utilities/ice_utils.sh
source ${EXT_DIR}/utilities/logging_utils.sh
################################
# Application Name and Version #
################################
# The build number for the builder is used for the version in the image tag
# For deployers this information is stored in the $BUILD_SELECTOR variable and can be pulled out
if [ -z "$APPLICATION_VERSION" ]; then
export SELECTED_BUILD=$(grep -Eo '[0-9]{1,100}' <<< "${BUILD_SELECTOR}")
if [ -z $SELECTED_BUILD ]; then
if [ -z $BUILD_NUMBER ]; then
export APPLICATION_VERSION=$(date +%s)
else
export APPLICATION_VERSION=$BUILD_NUMBER
fi
else
export APPLICATION_VERSION=$SELECTED_BUILD
fi
fi
# install necessary features
log_and_echo "$INFO" "Setting up prerequisites for IBM Security Static Analyzer. This will likely take several minutes"
debugme echo "enabling i386 architechture"
sudo dpkg --add-architecture i386 >/dev/null 2>&1
sudo apt-get update >/dev/null 2>&1
debugme echo "installing i386 libraries"
sudo apt-get install -y libc6:i386 libc6-i686 g++-multilib >/dev/null 2>&1
debugme echo "installing bc"
sudo apt-get install -y bc >/dev/null 2>&1
debugme echo "installing unzip"
sudo apt-get install -y unzip >/dev/null 2>&1
debugme echo "done installing prereqs"
if [ -n "$BUILD_OFFSET" ]; then
log_and_echo "$INFO" "Using BUILD_OFFSET of $BUILD_OFFSET"
export APPLICATION_VERSION=$(echo "$APPLICATION_VERSION + $BUILD_OFFSET" | bc)
export BUILD_NUMBER=$(echo "$BUILD_NUMBER + $BUILD_OFFSET" | bc)
fi
log_and_echo "$INFO" "APPLICATION_VERSION: $APPLICATION_VERSION"
################################
# Setup archive information #
################################
if [ -z $WORKSPACE ]; then
log_and_echo "$ERROR" "Please set WORKSPACE in the environment properties."
${EXT_DIR}/utilities/sendMessage.sh -l bad -m "Please set WORKSPACE in the environment properties."
exit 1
fi
if [ -z $ARCHIVE_DIR ]; then
log_and_echo "$LABEL" "ARCHIVE_DIR was not set, setting to WORKSPACE/archive."
export ARCHIVE_DIR="${WORKSPACE}"
fi
if [ -d $ARCHIVE_DIR ]; then
log_and_echo "$INFO" "Archiving to $ARCHIVE_DIR"
else
log_and_echo "$INFO" "Creating archive directory $ARCHIVE_DIR"
mkdir $ARCHIVE_DIR
fi
export LOG_DIR=$ARCHIVE_DIR
#############################
# Install Cloud Foundry CLI #
#############################
cf help &> /dev/null
RESULT=$?
if [ $RESULT -eq 0 ]; then
# if already have an old version installed, save a pointer to it
export OLDCF_LOCATION=`which cf`
fi
# get the newest version
log_and_echo "$INFO" "Installing Cloud Foundry CLI"
pushd . >/dev/null
cd $EXT_DIR
curl --silent -o cf-linux-amd64.tgz -v -L https://cli.run.pivotal.io/stable?release=linux64-binary &>/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}/utilities/sendMessage.sh -l bad -m "Could not install the cloud foundry CLI"
exit 1
fi
popd >/dev/null
log_and_echo "$SUCCESSFUL" "Successfully installed Cloud Foundry CLI"
##########################################
# setup bluemix env
##########################################
# attempt to target env automatically
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"
export BLUEMIX_API_HOST="api.stage1.ng.bluemix.net"
else
# on prod, make sure bm target is set for prod
export BLUEMIX_TARGET="prod"
export BLUEMIX_API_HOST="api.ng.bluemix.net"
fi
elif [ -n "$BLUEMIX_TARGET" ]; then
# cf not setup yet, try manual setup
if [ "$BLUEMIX_TARGET" == "staging" ]; then
log_and_echo "$INFO" "Targetting staging Bluemix"
export BLUEMIX_API_HOST="api.stage1.ng.bluemix.net"
elif [ "$BLUEMIX_TARGET" == "prod" ]; then
log_and_echo "$INFO" "Targetting production Bluemix"
export BLUEMIX_API_HOST="api.ng.bluemix.net"
else
log_and_echo "$INFO" "$ERROR" "Unknown Bluemix environment specified"
fi
else
log_and_echo "$INFO" "Targetting production Bluemix"
export BLUEMIX_API_HOST="api.ng.bluemix.net"
fi
################################
# Login to Container Service #
################################
if [ -n "$BLUEMIX_USER" ] || [ ! -f ~/.cf/config.json ]; then
# need to gather information from the environment
# Get the Bluemix user and password information
if [ -z "$BLUEMIX_USER" ]; then
log_and_echo "$ERROR" "Please set BLUEMIX_USER on environment"
${EXT_DIR}/utilities/sendMessage.sh -l bad -m "Please set BLUEMIX_USER as an environment property"
exit 1
fi
if [ -z "$BLUEMIX_PASSWORD" ]; then
log_and_echo "$ERROR" "Please set BLUEMIX_PASSWORD as an environment property environment"
${EXT_DIR}/utilities/sendMessage.sh -l bad -m "Please set BLUEMIX_PASSWORD as an environment property"
exit 1
fi
if [ -z "$BLUEMIX_ORG" ]; then
export BLUEMIX_ORG=$BLUEMIX_USER
log_and_echo "$LABEL" "Using ${BLUEMIX_ORG} for Bluemix organization, please set BLUEMIX_ORG if on the environment if you wish to change this."
fi
if [ -z "$BLUEMIX_SPACE" ]; then
export BLUEMIX_SPACE="dev"
log_and_echo "$LABEL" "Using ${BLUEMIX_SPACE} for Bluemix space, please set BLUEMIX_SPACE if on the environment if you wish to change this."
fi
log_and_echo "$LABEL" "Targetting information. Can be updated by setting environment variables"
log_and_echo "$INFO" "BLUEMIX_USER: ${BLUEMIX_USER}"
log_and_echo "$INFO" "BLUEMIX_SPACE: ${BLUEMIX_SPACE}"
log_and_echo "$INFO" "BLUEMIX_ORG: ${BLUEMIX_ORG}"
log_and_echo "$INFO" "BLUEMIX_PASSWORD: xxxxx"
echo ""
log_and_echo "$LABEL" "Logging in to Bluemix using environment properties"
debugme echo "login command: cf login -a ${BLUEMIX_API_HOST} -u ${BLUEMIX_USER} -p XXXXX -o ${BLUEMIX_ORG} -s ${BLUEMIX_SPACE}"
cf login -a ${BLUEMIX_API_HOST} -u ${BLUEMIX_USER} -p ${BLUEMIX_PASSWORD} -o ${BLUEMIX_ORG} -s ${BLUEMIX_SPACE} 2> /dev/null
RESULT=$?
else
# we are already logged in. Simply check via cf command
log_and_echo "$LABEL" "Logging into IBM Container Service using credentials passed from IBM DevOps Services"
cf target >/dev/null 2>/dev/null
RESULT=$?
if [ ! $RESULT -eq 0 ]; then
log_and_echo "$INFO" "cf target did not return successfully. Login failed."
fi
fi
# check login result
if [ $RESULT -eq 1 ]; then
log_and_echo "$ERROR" "Failed to login to IBM Bluemix"
${EXT_DIR}/utilities/sendMessage.sh -l bad -m "Failed to login to IBM Bluemix"
exit $RESULT
else
log_and_echo "$SUCCESSFUL" "Successfully logged into IBM Bluemix"
fi
log_and_echo "$INFO" "BLUEMIX_API_HOST: ${BLUEMIX_API_HOST}"
log_and_echo "$INFO" "BLUEMIX_TARGET: ${BLUEMIX_TARGET}"
########################
# get BLUEMIX_USER #
########################
if [ -z "$BLUEMIX_USER" ]; then
# set targeting information from config.json file
if [ -f ~/.cf/config.json ]; then
debugme echo $(cat ~/.cf/config.json)
get_targeting_info
fi
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
###############
# setup appscan
###############
# appscan has different targets as well for bluemix staging vs prod
if [ -n "$BLUEMIX_TARGET" ]; then
if [ "$BLUEMIX_TARGET" == "staging" ]; then
# staging
export APPSCAN_ENV=https://appscan-test.bluemix.net
#export APPSCAN_DOMAIN=https://appscan-test.bluemix.net
export APPSCAN_OPTS=-DBLUEMIX_SERVER=https://appscan-test.bluemix.net
elif [ "$BLUEMIX_TARGET" == "prod" ]; then
# prod
export APPSCAN_ENV=https://appscan.bluemix.net
#export APPSCAN_DOMAIN=https://appscan.bluemix.net
export APPSCAN_OPTS=-DBLUEMIX_SERVER=https://appscan.bluemix.net
else
# unknown, setup for prod
export APPSCAN_ENV=https://appscan.bluemix.net
#export APPSCAN_DOMAIN=https://appscan.bluemix.net
export APPSCAN_OPTS=-DBLUEMIX_SERVER=https://appscan.bluemix.net
fi
else
# none set, set for prod
export APPSCAN_ENV=https://appscan.bluemix.net
#export APPSCAN_DOMAIN=https://appscan.bluemix.net
export APPSCAN_OPTS=-DBLUEMIX_SERVER=https://appscan.bluemix.net
fi
# fetch the current version of utils
cur_dir=`pwd`
cd ${EXT_DIR}
#CLI is too large for extension, so always download, fail if can't
FORCE_NEWEST_CLI=1
if [[ $FORCE_NEWEST_CLI = 1 ]]; then
wget ${APPSCAN_ENV}/api/BlueMix/StaticAnalyzer/SAClientUtil?os=linux -O SAClientUtil.zip -o /dev/null
unzip -o -qq SAClientUtil.zip
if [ $? -eq 9 ]; then
log_and_echo "$ERROR" "Unable to download SAClient"
exit 1
fi
else
unzip -o -qq SAClientLocal.zip
fi
cd `ls -d SAClient*/`
export APPSCAN_INSTALL_DIR=`pwd`
cd $cur_dir
export PATH=$APPSCAN_INSTALL_DIR/bin:$PATH
export LD_LIBRARY_PATH=$APPSCAN_NSTALL_DIR/bin:$LD_LIBRARY_PATH
debugme appscan.sh version
############################
# setup DRA #
############################
pushd $EXT_DIR >/dev/null
git clone https://github.com/jparra5/dra_utilities.git dra_utilities
popd >/dev/null
# Call common initialization
source $EXT_DIR/dra_utilities/init.sh
log_and_echo "$LABEL" "Initialization complete"