new: add determining local host architecture for local build

use heredoc for RUN command
improve install script
add .env example
master
David Kebler 2023-04-01 07:17:43 -07:00
parent da61c57306
commit e5a57361dc
7 changed files with 96 additions and 56 deletions

View File

@ -10,12 +10,14 @@ WORKDIR $BUILD_DIR
COPY .src/packages ./packages COPY .src/packages ./packages
# PACKAGES # PACKAGES
RUN \ RUN <<eot
echo -e "\n ************************************************* \n"\ echo -e "\n ************************************************* \n"
echo "****** Building Image from Base: $BASE_IMAGE; : Distro: $LINUX_DISTRO; *****"; \ echo "Building Image from Base: $BASE_IMAGE"
echo " ---- running packages install script ---"; \ echo "Distro: $LINUX_DISTRO; *****"
cd packages; /bin/sh ./packages.sh; cd ..; \ echo " ---- running packages install script ---"
echo -e "\n********************************************************" cd packages; /bin/sh ./packages.sh; cd ..
echo -e "\n********************************************************"
eot
# END PACKAGES # END PACKAGES
COPY .src/init ./init COPY .src/init ./init

56
build
View File

@ -2,7 +2,7 @@
docker_image_build () { docker_image_build () {
local targets=(dev arm amd deploy private multi) local targets=(dev arm64 amd64 deploy private multi)
local verbose; local log_dir; local no_prompt local verbose; local log_dir; local no_prompt
local efile local efile
declare OPTION; declare OPTARG; declare OPTIND declare OPTION; declare OPTARG; declare OPTIND
@ -105,10 +105,9 @@ while getopts 'g:e:b:d:t:ncr:u:plhs:w:akvoi' OPTION; do
c) c)
try=true try=true
;; ;;
# TODO: push after build p)
# p) push=true
# push=true ;;
# ;;
n) n)
nocache="--no-cache" nocache="--no-cache"
;; ;;
@ -130,8 +129,6 @@ shift $((OPTIND - 1))
[[ ! $efile ]] && source_env_file [[ ! $efile ]] && source_env_file
TARGET=${TARGET:-dev}
# processing the build source directory # processing the build source directory
if [[ ! $BUILD_SRC ]]; then if [[ ! $BUILD_SRC ]]; then
echo no BUILD_SRC directory specified echo no BUILD_SRC directory specified
@ -196,13 +193,15 @@ LINUX_DISTRO=$(docker_image_distro $BASE_IMAGE)
fi fi
if [[ $1 ]]; then if [[ $1 ]]; then
[[ $NAME ]] && echo changing image name from $NAME to $1 && NAME=$1 [[ $NAME ]] && echo changing image name from $NAME to $1
NAME=$1
fi fi
if [[ $2 ]]; then if [[ $2 ]]; then
[[ $RUSER ]] && echo changing image username from $RUSER to $2 && RUSER=$2 [[ $RUSER ]] && echo changing image username from $RUSER to $2
RUSER=$2
fi fi
if [[ $NAME ]]; then if [[ $NAME ]]; then
[[ $add_distro ]] && NAME=$NAME-${LINUX_DISTRO} [[ $append_distro ]] && NAME=$NAME-${LINUX_DISTRO}
else else
echo no image name supplied using distro name $LINUX_DISTRO echo no image name supplied using distro name $LINUX_DISTRO
NAME=${LINUX_DISTRO} NAME=${LINUX_DISTRO}
@ -227,6 +226,8 @@ fi
# BASE_IMAGE=$([[ $BASE_IMAGE == *:* ]] && echo $BASE_IMAGE || echo $BASE_IMAGE:latest) # BASE_IMAGE=$([[ $BASE_IMAGE == *:* ]] && echo $BASE_IMAGE || echo $BASE_IMAGE:latest)
#$([[ ! $BASE_IMAGE == *:* ]] && echo :latest) #$([[ ! $BASE_IMAGE == *:* ]] && echo :latest)
ARCH=$(get_arch)
export BASE_IMAGE export BASE_IMAGE
export TAG export TAG
export IMAGE_NAME export IMAGE_NAME
@ -234,14 +235,18 @@ export LINUX_DISTRO
export BUILD_DIR export BUILD_DIR
export KEEP export KEEP
export SYSADMIN_PW export SYSADMIN_PW
export ARCH
echo -e "\e[1;37m********************" echo -e "\e[1;37m********************"
echo "Using scripts source directory at $BUILD_SRC" echo "Using scripts source directory at $BUILD_SRC"
echo "Building with base image: $BASE_IMAGE" echo "Building with base image: $BASE_IMAGE"
#todo based on target form image names
echo "Outputing to image name => $IMAGE_NAME<-arch>:${TAG:-latest}" echo "Outputing to image name => $IMAGE_NAME<-arch>:${TAG:-latest}"
[[ $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"
echo "Linux Distro: $LINUX_DISTRO" echo "Linux Distro: $LINUX_DISTRO"
echo "Using build target: $TARGET" echo "Using build target: ${TARGET:-default}"
echo "Build Command: docker buildx --builder ${builder} bake ${nocache} ${TARGET}" echo "Build Command: docker buildx --builder ${builder} bake ${nocache} ${TARGET}"
if [[ $verbose ]]; then if [[ $verbose ]]; then
echo -e "\n---------------------------------" echo -e "\n---------------------------------"
@ -261,7 +266,7 @@ if [[ ! $no_prompt ]]; then
fi fi
builder=default builder=default
if [ $TARGET == "deploy" ]; then if [[ $TARGET == "deploy" ]]; then
builder=deploy builder=deploy
if ! docker buildx ls | grep -q deploy ; then if ! docker buildx ls | grep -q deploy ; then
echo multiarch deploy builder does not exist, creating with docker-container driver echo multiarch deploy builder does not exist, creating with docker-container driver
@ -277,7 +282,7 @@ fi
rm -rf $BDIR/.src rm -rf $BDIR/.src
rsync -aAru ${BUILD_SRC:-src}/ $BDIR/.src rsync -aAru ${BUILD_SRC:-src}/ $BDIR/.src
ls -la $BDIR/.src ls -la $BDIR/.src
echo running build command: docker buildx --builder ${builder} bake ${nocache} ${TARGET}
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"
[[ $? == 0 ]] && echo succcess building image $IMAGE_NAME || exit_abnormal 5 [[ $? == 0 ]] && echo succcess building image $IMAGE_NAME || exit_abnormal 5
@ -290,19 +295,22 @@ if [[ ($try || $TARGET == "dev") ]] && [[ ! $no_prompt ]]; then
try_container -m opt $([[ $TARGET == "deploy" ]] && echo -p) $IMAGE_NAME try_container -m opt $([[ $TARGET == "deploy" ]] && echo -p) $IMAGE_NAME
fi fi
} 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
}
# if script was executed then call the function # if script was executed then call the function
(return 0 2>/dev/null) || docker_image_build $@ (return 0 2>/dev/null) || docker_image_build $@
# 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

