1#!/bin/bash
  2
  3# Create a CHART project image.
  4
  5# This contains all prerequisites, framework, project and DU code to run the application
  6# with a user ready to access the NFS partitions
  7
  8# Current directory
  9HERE="$( cd -- "$( dirname -- "${BASH_SOURCE[0]}" )" &> /dev/null && pwd )"
 10
 11# Read configuration from project.env
 12BUILD_ENV="${HERE}/project.env"
 13if [[ ! -f "${BUILD_ENV}" ]]; then
 14    echo "Cannot find build configuration ${BUILD_ENV}"
 15    exit -1
 16fi
 17source "${BUILD_ENV}"
 18
 19# Display configuration information
 20echo "From configuration file ${BUILD_ENV}:"
 21echo "    Using framework image: ${CHART_FRAMEWORK_IMAGE_NAME}"
 22echo "    Creating project image: ${CHART_PROJECT_IMAGE_NAME}"
 23echo ""
 24
 25# Read VCS info for project
 26cd ${HERE}/..
 27PROJECT_VCS_BRANCH=$(git rev-parse --abbrev-ref HEAD)
 28PROJECT_VCS_TAG=$(git tag --points-at HEAD)
 29PROJECT_VCS_COMMIT=$(git rev-parse HEAD)
 30echo "From .git directory:"
 31echo "    Project VCS branch: ${PROJECT_VCS_BRANCH}"
 32echo "    Project VCS tag: ${PROJECT_VCS_TAG}"
 33echo "    Project VCS commit: ${PROJECT_VCS_COMMIT}"
 34echo ""
 35
 36# Read VCS info for DU
 37OLDDIR=$(pwd)
 38cd ${GIT_DU_DIR}
 39# check branch against GIT_DU_1_BRANCH)
 40DU_VCS_BRANCH=$(git rev-parse --abbrev-ref HEAD)
 41DU_VCS_TAG=$(git tag --points-at HEAD)
 42DU_VCS_COMMIT=$(git rev-parse HEAD)
 43echo "From ${GIT_DU_DIR}/.git directory:"
 44echo "    DU VCS branch: ${DU_VCS_BRANCH}"
 45echo "    DU VCS tag: ${DU_VCS_TAG}"
 46echo "    DU VCS commit: ${DU_VCS_COMMIT}"
 47echo ""
 48cd ${OLDDIR}
 49
 50# Read VCS info for DU_SRDB
 51OLDDIR=$(pwd)
 52cd ${GIT_DU_SRDB_DIR}
 53# check branch against GIT_DU_SRDB_1_BRANCH)
 54DU_SRDB_VCS_BRANCH=$(git rev-parse --abbrev-ref HEAD)
 55DU_SRDB_VCS_TAG=$(git tag --points-at HEAD)
 56DU_SRDB_VCS_COMMIT=$(git rev-parse HEAD)
 57echo ""
 58echo "From ${GIT_DU_SRDB_DIR}/.git directory:"
 59echo "    DU_SRDB VCS branch: ${DU_SRDB_VCS_BRANCH}"
 60echo "    DU_SRDB VCS tag: ${DU_SRDB_VCS_TAG}"
 61echo "    DU_SRDB VCS commit: ${DU_SRDB_VCS_COMMIT}"
 62echo ""
 63cd ${OLDDIR}
 64
 65# Read system info
 66PROJECT_BUILD_USER=$(whoami)
 67PROJECT_BUILD_HOST=$(hostname)
 68PROJECT_BUILD_DIR=$(pwd)
 69echo "From system info:"
 70echo "    User: ${PROJECT_BUILD_USER}"
 71echo "    Host: ${PROJECT_BUILD_HOST}"
 72echo "    Directory: ${PROJECT_BUILD_DIR}"
 73echo ""
 74
 75# Check VCS tags are set
 76if [[ "${PROJECT_VCS_TAG}" == "" ]]; then
 77    echo "WARNING: No git tag set for the project - make sure to create one before building images for formal deployment"
 78    echo ""
 79fi
 80if [[ "${DU_VCS_TAG}" == "" ]]; then
 81    echo "WARNING: No git tag set for the DU - make sure to create one before building images for formal deployment"
 82    echo ""
 83fi
 84if [[ "${DU_SRDB_VCS_TAG}" == "" ]]; then
 85    echo "WARNING: No git tag set for the DU SRDB - make sure to create one before building images for formal deployment"
 86    echo ""
 87fi
 88
 89# Test for uncommitted changes to project
 90# if [[ -z "$(git diff-index --quiet HEAD)" ]]; then
 91if [[ -n "$(git status -s)" ]]; then
 92    echo -n "WARNING: The project directory contains local changes which are not commited to git. "
 93    echo -n "The build can continue but the images must not be used for any formal deployments "
 94    echo "and will be marked as a dirty build."
 95    echo ""
 96    PROJECT_BUILD_DIRTY=1
 97else
 98    PROJECT_BUILD_DIRTY=0
 99fi
