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
# PACKAGES
RUN \
echo -e "\n ************************************************* \n"\
echo "****** Building Image from Base: $BASE_IMAGE; : Distro: $LINUX_DISTRO; *****"; \
echo " ---- running packages install script ---"; \
cd packages; /bin/sh ./packages.sh; cd ..; \
echo -e "\n********************************************************"
RUN <<eot
echo -e "\n ************************************************* \n"
echo "Building Image from Base: $BASE_IMAGE"
echo "Distro: $LINUX_DISTRO; *****"
echo " ---- running packages install script ---"
cd packages; /bin/sh ./packages.sh; cd ..
echo -e "\n********************************************************"
eot
# END PACKAGES
COPY .src/init ./init

54
build
View File

@ -2,7 +2,7 @@
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 efile
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)
try=true
;;
# TODO: push after build
# p)
# push=true
# ;;
p)
push=true
;;
n)
nocache="--no-cache"
;;
@ -130,8 +129,6 @@ shift $((OPTIND - 1))
[[ ! $efile ]] && source_env_file
TARGET=${TARGET:-dev}
# processing the build source directory
if [[ ! $BUILD_SRC ]]; then
echo no BUILD_SRC directory specified
@ -196,13 +193,15 @@ LINUX_DISTRO=$(docker_image_distro $BASE_IMAGE)
fi
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
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
if [[ $NAME ]]; then
[[ $add_distro ]] && NAME=$NAME-${LINUX_DISTRO}
[[ $append_distro ]] && NAME=$NAME-${LINUX_DISTRO}
else
echo no image name supplied using 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 == *:* ]] && echo :latest)
ARCH=$(get_arch)
export BASE_IMAGE
export TAG
export IMAGE_NAME
@ -234,14 +235,18 @@ export LINUX_DISTRO
export BUILD_DIR
export KEEP
export SYSADMIN_PW
export ARCH
echo -e "\e[1;37m********************"
echo "Using scripts source directory at $BUILD_SRC"
echo "Building with base image: $BASE_IMAGE"
#todo based on target form image names
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 "Using build target: $TARGET"
echo "Using build target: ${TARGET:-default}"
echo "Build Command: docker buildx --builder ${builder} bake ${nocache} ${TARGET}"
if [[ $verbose ]]; then
echo -e "\n---------------------------------"
@ -261,7 +266,7 @@ if [[ ! $no_prompt ]]; then
fi
builder=default
if [ $TARGET == "deploy" ]; then
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
@ -277,7 +282,7 @@ fi
rm -rf $BDIR/.src
rsync -aAru ${BUILD_SRC:-src}/ $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"
[[ $? == 0 ]] && echo succcess building image $IMAGE_NAME || exit_abnormal 5
@ -290,6 +295,18 @@ if [[ ($try || $TARGET == "dev") ]] && [[ ! $no_prompt ]]; then
try_container -m opt $([[ $TARGET == "deploy" ]] && echo -p) $IMAGE_NAME
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
@ -297,12 +314,3 @@ fi
# 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" {
default = ""
}
variable "ARCH" {
default = ""
}
function "tag" {
params = [suffix]
@ -27,24 +30,24 @@ function "tag" {
}
# groups
group "dev" {
targets = ["amd"]
targets = ["${ARCH}"]
}
group "default" {
targets = ["amd"]
targets = ["${ARCH}"]
}
group "deploy" {
targets = ["multi"]
}
group "private" {
targets = [
"amd",
"arm"
"amd64",
"arm64"
]
}
# intended for use with default local docker builder
# uses 'dev' group in docker-bake.hcl
# assume dev machine is amd64 machine
target "amd" {
target "amd64" {
context = "."
dockerfile = "Dockerfile"
args = {
@ -61,8 +64,8 @@ target "amd" {
# intended for use with default docker driver on an arm64 machine
# use with 'arm' group
target "arm" {
inherits = ["amd"]
target "arm64" {
inherits = ["amd64"]
tags = tag("arm64")
platforms = ["linux/arm64"]
}
@ -70,7 +73,7 @@ target "arm" {
# must use with docker-container driver for multiarch image deployment to registry
# uses 'deploy' group in docker-bake.hcl
target "multi" {
inherits = ["amd"]
inherits = ["amd64"]
tags = tag("")
platforms = ["linux/amd64", "linux/arm64"]
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")")
target=$1
[[ $# -eq 2 ]] && target=$1/$2
target=${target:-/opt/bin/dbuild}
target=${target:-/opt/bin/udbuild}
cmd=$(basename $target)
parent=$(dirname $target)
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
if [[ $found ]]; then
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 \
|| echo install success: try \'$cmd -h\' now
else
@ -22,6 +22,8 @@ if [[ $found ]]; then
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
echo "add the following export somewhere in your shell (e.g. ~/.bashrc)"
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

View File

@ -7,11 +7,16 @@ image_push () {
# $1 name, $2 user(or repo), $3 repo
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}
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)
TAG=$OPTARG
;;
@ -21,8 +26,8 @@ u)
r)
REPO=$OPTARG
;;
d) # pull image from dockerhub if not available
hub=true
p) # pull image from dockerhub if not available
PULL=true
;;
*) echo unknown run option -$OPTARG
echo "USAGE: start <options>"
@ -35,34 +40,35 @@ shift $((OPTIND - 1))
# image tag
name=${1:-$NAME}
name=${NAME:-$LINUX_DISTRO}
name=${NAME:-alpine}
user=${2:-$RUSER}
repo=${3:-$REPO}
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
if ! docker image inspect $source2 > /dev/null 2>&1; then
echo "no image $source2 available to push"
[[ ! $hub ]] &&echo use -h to attempt to pull image from hub.docker.com
if [[ $hub ]]; then
[[ ! $PULL ]] && echo NOTE: use -p or set PULL=true to attempt to pull image from hub.docker.com
if [[ $PULL ]]; then
echo attempting to pull $source2
if ! docker pull $source2 > /dev/null 2>&1; then
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
if ! docker pull $platform $source > /dev/null 2>&1; then
echo unable to pull $platform $source, aborting
exit 2
else
hub=downloaded
PULL=downloaded
source2=$source
fi
else
hub=downloaded
PULL=downloaded
fi
else
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
fi
if [[ $hub == downloaded ]]; then
if [[ $PULL == downloaded ]]; then
echo removing $source2 downloaded from hub.docker.com docker
docker image rm $source2 > /dev/null 2>&1
fi

View File

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