2022-10-20 18:48:17 -07:00
|
|
|
#!/bin/bash
|
2023-01-17 15:20:30 -08:00
|
|
|
|
|
|
|
docker_image_build () {
|
|
|
|
|
2023-04-01 07:17:43 -07:00
|
|
|
local targets=(dev arm64 amd64 deploy private multi)
|
2023-03-27 11:07:25 -07:00
|
|
|
local verbose; local log_dir; local no_prompt
|
|
|
|
local efile
|
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 15:20:30 -08:00
|
|
|
BDIR=$(dirname "$(realpath "$BASH_SOURCE")")
|
2023-01-22 15:02:36 -08:00
|
|
|
export BDIR
|
2023-03-25 11:27:22 -07:00
|
|
|
# load script library
|
2023-01-17 15:20:30 -08:00
|
|
|
source $BDIR/lib/load.sh
|
2023-01-17 11:53:24 -08:00
|
|
|
|
2023-03-25 11:27:22 -07:00
|
|
|
# check for subcommands first
|
2023-01-17 15:20:30 -08:00
|
|
|
case "$1" in
|
2023-03-22 09:10:54 -07:00
|
|
|
try)
|
|
|
|
shift 1
|
|
|
|
popd > /dev/null || return 2
|
|
|
|
try_container "$@"
|
|
|
|
return $?
|
|
|
|
;;
|
2023-01-22 15:02:36 -08:00
|
|
|
image_name)
|
2023-01-17 15:20:30 -08:00
|
|
|
shift 1
|
2023-01-22 15:02:36 -08:00
|
|
|
image_name "$@"
|
2023-01-17 15:20:30 -08:00
|
|
|
return $?
|
|
|
|
;;
|
|
|
|
tag)
|
|
|
|
shift 1
|
|
|
|
image_tag "$@"
|
|
|
|
return $?
|
|
|
|
;;
|
2023-01-22 15:02:36 -08:00
|
|
|
push)
|
|
|
|
shift 1
|
|
|
|
image_push "$@"
|
|
|
|
return $?
|
|
|
|
;;
|
2023-01-17 15:20:30 -08:00
|
|
|
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-03-25 11:27:22 -07:00
|
|
|
|
2023-01-17 15:20:30 -08:00
|
|
|
[[ -z "$PS1" ]] || no_prompt=true
|
2023-01-22 15:02:36 -08:00
|
|
|
overwrite=true
|
2022-10-22 08:23:16 -07:00
|
|
|
|
2023-03-25 11:27:22 -07:00
|
|
|
while getopts 'g:e:b:d:t:ncr:u:plhs: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-03-27 11:07:25 -07:00
|
|
|
e)
|
|
|
|
if source_env_file $OPTARG; then efile=true; else return 2; fi
|
|
|
|
;;
|
2023-01-17 15:20:30 -08:00
|
|
|
o)
|
2023-01-22 15:02:36 -08:00
|
|
|
unset overwrite
|
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)
|
2023-01-22 15:02:36 -08:00
|
|
|
BUILD_DIR=$OPTARG
|
2023-01-17 11:53:24 -08:00
|
|
|
;;
|
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)
|
2023-03-25 11:27:22 -07:00
|
|
|
# building source from which to bind into build, default is src/ in current directory
|
|
|
|
BUILD_SRC=$OPTARG
|
2023-01-17 11:53:24 -08:00
|
|
|
;;
|
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
|
|
|
;;
|
2023-03-25 11:27:22 -07:00
|
|
|
l)
|
|
|
|
# append distro name to image name
|
|
|
|
append_distro=true
|
2022-10-20 18:48:17 -07:00
|
|
|
;;
|
2022-10-22 08:23:16 -07:00
|
|
|
t)
|
2023-03-27 11:07:25 -07:00
|
|
|
TARGET=$OPTARG
|
2023-01-22 15:02:36 -08:00
|
|
|
;;
|
|
|
|
g)
|
2022-10-22 08:23:16 -07:00
|
|
|
TAG=$OPTARG
|
|
|
|
;;
|
|
|
|
u)
|
|
|
|
RUSER=$OPTARG
|
|
|
|
;;
|
|
|
|
c)
|
|
|
|
try=true
|
|
|
|
;;
|
2023-04-01 07:17:43 -07:00
|
|
|
p)
|
|
|
|
push=true
|
|
|
|
;;
|
2022-10-22 08:23:16 -07:00
|
|
|
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"
|
2023-03-27 11:07:25 -07:00
|
|
|
exit_abnormal 1
|
2022-10-22 08:23:16 -07:00
|
|
|
;;
|
|
|
|
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-03-27 11:07:25 -07:00
|
|
|
[[ ! $efile ]] && source_env_file
|
|
|
|
|
|
|
|
# processing the build source directory
|
|
|
|
if [[ ! $BUILD_SRC ]]; then
|
|
|
|
echo no BUILD_SRC directory specified
|
|
|
|
echo using present directory $PWD
|
|
|
|
BUILD_SRC=$PWD
|
|
|
|
fi
|
2023-03-25 11:27:22 -07:00
|
|
|
|
|
|
|
if [[ ! $(isAbsPath $BUILD_SRC) ]] ; then
|
2023-03-27 11:07:25 -07:00
|
|
|
if [[ ${BUILD_SRC} == "_base_" ]]; then
|
|
|
|
BUILD_SRC=${BDIR}/src
|
|
|
|
else
|
|
|
|
BUILD_SRC=$(realpath ${BUILD_SRC})
|
|
|
|
fi
|
|
|
|
fi
|
|
|
|
|
|
|
|
if [[ ! ( -d $BUILD_SRC/packages && -d $BUILD_SRC/init ) ]]; then
|
|
|
|
echo -e "\e[1;31minvalid build source directory"
|
|
|
|
echo $BUILD_SRC
|
|
|
|
echo -e "it does not contain packages and init subirectories\e[1;37m"
|
|
|
|
if [[ ! $(basename $BUILD_SRC) == "src" ]]; then
|
|
|
|
echo checking in src/ subdirectory for source
|
|
|
|
if [[ ( -d $BUILD_SRC/src/packages && -d $BUILD_SRC/src/init ) ]]; then
|
|
|
|
BUILD_SRC=$BUILD_SRC/src
|
|
|
|
echo found source in src/ subdirectory changing to source directory to
|
|
|
|
echo $BUILD_SRC
|
2023-03-25 11:27:22 -07:00
|
|
|
else
|
2023-03-27 11:07:25 -07:00
|
|
|
echo -e "\e[1;31mERROR: no build source directory at $BUILD_SRC</src>"
|
|
|
|
echo -e "with init/ and packages/ subdirectores was found\e[1;37m"
|
|
|
|
if [[ $no_prompt ]] ; then
|
|
|
|
echo aborting the build...
|
|
|
|
echo -e "\e[1;31mNOTE: use '_base_' to explicitly use build source in uci-docker-build repo\e[1;37m"
|
|
|
|
return 2
|
|
|
|
else
|
|
|
|
echo "Do you want to use the uci-docker-build repo source scripts"
|
|
|
|
echo "at $BDIR/src "
|
|
|
|
read -n 1 -p "instead? [y]=>" REPLY
|
|
|
|
[[ $REPLY != "y" ]] && echo -e "\n" && return 2
|
|
|
|
BUILD_SRC=$BDIR/src
|
|
|
|
echo -e "\n\e[1;31mNOTE: use '_base_' to explicitly use build source in uci-docker-build repo\e[1;37m"
|
|
|
|
fi
|
|
|
|
fi
|
|
|
|
fi
|
|
|
|
fi
|
|
|
|
# done processing build source directory
|
2023-03-25 11:27:22 -07:00
|
|
|
|
|
|
|
log_dir=$PWD/logs
|
|
|
|
mkdir -p $log_dir
|
|
|
|
|
2023-03-27 11:07:25 -07:00
|
|
|
pushd "$BDIR" > /dev/null || return 3
|
2023-03-25 11:27:22 -07:00
|
|
|
|
2023-03-27 11:07:25 -07:00
|
|
|
[[ ! "${targets[@]}" =~ $TARGET ]] && echo $TARGET is not a valid target && echo valid targets are: ${targets[@]} && exit 4
|
2023-01-22 15:02:36 -08:00
|
|
|
|
2022-10-20 18:48:17 -07:00
|
|
|
LINUX_DISTRO=${LINUX_DISTRO:-alpine}
|
2023-03-22 09:10:54 -07:00
|
|
|
|
|
|
|
if [[ $BASE_IMAGE ]]; then
|
|
|
|
echo determining DISTRO of base image: $BASE_IMAGE
|
2023-03-27 11:07:25 -07:00
|
|
|
LINUX_DISTRO=$(docker_image_distro $BASE_IMAGE)
|
|
|
|
[[ ! $LINUX_DISTRO ]] && echo "unable to get base image OS for: $BASE_IMAGE, aborting build" && return 5
|
2023-03-22 09:10:54 -07:00
|
|
|
echo $BASE_IMAGE is built from distro $LINUX_DISTRO
|
|
|
|
else
|
|
|
|
BASE_IMAGE=$LINUX_DISTRO
|
|
|
|
fi
|
|
|
|
|
2023-03-27 11:07:25 -07:00
|
|
|
if [[ $1 ]]; then
|
2023-04-01 07:17:43 -07:00
|
|
|
[[ $NAME ]] && echo changing image name from $NAME to $1
|
|
|
|
NAME=$1
|
2023-03-27 11:07:25 -07:00
|
|
|
fi
|
|
|
|
if [[ $2 ]]; then
|
2023-04-01 07:17:43 -07:00
|
|
|
[[ $RUSER ]] && echo changing image username from $RUSER to $2
|
|
|
|
RUSER=$2
|
2023-03-27 11:07:25 -07:00
|
|
|
fi
|
2023-03-25 11:27:22 -07:00
|
|
|
if [[ $NAME ]]; then
|
2023-04-01 07:17:43 -07:00
|
|
|
[[ $append_distro ]] && NAME=$NAME-${LINUX_DISTRO}
|
2023-03-25 11:27:22 -07:00
|
|
|
else
|
2023-03-27 11:07:25 -07:00
|
|
|
echo no image name supplied using distro name $LINUX_DISTRO
|
2023-03-25 11:27:22 -07:00
|
|
|
NAME=${LINUX_DISTRO}
|
|
|
|
fi
|
2023-03-27 11:07:25 -07:00
|
|
|
|
|
|
|
|
2023-03-25 11:27:22 -07:00
|
|
|
IMAGE_NAME=$([[ $RUSER ]] && echo ${RUSER}/)${NAME}
|
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')
|
2023-01-22 15:02:36 -08:00
|
|
|
echo image exists retaging $(image_name $IMAGE_NAME) with tag :$newtag
|
2023-01-21 22:41:14 -08:00
|
|
|
image_tag $IMAGE_NAME $IMAGE_NAME:$newtag
|
|
|
|
image_tag -r $IMAGE_NAME
|
|
|
|
fi
|
|
|
|
fi
|
|
|
|
|
2022-10-20 18:48:17 -07:00
|
|
|
# BASE_IMAGE=$([[ $BASE_IMAGE == *:* ]] && echo $BASE_IMAGE || echo $BASE_IMAGE:latest)
|
|
|
|
#$([[ ! $BASE_IMAGE == *:* ]] && echo :latest)
|
|
|
|
|
2023-04-01 07:17:43 -07:00
|
|
|
ARCH=$(get_arch)
|
|
|
|
|
2022-10-20 18:48:17 -07:00
|
|
|
export BASE_IMAGE
|
|
|
|
export TAG
|
|
|
|
export IMAGE_NAME
|
|
|
|
export LINUX_DISTRO
|
2023-01-22 15:02:36 -08:00
|
|
|
export BUILD_DIR
|
2023-01-17 11:53:24 -08:00
|
|
|
export KEEP
|
2023-01-21 22:41:14 -08:00
|
|
|
export SYSADMIN_PW
|
2023-04-01 07:17:43 -07:00
|
|
|
export ARCH
|
2023-03-25 11:27:22 -07:00
|
|
|
|
2023-03-27 11:07:25 -07:00
|
|
|
echo -e "\e[1;37m********************"
|
2023-03-25 11:27:22 -07:00
|
|
|
echo "Using scripts source directory at $BUILD_SRC"
|
2023-01-17 15:20:30 -08:00
|
|
|
echo "Building with base image: $BASE_IMAGE"
|
2023-04-01 07:17:43 -07:00
|
|
|
#todo based on target form image names
|
2023-01-22 15:02:36 -08:00
|
|
|
echo "Outputing to image name => $IMAGE_NAME<-arch>:${TAG:-latest}"
|
2023-04-01 07:17:43 -07:00
|
|
|
[[ $push || $TARGET == "private" ]] && echo "Will push image to ${REPO:-hub.docker.com}"
|
|
|
|
[[ $TARGET == "deploy" ]] && echo "Will build and push both amd64 and arm64 images to hub.docker.com"
|
|
|
|
[[ $TARGET == "dev" || ! $TARGET ]] && echo "Building image for local machine with architecture $ARCH"
|
2022-10-20 18:48:17 -07:00
|
|
|
echo "Linux Distro: $LINUX_DISTRO"
|
2023-04-01 07:17:43 -07:00
|
|
|
echo "Using build target: ${TARGET:-default}"
|
2023-03-27 11:07:25 -07:00
|
|
|
echo "Build Command: docker buildx --builder ${builder} bake ${nocache} ${TARGET}"
|
2023-01-17 15:20:30 -08:00
|
|
|
if [[ $verbose ]]; then
|
|
|
|
echo -e "\n---------------------------------"
|
2023-03-27 11:07:25 -07:00
|
|
|
docker buildx bake --print $TARGET
|
2023-01-17 15:20:30 -08:00
|
|
|
echo -e "\n---------------------------------"
|
2023-03-25 11:27:22 -07:00
|
|
|
echo "build scripts at $BUILD_SRC to be copied to ${BUILD_DIR:-/build} in container ***** "
|
|
|
|
ls -la $BUILD_SRC
|
2023-01-17 15:20:30 -08:00
|
|
|
echo -e "\n----- base init script init.sh ------"
|
2023-03-25 11:27:22 -07:00
|
|
|
cat $BUILD_SRC/init.sh
|
2023-01-17 15:20:30 -08:00
|
|
|
echo -e "\n---------------------------------"
|
|
|
|
fi
|
2023-03-27 11:07:25 -07:00
|
|
|
echo -e "u********************\e[0;37m"
|
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
|
2023-03-27 11:07:25 -07:00
|
|
|
[[ $REPLY != "y" ]] && echo -e "\n" && return 4
|
2023-01-17 11:53:24 -08:00
|
|
|
fi
|
2022-10-20 18:48:17 -07:00
|
|
|
|
|
|
|
builder=default
|
2023-04-01 07:17:43 -07:00
|
|
|
if [[ $TARGET == "deploy" ]]; then
|
2022-10-20 18:48:17 -07:00
|
|
|
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
|
|
|
|
|
2023-03-27 11:07:25 -07:00
|
|
|
[[ $TARGET == "private" && ! $REPO ]] && echo "must use '-r <private repo>' if building to private repo" && exit 3
|
2022-10-20 18:48:17 -07:00
|
|
|
|
2023-03-22 09:10:54 -07:00
|
|
|
# copy source directory to temporary .src/ subdirectory
|
|
|
|
# MUST either be readable by all or group readable by docker group
|
|
|
|
rm -rf $BDIR/.src
|
2023-03-25 11:27:22 -07:00
|
|
|
rsync -aAru ${BUILD_SRC:-src}/ $BDIR/.src
|
2023-03-22 09:10:54 -07:00
|
|
|
ls -la $BDIR/.src
|
2023-04-01 07:17:43 -07:00
|
|
|
echo running build command: docker buildx --builder ${builder} bake ${nocache} ${TARGET}
|
2023-03-27 11:07:25 -07:00
|
|
|
docker buildx --builder ${builder} bake ${nocache} ${TARGET} 2>&1 | tee "$log_dir/${IMAGE_NAME//\//-}build.log"
|
|
|
|
[[ $? == 0 ]] && echo succcess building image $IMAGE_NAME || exit_abnormal 5
|
2023-01-17 11:53:24 -08:00
|
|
|
|
2023-03-22 09:10:54 -07:00
|
|
|
rm -rf $BDIR/.src
|
2023-01-17 11:53:24 -08:00
|
|
|
|
2023-03-22 09:10:54 -07:00
|
|
|
popd > /dev/null
|
2022-10-20 18:48:17 -07:00
|
|
|
|
2023-03-27 11:07:25 -07:00
|
|
|
if [[ ($try || $TARGET == "dev") ]] && [[ ! $no_prompt ]]; then
|
2023-01-17 15:20:30 -08:00
|
|
|
echo trying newly built image in a container
|
2023-03-27 11:07:25 -07: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
|
|
|
|
2023-04-01 07:17:43 -07:00
|
|
|
if [[ $TARGET == "private" ]]; then
|
|
|
|
# echo pushing arm64 image $IMAGE_NAME to ${REPO:-docker hub}
|
|
|
|
image_push -a -r $REPO $IMAGE_NAME
|
|
|
|
# echo pushing amd image $IMAGE_NAME to ${REPO:-docker hub}
|
|
|
|
image_push -r $REPO $IMAGE_NAME
|
|
|
|
else
|
|
|
|
if [[ $push && (! $TARGET == "dev") ]];then
|
|
|
|
# echo pushing $IMAGE_NAME to ${REPO:-docker hub}
|
|
|
|
image_push $([[ $TARGET == "arm" ]] && echo -a) -r $REPO $IMAGE_NAME
|
|
|
|
fi
|
|
|
|
fi
|
|
|
|
|
|
|
|
}
|
2023-01-17 15:20:30 -08:00
|
|
|
|
|
|
|
# if script was executed then call the function
|
2023-03-27 11:07:25 -07:00
|
|
|
(return 0 2>/dev/null) || docker_image_build $@
|
|
|
|
|
|
|
|
|
|
|
|
|