improved setting options for source script directory to accept absoulte path and to force use of base repo source (src)

master
Kebler Network System Administrator 2023-01-17 11:53:24 -08:00
parent 6cef778b83
commit 4f3b4b94fe
5 changed files with 97 additions and 20 deletions

2
.gitignore vendored
View File

@ -1,3 +1,3 @@
/archive/ /archive/
/images/
/build.log /build.log
_opt/

View File

@ -1,15 +1,20 @@
ARG BASE_IMAGE ARG BASE_IMAGE
FROM $BASE_IMAGE FROM $BASE_IMAGE
ARG BASE_IMAGE
ARG KEEP
ARG LINUX_DISTRO=alpine ARG LINUX_DISTRO=alpine
WORKDIR /opt/$LINUX_DISTRO ARG SCRIPTS=/build
COPY ./distros/$LINUX_DISTRO ./distros/common ./ WORKDIR $SCRIPTS
COPY .src ./
RUN echo "****** Building UCI Image ffrom Base $BASE_IMAGE; Distro: $LINUX_DISTRO *****"; \ RUN echo -e "\n ************************************************* \n"\
chmod -R +x /opt/$LINUX_DISTRO; ls -la; pwd; \ echo "****** Building Image from Base: $BASE_IMAGE; : Distro: $LINUX_DISTRO; *****"; \
echo " **** Running ${LINUX_DISTRO} specific init script ***** "; cat init.sh; ./init.sh; \ chmod -R +x .; \
echo " ***** Running common initialzation script *****"; cat common.sh; ./common.sh \ pwd; ls -la; \
echo " ********************************************************" echo " ---- running init script ---"; ./init.sh; \
echo -e "\n********************************************************"
VOLUME [ "/data", "/opt", "/shell" ]
WORKDIR /opt
# ENTRYPOINT ["entrypoint.sh"] # ENTRYPOINT ["entrypoint.sh"]
CMD ["/bin/bash"] CMD ["/bin/bash"]

84
build
View File

