blob: a94146ff9ebf0fbc42da67c2ccf4830f2fef2197 [file] [log] [blame]
pierventre83611422020-08-14 22:53:15 +02001#!/bin/bash
2
3# Copyright 2020-present Open Networking Foundation
4#
5# Licensed under the Apache License, Version 2.0 (the "License");
6# you may not use this file except in compliance with the License.
7# You may obtain a copy of the License at
8#
9# http://www.apache.org/licenses/LICENSE-2.0
10#
11# Unless required by applicable law or agreed to in writing, software
12# distributed under the License is distributed on an "AS IS" BASIS,
13# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14# See the License for the specific language governing permissions and
15# limitations under the License.
16
17# Build the apps
18
19# General purposes vars
20CURRENT_UID=$(id -u)
21CURRENT_GID=$(id -g)
22CURRENT_DIR=$(pwd)
23MVN=1
24PROJECT_VERSION=0
25
26# Docker related vars
27DOCKER_MVN_TAG=3.6.3-openjdk-11-slim
28DOCKER_MVN_IMAGE=maven:${DOCKER_MVN_TAG}
29
30# Do not attach stdin if running in an environment without it (e.g., Jenkins)
31IT=$(test -t 0 && echo "-it" || echo "-t")
32
33# Trellis-control related vars
34TRELLIS_CONTROL_GROUPID=org.onosproject
Charles Chan3533f832020-09-04 21:56:34 -070035TRELLIS_CONTROL_ARTIFACTID=segmentrouting-app
pierventre83611422020-08-14 22:53:15 +020036TRELLIS_CONTROL_ARTIFACT=${TRELLIS_CONTROL_GROUPID}:${TRELLIS_CONTROL_ARTIFACTID}
Charles Chan3533f832020-09-04 21:56:34 -070037TRELLIS_CONTROL_OAR=${TRELLIS_CONTROL_ROOT}/app/target/${TRELLIS_CONTROL_ARTIFACTID}-${TRELLIS_CONTROL_VERSION}.oar
pierventre83611422020-08-14 22:53:15 +020038
39# Trellis-t3 related vars
40TRELLIS_T3_GROUPID=org.onosproject
41TRELLIS_T3_ARTIFACTID=t3-app
42TRELLIS_T3_ARTIFACT=${TRELLIS_T3_GROUPID}:${TRELLIS_T3_ARTIFACTID}
43TRELLIS_T3_OAR=${TRELLIS_T3_ROOT}/app/target/${TRELLIS_T3_ARTIFACTID}-${TRELLIS_T3_VERSION}.oar
44
pierventre83611422020-08-14 22:53:15 +020045# UP4 related vars
46UP4_GROUPID=org.omecproject
47UP4_ARTIFACTID=up4-app
48UP4_ARTIFACT=${UP4_GROUPID}:${UP4_ARTIFACTID}
49UP4_TARGETS=_prepare_app_build
50UP4_OAR=${UP4_ROOT}/app/app/target/${UP4_ARTIFACTID}-${UP4_VERSION}.oar
51
pierventre83611422020-08-14 22:53:15 +020052# Fabric-tna related vars
53FABRIC_TNA_GROUPID=org.stratumproject
54FABRIC_TNA_ARTIFACTID=fabric-tna
55FABRIC_TNA_ARTIFACT=${FABRIC_TNA_GROUPID}:${FABRIC_TNA_ARTIFACTID}
pierventre58432d62020-09-03 19:38:54 +020056FABRIC_TNA_TARGETS=(fabric fabric-spgw fabric-int fabric-spgw-int)
pierventre83611422020-08-14 22:53:15 +020057FABRIC_TNA_OAR=${FABRIC_TNA_ROOT}/target/${FABRIC_TNA_ARTIFACTID}-${FABRIC_TNA_VERSION}.oar
58
59set -eu -o pipefail
60
61function extract_version {
62 # Enter in the project folder
63 cd "$1" || exit 1
64 # Verify if the VERSION file exists
65 if [ -f "VERSION" ]; then
66 NEW_VERSION=$(head -n1 "VERSION")
67 # If this is a golang project, use funky v-prefixed versions
68 if [ -f "Gopkg.toml" ] || [ -f "go.mod" ]; then
69 PROJECT_VERSION=v${NEW_VERSION}
70 else
71 PROJECT_VERSION=${NEW_VERSION}
72 fi
73 # If this is a node.js project
74 elif [ -f "package.json" ]; then
75 NEW_VERSION=$(python -c 'import json,sys;obj=json.load(sys.stdin); print obj["version"]' < package.json)
76 PROJECT_VERSION=${NEW_VERSION}
77 # If this is a mvn project
78 elif [ -f "pom.xml" ]; then
79 NEW_VERSION=$(xmllint --xpath '/*[local-name()="project"]/*[local-name()="version"]/text()' pom.xml)
80 PROJECT_VERSION=${NEW_VERSION}
81 else
82 echo "ERROR: No versioning file found!"
83 exit 1
84 fi
85 cd ../
86}
87
88# Generic function to build an app
89function build_app {
90 # First step is to remove the oar dir
91 rm -rf "$1"
92 # Settings are needed by both build processes - contains proxy settings and extra
93 cp mvn_settings.xml "$2"
94 # Dependencies are needed only by the mvn copy - contains repo settings
95 cp dependencies.xml "$2"
96 # Mounting the current dir allows to cache the .m2 folder that is persisted and leveraged by subsequent builds
97 docker run "${IT}" --rm -v "${CURRENT_DIR}":/root -w /root/"$3" "${DOCKER_MVN_IMAGE}" \
98 bash -c "mvn dependency:copy -Dartifact=$4:$5:oar \
99 -DoutputDirectory=$6 -Dmdep.useBaseVersion=true \
100 -Dmdep.overWriteReleases=true -Dmdep.overWriteSnapshots=true -f dependencies.xml \
101 -s mvn_settings.xml; \
102 chown -R ${CURRENT_UID}:${CURRENT_GID} /root"
103 # If the oar is not found - try to build using the source code
104 if [ ! -f "$7" ]; then
105 cd "$2" || exit 1
106 # Verify for the last time if the VERSION is a checkout object or a review
pierventre3b5f9f12020-10-22 16:24:34 +0200107 if ! (git checkout "origin/$5" || git checkout "$5"); then
pierventre83611422020-08-14 22:53:15 +0200108 if ! (git fetch "$8" "$5" && git checkout FETCH_HEAD); then
109 exit 1
110 fi
111 fi
112 cd ../
113 # Having the same mount file allows to reduce build time
114 docker run "${IT}" --rm -v "${CURRENT_DIR}":/root -w /root/"$3" "${DOCKER_MVN_IMAGE}" \
115 bash -c "mvn clean install -s mvn_settings.xml; \
116 chown -R ${CURRENT_UID}:${CURRENT_GID} /root"
117 # We need to override the version variable because it is not valid at this point
118 MVN=0
119 fi
120}
121
122function trellis-control-build {
123 # Build function
Charles Chan3533f832020-09-04 21:56:34 -0700124 build_app "${TRELLIS_CONTROL_ROOT}"/app/target \
pierventre83611422020-08-14 22:53:15 +0200125 "${TRELLIS_CONTROL_ROOT}"/ "trellis-control" \
126 "${TRELLIS_CONTROL_ARTIFACT}" "${TRELLIS_CONTROL_VERSION}" \
Charles Chan3533f832020-09-04 21:56:34 -0700127 "app/target" "${TRELLIS_CONTROL_OAR}" "${TRELLIS_CONTROL_REPO}"
pierventre83611422020-08-14 22:53:15 +0200128 # If MVN was not successful - built from sources
129 if [ "$MVN" -eq "0" ]; then
130 # Update VERSION
131 extract_version "${TRELLIS_CONTROL_ROOT}"
132 # Update OAR
Charles Chan3533f832020-09-04 21:56:34 -0700133 TRELLIS_CONTROL_OAR="${TRELLIS_CONTROL_ROOT}"/app/target/"${TRELLIS_CONTROL_ARTIFACTID}"-"${PROJECT_VERSION}".oar
pierventre83611422020-08-14 22:53:15 +0200134 fi
135 # Final step requires to move the oar to the folder used by the tost docker file. Moreover, it will help catch up errors
136 cp "${TRELLIS_CONTROL_OAR}" "${LOCAL_APPS}"/
137}
138
139function trellis-t3-build {
140 build_app "${TRELLIS_T3_ROOT}"/app/target \
141 "${TRELLIS_T3_ROOT}"/ "trellis-t3" \
142 "${TRELLIS_T3_ARTIFACT}" "${TRELLIS_T3_VERSION}" \
143 "app/target" "${TRELLIS_T3_OAR}" "${TRELLIS_T3_REPO}"
144 if [ "$MVN" -eq "0" ]; then
145 extract_version "${TRELLIS_T3_ROOT}"
146 TRELLIS_T3_OAR="${TRELLIS_T3_ROOT}"/app/target/"${TRELLIS_T3_ARTIFACTID}"-"${PROJECT_VERSION}".oar
147 fi
148 cp "${TRELLIS_T3_OAR}" "${LOCAL_APPS}"/
149}
150
pierventre83611422020-08-14 22:53:15 +0200151function up4-build {
152 # Prepares app folder
153 cd "${UP4_ROOT}" || exit 1 && make "${UP4_TARGETS}"
154 cd ../
155 build_app "${UP4_ROOT}"/app/app/target \
156 "${UP4_ROOT}"/app "up4/app" \
157 "${UP4_ARTIFACT}" "${UP4_VERSION}" \
158 "app/app/target" "${UP4_OAR}" "${UP4_REPO}"
159 if [ "$MVN" -eq "0" ]; then
160 extract_version "${UP4_ROOT}"/app
161 cd ../
162 UP4_OAR="${UP4_ROOT}"/app/app/target/"${UP4_ARTIFACTID}"-"${PROJECT_VERSION}".oar
163 fi
164 cp "${UP4_OAR}" "${LOCAL_APPS}"/
165}
166
pierventre83611422020-08-14 22:53:15 +0200167function fabric-tna-build {
168 # This workaround is temporary - typically we need to build only the pipeconf
Carmelo Casconeec24e192021-02-23 15:52:25 -0800169 cd "${FABRIC_TNA_ROOT}" || exit 1 && make "${FABRIC_TNA_TARGETS[@]}"
pierventre83611422020-08-14 22:53:15 +0200170 cd ../
171 build_app "${FABRIC_TNA_ROOT}"/target \
172 "${FABRIC_TNA_ROOT}"/ "fabric-tna" \
173 "${FABRIC_TNA_ARTIFACT}" "${FABRIC_TNA_VERSION}" \
174 "target" "${FABRIC_TNA_OAR}" "${FABRIC_TNA_REPO}"
175 if [ "$MVN" -eq "0" ]; then
176 extract_version "${FABRIC_TNA_ROOT}"
177 FABRIC_TNA_OAR="${FABRIC_TNA_ROOT}"/target/"${FABRIC_TNA_ARTIFACTID}"-"${PROJECT_VERSION}".oar
178 fi
179 cp "${FABRIC_TNA_OAR}" "${LOCAL_APPS}"/
180}
181
182"$1"