2023-03-28 22:48:58 +00:00
|
|
|
# Copyright (c) HashiCorp, Inc.
|
|
|
|
# SPDX-License-Identifier: MPL-2.0
|
|
|
|
|
2020-12-30 04:04:19 +00:00
|
|
|
function supported_osarch {
|
|
|
|
# Arguments:
|
|
|
|
# $1 - osarch - example, linux/amd64
|
|
|
|
#
|
|
|
|
# Returns:
|
|
|
|
# 0 - supported
|
|
|
|
# * - not supported
|
|
|
|
local osarch="$1"
|
|
|
|
for valid in $(go tool dist list)
|
|
|
|
do
|
|
|
|
if test "${osarch}" = "${valid}"
|
|
|
|
then
|
|
|
|
return 0
|
|
|
|
fi
|
|
|
|
done
|
|
|
|
return 1
|
|
|
|
}
|
|
|
|
|
2018-06-12 20:55:52 +00:00
|
|
|
function refresh_docker_images {
|
|
|
|
# Arguments:
|
|
|
|
# $1 - Path to top level Consul source
|
|
|
|
# $2 - Which make target to invoke (optional)
|
|
|
|
#
|
|
|
|
# Return:
|
|
|
|
# 0 - success
|
|
|
|
# * - failure
|
2018-10-19 18:33:23 +00:00
|
|
|
|
2018-06-12 20:55:52 +00:00
|
|
|
if ! test -d "$1"
|
|
|
|
then
|
2018-10-19 18:33:23 +00:00
|
|
|
err "ERROR: '$1' is not a directory. refresh_docker_images must be called with the path to the top level source as the first argument'"
|
2018-06-12 20:55:52 +00:00
|
|
|
return 1
|
|
|
|
fi
|
2018-10-19 18:33:23 +00:00
|
|
|
|
2018-06-12 20:55:52 +00:00
|
|
|
local sdir="$1"
|
|
|
|
local targets="$2"
|
2018-10-19 18:33:23 +00:00
|
|
|
|
2018-06-15 01:25:59 +00:00
|
|
|
test -n "${targets}" || targets="docker-images"
|
2018-10-19 18:33:23 +00:00
|
|
|
|
2018-06-15 01:25:59 +00:00
|
|
|
make -C "${sdir}" ${targets}
|
2018-06-12 20:55:52 +00:00
|
|
|
return $?
|
|
|
|
}
|
|
|
|
|
|
|
|
function build_ui {
|
|
|
|
# Arguments:
|
|
|
|
# $1 - Path to the top level Consul source
|
|
|
|
# $2 - The docker image to run the build within (optional)
|
2018-06-26 13:39:34 +00:00
|
|
|
# $3 - Version override
|
2018-06-12 20:55:52 +00:00
|
|
|
#
|
|
|
|
# Returns:
|
|
|
|
# 0 - success
|
|
|
|
# * - error
|
|
|
|
#
|
|
|
|
# Notes:
|
|
|
|
# Use the GIT_COMMIT environment variable to pass off to the build
|
2020-01-24 15:40:14 +00:00
|
|
|
# Use the GIT_COMMIT_YEAR environment variable to pass off to the build
|
2018-10-19 18:33:23 +00:00
|
|
|
|
2018-06-12 20:55:52 +00:00
|
|
|
if ! test -d "$1"
|
|
|
|
then
|
2018-10-19 18:33:23 +00:00
|
|
|
err "ERROR: '$1' is not a directory. build_ui must be called with the path to the top level source as the first argument'"
|
2018-06-12 20:55:52 +00:00
|
|
|
return 1
|
|
|
|
fi
|
2018-10-19 18:33:23 +00:00
|
|
|
|
2018-06-12 20:55:52 +00:00
|
|
|
local image_name=${UI_BUILD_CONTAINER_DEFAULT}
|
|
|
|
if test -n "$2"
|
|
|
|
then
|
|
|
|
image_name="$2"
|
2018-10-19 18:33:23 +00:00
|
|
|
fi
|
|
|
|
|
2018-06-12 20:55:52 +00:00
|
|
|
local sdir="$1"
|
2020-10-21 14:23:16 +00:00
|
|
|
local ui_dir="${1}/ui"
|
2018-10-19 18:33:23 +00:00
|
|
|
|
2018-06-12 20:55:52 +00:00
|
|
|
# parse the version
|
|
|
|
version=$(parse_version "${sdir}")
|
2018-10-19 18:33:23 +00:00
|
|
|
|
2018-06-26 13:39:34 +00:00
|
|
|
if test -n "$3"
|
|
|
|
then
|
|
|
|
version="$3"
|
|
|
|
fi
|
2018-10-19 18:33:23 +00:00
|
|
|
|
2018-06-12 20:55:52 +00:00
|
|
|
local commit_hash="${GIT_COMMIT}"
|
|
|
|
if test -z "${commit_hash}"
|
|
|
|
then
|
|
|
|
commit_hash=$(git rev-parse --short HEAD)
|
|
|
|
fi
|
2020-07-14 19:17:45 +00:00
|
|
|
|
2020-01-24 15:40:14 +00:00
|
|
|
local commit_year="${GIT_COMMIT_YEAR}"
|
|
|
|
if test -z "${commit_year}"
|
|
|
|
then
|
|
|
|
commit_year=$(git show -s --format=%cd --date=format:%Y HEAD)
|
|
|
|
fi
|
2020-07-14 19:17:45 +00:00
|
|
|
|
2018-07-30 16:59:43 +00:00
|
|
|
local logo_type="${CONSUL_BINARY_TYPE}"
|
|
|
|
if test "$logo_type" != "oss"
|
|
|
|
then
|
|
|
|
logo_type="enterprise"
|
|
|
|
fi
|
2018-10-19 18:33:23 +00:00
|
|
|
|
2018-06-12 20:55:52 +00:00
|
|
|
# make sure we run within the ui dir
|
|
|
|
pushd ${ui_dir} > /dev/null
|
2018-10-19 18:33:23 +00:00
|
|
|
|
2018-06-26 15:21:37 +00:00
|
|
|
status "Creating the UI Build Container with image: ${image_name} and version '${version}'"
|
2020-01-24 15:40:14 +00:00
|
|
|
local container_id=$(docker create -it -e "CONSUL_GIT_SHA=${commit_hash}" -e "CONSUL_COPYRIGHT_YEAR=${commit_year}" -e "CONSUL_VERSION=${version}" -e "CONSUL_BINARY_TYPE=${CONSUL_BINARY_TYPE}" ${image_name})
|
2018-06-12 20:55:52 +00:00
|
|
|
local ret=$?
|
|
|
|
if test $ret -eq 0
|
|
|
|
then
|
|
|
|
status "Copying the source from '${ui_dir}' to /consul-src within the container"
|
|
|
|
(
|
2018-07-06 14:08:11 +00:00
|
|
|
tar -c $(ls -A | grep -v "^(node_modules\|dist\|tmp)") | docker cp - ${container_id}:/consul-src &&
|
2018-06-12 20:55:52 +00:00
|
|
|
status "Running build in container" && docker start -i ${container_id} &&
|
2020-10-21 14:23:16 +00:00
|
|
|
rm -rf ${1}/ui/dist &&
|
|
|
|
status "Copying back artifacts" && docker cp ${container_id}:/consul-src/packages/consul-ui/dist ${1}/ui/dist
|
2018-06-12 20:55:52 +00:00
|
|
|
)
|
|
|
|
ret=$?
|
|
|
|
docker rm ${container_id} > /dev/null
|
|
|
|
fi
|
2018-07-30 16:59:43 +00:00
|
|
|
|
|
|
|
# Check the version is baked in correctly
|
2018-06-13 19:10:02 +00:00
|
|
|
if test ${ret} -eq 0
|
2018-06-12 20:55:52 +00:00
|
|
|
then
|
2020-10-21 14:23:16 +00:00
|
|
|
local ui_vers=$(ui_version "${1}/ui/dist/index.html")
|
2018-06-26 15:21:37 +00:00
|
|
|
if test "${version}" != "${ui_vers}"
|
|
|
|
then
|
|
|
|
err "ERROR: UI version mismatch. Expecting: '${version}' found '${ui_vers}'"
|
|
|
|
ret=1
|
|
|
|
fi
|
2018-06-12 20:55:52 +00:00
|
|
|
fi
|
2018-07-30 16:59:43 +00:00
|
|
|
|
|
|
|
# Check the logo is baked in correctly
|
|
|
|
if test ${ret} -eq 0
|
|
|
|
then
|
2020-10-21 14:23:16 +00:00
|
|
|
local ui_logo_type=$(ui_logo_type "${1}/ui/dist/index.html")
|
2018-07-30 16:59:43 +00:00
|
|
|
if test "${logo_type}" != "${ui_logo_type}"
|
|
|
|
then
|
|
|
|
err "ERROR: UI logo type mismatch. Expecting: '${logo_type}' found '${ui_logo_type}'"
|
|
|
|
ret=1
|
|
|
|
fi
|
|
|
|
fi
|
|
|
|
|
|
|
|
# Copy UI over ready to be packaged into the binary
|
|
|
|
if test ${ret} -eq 0
|
|
|
|
then
|
2022-05-31 19:33:56 +00:00
|
|
|
rm -rf ${1}/agent/uiserver/dist
|
|
|
|
cp -r ${1}/ui/dist ${1}/agent/uiserver/
|
2018-07-30 16:59:43 +00:00
|
|
|
fi
|
2018-10-19 18:33:23 +00:00
|
|
|
|
2018-06-12 20:55:52 +00:00
|
|
|
popd > /dev/null
|
|
|
|
return $ret
|
|
|
|
}
|
|
|
|
|
|
|
|
function build_consul_post {
|
|
|
|
# Arguments
|
|
|
|
# $1 - Path to the top level Consul source
|
2018-06-18 15:23:32 +00:00
|
|
|
# $2 - Subdirectory under pkg/bin (Optional)
|
2018-06-12 20:55:52 +00:00
|
|
|
#
|
|
|
|
# Returns:
|
|
|
|
# 0 - success
|
|
|
|
# * - error
|
|
|
|
#
|
|
|
|
# Notes:
|
|
|
|
# pkg/bin is where to place binary packages
|
|
|
|
# pkg.bin.new is where the just built binaries are located
|
|
|
|
# bin is where to place the local systems versions
|
2018-10-19 18:33:23 +00:00
|
|
|
|
2018-06-12 20:55:52 +00:00
|
|
|
if ! test -d "$1"
|
|
|
|
then
|
2018-10-19 18:33:23 +00:00
|
|
|
err "ERROR: '$1' is not a directory. build_consul_post must be called with the path to the top level source as the first argument'"
|
2018-06-12 20:55:52 +00:00
|
|
|
return 1
|
|
|
|
fi
|
2018-10-19 18:33:23 +00:00
|
|
|
|
2018-06-12 20:55:52 +00:00
|
|
|
local sdir="$1"
|
2018-10-19 18:33:23 +00:00
|
|
|
|
2018-06-18 15:23:32 +00:00
|
|
|
local extra_dir_name="$2"
|
|
|
|
local extra_dir=""
|
2018-10-19 18:33:23 +00:00
|
|
|
|
2018-06-18 15:23:32 +00:00
|
|
|
if test -n "${extra_dir_name}"
|
|
|
|
then
|
|
|
|
extra_dir="${extra_dir_name}/"
|
|
|
|
fi
|
2018-10-19 18:33:23 +00:00
|
|
|
|
2018-06-12 20:55:52 +00:00
|
|
|
pushd "${sdir}" > /dev/null
|
2018-10-19 18:33:23 +00:00
|
|
|
|
2018-06-12 20:55:52 +00:00
|
|
|
# recreate the pkg dir
|
2018-06-18 15:23:32 +00:00
|
|
|
rm -r pkg/bin/${extra_dir}* 2> /dev/null
|
|
|
|
mkdir -p pkg/bin/${extra_dir} 2> /dev/null
|
2018-06-12 20:55:52 +00:00
|
|
|
|
|
|
|
# move all files in pkg.new into pkg
|
2018-06-18 15:23:32 +00:00
|
|
|
cp -r pkg.bin.new/${extra_dir}* pkg/bin/${extra_dir}
|
2018-06-12 20:55:52 +00:00
|
|
|
rm -r pkg.bin.new
|
2018-10-19 18:33:23 +00:00
|
|
|
|
2018-06-18 15:23:32 +00:00
|
|
|
DEV_PLATFORM="./pkg/bin/${extra_dir}$(go env GOOS)_$(go env GOARCH)"
|
2018-07-25 19:21:11 +00:00
|
|
|
for F in $(find ${DEV_PLATFORM} -mindepth 1 -maxdepth 1 -type f 2>/dev/null)
|
2018-06-12 20:55:52 +00:00
|
|
|
do
|
|
|
|
# recreate the bin dir
|
|
|
|
rm -r bin/* 2> /dev/null
|
|
|
|
mkdir -p bin 2> /dev/null
|
2018-10-19 18:33:23 +00:00
|
|
|
|
2018-06-12 20:55:52 +00:00
|
|
|
cp ${F} bin/
|
|
|
|
cp ${F} ${MAIN_GOPATH}/bin
|
|
|
|
done
|
2018-10-19 18:33:23 +00:00
|
|
|
|
2018-06-12 20:55:52 +00:00
|
|
|
popd > /dev/null
|
2018-10-19 18:33:23 +00:00
|
|
|
|
2018-06-12 20:55:52 +00:00
|
|
|
return 0
|
|
|
|
}
|
|
|
|
|
|
|
|
function build_consul {
|
|
|
|
# Arguments:
|
|
|
|
# $1 - Path to the top level Consul source
|
2018-06-18 15:23:32 +00:00
|
|
|
# $2 - Subdirectory to put binaries in under pkg/bin (optional - must specify if needing to specify the docker image)
|
2018-06-12 20:55:52 +00:00
|
|
|
# $3 - The docker image to run the build within (optional)
|
|
|
|
#
|
|
|
|
# Returns:
|
|
|
|
# 0 - success
|
|
|
|
# * - error
|
|
|
|
#
|
|
|
|
# Note:
|
|
|
|
# The GOLDFLAGS and GOTAGS environment variables will be used if set
|
|
|
|
# If the CONSUL_DEV environment var is truthy only the local platform/architecture is built.
|
|
|
|
# If the XC_OS or the XC_ARCH environment vars are present then only those platforms/architectures
|
|
|
|
# will be built. Otherwise all supported platform/architectures are built
|
2018-10-19 18:33:23 +00:00
|
|
|
|
2018-06-12 20:55:52 +00:00
|
|
|
if ! test -d "$1"
|
|
|
|
then
|
2018-10-19 18:33:23 +00:00
|
|
|
err "ERROR: '$1' is not a directory. build_consul must be called with the path to the top level source as the first argument'"
|
2018-06-12 20:55:52 +00:00
|
|
|
return 1
|
|
|
|
fi
|
2018-10-19 18:33:23 +00:00
|
|
|
|
2018-06-12 20:55:52 +00:00
|
|
|
local sdir="$1"
|
2018-06-18 15:23:32 +00:00
|
|
|
local extra_dir_name="$2"
|
|
|
|
local extra_dir=""
|
2018-06-12 20:55:52 +00:00
|
|
|
local image_name=${GO_BUILD_CONTAINER_DEFAULT}
|
|
|
|
if test -n "$3"
|
|
|
|
then
|
|
|
|
image_name="$3"
|
2018-10-19 18:33:23 +00:00
|
|
|
fi
|
|
|
|
|
2018-06-12 20:55:52 +00:00
|
|
|
pushd ${sdir} > /dev/null
|
|
|
|
if is_set "${CONSUL_DEV}"
|
|
|
|
then
|
|
|
|
if test -z "${XC_OS}"
|
|
|
|
then
|
|
|
|
XC_OS=$(go env GOOS)
|
|
|
|
fi
|
2018-10-19 18:33:23 +00:00
|
|
|
|
2018-06-12 20:55:52 +00:00
|
|
|
if test -z "${XC_ARCH}"
|
|
|
|
then
|
|
|
|
XC_ARCH=$(go env GOARCH)
|
|
|
|
fi
|
|
|
|
fi
|
|
|
|
XC_OS=${XC_OS:-"solaris darwin freebsd linux windows"}
|
|
|
|
XC_ARCH=${XC_ARCH:-"386 amd64 arm arm64"}
|
2018-10-19 18:32:35 +00:00
|
|
|
|
2018-06-18 15:23:32 +00:00
|
|
|
if test -n "${extra_dir_name}"
|
|
|
|
then
|
|
|
|
extra_dir="${extra_dir_name}/"
|
|
|
|
fi
|
2018-06-12 20:55:52 +00:00
|
|
|
|
2019-04-12 19:17:13 +00:00
|
|
|
# figure out if the compiler supports modules
|
|
|
|
local use_modules=0
|
|
|
|
if go help modules >/dev/null 2>&1
|
|
|
|
then
|
|
|
|
use_modules=1
|
|
|
|
elif test -n "${GO111MODULE}"
|
|
|
|
then
|
|
|
|
use_modules=1
|
|
|
|
fi
|
|
|
|
|
|
|
|
local volume_mount=
|
|
|
|
if is_set "${use_modules}"
|
|
|
|
then
|
|
|
|
status "Ensuring Go modules are up to date"
|
|
|
|
# ensure our go module cache is correct
|
|
|
|
go_mod_assert || return 1
|
2022-04-12 16:44:41 +00:00
|
|
|
# Setup to bind mount our hosts module cache into the container
|
2019-04-12 19:17:13 +00:00
|
|
|
volume_mount="--mount=type=bind,source=${MAIN_GOPATH}/pkg/mod,target=/go/pkg/mod"
|
|
|
|
fi
|
|
|
|
|
|
|
|
status "Creating the Go Build Container with image: ${image_name}"
|
|
|
|
local container_id=$(docker create -it \
|
|
|
|
${volume_mount} \
|
|
|
|
-e CGO_ENABLED=0 \
|
2019-10-08 17:42:29 +00:00
|
|
|
-e GOLDFLAGS="${GOLDFLAGS}" \
|
|
|
|
-e GOTAGS="${GOTAGS}" \
|
2022-02-16 19:52:44 +00:00
|
|
|
${image_name} make linux)
|
2018-06-12 20:55:52 +00:00
|
|
|
ret=$?
|
|
|
|
|
|
|
|
if test $ret -eq 0
|
|
|
|
then
|
2019-03-29 15:29:27 +00:00
|
|
|
status "Copying the source from '${sdir}' to /consul"
|
2018-06-12 20:55:52 +00:00
|
|
|
(
|
2020-10-21 14:23:16 +00:00
|
|
|
tar -c $(ls | grep -v "^(ui\|website\|bin\|pkg\|.git)") | docker cp - ${container_id}:/consul &&
|
2018-06-12 20:55:52 +00:00
|
|
|
status "Running build in container" &&
|
|
|
|
docker start -i ${container_id} &&
|
|
|
|
status "Copying back artifacts" &&
|
2019-03-29 15:29:27 +00:00
|
|
|
docker cp ${container_id}:/consul/pkg/bin pkg.bin.new
|
2018-06-12 20:55:52 +00:00
|
|
|
)
|
|
|
|
ret=$?
|
|
|
|
docker rm ${container_id} > /dev/null
|
2018-10-19 18:33:23 +00:00
|
|
|
|
2018-06-12 20:55:52 +00:00
|
|
|
if test $ret -eq 0
|
|
|
|
then
|
2018-06-18 15:23:32 +00:00
|
|
|
build_consul_post "${sdir}" "${extra_dir_name}"
|
2018-06-12 20:55:52 +00:00
|
|
|
ret=$?
|
|
|
|
else
|
|
|
|
rm -r pkg.bin.new 2> /dev/null
|
|
|
|
fi
|
|
|
|
fi
|
2018-10-19 18:33:23 +00:00
|
|
|
popd > /dev/null
|
2018-06-12 20:55:52 +00:00
|
|
|
return $ret
|
|
|
|
}
|