View File

@ -20,6 +20,9 @@ variable "KEEP" {
variable "SYSADMIN_PW" { variable "SYSADMIN_PW" {
default = "" default = ""
} }
variable "ARCH" {
default = ""
}
function "tag" { function "tag" {
params = [suffix] params = [suffix]
@ -27,24 +30,24 @@ function "tag" {
} }
# groups # groups
group "dev" { group "dev" {
targets = ["amd"] targets = ["${ARCH}"]
} }
group "default" { group "default" {
targets = ["amd"] targets = ["${ARCH}"]
} }
group "deploy" { group "deploy" {
targets = ["multi"] targets = ["multi"]
} }
group "private" { group "private" {
targets = [ targets = [
"amd", "amd64",
"arm" "arm64"
] ]
} }
# intended for use with default local docker builder # intended for use with default local docker builder
# uses 'dev' group in docker-bake.hcl # uses 'dev' group in docker-bake.hcl
# assume dev machine is amd64 machine # assume dev machine is amd64 machine
target "amd" { target "amd64" {
context = "." context = "."
dockerfile = "Dockerfile" dockerfile = "Dockerfile"
args = { args = {
@ -61,8 +64,8 @@ target "amd" {
# intended for use with default docker driver on an arm64 machine # intended for use with default docker driver on an arm64 machine
# use with 'arm' group # use with 'arm' group
target "arm" { target "arm64" {
inherits = ["amd"] inherits = ["amd64"]
tags = tag("arm64") tags = tag("arm64")
platforms = ["linux/arm64"] platforms = ["linux/arm64"]
} }
@ -70,7 +73,7 @@ target "arm" {
# must use with docker-container driver for multiarch image deployment to registry # must use with docker-container driver for multiarch image deployment to registry
# uses 'deploy' group in docker-bake.hcl # uses 'deploy' group in docker-bake.hcl
target "multi" { target "multi" {
inherits = ["amd"] inherits = ["amd64"]
tags = tag("") tags = tag("")
platforms = ["linux/amd64", "linux/arm64"] platforms = ["linux/amd64", "linux/arm64"]
output = ["type=registry"] output = ["type=registry"]

6
examples/.env Normal file
View File

@ -0,0 +1,6 @@
SYSADMIN_PW=ucommandit
# default is alpine
# LINUX_DISTRO=alpine
RUSER=ucommandit
# TARGET=deploy
BUILD_SRC=../src

10
install
View File

@ -2,7 +2,7 @@
builder=$(dirname "$(realpath "$BASH_SOURCE")") builder=$(dirname "$(realpath "$BASH_SOURCE")")
target=$1 target=$1
[[ $# -eq 2 ]] && target=$1/$2 [[ $# -eq 2 ]] && target=$1/$2
target=${target:-/opt/bin/dbuild} target=${target:-/opt/bin/udbuild}
cmd=$(basename $target) cmd=$(basename $target)
parent=$(dirname $target) parent=$(dirname $target)
echo NOTE: the uci build script can be called directly at $builder echo NOTE: the uci build script can be called directly at $builder
@ -11,7 +11,7 @@ declare -a a="(${PATH//:/ })"
for i in ${a[*]}; do [[ $i == $parent ]] && found=true; done for i in ${a[*]}; do [[ $i == $parent ]] && found=true; done
if [[ $found ]]; then if [[ $found ]]; then
echo creating a link \'$cmd\' in \'$parent\' to \'$builder\' echo creating a link \'$cmd\' in \'$parent\' to \'$builder\'
if ln -ns $builder/build ${1:-/opt/bin/dbuild}; then if ln -ns $builder/build $target; then
[[ ! $(command -v $cmd) ]] && echo FATAL: link failed $cmd not found in path \ [[ ! $(command -v $cmd) ]] && echo FATAL: link failed $cmd not found in path \
|| echo install success: try \'$cmd -h\' now || echo install success: try \'$cmd -h\' now
else else
@ -22,6 +22,8 @@ if [[ $found ]]; then
echo $parent not in current path echo $parent not in current path
echo $PATH echo $PATH
echo link to script not created echo link to script not created
echo Alternatively export an environment variable with $builder echo "add the following export somewhere in your shell (e.g. ~/.bashrc)"
echo and use that to invoke the script echo "export UDBUILD=$builder/build"
echo 'and then use $UDBUILD to invoke the build script ( e.g $UDBUILD -e mybuild.env)'
echo "or rerun this script using a directory in the system path (e.g ./install /usr/bin build)"
fi fi

View File

@ -7,11 +7,16 @@ image_push () {
# $1 name, $2 user(or repo), $3 repo # $1 name, $2 user(or repo), $3 repo
declare OPTION; declare OPTARG; declare OPTIND declare OPTION; declare OPTARG; declare OPTIND
while getopts 'e:dt:r:u:' OPTION; do while getopts 'ae:pt:r:u:' 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) if ! source_env_file $OPTARG ; then return 2; fi
;; e)
if ! source_env_file $OPTARG; then return 2; fi
;;
a)
ARM=arm64
;;
t) t)
TAG=$OPTARG TAG=$OPTARG
;; ;;
@ -21,8 +26,8 @@ u)
r) r)
REPO=$OPTARG REPO=$OPTARG
;; ;;
d) # pull image from dockerhub if not available p) # pull image from dockerhub if not available
hub=true PULL=true
;; ;;
*) echo unknown run option -$OPTARG *) echo unknown run option -$OPTARG
echo "USAGE: start <options>" echo "USAGE: start <options>"
@ -35,34 +40,35 @@ shift $((OPTIND - 1))
# image tag # image tag
name=${1:-$NAME} name=${1:-$NAME}
name=${NAME:-$LINUX_DISTRO}
name=${NAME:-alpine}
user=${2:-$RUSER} user=${2:-$RUSER}
repo=${3:-$REPO} repo=${3:-$REPO}
source=$([[ $user ]] && echo ${user}/)$name:${TAG:-latest} source=$([[ $user ]] && echo ${user}/)$name:${TAG:-latest}
source2=$([[ $arm ]] && echo ${source//:/-arm64:} || echo $source) source2=$([[ $ARM ]] && echo ${source//:/-arm64:} || echo $source)
target=$([[ $repo ]] && echo ${repo}/)$source2 target=$([[ $repo ]] && echo ${repo}/)$source2
if ! docker image inspect $source2 > /dev/null 2>&1; then if ! docker image inspect $source2 > /dev/null 2>&1; then
echo "no image $source2 available to push" echo "no image $source2 available to push"
[[ ! $hub ]] &&echo use -h to attempt to pull image from hub.docker.com [[ ! $PULL ]] && echo NOTE: use -p or set PULL=true to attempt to pull image from hub.docker.com
if [[ $hub ]]; then if [[ $PULL ]]; then
echo attempting to pull $source2 echo attempting to pull $source2
if ! docker pull $source2 > /dev/null 2>&1; then if ! docker pull $source2 > /dev/null 2>&1; then
echo unable to pull $source2 from hub.docker.com echo unable to pull $source2 from hub.docker.com
platform=$([[ $arm ]] && echo "--platform linux/$arm") platform=$([[ $ARM ]] && echo "--platform linux/$ARM")
echo trying to pull $platform $source from hub.docker.com echo trying to pull $platform $source from hub.docker.com
if ! docker pull $platform $source > /dev/null 2>&1; then if ! docker pull $platform $source > /dev/null 2>&1; then
echo unable to pull $platform $source, aborting echo unable to pull $platform $source, aborting
exit 2 exit 2
else else
hub=downloaded PULL=downloaded
source2=$source source2=$source
fi fi
else else
hub=downloaded PULL=downloaded
fi fi
else else
exit 1 exit 1
@ -75,7 +81,7 @@ if ! docker image push $target > /dev/null 2>&1; then
echo ERROR: unable to push $source2 to repository at $1 as $target echo ERROR: unable to push $source2 to repository at $1 as $target
fi fi
if [[ $hub == downloaded ]]; then if [[ $PULL == downloaded ]]; then
echo removing $source2 downloaded from hub.docker.com docker echo removing $source2 downloaded from hub.docker.com docker
docker image rm $source2 > /dev/null 2>&1 docker image rm $source2 > /dev/null 2>&1
fi fi

View File

@ -1,5 +1,19 @@
#!/bin/bash #!/bin/bash
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() { isAbsPath() {
if [[ "${1:0:1}" == / || "${1:0:2}" == ~[/a-z] ]] if [[ "${1:0:1}" == / || "${1:0:2}" == ~[/a-z] ]]
then then
@ -89,7 +103,6 @@ source_env_file () {
fi fi
return 2 return 2
fi fi
} }
docker_image_distro() { docker_image_distro() {