2022-10-20 18:48:17 -07:00
|
|
|
#!/bin/bash
|
2023-01-17 15:20:30 -08:00
|
|
|
|
|
|
|
docker_image_build () {
|
|
|
|
|
|
|
|
local targets=(dev arm amd deploy private multi)
|
|
|
|
local verbose
|
2022-10-20 18:48:17 -07:00
|
|
|
declare OPTION; declare OPTARG; declare OPTIND
|
2022-10-22 08:23:16 -07:00
|
|
|
|
2023-01-17 11:53:24 -08:00
|
|
|
SDIR=$(pwd)
|
2023-01-17 15:20:30 -08:00
|
|
|
BDIR=$(dirname "$(realpath "$BASH_SOURCE")")
|
2023-01-17 11:53:24 -08:00
|
|
|
pushd $BDIR > /dev/null
|
|
|
|
|
2023-01-17 15:20:30 -08:00
|
|
|
source $BDIR/lib/load.sh
|
2023-01-17 11:53:24 -08:00
|
|
|
|
2023-01-17 15:20:30 -08:00
|
|
|
case "$1" in
|
|
|
|
try)
|
|
|
|
shift 1
|
|
|
|
try_container "$@"
|
|
|
|
return $?
|
|
|
|
;;
|
|
|
|
tag)
|
|
|
|
shift 1
|
|
|
|
image_tag "$@"
|
|
|
|
return $?
|
|
|
|
;;
|
|
|
|
info)
|
|
|
|
shift 1
|
|
|
|
[[ $1 == "arch" ]] && { shift 1; image_arch "$@"; return $?; }
|
|
|
|
[[ $1 == "exists" ]] && { shift 1; image_exists "$@"; return $?; }
|
|
|
|
[[ $1 == "id" ]] && { shift 1; image_id "$@"; return $?; }
|
|
|
|
image_info "$@"; return $?
|
|
|
|
;;
|
|
|
|
esac
|
2022-10-22 08:23:16 -07:00
|
|
|
|
|
|
|
exit_abnormal() { # Function: Exit with error.
|
|
|
|
usage
|
2023-01-17 15:20:30 -08:00
|
|
|
return ${1:-1}
|
2022-10-22 08:23:16 -07:00
|
|
|
}
|
|
|
|
|
2023-01-17 11:53:24 -08:00
|
|
|
scripts_dir=$SDIR/src
|
2023-01-17 15:20:30 -08:00
|
|
|
[[ -z "$PS1" ]] || no_prompt=true
|
2022-10-22 08:23:16 -07:00
|
|
|
|
2023-01-21 22:41:14 -08:00
|
|
|
while getopts ':b:d:t:ncr:u:pxhs:w:akvoi' OPTION; do
|
2022-10-22 08:23:16 -07:00
|
|
|
# echo processing: option:$OPTION argument:$OPTARG index:$OPTIND remaining:${@:$OPTIND}
|
|
|
|
case "$OPTION" in
|
2023-01-17 15:20:30 -08:00
|
|
|
o)
|
|
|
|
overwrite=true
|
|
|
|
;;
|
2023-01-21 22:41:14 -08:00
|
|
|
i)
|
|
|
|
BUST_INIT_CACHE=$(date)
|
|
|
|
# export BUST_INIT_CACHE=$(date)
|
|
|
|
;;
|
2023-01-17 15:20:30 -08:00
|
|
|
v)
|
|
|
|
verbose=true
|
|
|
|
;;
|
2023-01-17 11:53:24 -08:00
|
|
|
a)
|
|
|
|
# automated - script is to be run without prompt (non-interactive)
|
|
|
|
no_prompt=true
|
|
|
|
;;
|
|
|
|
k)
|
|
|
|
# keep the build scripts in the image, by default they are removed
|
|
|
|
KEEP=true
|
|
|
|
;;
|
|
|
|
w)
|
|
|
|
# the work directory to put the build scripts in container (default is /opt/build)
|
|
|
|
SCRIPTS=$OPTARG
|
|
|
|
;;
|
2022-10-22 08:23:16 -07:00
|
|
|
b)
|
|
|
|
# CUSTOM BASE IMAGE
|
|
|
|
BASE_IMAGE=$OPTARG
|
2022-10-20 18:48:17 -07:00
|
|
|
;;
|
2023-01-17 11:53:24 -08:00
|
|
|
s)
|
|
|
|
# scripts subdirectory from which to bind to scripts/, default is src/
|
|
|
|
if isAbsPath $OPTARG; then
|
|
|
|
scripts_dir=$OPTARG
|
|
|
|
elif [ "$OPTARG" == "_base_" ]; then
|
|
|
|
scripts_dir=$BDIR/src
|
|
|
|
else
|
|
|
|
scripts_dir=$SDIR/$OPTARG
|
|
|
|
fi
|
|
|
|
;;
|
2022-10-22 08:23:16 -07:00
|
|
|
d)
|
2023-01-17 11:53:24 -08:00
|
|
|
# LINUX_DISTRO=$OPTARG
|
2022-10-22 08:23:16 -07:00
|
|
|
LINUX_DISTRO=$OPTARG
|
2022-10-20 18:48:17 -07:00
|
|
|
;;
|
2022-10-22 08:23:16 -07:00
|
|
|
x)
|
|
|
|
# Exclude distro from image name
|
|
|
|
exclude_distro=true
|
2022-10-20 18:48:17 -07:00
|
|
|
;;
|
2022-10-22 08:23:16 -07:00
|
|
|
t)
|
|
|
|
TAG=$OPTARG
|
|
|
|
;;
|
|
|
|
u)
|
|
|
|
RUSER=$OPTARG
|
|
|
|
;;
|
|
|
|
c)
|
|
|
|
try=true
|
|
|
|
;;
|
|
|
|
p)
|
|
|
|
push=true
|
|
|
|
;;
|
|
|
|
n)
|
|
|
|
nocache="--no-cache"
|
|
|
|
;;
|
|
|
|
r)
|
|
|
|
REPO=$OPTARG
|
|
|
|
;;
|
|
|
|
h)
|
|
|
|
exit_abnormal 0
|
2023-01-17 15:42:56 -08:00
|
|
|
return 0
|
2022-10-22 08:23:16 -07:00
|
|
|
;;
|
|
|
|
*)
|
|
|
|
echo "unknown $0 option -$OPTARG"
|
|
|
|
exit_abnormal 4
|
|
|
|
;;
|
|
|
|
esac
|
|
|
|
done
|
2022-10-20 18:48:17 -07:00
|
|
|
|
2022-10-22 08:23:16 -07:00
|
|
|
shift $((OPTIND - 1))
|
2022-10-20 18:48:17 -07:00
|
|
|
|
2023-01-21 22:41:14 -08:00
|
|
|
if [[ ! -d $scripts_dir ]]; then
|
|
|
|
if [[ ! $no_prompt ]]; then
|
|
|
|
echo -e "\e[1;31mWARNING: build scripts directory$scripts_dir does not exist.\e[1;37m"
|
|
|
|
echo "Do you want to use the base scripts"
|
|
|
|
read -n 1 -p "directory at $BDIR/src? to continue [y]=>" REPLY
|
|
|
|
[[ $REPLY != "y" ]] && echo -e "\n" && return 0
|
|
|
|
echo
|
|
|
|
fi
|
|
|
|
echo -e "\e[1;31mscripts directory $scripts_dir was not found using base build scripts at $BDIR/src\e[1;37m"
|
|
|
|
scripts_dir=$BDIR/src
|
|
|
|
fi
|
|
|
|
|
2022-10-20 18:48:17 -07:00
|
|
|
target=${1:-dev}
|
|
|
|
LINUX_DISTRO=${LINUX_DISTRO:-alpine}
|
|
|
|
name=$2
|
|
|
|
RUSER=${3:-$RUSER}
|
|
|
|
|
|
|
|
IMAGE_NAME=$([[ $RUSER ]] && echo ${RUSER}/)${name}$([[ (! $exclude_distro) && $name ]] && echo "-")$([[ ! $exclude_distro ]] && echo ${LINUX_DISTRO})
|
2023-01-17 15:20:30 -08:00
|
|
|
# TODO writing to existing tag untags existing image so write a new tag to that image then continue
|
2023-01-21 22:41:14 -08:00
|
|
|
|
|
|
|
# retag existing image and remove former tag
|
|
|
|
if [[ $(image_exists $IMAGE_NAME) ]]; then
|
|
|
|
if [[ $overwrite ]]; then
|
|
|
|
image_delete $IMAGE_NAME
|
|
|
|
else
|
|
|
|
newtag=$(date +'%d%H%M%S')
|
|
|
|
echo image exists retaging $(make_tag $IMAGE_NAME) with tag :$newtag
|
|
|
|
image_tag $IMAGE_NAME $IMAGE_NAME:$newtag
|
|
|
|
image_tag -r $IMAGE_NAME
|
|
|
|
fi
|
|
|
|
fi
|
|
|
|
|
2023-01-17 15:20:30 -08:00
|
|
|
|
2022-10-20 18:48:17 -07:00
|
|
|
if [[ $BASE_IMAGE ]]; then
|
|
|
|
echo determining DISTRO of base image: $BASE_IMAGE
|
2023-01-17 11:53:24 -08:00
|
|
|
LINUX_DISTRO=$(get_distro -d $BASE_IMAGE)
|
2022-10-20 18:48:17 -07:00
|
|
|
[[ ! $LINUX_DISTRO ]] && echo "unable to get base image OS for: $BASE_IMAGE, aborting build" && exit 1
|
|
|
|
else
|
|
|
|
BASE_IMAGE=$LINUX_DISTRO
|
|
|
|
fi
|
|
|
|
|
|
|
|
# BASE_IMAGE=$([[ $BASE_IMAGE == *:* ]] && echo $BASE_IMAGE || echo $BASE_IMAGE:latest)
|
|
|
|
#$([[ ! $BASE_IMAGE == *:* ]] && echo :latest)
|
|
|
|
|
|
|
|
export BASE_IMAGE
|
|
|
|
export TAG
|
|
|
|
export IMAGE_NAME
|
|
|
|
export LINUX_DISTRO
|
2023-01-17 11:53:24 -08:00
|
|
|
export SCRIPTS
|
|
|
|
export KEEP
|
2023-01-21 22:41:14 -08:00
|
|
|
export SYSADMIN_PW
|
|
|
|
export BUST_INIT_CACHE
|
2022-10-20 18:48:17 -07:00
|
|
|
|
2023-01-17 15:20:30 -08:00
|
|
|
echo "******************************************"
|
|
|
|
echo "Building with base image: $BASE_IMAGE"
|
|
|
|
echo "Outputing to image name => $IMAGE_NAME"
|
2022-10-20 18:48:17 -07:00
|
|
|
echo "Linux Distro: $LINUX_DISTRO"
|
2023-01-17 11:53:24 -08:00
|
|
|
echo "Using build target: $target"
|
2023-01-17 15:20:30 -08:00
|
|
|
if [[ $verbose ]]; then
|
|
|
|
echo -e "\n---------------------------------"
|
|
|
|
docker buildx bake --print $target
|
|
|
|
echo -e "\n---------------------------------"
|
|
|
|
echo "build scripts at $scripts_dir to be copied to ${SCRIPTS:-/opt/build} in container ***** "
|
|
|
|
ls -la $scripts_dir
|
|
|
|
echo -e "\n----- base init script init.sh ------"
|
|
|
|
cat $scripts_dir/init.sh
|
|
|
|
echo -e "\n---------------------------------"
|
|
|
|
fi
|
2023-01-17 11:53:24 -08:00
|
|
|
echo -e "\n***************************************"
|
2023-01-17 15:20:30 -08:00
|
|
|
|
2023-01-17 11:53:24 -08:00
|
|
|
if [[ ! $no_prompt ]]; then
|
|
|
|
read -n 1 -p "do you want to continue [y]=>" REPLY
|
|
|
|
[[ $REPLY != "y" ]] && echo -e "\n" && exit 0
|
|
|
|
fi
|
2022-10-20 18:48:17 -07:00
|
|
|
|
|
|
|
[[ ! "${targets[@]}" =~ $target ]] && echo $target is not a valid target && echo valid targets are: ${targets[@]} && exit 4
|
|
|
|
|
|
|
|
builder=default
|
|
|
|
if [ $target == "deploy" ]; then
|
|
|
|
builder=deploy
|
|
|
|
if ! docker buildx ls | grep -q deploy ; then
|
|
|
|
echo multiarch deploy builder does not exist, creating with docker-container driver
|
|
|
|
docker buildx create --name deploy --driver docker-container >/dev/null
|
|
|
|
docker buildx ls | grep deploy
|
|
|
|
fi
|
|
|
|
fi
|
|
|
|
|
|
|
|
[[ $target == "private" && ! $REPO ]] && echo "must use '-r <private repo>' if building to private repo" && exit 3
|
|
|
|
|
2023-01-17 11:53:24 -08:00
|
|
|
# mount source directory to temporary .src/ subdirectory
|
|
|
|
|
|
|
|
fusermount -u .src > /dev/null 2>&1
|
|
|
|
mkdir .src
|
|
|
|
bindfs ${scripts_dir:-src} .src
|
|
|
|
|
2023-01-21 22:41:14 -08:00
|
|
|
docker buildx --builder ${builder} bake ${nocache} ${target} 2>&1 | tee $SDIR/${exIMAGE_NAME//\//-}build.log
|
2023-01-17 11:53:24 -08:00
|
|
|
|
|
|
|
fusermount -u .src > /dev/null 2>&1
|
|
|
|
rm -rf .src/
|
|
|
|
|
|
|
|
[[ $? == 0 ]] && echo succcess building image $IMAGE_NAME || exit_abnormal 1
|
2022-10-20 18:48:17 -07:00
|
|
|
|
|
|
|
if [[ $target == "private" ]]; then
|
|
|
|
./push -a -r $REPO $IMAGE_NAME
|
|
|
|
./push -r $REPO $IMAGE_NAME
|
|
|
|
else
|
|
|
|
if [[ $push && (! $target == "dev") ]];then
|
|
|
|
echo pushing now
|
|
|
|
./push $([[ $target == "arm" ]] && echo -a) -r $REPO $IMAGE_NAME
|
|
|
|
fi
|
|
|
|
fi
|
|
|
|
|
2023-01-17 15:20:30 -08:00
|
|
|
if [[ ($try || $target == "dev") ]] && [[ ! $no_prompt ]]; then
|
|
|
|
echo trying newly built image in a container
|
2023-01-21 22:41:14 -08:00
|
|
|
try_container -m opt $([[ $target == "deploy" ]] && echo -p) $IMAGE_NAME
|
2023-01-17 15:20:30 -08:00
|
|
|
fi
|
2023-01-17 11:53:24 -08:00
|
|
|
|
|
|
|
popd > /dev/null
|
2023-01-17 15:20:30 -08:00
|
|
|
#echo reset to calling directory $PWD
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
# if script was executed then call the function
|
|
|
|
(return 0 2>/dev/null) || docker_image_build $@
|