268 lines
7.9 KiB
Bash
Executable File
268 lines
7.9 KiB
Bash
Executable File
#!/bin/bash
|
|
|
|
quiet () {
|
|
if [[ $VERBOSE ]]; then $@; fi
|
|
}
|
|
|
|
get_arch () {
|
|
local arch="$(uname -m)"
|
|
case "$arch" in
|
|
x86_64) arch='amd64' ;;
|
|
armhf) arch='armv6' ;;
|
|
armv7) arch='armv7' ;;
|
|
aarch64) arch='arm64' ;;
|
|
ppc64el|ppc64le) arch='ppc64le' ;;
|
|
s390x) arch='s390x';;
|
|
*) return 1 ;;\
|
|
esac;
|
|
echo $arch
|
|
}
|
|
|
|
isAbsPath() {
|
|
if [[ "${1:0:1}" == / || "${1:0:2}" == ~[/a-z] ]]
|
|
then
|
|
echo "true"
|
|
return 0
|
|
else
|
|
return 1
|
|
fi
|
|
}
|
|
|
|
sed_ignore_comments () {
|
|
cmd="sed -r 'h;s/[^#]*//1;x;s/#.*//;${1};G;s/(.*)\n/\1/'"
|
|
if (( $# == 2 )) ; then
|
|
eval $cmd <<< "$2"
|
|
else
|
|
eval $cmd < /dev/stdin
|
|
fi
|
|
}
|
|
|
|
clean_env_file () {
|
|
local compact; local lines
|
|
# -c will also remove comments and all empty lines
|
|
[[ $1 == "-c" ]] && compact=true && shift 1
|
|
#
|
|
# remove trailing spaces | remove double quotes
|
|
# | remove blanks after equals and quote value | remove any spaces before afer =
|
|
# remove blank lines | remove comment lines if requested
|
|
[[ -f $1 ]] && lines=$(<$1) || lines="$1"
|
|
echo "$lines" | sed_ignore_comments s/\\s*$//g | sed_ignore_comments s/\"//g \
|
|
| sed_ignore_comments s/\(=[[:blank:]]*\)\(.*\)/\\1\"\\2\"/ \
|
|
| sed_ignore_comments s/\\s*=\\s*/=/g \
|
|
| sed -rz 's/^\n+//; s/\n+$/\n/g' | if [[ $compact ]]; then grep -v '^#' | grep -v "^$" ; else cat; fi
|
|
}
|
|
|
|
env_file () {
|
|
env=${1:-.env}
|
|
[[ -f "${env}" ]] && { echo $env; return 0; } # || echo not $env
|
|
[[ -f "${env}.env" ]] && { echo "${env}.env"; return 0; } # || echo not ${env}.env
|
|
[[ -f "${env}/.env" ]] && { echo "${env}/.env"; return 0; } # || echo not ${env}/.env
|
|
return 1
|
|
}
|
|
|
|
read_env_file() {
|
|
local evar
|
|
while read line; do
|
|
evar=$(echo $line | cut -d '=' -f1)
|
|
if [[ ! ${!evar} ]]; then
|
|
# echo DECLARE $evar via $line
|
|
if declare -gx "$(echo "${line}" | sed 's/\"//g' )"; then
|
|
quiet echo loaded: ${evar}=${!evar}
|
|
else
|
|
quiet echo error setting $evar
|
|
return 1
|
|
|
|
fi
|
|
else
|
|
quiet echo $evar already set to ${!evar}
|
|
fi
|
|
done < <(clean_env_file -c $1)
|
|
}
|
|
|
|
source_env_file () {
|
|
local default;local efile
|
|
if efile=$(env_file ${1:-$BUILD_EFILE}); then
|
|
[[ $efile == ".env" ]] && default=true
|
|
[[ ! $(isAbsPath $efile) ]] && efile=$(realpath $efile)
|
|
quiet echo -e "\e[1;37m********************\e[0;37m"
|
|
quiet echo loading build environment with environment file
|
|
quiet echo $efile
|
|
quiet echo ----------------
|
|
quiet cat $efile
|
|
quiet echo ----------------
|
|
if read_env_file "$efile"; then
|
|
[[ $default ]] && quiet echo -e "\e[1;31mNOTE: sourced default .env file in present directory\e[0;37m"
|
|
quiet echo -e "\e[1;37m********************\e[0;37m"
|
|
BUILD_EFILE=$efile
|
|
else
|
|
quiet echo error occured while loading environment file
|
|
quiet echo $efile
|
|
quiet echo exiting
|
|
quiet echo -e "\e[1;37m********************\e[0;37m"
|
|
return 2
|
|
fi
|
|
else
|
|
if [[ $efile ]]; then
|
|
quiet echo unable to find an environment file with passed ${1}
|
|
else
|
|
quiet echo unable to find default environment file .env
|
|
quiet echo using the current environment plus default build options
|
|
fi
|
|
return 2
|
|
fi
|
|
|
|
}
|
|
|
|
docker_image_distro() {
|
|
local temp=/tmp/os-release.tmp
|
|
docker create --name dummy $1 > /dev/null
|
|
docker cp -L dummy:/etc/os-release $temp
|
|
docker rm -f dummy > /dev/null
|
|
echo $(cat $temp | tr [:upper:] [:lower:] | grep -Poi '(debian|ubuntu|red hat|centos|arch|alpine)' | uniq)
|
|
rm $temp
|
|
}
|
|
|
|
get_distro() {
|
|
|
|
LINUX_DISTRO=${LINUX_DISTRO:-alpine}
|
|
|
|
if [[ $BASE_IMAGE ]]; then
|
|
quiet echo determining DISTRO of base image: $BASE_IMAGE
|
|
LINUX_DISTRO=$(docker_image_distro $BASE_IMAGE)
|
|
[[ ! $LINUX_DISTRO ]] && quiet echo "unable to get base image OS for: $BASE_IMAGE, aborting build" && return 5
|
|
quiet echo $BASE_IMAGE is built from distro $LINUX_DISTRO
|
|
else
|
|
BASE_IMAGE=$LINUX_DISTRO
|
|
fi
|
|
}
|
|
|
|
make_image_name () {
|
|
|
|
local arch
|
|
|
|
# echo making image name $@
|
|
# echo name: $NAME
|
|
# echo APPEND: $APPEND_DISTRO
|
|
# echo user: $RUSER
|
|
# echo distro $LINUX_DISTRO
|
|
|
|
if [[ $1 ]]; then
|
|
# [[ $NAME ]] && echo changing image name from $NAME to $1
|
|
NAME=$1
|
|
fi
|
|
if [[ $2 ]]; then
|
|
# [[ $RUSER ]] && echo changing image username from $RUSER to $2
|
|
RUSER=$2
|
|
fi
|
|
if [[ $NAME ]]; then
|
|
[[ $APPEND_DISTRO ]] && NAME=$NAME-${LINUX_DISTRO}
|
|
else
|
|
# echo no image name supplied using distro name ${LINUX_DISTRO:-alpine}
|
|
NAME=${LINUX_DISTRO:-alpine}
|
|
fi
|
|
|
|
[[ $TARGET == "arm64" && ! $TARGET == $(get_arch) ]] && arch=-$TARGET
|
|
[[ $TARGET == "amd64" && ! $TARGET == $(get_arch) ]] && arch=-$TARGET
|
|
|
|
echo $([[ $RUSER ]] && echo ${RUSER}/)${NAME}$arch
|
|
|
|
}
|
|
|
|
get_build_src () {
|
|
# processing the build source directory
|
|
local src; local spath; local spaths
|
|
check_dir () {
|
|
[[ ( -d $1/packages && -d $1/init ) ]] || return 1
|
|
}
|
|
|
|
src=${1:-$BUILD_SRC}
|
|
[[ $src == "_default_" ]] && src=${BDIR}/src
|
|
src=${src:-src}
|
|
unset BUILD_SRC
|
|
|
|
if check_dir $src; then
|
|
BUILD_SRC=$(realpath $src)
|
|
return 0
|
|
fi
|
|
|
|
echo build source path \'$src\' not initially found
|
|
echo looking in parent directories for a valid build source directory
|
|
echo to avoid this search use BUILD_SRC= in an environment file
|
|
spaths="$PWD $(dirname $PWD)/$src $(dirname $PWD)"
|
|
for spath in $spaths; do
|
|
echo checking for source in: $spath
|
|
if check_dir $spath; then
|
|
echo found valid source directory!
|
|
echo using $spath
|
|
BUILD_SRC=$spath
|
|
return 0
|
|
fi
|
|
done
|
|
|
|
echo -e "\e[1;31mERROR: unable to find a build source directory as $src \e[1;37m"
|
|
return 1
|
|
}
|
|
|
|
build_info () {
|
|
|
|
|
|
echo "Build Command: docker buildx --builder ${builder} bake ${nocache} ${TARGET}"
|
|
if [[ $VERBOSE ]]; then
|
|
echo -e "\n!!!!!!!!!!!!!!!!EXTRA BUILD INFO !!!!!!!!!!!!!!!!!!!!!!!!!!!!!"
|
|
pushd "$BDIR" > /dev/null || return 3
|
|
docker buildx bake --print $TARGET
|
|
popd > /dev/null || return 4
|
|
echo -e "\n---------------------------------"
|
|
echo "build source at $BUILD_SRC to be mounted to /build in container ***** "
|
|
ls -la $BUILD_SRC
|
|
echo -e "\n----- base init script init.sh ------"
|
|
cat $BUILD_SRC/init/init.sh
|
|
echo -e "\n!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!"
|
|
fi
|
|
|
|
echo -e "\e[1;37m**************BUILD PARAMETERS *******************************"
|
|
echo "Architecture of this machine doing the building: $ARCH"
|
|
echo "Using scripts source directory at $BUILD_SRC"
|
|
echo "Building with base image: $BASE_IMAGE"
|
|
echo "Build logs can be found in directory $log_dir"
|
|
echo "Linux Distro of Image: $LINUX_DISTRO"
|
|
echo "Using build target: ${TARGET}"
|
|
echo "----- output --------"
|
|
echo creating image
|
|
case $TARGET in
|
|
|
|
multi)
|
|
echo "names for both amd64 and arm64, image without architecture suffix is $ARCH"
|
|
echo -e "\e[1;31m$IMAGE_NAME$([[ ! $ARCH == amd64 ]] && echo -amd64):${TAG:-latest}\n$IMAGE_NAME$([[ ! $ARCH == arm64 ]] && echo -arm64):${TAG:-latest}\e[1;37m"
|
|
echo "building locally and then pushing to ${REPO:-hub.docker.com}"
|
|
if [[ ! $REPO ]]; then
|
|
echo "NOTE: use target 'publish' to build and push these both"
|
|
echo "to docker hub without architecture suffix or local copy"
|
|
fi
|
|
;;
|
|
dev)
|
|
;&
|
|
default)
|
|
echo -e "name => \e[1;31m$IMAGE_NAME:${TAG:-latest}\e[1;37m"
|
|
echo "Will build this single image on this machine with architecture $ARCH"
|
|
;;
|
|
arm64)
|
|
echo -e "name => \e[1;31m$IMAGE_NAME$([[ ! $ARCH == arm64 ]] && echo -arm64):${TAG:-latest}\e[1;37m"
|
|
echo "Will build this single arm64 image on this machine"
|
|
;;
|
|
amd64)
|
|
echo -e "name => \e[1;31m$IMAGE_NAME$([[ ! $ARCH == amd64 ]] && echo -amd64):${TAG:-latest}\e[1;37m"
|
|
echo "Will build this single amd64 image on this machine"
|
|
;;
|
|
publish)
|
|
echo -e "image name \e[1;31m$IMAGE_NAME:${TAG:-latest}\e[1;37m"
|
|
echo "Will build and push both amd64 and arm64"
|
|
echo "architecture images to hub.docker.com with this single name"
|
|
echo "and no local copy will remain"
|
|
|
|
;;
|
|
esac
|
|
echo "------------------"
|
|
echo -e "********************\e[0;37m"
|
|
|
|
} |