refactor: new BUIlD_SRC supported from multiple locations including environment

try: add entrypoint and env-file options, fix genric options
TODO: bind mount the source into the build directly (see dockerfile buildkit)
added example folder with examples
refactored: install script much more robust, allows custom target
removedj volume command from dockerfile
master
David Kebler 2023-03-25 11:27:22 -07:00
parent b88ea929b0
commit 40a8dc2297
8 changed files with 136 additions and 61 deletions

1
.dockerignore Normal file
View File

@ -0,0 +1 @@
archive/*

View File

@ -1,5 +1,7 @@
# syntax=docker/dockerfile:latest
ARG BASE_IMAGE ARG BASE_IMAGE
FROM $BASE_IMAGE FROM $BASE_IMAGE
ARG PENV
ARG BASE_IMAGE ARG BASE_IMAGE
ARG KEEP ARG KEEP
ARG SYSADMIN_PW ARG SYSADMIN_PW
@ -31,7 +33,9 @@ RUN echo -e "\n ************************************************* \n" \
echo -e "\n ************* End Initialzation ************************" echo -e "\n ************* End Initialzation ************************"
# END INITIALIZATION # END INITIALIZATION
VOLUME [ "/data", "/opt", "/shell, /home/sysadmin" ] # default command
WORKDIR /opt
# ENTRYPOINT ["entrypoint.sh"]
CMD ["/bin/bash", "-l"] CMD ["/bin/bash", "-l"]
# default
WORKDIR /opt

112
build
View File

@ -3,17 +3,15 @@
docker_image_build () { docker_image_build () {
local targets=(dev arm amd deploy private multi) local targets=(dev arm amd deploy private multi)
local verbose; local scripts_dir; local log_dir; local verbose; local BUILD_SRC; local log_dir; local no_prompt
declare OPTION; declare OPTARG; declare OPTIND declare OPTION; declare OPTARG; declare OPTIND
SDIR=$(pwd)
BDIR=$(dirname "$(realpath "$BASH_SOURCE")") BDIR=$(dirname "$(realpath "$BASH_SOURCE")")
export SDIR
export BDIR export BDIR
pushd $BDIR > /dev/null # load script library
source $BDIR/lib/load.sh source $BDIR/lib/load.sh
# check for subcommands first
case "$1" in case "$1" in
try) try)
shift 1 shift 1
@ -50,30 +48,29 @@ exit_abnormal() { # Function: Exit with error.
return ${1:-1} return ${1:-1}
} }
scripts_dir=$SDIR/src
# TODO allow log directory option
log_dir=$SDIR/logs
mkdir -p $log_dir
[[ -z "$PS1" ]] || no_prompt=true [[ -z "$PS1" ]] || no_prompt=true
overwrite=true overwrite=true
target=dev target=dev
while getopts 'g:e:b:d:t:ncr:u:pxhs:w:akvoi' OPTION; do while getopts 'g:e:b:d:t:ncr:u:plhs:w:akvoi' 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
e) e)
local efile=$SDIR/$OPTARG local efile
if [[ -f $efile ]]; then echo $PWD
source "$efile" [[ $(isAbsPath $OPTARG) ]] && efile=$OPTARG || efile=$(realpath $OPTARG)
[[ ! $? -eq 0 ]] && echo source of $efile failed, exiting && return 2 if source "$efile"; then
echo "********************"
echo loaded build environment file at
echo $efile
echo "----------"
cat $efile
echo "*********************"
else else
echo no environment file at $efile, exiting echo unable to source $efile, exiting && return 2
return 2 return 2
fi fi
echo "----------"
echo loaded environment filen $efile
cat $efile
echo "----------"
;; ;;
o) o)
unset overwrite unset overwrite
@ -98,22 +95,16 @@ while getopts 'g:e:b:d:t:ncr:u:pxhs:w:akvoi' OPTION; do
BASE_IMAGE=$OPTARG BASE_IMAGE=$OPTARG
;; ;;
s) s)
# scripts subdirectory from which to bind to scripts/, default is src/ # building source from which to bind into build, default is src/ in current directory
if isAbsPath $OPTARG; then BUILD_SRC=$OPTARG
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) l)
# Exclude distro from image name # append distro name to image name
exclude_distro=true append_distro=true
;; ;;
t) t)
target=$OPTARG target=$OPTARG
@ -149,18 +140,40 @@ done
shift $((OPTIND - 1)) shift $((OPTIND - 1))
if [[ ! -d $scripts_dir ]]; then BUILD_SRC=${BUILD_SRC:-${PWD}/src}
if [[ ! $no_prompt ]]; then
echo -e "\e[1;31mWARNING: build scripts directory$scripts_dir does not exist.\e[1;37m" if [[ ! $(isAbsPath $BUILD_SRC) ]] ; then
echo "Do you want to use the base scripts" if [[ ${BUILD_SRC} == "_base_" ]]; then
read -n 1 -p "directory at $BDIR/src? to continue [y]=>" REPLY BUILD_SRC=${BDIR}/src
else
BUILD_SRC=$(realpath ${BUILD_SRC})
fi
fi
log_dir=$PWD/logs
mkdir -p $log_dir
if [[ ! -d $BUILD_SRC ]]; then
if $no_prompt ; then
echo -e "\e[1;31mWARNING: no build source directory at $BUILD_SRC was not found"
echo aborting...
echo -e "\e[1;31mNOTE: use '_base_' to explicitly use the in uci-docker-build repo\e[1;37m"
return 3
else
echo -e "\e[1;31mWARNING: build scripts source directory at $BUILD_SRC does not exist.\e[1;37m"
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 0 [[ $REPLY != "y" ]] && echo -e "\n" && return 0
echo BUILD_SRC=$BDIR/src
echo -e "\n\e[1;31mNOTE: use '_base_' to explicitly use the in uci-docker-build repo\e[1;37m"
fi 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 fi
pushd $BDIR || return 3
[[ ! "${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
LINUX_DISTRO=${LINUX_DISTRO:-alpine} LINUX_DISTRO=${LINUX_DISTRO:-alpine}
@ -174,10 +187,14 @@ LINUX_DISTRO=$(get_distro -d $BASE_IMAGE)
BASE_IMAGE=$LINUX_DISTRO BASE_IMAGE=$LINUX_DISTRO
fi fi
name=$1 NAME=${NAME:-$1}
RUSER=${2:-$RUSER} RUSER=${2:-$RUSER}
if [[ $NAME ]]; then
IMAGE_NAME=$([[ $RUSER ]] && echo ${RUSER}/)${name}$([[ ! $exclude_distro ]] && echo -${LINUX_DISTRO}) [[ $add_distro ]] && NAME=$NAME-${LINUX_DISTRO}
else
NAME=${LINUX_DISTRO}
fi
IMAGE_NAME=$([[ $RUSER ]] && echo ${RUSER}/)${NAME}
# TODO writing to existing tag untags existing image so write a new tag to that image then continue # TODO writing to existing tag untags existing image so write a new tag to that image then continue
# retag existing image and remove former tag # retag existing image and remove former tag
@ -202,9 +219,10 @@ export LINUX_DISTRO
export BUILD_DIR export BUILD_DIR
export KEEP export KEEP
export SYSADMIN_PW export SYSADMIN_PW
export BUST_INIT_CACHE
echo "******************************************"
echo -e "\n******************************************"
echo "Using scripts source directory at $BUILD_SRC"
echo "Building with base image: $BASE_IMAGE" echo "Building with base image: $BASE_IMAGE"
echo "Outputing to image name => $IMAGE_NAME<-arch>:${TAG:-latest}" echo "Outputing to image name => $IMAGE_NAME<-arch>:${TAG:-latest}"
echo "Linux Distro: $LINUX_DISTRO" echo "Linux Distro: $LINUX_DISTRO"
@ -214,10 +232,10 @@ if [[ $verbose ]]; then
echo -e "\n---------------------------------" echo -e "\n---------------------------------"
docker buildx bake --print $target docker buildx bake --print $target
echo -e "\n---------------------------------" echo -e "\n---------------------------------"
echo "build scripts at $scripts_dir to be copied to ${BUILD_DIR:-/build} in container ***** " echo "build scripts at $BUILD_SRC to be copied to ${BUILD_DIR:-/build} in container ***** "
ls -la $scripts_dir ls -la $BUILD_SRC
echo -e "\n----- base init script init.sh ------" echo -e "\n----- base init script init.sh ------"
cat $scripts_dir/init.sh cat $BUILD_SRC/init.sh
echo -e "\n---------------------------------" echo -e "\n---------------------------------"
fi fi
echo -e "\n***************************************" echo -e "\n***************************************"
@ -242,7 +260,7 @@ fi
# copy source directory to temporary .src/ subdirectory # copy source directory to temporary .src/ subdirectory
# MUST either be readable by all or group readable by docker group # MUST either be readable by all or group readable by docker group
rm -rf $BDIR/.src rm -rf $BDIR/.src
rsync -aAru ${scripts_dir:-src}/ $BDIR/.src rsync -aAru ${BUILD_SRC:-src}/ $BDIR/.src
ls -la $BDIR/.src ls -la $BDIR/.src
docker buildx --builder ${builder} bake ${nocache} ${target} 2>&1 | tee "$log_dir/${IMAGE_NAME//\//-}build.log" docker buildx --builder ${builder} bake ${nocache} ${target} 2>&1 | tee "$log_dir/${IMAGE_NAME//\//-}build.log"

13
examples/aliases Executable file
View File

@ -0,0 +1,13 @@
#!/bin/bash
# three ways to invoke with --no-cache
# inline
# NO_CACHE=true ./build "$@"
# with -n option (prefered)
./build -n "$@"
alias rebuild="build -nfunction_list"
# as export
#export NO_CACHE=true
#./build "$@"

View File

@ -0,0 +1,9 @@
# do not set LINUX_DISTRO if setting base image, it will be ignorned
LINUX_DISTRO=alpine
# BASE_IMAGE="dockerhubuser/mybase"
TAG=1.0.0
RUSER=dockerhubuser
REPO=my.priviate.repo.net

1
examples/example.build Normal file
View File

@ -0,0 +1 @@
dbuild -e example-build.env "$@"

28
install
View File

@ -1,3 +1,27 @@
#!/bin/bash #!/bin/bash
ln -nsf $(dirname "$(realpath "$BASH_SOURCE")")/build /opt/bin/dbuild builder=$(dirname "$(realpath "$BASH_SOURCE")")
dbuild -h target=$1
[[ $# -eq 2 ]] && target=$1/$2
target=${target:-/opt/bin/dbuild}
cmd=$(basename $target)
parent=$(dirname $target)
echo NOTE: the uci build script can be called directly at $builder
# echo "$PATH --- $parent"
declare -a a="(${PATH//:/ })"
for i in ${a[*]}; do [[ $i == $parent ]] && found=true; done
if [[ $found ]]; then
echo creating a link \'$cmd\' in \'$parent\' to \'$builder\'
if ln -ns $builder/build ${1:-/opt/bin/dbuild}; then
[[ ! $(command -v $cmd) ]] && echo FATAL: link failed $cmd not found in path \
|| echo install success: try \'$cmd -h\' now
else
echo Error creating link
echo if \': Permission denied\' 'then' run \'sudo ./install\'
fi
else
echo $parent not in current path
echo $PATH
echo link to script not created
echo Alternatively export an environment variable with $builder
echo and use that to invoke the script
fi

View File

@ -10,15 +10,21 @@ try_container () {
declare -A arch=( ["x86_64"]="" ["aarch64"]="-arm64") declare -A arch=( ["x86_64"]="" ["aarch64"]="-arm64")
local mp;local cuser; local hmp; local bind; local vname; local prod; local priv local mp;local cuser; local hmp; local bind; local vname; local prod; local priv
local dkpath; local hostmp; local evar; local hostmap; local cleanup local dkpath; local hostmp; local evar; local hostmap; local cleanup; local efile
[[ $# -lt 1 ]] && echo "image name required to try" && return 1 [[ $# -lt 1 ]] && echo "image name required to try" && return 1
declare OPTION; declare OPTARG; declare OPTIND declare OPTION; declare OPTARG; declare OPTIND
OPTIND=0 OPTIND=0
while getopts 'o:dpr:t:u:m:h:ke:' OPTION; do while getopts 'f:o:dpr:t:u:m:h:ke:s:' 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
s)
entrypoint="--entrypoint $OPTARG"
;;
f)
efile"--env-file $OPTARG"
;;
d) d)
dryrun="echo " dryrun="echo "
;; ;;
@ -35,7 +41,7 @@ try_container () {
mp=$OPTARG mp=$OPTARG
;; ;;
o) o)
options="$option $OPTARG" options=$OPTARG
;; ;;
h) h)
hmp=$OPTARG hmp=$OPTARG
@ -83,9 +89,7 @@ try_container () {
docker rm try-$name > /dev/null 2>&1 docker rm try-$name > /dev/null 2>&1
if [[ $mp ]]; then if [[ $mp ]]; then
hostmp="${hmp:-${PWD}/mnt/$mp}" hostmp="${hmp:-${PWD}/mnt/$mp}"
if [[ ! $(isAbsPath $hostmp) ]]; then [[ ! $(isAbsPath $hostmp) ]] && hostmp=$PWD/$hostmp
[[ $SDIR ]] && hostmp=$SDIR/$hostmp || hostmp=$PWD/$hostmp
fi
vname="try-$name${dir//\//-}" vname="try-$name${dir//\//-}"
mkdir -p "$hostmp" mkdir -p "$hostmp"
# echo bind $dir to volume $vname # echo bind $dir to volume $vname
@ -109,6 +113,7 @@ try_container () {
# --entrypoint /opt/scripts/entrypoint.sh \ # --entrypoint /opt/scripts/entrypoint.sh \
$dryrun docker run -i -t --rm $priv $evar $hostmap $options \ $dryrun docker run -i -t --rm $priv $evar $hostmap $options \
${entrypoint} ${efile} \
$([[ $cuser ]] && echo "--user $cuser") \ $([[ $cuser ]] && echo "--user $cuser") \
--name try-$name --hostname try-$host-$name \ --name try-$name --hostname try-$host-$name \
$([[ $mp ]] && echo "-v $vname:/$mp") \ $([[ $mp ]] && echo "-v $vname:/$mp") \
@ -134,4 +139,4 @@ try_container () {
} }
# if script was executed then call the function # if script was executed then call the function
(return 0 2>/dev/null) || try_container $@ (return 0 2>/dev/null) || try_container "$@"