blob: f955fbba727a5dc58f3e88adf383534dae48d17a [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}
Carmelo Cascone44f3d252022-02-04 13:38:17 -080056FABRIC_TNA_TARGETS=all
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
Daniele Morof969c742022-01-03 22:01:00 +0100130 # When building from source api and app jars are automatically put into the local .m2 folder.
pierventre83611422020-08-14 22:53:15 +0200131 # Update VERSION
132 extract_version "${TRELLIS_CONTROL_ROOT}"
133 # Update OAR
Charles Chan3533f832020-09-04 21:56:34 -0700134 TRELLIS_CONTROL_OAR="${TRELLIS_CONTROL_ROOT}"/app/target/"${TRELLIS_CONTROL_ARTIFACTID}"-"${PROJECT_VERSION}".oar
Daniele Morof969c742022-01-03 22:01:00 +0100135 else
136 # Fetch trellis-control api and app JARs that may be needed to build other apps when using local maven cache (i.e., T3 and fabric-tna).
137 # Fetched jars will be places in the local .m2 folder.
138 docker run "${IT}" --rm -v "${CURRENT_DIR}":/root -w /root/"trellis-control" "${DOCKER_MVN_IMAGE}" \
139 bash -c "mvn dependency:copy -Dartifact=${TRELLIS_CONTROL_GROUPID}:segmentrouting-api:${TRELLIS_CONTROL_VERSION} \
140 -Dmdep.useBaseVersion=true -Dmdep.overWriteReleases=true -Dmdep.overWriteSnapshots=true -f dependencies.xml \
141 -s mvn_settings.xml; \
142 mvn dependency:copy -Dartifact=${TRELLIS_CONTROL_GROUPID}:segmentrouting-app:${TRELLIS_CONTROL_VERSION} \
143 -Dmdep.useBaseVersion=true -Dmdep.overWriteReleases=true -Dmdep.overWriteSnapshots=true -f dependencies.xml \
144 -s mvn_settings.xml"
pierventre83611422020-08-14 22:53:15 +0200145 fi
Carmelo Cascone68528e82022-02-04 18:42:34 -0800146 # Final step requires to move the oar to the folder used by Dockerfile. Moreover, it will help catch up errors
pierventre83611422020-08-14 22:53:15 +0200147 cp "${TRELLIS_CONTROL_OAR}" "${LOCAL_APPS}"/
148}
149
150function trellis-t3-build {
151 build_app "${TRELLIS_T3_ROOT}"/app/target \
152 "${TRELLIS_T3_ROOT}"/ "trellis-t3" \
153 "${TRELLIS_T3_ARTIFACT}" "${TRELLIS_T3_VERSION}" \
154 "app/target" "${TRELLIS_T3_OAR}" "${TRELLIS_T3_REPO}"
155 if [ "$MVN" -eq "0" ]; then
156 extract_version "${TRELLIS_T3_ROOT}"
157 TRELLIS_T3_OAR="${TRELLIS_T3_ROOT}"/app/target/"${TRELLIS_T3_ARTIFACTID}"-"${PROJECT_VERSION}".oar
158 fi
159 cp "${TRELLIS_T3_OAR}" "${LOCAL_APPS}"/
160}
161
pierventre83611422020-08-14 22:53:15 +0200162function up4-build {
163 # Prepares app folder
164 cd "${UP4_ROOT}" || exit 1 && make "${UP4_TARGETS}"
165 cd ../
166 build_app "${UP4_ROOT}"/app/app/target \
167 "${UP4_ROOT}"/app "up4/app" \
168 "${UP4_ARTIFACT}" "${UP4_VERSION}" \
169 "app/app/target" "${UP4_OAR}" "${UP4_REPO}"
170 if [ "$MVN" -eq "0" ]; then
171 extract_version "${UP4_ROOT}"/app
172 cd ../
173 UP4_OAR="${UP4_ROOT}"/app/app/target/"${UP4_ARTIFACTID}"-"${PROJECT_VERSION}".oar
174 fi
175 cp "${UP4_OAR}" "${LOCAL_APPS}"/
176}
177
pierventre83611422020-08-14 22:53:15 +0200178function fabric-tna-build {
179 # This workaround is temporary - typically we need to build only the pipeconf
Daniele Morob57f5af2022-03-30 20:00:56 +0200180 cd "${FABRIC_TNA_ROOT}" || exit 1 && make "${FABRIC_TNA_TARGETS[@]}" SHOW_SENSITIVE_OUTPUT="${SHOW_SENSITIVE_OUTPUT:-false}"
pierventre83611422020-08-14 22:53:15 +0200181 cd ../
182 build_app "${FABRIC_TNA_ROOT}"/target \
183 "${FABRIC_TNA_ROOT}"/ "fabric-tna" \
184 "${FABRIC_TNA_ARTIFACT}" "${FABRIC_TNA_VERSION}" \
185 "target" "${FABRIC_TNA_OAR}" "${FABRIC_TNA_REPO}"
186 if [ "$MVN" -eq "0" ]; then
187 extract_version "${FABRIC_TNA_ROOT}"
188 FABRIC_TNA_OAR="${FABRIC_TNA_ROOT}"/target/"${FABRIC_TNA_ARTIFACTID}"-"${PROJECT_VERSION}".oar
189 fi
190 cp "${FABRIC_TNA_OAR}" "${LOCAL_APPS}"/
191}
192
193"$1"