pierventre | 8361142 | 2020-08-14 22:53:15 +0200 | [diff] [blame] | 1 | #!/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 |
| 20 | CURRENT_UID=$(id -u) |
| 21 | CURRENT_GID=$(id -g) |
| 22 | CURRENT_DIR=$(pwd) |
| 23 | MVN=1 |
| 24 | PROJECT_VERSION=0 |
| 25 | |
| 26 | # Docker related vars |
| 27 | DOCKER_MVN_TAG=3.6.3-openjdk-11-slim |
| 28 | DOCKER_MVN_IMAGE=maven:${DOCKER_MVN_TAG} |
| 29 | |
| 30 | # Do not attach stdin if running in an environment without it (e.g., Jenkins) |
| 31 | IT=$(test -t 0 && echo "-it" || echo "-t") |
| 32 | |
| 33 | # Trellis-control related vars |
| 34 | TRELLIS_CONTROL_GROUPID=org.onosproject |
Charles Chan | 3533f83 | 2020-09-04 21:56:34 -0700 | [diff] [blame] | 35 | TRELLIS_CONTROL_ARTIFACTID=segmentrouting-app |
pierventre | 8361142 | 2020-08-14 22:53:15 +0200 | [diff] [blame] | 36 | TRELLIS_CONTROL_ARTIFACT=${TRELLIS_CONTROL_GROUPID}:${TRELLIS_CONTROL_ARTIFACTID} |
Charles Chan | 3533f83 | 2020-09-04 21:56:34 -0700 | [diff] [blame] | 37 | TRELLIS_CONTROL_OAR=${TRELLIS_CONTROL_ROOT}/app/target/${TRELLIS_CONTROL_ARTIFACTID}-${TRELLIS_CONTROL_VERSION}.oar |
pierventre | 8361142 | 2020-08-14 22:53:15 +0200 | [diff] [blame] | 38 | |
| 39 | # Trellis-t3 related vars |
| 40 | TRELLIS_T3_GROUPID=org.onosproject |
| 41 | TRELLIS_T3_ARTIFACTID=t3-app |
| 42 | TRELLIS_T3_ARTIFACT=${TRELLIS_T3_GROUPID}:${TRELLIS_T3_ARTIFACTID} |
| 43 | TRELLIS_T3_OAR=${TRELLIS_T3_ROOT}/app/target/${TRELLIS_T3_ARTIFACTID}-${TRELLIS_T3_VERSION}.oar |
| 44 | |
pierventre | 8361142 | 2020-08-14 22:53:15 +0200 | [diff] [blame] | 45 | # UP4 related vars |
| 46 | UP4_GROUPID=org.omecproject |
| 47 | UP4_ARTIFACTID=up4-app |
| 48 | UP4_ARTIFACT=${UP4_GROUPID}:${UP4_ARTIFACTID} |
| 49 | UP4_TARGETS=_prepare_app_build |
| 50 | UP4_OAR=${UP4_ROOT}/app/app/target/${UP4_ARTIFACTID}-${UP4_VERSION}.oar |
| 51 | |
pierventre | 8361142 | 2020-08-14 22:53:15 +0200 | [diff] [blame] | 52 | # Fabric-tna related vars |
| 53 | FABRIC_TNA_GROUPID=org.stratumproject |
| 54 | FABRIC_TNA_ARTIFACTID=fabric-tna |
| 55 | FABRIC_TNA_ARTIFACT=${FABRIC_TNA_GROUPID}:${FABRIC_TNA_ARTIFACTID} |
Carmelo Cascone | 44f3d25 | 2022-02-04 13:38:17 -0800 | [diff] [blame] | 56 | FABRIC_TNA_TARGETS=all |
pierventre | 8361142 | 2020-08-14 22:53:15 +0200 | [diff] [blame] | 57 | FABRIC_TNA_OAR=${FABRIC_TNA_ROOT}/target/${FABRIC_TNA_ARTIFACTID}-${FABRIC_TNA_VERSION}.oar |
| 58 | |
| 59 | set -eu -o pipefail |
| 60 | |
| 61 | function 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 |
| 89 | function 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 |
pierventre | 3b5f9f1 | 2020-10-22 16:24:34 +0200 | [diff] [blame] | 107 | if ! (git checkout "origin/$5" || git checkout "$5"); then |
pierventre | 8361142 | 2020-08-14 22:53:15 +0200 | [diff] [blame] | 108 | 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 | |
| 122 | function trellis-control-build { |
| 123 | # Build function |
Charles Chan | 3533f83 | 2020-09-04 21:56:34 -0700 | [diff] [blame] | 124 | build_app "${TRELLIS_CONTROL_ROOT}"/app/target \ |
pierventre | 8361142 | 2020-08-14 22:53:15 +0200 | [diff] [blame] | 125 | "${TRELLIS_CONTROL_ROOT}"/ "trellis-control" \ |
| 126 | "${TRELLIS_CONTROL_ARTIFACT}" "${TRELLIS_CONTROL_VERSION}" \ |
Charles Chan | 3533f83 | 2020-09-04 21:56:34 -0700 | [diff] [blame] | 127 | "app/target" "${TRELLIS_CONTROL_OAR}" "${TRELLIS_CONTROL_REPO}" |
pierventre | 8361142 | 2020-08-14 22:53:15 +0200 | [diff] [blame] | 128 | # If MVN was not successful - built from sources |
| 129 | if [ "$MVN" -eq "0" ]; then |
Daniele Moro | f969c74 | 2022-01-03 22:01:00 +0100 | [diff] [blame] | 130 | # When building from source api and app jars are automatically put into the local .m2 folder. |
pierventre | 8361142 | 2020-08-14 22:53:15 +0200 | [diff] [blame] | 131 | # Update VERSION |
| 132 | extract_version "${TRELLIS_CONTROL_ROOT}" |
| 133 | # Update OAR |
Charles Chan | 3533f83 | 2020-09-04 21:56:34 -0700 | [diff] [blame] | 134 | TRELLIS_CONTROL_OAR="${TRELLIS_CONTROL_ROOT}"/app/target/"${TRELLIS_CONTROL_ARTIFACTID}"-"${PROJECT_VERSION}".oar |
Daniele Moro | f969c74 | 2022-01-03 22:01:00 +0100 | [diff] [blame] | 135 | 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" |
pierventre | 8361142 | 2020-08-14 22:53:15 +0200 | [diff] [blame] | 145 | fi |
Carmelo Cascone | 68528e8 | 2022-02-04 18:42:34 -0800 | [diff] [blame] | 146 | # Final step requires to move the oar to the folder used by Dockerfile. Moreover, it will help catch up errors |
pierventre | 8361142 | 2020-08-14 22:53:15 +0200 | [diff] [blame] | 147 | cp "${TRELLIS_CONTROL_OAR}" "${LOCAL_APPS}"/ |
| 148 | } |
| 149 | |
| 150 | function 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 | |
pierventre | 8361142 | 2020-08-14 22:53:15 +0200 | [diff] [blame] | 162 | function 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 | |
pierventre | 8361142 | 2020-08-14 22:53:15 +0200 | [diff] [blame] | 178 | function fabric-tna-build { |
| 179 | # This workaround is temporary - typically we need to build only the pipeconf |
Daniele Moro | b57f5af | 2022-03-30 20:00:56 +0200 | [diff] [blame] | 180 | cd "${FABRIC_TNA_ROOT}" || exit 1 && make "${FABRIC_TNA_TARGETS[@]}" SHOW_SENSITIVE_OUTPUT="${SHOW_SENSITIVE_OUTPUT:-false}" |
pierventre | 8361142 | 2020-08-14 22:53:15 +0200 | [diff] [blame] | 181 | 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" |