blob: 567850dc63fc49649fe2d463d02dc1e7fd07d1e1 [file] [log] [blame]
Andrew Gauldb34cbd02017-08-24 16:46:17 -04001#!/bin/bash
Lusheng Jic38ca2f2017-09-11 20:11:29 +00002
3# ================================================================================
Andrew Gauldb34cbd02017-08-24 16:46:17 -04004# Copyright (c) 2017 AT&T Intellectual Property. All rights reserved.
Lusheng Jic38ca2f2017-09-11 20:11:29 +00005# ================================================================================
Andrew Gauldb34cbd02017-08-24 16:46:17 -04006# Licensed under the Apache License, Version 2.0 (the "License");
7# you may not use this file except in compliance with the License.
8# You may obtain a copy of the License at
9#
Lusheng Jic38ca2f2017-09-11 20:11:29 +000010# http://www.apache.org/licenses/LICENSE-2.0
Andrew Gauldb34cbd02017-08-24 16:46:17 -040011#
12# Unless required by applicable law or agreed to in writing, software
13# distributed under the License is distributed on an "AS IS" BASIS,
14# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15# See the License for the specific language governing permissions and
16# limitations under the License.
Lusheng Jic38ca2f2017-09-11 20:11:29 +000017# ============LICENSE_END=========================================================
18#
19# ECOMP is a trademark and service mark of AT&T Intellectual Property.
20
21set -ex
22
Andrew Gauldb34cbd02017-08-24 16:46:17 -040023
24echo "running script: [$0] for module [$1] at stage [$2]"
25
Lusheng Jic38ca2f2017-09-11 20:11:29 +000026MVN_PROJECT_MODULEID="$1"
27MVN_PHASE="$2"
Andrew Gauldb34cbd02017-08-24 16:46:17 -040028
Lusheng Jic38ca2f2017-09-11 20:11:29 +000029
30FQDN="${MVN_PROJECT_GROUPID}.${MVN_PROJECT_ARTIFACTID}"
31if [ "$MVN_PROJECT_MODULEID" == "__" ]; then
32 MVN_PROJECT_MODULEID=""
33fi
34
35if [[ "$MVN_PROJECT_VERSION" == *SNAPSHOT ]]; then
36 echo "=> for SNAPSHOT artifact build"
37 MVN_DEPLOYMENT_TYPE='SNAPSHOT'
38else
39 echo "=> for STAGING/RELEASE artifact build"
40 MVN_DEPLOYMENT_TYPE='STAGING'
41fi
42echo "MVN_DEPLOYMENT_TYPE is [$MVN_DEPLOYMENT_TYPE]"
43
Andrew Gauldb34cbd02017-08-24 16:46:17 -040044
45TIMESTAMP=$(date +%C%y%m%dT%H%M%S)
Andrew Gauldb34cbd02017-08-24 16:46:17 -040046
47# expected environment variables
48if [ -z "${MVN_NEXUSPROXY}" ]; then
49 echo "MVN_NEXUSPROXY environment variable not set. Cannot proceed"
50 exit
51fi
Lusheng Jic38ca2f2017-09-11 20:11:29 +000052MVN_NEXUSPROXY_HOST=$(echo "$MVN_NEXUSPROXY" |cut -f3 -d'/' | cut -f1 -d':')
53echo "=> Nexus Proxy at $MVN_NEXUSPROXY_HOST, $MVN_NEXUSPROXY"
Andrew Gauldb34cbd02017-08-24 16:46:17 -040054
Lusheng Jic38ca2f2017-09-11 20:11:29 +000055if [ -z "$WORKSPACE" ]; then
56 WORKSPACE=$(pwd)
57fi
Andrew Gauldb34cbd02017-08-24 16:46:17 -040058
Lusheng Jic38ca2f2017-09-11 20:11:29 +000059if [ -z "$SETTINGS_FILE" ]; then
60 echo "SETTINGS_FILE environment variable not set. Cannot proceed"
61 exit
62fi
63
64
Andrew Gauldb34cbd02017-08-24 16:46:17 -040065
66# mvn phase in life cycle
67MVN_PHASE="$2"
68
Lusheng Jic38ca2f2017-09-11 20:11:29 +000069
70echo "MVN_PROJECT_MODULEID is [$MVN_PROJECT_MODULEID]"
71echo "MVN_PHASE is [$MVN_PHASE]"
72echo "MVN_PROJECT_GROUPID is [$MVN_PROJECT_GROUPID]"
73echo "MVN_PROJECT_ARTIFACTID is [$MVN_PROJECT_ARTIFACTID]"
74echo "MVN_PROJECT_VERSION is [$MVN_PROJECT_VERSION]"
75echo "MVN_NEXUSPROXY is [$MVN_NEXUSPROXY]"
76echo "MVN_RAWREPO_BASEURL_UPLOAD is [$MVN_RAWREPO_BASEURL_UPLOAD]"
77echo "MVN_RAWREPO_BASEURL_DOWNLOAD is [$MVN_RAWREPO_BASEURL_DOWNLOAD]"
78MVN_RAWREPO_HOST=$(echo "$MVN_RAWREPO_BASEURL_UPLOAD" | cut -f3 -d'/' |cut -f1 -d':')
79echo "MVN_RAWREPO_HOST is [$MVN_RAWREPO_HOST]"
80echo "MVN_RAWREPO_SERVERID is [$MVN_RAWREPO_SERVERID]"
81echo "MVN_DOCKERREGISTRY_DAILY is [$MVN_DOCKERREGISTRY_DAILY]"
82echo "MVN_DOCKERREGISTRY_RELEASE is [$MVN_DOCKERREGISTRY_RELEASE]"
83
84clean_templated_files()
85{
86 TEMPLATE_FILES=$(find . -name "*-template")
87 for F in $TEMPLATE_FILES; do
88 F2=$(echo "$F" | sed 's/-template$//')
89 rm -f "$F2"
90 done
91}
92
93
94expand_templates()
95{
96 # set up env variables, get ready for template resolution
97 # NOTE: CCSDK artifacts do not distinguish REALESE vs SNAPSHOTs
98 export ONAPTEMPLATE_RAWREPOURL_org_onap_ccsdk_platform_plugins_releases="$MVN_RAWREPO_BASEURL_DOWNLOAD/org.onap.ccsdk.platform.plugins"
99 export ONAPTEMPLATE_RAWREPOURL_org_onap_ccsdk_platform_plugins_snapshots="$MVN_RAWREPO_BASEURL_DOWNLOAD/org.onap.ccsdk.platform.plugins"
100 export ONAPTEMPLATE_RAWREPOURL_org_onap_ccsdk_platform_blueprints_releases="$MVN_RAWREPO_BASEURL_DOWNLOAD/org.onap.ccsdk.platform.blueprints"
101 export ONAPTEMPLATE_RAWREPOURL_org_onap_ccsdk_platform_blueprints_snapshots="$MVN_RAWREPO_BASEURL_DOWNLOAD/org.onap.ccsdk.platform.blueprints"
102
103 export ONAPTEMPLATE_RAWREPOURL_org_onap_dcaegen2_releases="$MVN_RAWREPO_BASEURL_DOWNLOAD/org.onap.dcaegen2/releases"
104 export ONAPTEMPLATE_RAWREPOURL_org_onap_dcaegen2_snapshots="$MVN_RAWREPO_BASEURL_DOWNLOAD/org.onap.dcaegen2/snapshots"
105 export ONAPTEMPLATE_RAWREPOURL_org_onap_dcaegen2_platform_plugins_releases="$MVN_RAWREPO_BASEURL_DOWNLOAD/org.onap.dcaegen2.platform.plugins/releases"
106 export ONAPTEMPLATE_RAWREPOURL_org_onap_dcaegen2_platform_plugins_snapshots="$MVN_RAWREPO_BASEURL_DOWNLOAD/org.onap.dcaegen2.platform.plugins/snapshots"
107 export ONAPTEMPLATE_RAWREPOURL_org_onap_dcaegen2_platform_blueprints_releases="$MVN_RAWREPO_BASEURL_DOWNLOAD/org.onap.dcaegen2.platform.blueprints/releases"
108 export ONAPTEMPLATE_RAWREPOURL_org_onap_dcaegen2_platform_blueprints_snapshots="$MVN_RAWREPO_BASEURL_DOWNLOAD/org.onap.dcaegen2.platform.blueprints/snapshots"
109
110 export ONAPTEMPLATE_PYPIURL_org_onap_dcaegen2="${MVN_NEXUSPROXY}/content/sites/pypi"
111
112 export ONAPTEMPLATE_DOCKERREGURL_org_onap_dcaegen2_releases="$MVN_DOCKERREGISTRY_DAILY"
113 export ONAPTEMPLATE_DOCKERREGURL_org_onap_dcaegen2_snapshots="$MVN_DOCKERREGISTRY_DAILY/snapshots"
114
115
116 TEMPLATE_FILES=$(find . -name "*-template")
117 for F in $TEMPLATE_FILES; do
118 F2=$(echo "$F" | sed 's/-template$//')
119 cp "$F" "$F2"
120 MOD=$(stat --format '%a' "$F")
121 chmod "$MOD" "$F2"
122 done
123
124
125 TEMPLATES=$(env |grep ONAPTEMPLATE)
126 if [ -z "$TEMPLATES" ]; then
127 return 0
128 fi
129
130 echo "====> Resolving the following temaplate from environment variables "
131 echo "[$TEMPLATES]"
132 SELFFILE=$(echo "$0" | rev | cut -f1 -d '/' | rev)
133 for TEMPLATE in $TEMPLATES; do
134 KEY=$(echo "$TEMPLATE" | cut -f1 -d'=')
135 VALUE=$(echo "$TEMPLATE" | cut -f2 -d'=')
136 VALUE2=$(echo "$TEMPLATE" | cut -f2 -d'=' |sed 's/\//\\\//g')
137 FILES=$(grep -rlv "$KEY")
138
139 if [ -z "$FILES" ]; then
140 continue
141 fi
142
143 # assuming FILES is not longer than 2M bytes, the limit for variable value max size on this VM
144 for F in $FILES; do
145 if [[ $F == *"$SELFFILE" ]]; then
146 continue
147 fi
148 if [[ "$F" == *-template ]]; then
149 continue
150 fi
151
152 echo "======> Resolving template $KEY to value $VALUE for file $F"
153 sed -i "s/{{[[:space:]]*$KEY[[:space:]]*}}/$VALUE2/g" "$F"
154 #cat "$F"
155 done
156
157 #if [ ! -z "$FILES" ]; then
158 # echo "====> Resolving template $VALUE to value $VALUE"
159 # #CMD="grep -rl \"$VALUE\" | tr '\n' '\0' | xargs -0 sed -i \"s/{{[[:space:]]*$VALUE[[:space:]]*}}/$VALUE/g\""
160 # grep -rl "$KEY" | tr '\n' '\0' | xargs -0 sed -i 's/$KEY/$VALUE2/g'
161 # #echo $CMD
162 # #eval $CMD
163 #fi
164 done
165 echo "====> Done template reolving"
166}
167
168
169run_tox_test()
170{
171 set -x
172 CURDIR=$(pwd)
173 TOXINIS=$(find . -name "tox.ini")
174 for TOXINI in "${TOXINIS[@]}"; do
175 DIR=$(echo "$TOXINI" | rev | cut -f2- -d'/' | rev)
176 cd "${CURDIR}/${DIR}"
177 rm -rf ./venv-tox ./.tox
178 virtualenv ./venv-tox
179 source ./venv-tox/bin/activate
180 pip install --upgrade pip
181 pip install --upgrade tox argparse
182 pip freeze
183 tox
184 deactivate
185 rm -rf ./venv-tox ./.tox
186 done
187}
188
189build_wagons()
190{
191 rm -rf ./*.wgn venv-pkg
192
193 SETUPFILES=$(find . -name "setup.py")
194 for SETUPFILE in $SETUPFILES; do
195 PLUGIN_DIR=$(echo "$SETUPFILE" |rev | cut -f 2- -d '/' |rev)
196 PLUGIN_NAME=$(grep 'name' "$SETUPFILE" | cut -f2 -d'=' | sed 's/[^0-9a-zA-Z\.]*//g')
197 PLUGIN_VERSION=$(grep 'version' "$SETUPFILE" | cut -f2 -d'=' | sed 's/[^0-9\.]*//g')
198
199 echo "In $PLUGIN_DIR, $PLUGIN_NAME, $PLUGIN_VERSION"
200
201 virtualenv ./venv-pkg
202 source ./venv-pkg/bin/activate
203 pip install --upgrade pip
204 pip install wagon
205 wagon create --format tar.gz "$PLUGIN_DIR"
206 deactivate
207 rm -rf venv-pkg
208
209 PKG_FILE_NAMES=( "${PLUGIN_NAME}-${PLUGIN_VERSION}"*.wgn )
210 echo Built package: "${PKG_FILE_NAMES[@]}"
211 done
212}
213
214
215upload_raw_file()
216{
217 # Extract the username and password to the nexus repo from the settings file
218 USER=$(xpath -q -e "//servers/server[id='$MVN_RAWREPO_SERVERID']/username/text()" "$SETTINGS_FILE")
219 PASS=$(xpath -q -e "//servers/server[id='$MVN_RAWREPO_SERVERID']/password/text()" "$SETTINGS_FILE")
220 NETRC=$(mktemp)
221 echo "machine $MVN_RAWREPO_HOST login $USER password $PASS" > "$NETRC"
222
223 REPO="$MVN_RAWREPO_BASEURL_UPLOAD"
224
225 OUTPUT_FILE=$1
226 EXT=$(echo "$OUTPUT_FILE" | rev |cut -f1 -d '.' |rev)
227 if [ "$EXT" == 'yaml' ]; then
228 OUTPUT_FILE_TYPE='text/x-yaml'
229 elif [ "$EXT" == 'sh' ]; then
230 OUTPUT_FILE_TYPE='text/x-shellscript'
231 elif [ "$EXT" == 'gz' ]; then
232 OUTPUT_FILE_TYPE='application/gzip'
233 elif [ "$EXT" == 'wgn' ]; then
234 OUTPUT_FILE_TYPE='application/gzip'
235 else
236 OUTPUT_FILE_TYPE='application/octet-stream'
237 fi
238
239
240 if [ "$MVN_DEPLOYMENT_TYPE" == 'SNAPSHOT' ]; then
241 SEND_TO="${REPO}/${FQDN}/snapshots"
242 elif [ "$MVN_DEPLOYMENT_TYPE" == 'STAGING' ]; then
243 SEND_TO="${REPO}/${FQDN}/releases"
244 else
245 echo "Unreconfnized deployment type, quit"
246 exit
247 fi
248 if [ ! -z "$MVN_PROJECT_MODULEID" ]; then
249 SEND_TO="$SEND_TO/$MVN_PROJECT_MODULEID"
250 fi
251
252 echo "Sending ${OUTPUT_FILE} to Nexus: ${SEND_TO}"
253 curl -vkn --netrc-file "${NETRC}" --upload-file "${OUTPUT_FILE}" -X PUT -H "Content-Type: $OUTPUT_FILE_TYPE" "${SEND_TO}/${OUTPUT_FILE}-${MVN_PROJECT_VERSION}-${TIMESTAMP}"
254 curl -vkn --netrc-file "${NETRC}" --upload-file "${OUTPUT_FILE}" -X PUT -H "Content-Type: $OUTPUT_FILE_TYPE" "${SEND_TO}/${OUTPUT_FILE}-${MVN_PROJECT_VERSION}"
255 curl -vkn --netrc-file "${NETRC}" --upload-file "${OUTPUT_FILE}" -X PUT -H "Content-Type: $OUTPUT_FILE_TYPE" "${SEND_TO}/${OUTPUT_FILE}"
256}
257
258
259
260upload_wagons_and_type_yamls()
261{
262 WAGONS=$(ls -1 ./*.wgn)
263 for WAGON in $WAGONS ; do
264 WAGON_NAME=$(echo "$WAGON" | cut -f1 -d '-')
265 WAGON_VERSION=$(echo "$WAGON" | cut -f2 -d '-')
266 WAGON_TYPEFILE=$(grep -rl "$WAGON_NAME" | grep yaml | head -1)
267
268 upload_raw_file "$WAGON"
269 upload_raw_file "$WAGON_TYPEFILE"
270 done
271}
272
273upload_files_of_extension()
274{
275 FILES=$(ls -1 ./*."$1")
276 for F in $FILES ; do
277 upload_raw_file "$F"
278 done
279}
280
281
282
283build_and_push_docker()
284{
285 IMAGENAME="onap/${FQDN}.${MVN_PROJECT_MODULEID}"
286 IMAGENAME=$(echo "$IMAGENAME" | sed -e 's/_*$//g' -e 's/\.*$//g')
287
288 # use the major and minor version of the MVN artifact version as docker image version
289 VERSION="${MVN_PROJECT_VERSION//[^0-9.]/}"
290 VERSION2=$(echo "$VERSION" | cut -f1-2 -d'.')
291
292 LFQI="${IMAGENAME}:${VERSION}-${TIMESTAMP}"
293 BUILD_PATH="${WORKSPACE}"
294 # build a docker image
295 docker build --rm -f "${WORKSPACE}"/Dockerfile -t "${LFQI}" "${BUILD_PATH}"
296
297 REPO=""
298 if [ $MVN_DEPLOYMENT_TYPE == "SNAPSHOT" ]; then
299 REPO=$MVN_DOCKERREGISTRY_DAILY
300 elif [ $MVN_DEPLOYMENT_TYPE == "STAGING" ]; then
301 # there seems to be no staging docker registry? set to use SNAPSHOT also
302 #REPO=$MVN_DOCKERREGISTRY_RELEASE
303 REPO=$MVN_DOCKERREGISTRY_DAILY
304 else
305 echo "Fail to determine DEPLOYMENT_TYPE"
306 REPO=$MVN_DOCKERREGISTRY_DAILY
307 fi
308 echo "DEPLOYMENT_TYPE is: $MVN_DEPLOYMENT_TYPE, repo is $REPO"
309
310 if [ ! -z "$REPO" ]; then
311 USER=$(xpath -e "//servers/server[id='$REPO']/username/text()" "$SETTINGS_FILE")
312 PASS=$(xpath -e "//servers/server[id='$REPO']/password/text()" "$SETTINGS_FILE")
313 if [ -z "$USER" ]; then
314 echo "Error: no user provided"
315 fi
316 if [ -z "$PASS" ]; then
317 echo "Error: no password provided"
318 fi
319 [ -z "$PASS" ] && PASS_PROVIDED="<empty>" || PASS_PROVIDED="<password>"
320 echo docker login "$REPO" -u "$USER" -p "$PASS_PROVIDED"
321 docker login "$REPO" -u "$USER" -p "$PASS"
322
323 if [ $MVN_DEPLOYMENT_TYPE == "SNAPSHOT" ]; then
324 REPO="$REPO/snapshots"
325 elif [ $MVN_DEPLOYMENT_TYPE == "STAGING" ]; then
326 # there seems to be no staging docker registry? set to use SNAPSHOT also
327 #REPO=$MVN_DOCKERREGISTRY_RELEASE
328 REPO="$REPO"
329 else
330 echo "Fail to determine DEPLOYMENT_TYPE"
331 REPO="$REPO/unknown"
332 fi
333
334 OLDTAG="${LFQI}"
335 PUSHTAGS="${REPO}/${IMAGENAME}:${VERSION2}-${TIMESTAMP} ${REPO}/${IMAGENAME}:${VERSION2} ${REPO}/${IMAGENAME}:${VERSION2}-latest"
336 for NEWTAG in ${PUSHTAGS}
337 do
338 echo "tagging ${OLDTAG} to ${NEWTAG}"
339 docker tag "${OLDTAG}" "${NEWTAG}"
340 echo "pushing ${NEWTAG}"
341 docker push "${NEWTAG}"
342 OLDTAG="${NEWTAG}"
343 done
344 fi
345
346}
347
348
349
350# Customize the section below for each project
Andrew Gauldb34cbd02017-08-24 16:46:17 -0400351case $MVN_PHASE in
352clean)
353 echo "==> clean phase script"
Lusheng Jic38ca2f2017-09-11 20:11:29 +0000354 clean_templated_files
355 rm -rf ./venv-* ./*.wgn
Andrew Gauldb34cbd02017-08-24 16:46:17 -0400356 ;;
357generate-sources)
358 echo "==> generate-sources phase script"
Lusheng Jic38ca2f2017-09-11 20:11:29 +0000359 expand_templates
Andrew Gauldb34cbd02017-08-24 16:46:17 -0400360 ;;
361compile)
362 echo "==> compile phase script"
Andrew Gauldb34cbd02017-08-24 16:46:17 -0400363 ;;
364test)
365 echo "==> test phase script"
Andrew Gauldb34cbd02017-08-24 16:46:17 -0400366 ;;
367package)
368 echo "==> package phase script"
Andrew Gauldb34cbd02017-08-24 16:46:17 -0400369 ;;
370install)
371 echo "==> install phase script"
Andrew Gauldb34cbd02017-08-24 16:46:17 -0400372 ;;
373deploy)
374 echo "==> deploy phase script"
Lusheng Jic38ca2f2017-09-11 20:11:29 +0000375 case $MVN_PROJECT_MODULEID in
376 bootstrap)
377 build_and_push_docker
378 ;;
379 scripts)
380 upload_files_of_extension sh
381 ;;
382 *)
383 echo "====> unknown mvn project module"
384 ;;
385 esac
Andrew Gauldb34cbd02017-08-24 16:46:17 -0400386 ;;
387*)
388 echo "==> unprocessed phase"
389 ;;
390esac
391