blob: 473eb2921ce2439ce15439925ec6f45621ce8c21 [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
45# Fabric-tofino related vars
46FABRIC_TOFINO_GROUPID=org.opencord
47FABRIC_TOFINO_ARTIFACTID=fabric-tofino
48FABRIC_TOFINO_ARTIFACT=${FABRIC_TOFINO_GROUPID}:${FABRIC_TOFINO_ARTIFACTID}
49FABRIC_TOFINO_TARGETS=(fabric-spgw)
50FABRIC_TOFINO_SDE_DOCKER_IMG=opennetworking/bf-sde:9.0.0-p4c
51FABRIC_TOFINO_P4CFLAGS="-DS1U_SGW_PREFIX='(8w192++8w0++8w0++8w0)' -DS1U_SGW_PREFIX_LEN=8"
52FABRIC_TOFINO_OAR=${FABRIC_TOFINO_ROOT}/target/${FABRIC_TOFINO_ARTIFACTID}-${FABRIC_TOFINO_VERSION}.oar
53
54# UP4 related vars
55UP4_GROUPID=org.omecproject
56UP4_ARTIFACTID=up4-app
57UP4_ARTIFACT=${UP4_GROUPID}:${UP4_ARTIFACTID}
58UP4_TARGETS=_prepare_app_build
59UP4_OAR=${UP4_ROOT}/app/app/target/${UP4_ARTIFACTID}-${UP4_VERSION}.oar
60
61# Kafka-onos related vars
62KAFKA_ONOS_GROUPID=org.opencord
63KAFKA_ONOS_ARTIFACTID=kafka
64KAFKA_ONOS_ARTIFACT=${KAFKA_ONOS_GROUPID}:${KAFKA_ONOS_ARTIFACTID}
65KAFKA_ONOS_OAR=${KAFKA_ONOS_ROOT}/target/${KAFKA_ONOS_ARTIFACTID}-${KAFKA_ONOS_VERSION}.oar
66
67# Fabric-tna related vars
68FABRIC_TNA_GROUPID=org.stratumproject
69FABRIC_TNA_ARTIFACTID=fabric-tna
70FABRIC_TNA_ARTIFACT=${FABRIC_TNA_GROUPID}:${FABRIC_TNA_ARTIFACTID}
pierventre58432d62020-09-03 19:38:54 +020071FABRIC_TNA_TARGETS=(fabric fabric-spgw fabric-int fabric-spgw-int)
pierventre83611422020-08-14 22:53:15 +020072FABRIC_TNA_SDE_DOCKER_IMG=opennetworking/bf-sde:9.2.0-p4c
73FABRIC_TNA_OAR=${FABRIC_TNA_ROOT}/target/${FABRIC_TNA_ARTIFACTID}-${FABRIC_TNA_VERSION}.oar
74
75set -eu -o pipefail
76
77function extract_version {
78 # Enter in the project folder
79 cd "$1" || exit 1
80 # Verify if the VERSION file exists
81 if [ -f "VERSION" ]; then
82 NEW_VERSION=$(head -n1 "VERSION")
83 # If this is a golang project, use funky v-prefixed versions
84 if [ -f "Gopkg.toml" ] || [ -f "go.mod" ]; then
85 PROJECT_VERSION=v${NEW_VERSION}
86 else
87 PROJECT_VERSION=${NEW_VERSION}
88 fi
89 # If this is a node.js project
90 elif [ -f "package.json" ]; then
91 NEW_VERSION=$(python -c 'import json,sys;obj=json.load(sys.stdin); print obj["version"]' < package.json)
92 PROJECT_VERSION=${NEW_VERSION}
93 # If this is a mvn project
94 elif [ -f "pom.xml" ]; then
95 NEW_VERSION=$(xmllint --xpath '/*[local-name()="project"]/*[local-name()="version"]/text()' pom.xml)
96 PROJECT_VERSION=${NEW_VERSION}
97 else
98 echo "ERROR: No versioning file found!"
99 exit 1
100 fi
101 cd ../
102}
103
104# Generic function to build an app
105function build_app {
106 # First step is to remove the oar dir
107 rm -rf "$1"
108 # Settings are needed by both build processes - contains proxy settings and extra
109 cp mvn_settings.xml "$2"
110 # Dependencies are needed only by the mvn copy - contains repo settings
111 cp dependencies.xml "$2"
112 # Mounting the current dir allows to cache the .m2 folder that is persisted and leveraged by subsequent builds
113 docker run "${IT}" --rm -v "${CURRENT_DIR}":/root -w /root/"$3" "${DOCKER_MVN_IMAGE}" \
114 bash -c "mvn dependency:copy -Dartifact=$4:$5:oar \
115 -DoutputDirectory=$6 -Dmdep.useBaseVersion=true \
116 -Dmdep.overWriteReleases=true -Dmdep.overWriteSnapshots=true -f dependencies.xml \
117 -s mvn_settings.xml; \
118 chown -R ${CURRENT_UID}:${CURRENT_GID} /root"
119 # If the oar is not found - try to build using the source code
120 if [ ! -f "$7" ]; then
121 cd "$2" || exit 1
122 # Verify for the last time if the VERSION is a checkout object or a review
123 if ! (git checkout "$5"); then
124 if ! (git fetch "$8" "$5" && git checkout FETCH_HEAD); then
125 exit 1
126 fi
127 fi
128 cd ../
129 # Having the same mount file allows to reduce build time
130 docker run "${IT}" --rm -v "${CURRENT_DIR}":/root -w /root/"$3" "${DOCKER_MVN_IMAGE}" \
131 bash -c "mvn clean install -s mvn_settings.xml; \
132 chown -R ${CURRENT_UID}:${CURRENT_GID} /root"
133 # We need to override the version variable because it is not valid at this point
134 MVN=0
135 fi
136}
137
138function trellis-control-build {
139 # Build function
Charles Chan3533f832020-09-04 21:56:34 -0700140 build_app "${TRELLIS_CONTROL_ROOT}"/app/target \
pierventre83611422020-08-14 22:53:15 +0200141 "${TRELLIS_CONTROL_ROOT}"/ "trellis-control" \
142 "${TRELLIS_CONTROL_ARTIFACT}" "${TRELLIS_CONTROL_VERSION}" \
Charles Chan3533f832020-09-04 21:56:34 -0700143 "app/target" "${TRELLIS_CONTROL_OAR}" "${TRELLIS_CONTROL_REPO}"
pierventre83611422020-08-14 22:53:15 +0200144 # If MVN was not successful - built from sources
145 if [ "$MVN" -eq "0" ]; then
146 # Update VERSION
147 extract_version "${TRELLIS_CONTROL_ROOT}"
148 # Update OAR
Charles Chan3533f832020-09-04 21:56:34 -0700149 TRELLIS_CONTROL_OAR="${TRELLIS_CONTROL_ROOT}"/app/target/"${TRELLIS_CONTROL_ARTIFACTID}"-"${PROJECT_VERSION}".oar
pierventre83611422020-08-14 22:53:15 +0200150 fi
151 # Final step requires to move the oar to the folder used by the tost docker file. Moreover, it will help catch up errors
152 cp "${TRELLIS_CONTROL_OAR}" "${LOCAL_APPS}"/
153}
154
155function trellis-t3-build {
156 build_app "${TRELLIS_T3_ROOT}"/app/target \
157 "${TRELLIS_T3_ROOT}"/ "trellis-t3" \
158 "${TRELLIS_T3_ARTIFACT}" "${TRELLIS_T3_VERSION}" \
159 "app/target" "${TRELLIS_T3_OAR}" "${TRELLIS_T3_REPO}"
160 if [ "$MVN" -eq "0" ]; then
161 extract_version "${TRELLIS_T3_ROOT}"
162 TRELLIS_T3_OAR="${TRELLIS_T3_ROOT}"/app/target/"${TRELLIS_T3_ARTIFACTID}"-"${PROJECT_VERSION}".oar
163 fi
164 cp "${TRELLIS_T3_OAR}" "${LOCAL_APPS}"/
165}
166
167function fabric-tofino-build {
168 # This workaround is temporary - typically we need to build only the pipeconf
169 cd "${FABRIC_TOFINO_ROOT}" || exit 1 && make "${FABRIC_TOFINO_TARGETS[@]}" SDE_DOCKER_IMG="${FABRIC_TOFINO_SDE_DOCKER_IMG}" P4CFLAGS="${FABRIC_TOFINO_P4CFLAGS}"
170 cd ../
171 build_app "${FABRIC_TOFINO_ROOT}"/target \
172 "${FABRIC_TOFINO_ROOT}"/ "fabric-tofino" \
173 "${FABRIC_TOFINO_ARTIFACT}" "${FABRIC_TOFINO_VERSION}" \
174 "target" "${FABRIC_TOFINO_OAR}" "${FABRIC_TOFINO_REPO}"
175 if [ "$MVN" -eq "0" ]; then
176 extract_version "${FABRIC_TOFINO_ROOT}"
177 FABRIC_TOFINO_OAR="${FABRIC_TOFINO_ROOT}"/target/"${FABRIC_TOFINO_ARTIFACTID}"-"${PROJECT_VERSION}".oar
178 fi
179 cp "${FABRIC_TOFINO_OAR}" "${LOCAL_APPS}"/
pierventree8963832020-09-11 16:50:27 +0200180 # Extra step to avoid build failure; clean up artifacts to ensure
181 # the release process won't complain about uncommitted changes.
182 cd "${FABRIC_TOFINO_ROOT}" || exit 1 && git checkout .
183 cd ../
pierventre83611422020-08-14 22:53:15 +0200184}
185
186function up4-build {
187 # Prepares app folder
188 cd "${UP4_ROOT}" || exit 1 && make "${UP4_TARGETS}"
189 cd ../
190 build_app "${UP4_ROOT}"/app/app/target \
191 "${UP4_ROOT}"/app "up4/app" \
192 "${UP4_ARTIFACT}" "${UP4_VERSION}" \
193 "app/app/target" "${UP4_OAR}" "${UP4_REPO}"
194 if [ "$MVN" -eq "0" ]; then
195 extract_version "${UP4_ROOT}"/app
196 cd ../
197 UP4_OAR="${UP4_ROOT}"/app/app/target/"${UP4_ARTIFACTID}"-"${PROJECT_VERSION}".oar
198 fi
199 cp "${UP4_OAR}" "${LOCAL_APPS}"/
200}
201
202function kafka-onos-build {
203 build_app "${KAFKA_ONOS_ROOT}"/target \
204 "${KAFKA_ONOS_ROOT}"/ "kafka-onos" \
205 "${KAFKA_ONOS_ARTIFACT}" "${KAFKA_ONOS_VERSION}" \
206 "target" "${KAFKA_ONOS_OAR}" "${KAFKA_ONOS_REPO}"
207 if [ "$MVN" -eq "0" ]; then
208 extract_version "${KAFKA_ONOS_ROOT}"
209 KAFKA_ONOS_OAR="${KAFKA_ONOS_ROOT}"/target/"${KAFKA_ONOS_ARTIFACTID}"-"${PROJECT_VERSION}".oar
210 fi
211 cp "${KAFKA_ONOS_OAR}" "${LOCAL_APPS}"/
212}
213
214function fabric-tna-build {
215 # This workaround is temporary - typically we need to build only the pipeconf
216 cd "${FABRIC_TNA_ROOT}" || exit 1 && make "${FABRIC_TNA_TARGETS[@]}" SDE_DOCKER_IMG="${FABRIC_TNA_SDE_DOCKER_IMG}"
217 cd ../
218 build_app "${FABRIC_TNA_ROOT}"/target \
219 "${FABRIC_TNA_ROOT}"/ "fabric-tna" \
220 "${FABRIC_TNA_ARTIFACT}" "${FABRIC_TNA_VERSION}" \
221 "target" "${FABRIC_TNA_OAR}" "${FABRIC_TNA_REPO}"
222 if [ "$MVN" -eq "0" ]; then
223 extract_version "${FABRIC_TNA_ROOT}"
224 FABRIC_TNA_OAR="${FABRIC_TNA_ROOT}"/target/"${FABRIC_TNA_ARTIFACTID}"-"${PROJECT_VERSION}".oar
225 fi
226 cp "${FABRIC_TNA_OAR}" "${LOCAL_APPS}"/
227}
228
229"$1"