@ -2,6 +2,15 @@
declare targets=(dev arm amd deploy private multi) declare targets=(dev arm amd deploy private multi)
declare OPTION; declare OPTARG; declare OPTIND declare OPTION; declare OPTARG; declare OPTIND
SDIR=$(pwd)
BDIR=$(dirname "$(realpath "$0")")
pushd $BDIR > /dev/null
if [ $1 == "try" ]; then
./try $2 $SDIR
exit
fi
usage() { # Function: Print a help message. usage() { # Function: Print a help message.
echo "Image Build Script: Creates one or more images using a target in the docker-bake.hcl file" echo "Image Build Script: Creates one or more images using a target in the docker-bake.hcl file"
echo "USAGE: $0 <setup options> buildtarget <imagename, imageuser>" echo "USAGE: $0 <setup options> buildtarget <imagename, imageuser>"
@ -12,10 +21,12 @@ usage() { # Function: Print a help message.
echo "-n for --no_cache" echo "-n for --no_cache"
echo "-p push to repository; after build push to repository default is hub.docker.common (not need for deploy target)" echo "-p push to repository; after build push to repository default is hub.docker.common (not need for deploy target)"
echo required argument options: echo required argument options:
echo "-d <LINUX_DISTRO> supported: alpine, debian, ubuntu, default: alpine; if base image set distro will be determined"
echo "-t <TAG> tag following : in output image name (i.e. REPO/USER/NAME:TAG), default: latest" echo "-t <TAG> tag following : in output image name (i.e. REPO/USER/NAME:TAG), default: latest"
echo "-u <USER>; repository user prefix in output image name (i.e. REPO/USER/NAME:TAG)" echo "-u <USER>; repository user prefix in output image name (i.e. REPO/USER/NAME:TAG)"
echo "-r <REPO>; private repo name, do not use for hub.docker.com (docker.io)" echo "-r <REPO>; private repo name, do not use for hub.docker.com (docker.io)"
echo "-b <BASE_IMAGE>; used in FROM in Dockerfile, default is official distro image (e.g. alpine:latest)" echo "-b <BASE_IMAGE>; used in FROM in Dockerfile, default is official distro image (e.g. alpine:latest)"
echo "-w <SCRIPTS>; set a custom WORKDIR in Dockerfile, default is /opt/build"
} }
exit_abnormal() { # Function: Exit with error. exit_abnormal() { # Function: Exit with error.
@ -23,16 +34,41 @@ exit_abnormal() { # Function: Exit with error.
exit ${1:-1} exit ${1:-1}
} }
source $BDIR/helpers.lib
while getopts ':b:d:t:ncr:u:pxh' OPTION; do scripts_dir=$SDIR/src
while getopts ':b:d:t:ncr:u:pxhs:w:ak' OPTION; do
# echo processing: option:$OPTION argument:$OPTARG index:$OPTIND remaining:${@:$OPTIND} # echo processing: option:$OPTION argument:$OPTARG index:$OPTIND remaining:${@:$OPTIND}
case "$OPTION" in case "$OPTION" in
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
;;
b) b)
# CUSTOM BASE IMAGE # CUSTOM BASE IMAGE
BASE_IMAGE=$OPTARG BASE_IMAGE=$OPTARG
;; ;;
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
;;
d) d)
# LINUX_DISTRO=$OPTARG # LINUX_DISTRO=$OPTARG
LINUX_DISTRO=$OPTARG LINUX_DISTRO=$OPTARG
;; ;;
x) x)
@ -69,15 +105,18 @@ done
shift $((OPTIND - 1)) shift $((OPTIND - 1))
target=${1:-dev} target=${1:-dev}
LINUX_DISTRO=${LINUX_DISTRO:-alpine} LINUX_DISTRO=${LINUX_DISTRO:-alpine}
name=$2 name=$2
RUSER=${3:-$RUSER} RUSER=${3:-$RUSER}
IMAGE_NAME=$([[ $RUSER ]] && echo ${RUSER}/)${name}$([[ (! $exclude_distro) && $name ]] && echo "-")$([[ ! $exclude_distro ]] && echo ${LINUX_DISTRO}) IMAGE_NAME=$([[ $RUSER ]] && echo ${RUSER}/)${name}$([[ (! $exclude_distro) && $name ]] && echo "-")$([[ ! $exclude_distro ]] && echo ${LINUX_DISTRO})
if [[ $BASE_IMAGE ]]; then if [[ $BASE_IMAGE ]]; then
echo determining DISTRO of base image: $BASE_IMAGE echo determining DISTRO of base image: $BASE_IMAGE
LINUX_DISTRO=$(./image-distro $BASE_IMAGE) LINUX_DISTRO=$(get_distro -d $BASE_IMAGE)
[[ ! $LINUX_DISTRO ]] && echo "unable to get base image OS for: $BASE_IMAGE, aborting build" && exit 1 [[ ! $LINUX_DISTRO ]] && echo "unable to get base image OS for: $BASE_IMAGE, aborting build" && exit 1
else else
BASE_IMAGE=$LINUX_DISTRO BASE_IMAGE=$LINUX_DISTRO
@ -90,14 +129,25 @@ export BASE_IMAGE
export TAG export TAG
export IMAGE_NAME export IMAGE_NAME
export LINUX_DISTRO export LINUX_DISTRO
export SCRIPTS
export KEEP
echo "building with base image: $BASE_IMAGE, output image name => $IMAGE_NAME" echo " ******************************************"
echo "Building with base image: $BASE_IMAGE, output image name => $IMAGE_NAME"
echo "Linux Distro: $LINUX_DISTRO" echo "Linux Distro: $LINUX_DISTRO"
echo "***** using build target: $target ****" echo "Using build target: $target"
docker buildx bake --print $target docker buildx bake --print $target
echo "******************" echo -e "\n---------------------------------"
read -n 1 -p "do you want to continue [y]=>" REPLY echo "build scripts at $scripts_dir to be copied to ${SCRIPTS:-/opt/build} in container ***** "
[[ $REPLY != "y" ]] && echo -e "\n" && exit 0 ls -la $scripts_dir
echo -e "\n----- base init script init.sh ------"
cat $scripts_dir/init.sh
echo -e "\n---------------------------------"
echo -e "\n***************************************"
if [[ ! $no_prompt ]]; then
read -n 1 -p "do you want to continue [y]=>" REPLY
[[ $REPLY != "y" ]] && echo -e "\n" && exit 0
fi
[[ ! "${targets[@]}" =~ $target ]] && echo $target is not a valid target && echo valid targets are: ${targets[@]} && exit 4 [[ ! "${targets[@]}" =~ $target ]] && echo $target is not a valid target && echo valid targets are: ${targets[@]} && exit 4
@ -113,7 +163,18 @@ fi
[[ $target == "private" && ! $REPO ]] && echo "must use '-r <private repo>' if building to private repo" && exit 3 [[ $target == "private" && ! $REPO ]] && echo "must use '-r <private repo>' if building to private repo" && exit 3
docker buildx --builder ${builder} bake ${nocache} ${target} 2>&1 | tee build.log # mount source directory to temporary .src/ subdirectory
fusermount -u .src > /dev/null 2>&1
mkdir .src
bindfs ${scripts_dir:-src} .src
docker buildx --builder ${builder} bake ${nocache} ${target} 2>&1 | tee build.log
fusermount -u .src > /dev/null 2>&1
rm -rf .src/
[[ $? == 0 ]] && echo succcess building image $IMAGE_NAME || exit_abnormal 1
if [[ $target == "private" ]]; then if [[ $target == "private" ]]; then
./push -a -r $REPO $IMAGE_NAME ./push -a -r $REPO $IMAGE_NAME
@ -125,4 +186,7 @@ if [[ $target == "private" ]]; then
fi fi
fi fi
[[ $try || $target == "dev" ]] && ./try $([[ $target == "deploy" ]] && echo -p) $name $RUSER [[ ($try || $target == "dev") ]] && ./try $([[ $target == "deploy" ]] && echo -p) $IMAGE_NAME
popd > /dev/null
#echo reset to calling directory $PWD