100
101# Check for uncomitted changes to DU
102OLDDIR=$(pwd)
103cd ${GIT_DU_DIR}
104if [[ -n "$(git status -s)" ]]; then
105    echo -n "WARNING: The DU directory contains local changes which are not commited to git. "
106    echo -n "The build can continue but the images must not be used for any formal deployments "
107    echo "and will be marked as a dirty build."
108    echo ""
109    DU_BUILD_DIRTY=1
110else
111    DU_BUILD_DIRTY=0
112fi
113cd ${OLDDIR}
114
115# Check for uncomitted changes to DU SRDB
116OLDDIR=$(pwd)
117cd ${GIT_DU_SRDB_DIR}
118if [[ -n "$(git status -s)" ]]; then
119    echo -n "WARNING: The DU SRDB directory contains local changes which are not commited to git. "
120    echo -n "The build can continue but the images must not be used for any formal deployments "
121    echo "and will be marked as a dirty build."
122    echo ""
123    DU_SRDB_BUILD_DIRTY=1
124else
125    DU_SRDB_BUILD_DIRTY=0
126fi
127cd ${OLDDIR}
128
129# Pause to let the user consider
130if [[ "${CHART_NO_PROMPT_BUILD}" != "1" ]]; then
131    echo "Press ENTER to continue"
132    read
133fi
134
135# Set up build logging
136LOGS="$(realpath --canonicalize-missing --relative-to=$PWD ${HERE}/../tmp/buildlogs)"
137echo "Log dir ${LOGS}"
138mkdir -p "${LOGS}"
139
140# build and tag the base image
141cd ${HERE}/..
142PROJECT_LOG="${LOGS}/build-project.log"
143echo "Building project image logging to ${PROJECT_LOG}"
144docker buildx build \
145       --build-arg "CHART_FRAMEWORK_IMAGE_NAME=${CHART_FRAMEWORK_IMAGE_NAME}" \
146       --build-arg "CHART_REPO_NAME=${CHART_REPO_NAME}" \
147       --build-arg "CHART_PROJECT_NAME=${CHART_PROJECT_NAME}" \
148       --build-arg "CHART_LAUNCHER_NAME=${CHART_LAUNCHER_NAME}" \
149       --build-arg "PROJECT_VCS_BRANCH=${PROJECT_VCS_BRANCH}" \
150       --build-arg "PROJECT_VCS_TAG=${PROJECT_VCS_TAG}" \
151       --build-arg "PROJECT_VCS_COMMIT=${PROJECT_VCS_COMMIT}" \
152       --build-arg "PROJECT_BUILD_DIRTY=${PROJECT_BUILD_DIRTY}" \
153       --build-arg "DU_VCS_BRANCH=${DU_VCS_BRANCH}" \
154       --build-arg "DU_VCS_TAG=${DU_VCS_TAG}" \
155       --build-arg "DU_VCS_COMMIT=${DU_VCS_COMMIT}" \
156       --build-arg "DU_BUILD_DIRTY=${DU_BUILD_DIRTY}" \
157       --build-arg "DU_SRDB_VCS_BRANCH=${DU_SRDB_VCS_BRANCH}" \
158       --build-arg "DU_SRDB_VCS_TAG=${DU_SRDB_VCS_TAG}" \
159       --build-arg "DU_SRDB_VCS_COMMIT=${DU_SRDB_VCS_COMMIT}" \
160       --build-arg "DU_SRDB_BUILD_DIRTY=${DU_SRDB_BUILD_DIRTY}" \
161       --build-arg "PROJECT_BUILD_USER=${PROJECT_BUILD_USER}" \
162       --build-arg "PROJECT_BUILD_HOST=${PROJECT_BUILD_HOST}" \
163       --build-arg "PROJECT_BUILD_DIR=${PROJECT_BUILD_DIR}" \
164       --tag "${CHART_PROJECT_IMAGE_NAME}" \
165       . \
166       --file docker/project/Dockerfile > "${PROJECT_LOG}"
167exit_code=$?
168if [ "$exit_code" -eq 1 ]; then
169    echo "Build failed, see log for details. Image has not been created"
170    exit -1
171fi
172echo "Tagged ${CHART_PROJECT_IMAGE_NAME}"