View File

@ -5,12 +5,18 @@ variable "TAG" {
variable "LINUX_DISTRO" { variable "LINUX_DISTRO" {
default = "alpine" default = "alpine"
} }
variable "SCRIPTS" {
default = ""
}
variable "IMAGE_NAME" { variable "IMAGE_NAME" {
default = "alpine" default = "alpine"
} }
variable "BASE_IMAGE" { variable "BASE_IMAGE" {
default = "alpine" default = "alpine"
} }
variable "KEEP" {
default = ""
}
function "tag" { function "tag" {
params = [suffix] params = [suffix]
@ -42,6 +48,8 @@ target "amd" {
LINUX_DISTRO = "${LINUX_DISTRO}" LINUX_DISTRO = "${LINUX_DISTRO}"
BASE_IMAGE = "${BASE_IMAGE}" BASE_IMAGE = "${BASE_IMAGE}"
TAG = "${TAG}" TAG = "${TAG}"
SCRIPTS = "/build/${SCRIPTS}"
KEEP = "${KEEP}"
} }
tags = tag("") tags = tag("")
platforms = ["linux/amd64"] platforms = ["linux/amd64"]

View File

@ -14,7 +14,7 @@ By using the -b flag you can set an alternative FROM image. In this way you can
The repo also supports (with scripts) pushing to alternate private repositories (like self hosted gitea) The repo also supports (with scripts) pushing to alternate private repositories (like self hosted gitea)
starting a new branch is a good way to work on a new image by editing the script files in the distros folder starting a new branch is a good way to work on a new image by editing the script files in the src folder