move making image name to a function

support try using build environment file
refactor Dockerfile to use heredoc and RUN --mount instead of COPY
refactor Dockerfile init RUN to copy /bin and env directories from source always
user build.env and run.env for additional build and run environment
bake run.env into image
master
David Kebler 2023-04-03 14:14:17 -07:00
parent e5a57361dc
commit 16bc3bd929
20 changed files with 122 additions and 105 deletions

View File

@ -2,15 +2,13 @@
ARG BASE_IMAGE
FROM $BASE_IMAGE
ARG BASE_IMAGE
ARG KEEP
ARG SYSADMIN_PW
ARG LINUX_DISTRO=alpine
ARG BUILD_DIR=/build
WORKDIR $BUILD_DIR
WORKDIR /build
COPY .src/packages ./packages
# PACKAGES
RUN <<eot
RUN --mount=type=bind,source=.src/packages,target=/build/packages \
<<eot
echo -e "\n ************************************************* \n"
echo "Building Image from Base: $BASE_IMAGE"
echo "Distro: $LINUX_DISTRO; *****"
@ -20,17 +18,41 @@ echo -e "\n********************************************************"
eot
# END PACKAGES
COPY .src/init ./init
# INITIALIZATION
RUN echo -e "\n ************************************************* \n" \
echo "****** Initializing Image "; \
cd init; \
chmod -R +x .; \
pwd; ls -la; \
echo " ---- running init script ---"; \
/bin/bash ./init.sh; \
RUN --mount=type=bind,source=.src/init,target=/build/init \
<<eot
echo -e "\n ************************************************* \n"
echo "****** Initializing Image with build source ******"
cd init
pwd; ls -la
if [[ -d env/ ]]; then
echo "copying env/ to /opt/env"
/bin/cp -R -p env/. /opt/env
ls -la /opt/env
fi
if [[ -f env/build.env ]]; then
source /opt/env/build.env
rm -f /opt/env/build.env
echo sourced /opt/env/build.env and deleted
fi
if [[ -d bin/ ]]; then
echo "copying bin/ to /opt/bin"
/bin/cp -R -p bin/. /opt/bin
chmod -R +x /opt/bin
ls -la /opt/bin
fi
echo " ---- running init script init.sh ---"
/bin/bash ./init.sh
echo "############## Finished run init build script(s) ###########################"
if [[ -f /opt/env/run.env ]]; then
echo "sourcing /opt/env/run.env from /etc/profile"
echo "&&&&&& last 10 of /etc/profile &&&&&"
echo -e "# added from image build\nsource /opt/env/run.env" >> /etc/profile
tail /etc/profile
echo "%%%%%%%%%%%%%%%%%%%%%%%%%%%"
fi
echo -e "\n ************* End Initialzation ************************"
eot
# END INITIALIZATION
# default command

36
build
View File

@ -16,7 +16,7 @@ source $BDIR/lib/load.sh
case "$1" in
try)
shift 1
popd > /dev/null || return 2
# type try_container
try_container "$@"
return $?
;;
@ -49,11 +49,10 @@ exit_abnormal() { # Function: Exit with error.
return ${1:-1}
}
[[ -z "$PS1" ]] || no_prompt=true
overwrite=true
while getopts 'g:e:b:d:t:ncr:u:plhs:w:akvoi' OPTION; do
while getopts 'g:e:b:d:t:ncr:u:plhs:avo' OPTION; do
# echo processing: option:$OPTION argument:$OPTARG index:$OPTIND remaining:${@:$OPTIND}
case "$OPTION" in
e)
@ -69,14 +68,6 @@ while getopts 'g:e:b:d:t:ncr:u:plhs:w:akvoi' OPTION; do
# 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)
BUILD_DIR=$OPTARG
;;
b)
# CUSTOM BASE IMAGE
BASE_IMAGE=$OPTARG
@ -192,25 +183,9 @@ LINUX_DISTRO=$(docker_image_distro $BASE_IMAGE)
BASE_IMAGE=$LINUX_DISTRO
fi
if [[ $1 ]]; then
[[ $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
fi
if [[ $NAME ]]; then
[[ $append_distro ]] && NAME=$NAME-${LINUX_DISTRO}
else
echo no image name supplied using distro name $LINUX_DISTRO
NAME=${LINUX_DISTRO}
fi
IMAGE_NAME=$(make_image_name $@)
IMAGE_NAME=$([[ $RUSER ]] && echo ${RUSER}/)${NAME}
# 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
if [[ $(image_exists $IMAGE_NAME) ]]; then
if [[ $overwrite ]]; then
@ -223,16 +198,13 @@ if [[ $(image_exists $IMAGE_NAME) ]]; then
fi
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
export LINUX_DISTRO
export BUILD_DIR
export BUILD_SRC
export KEEP
export SYSADMIN_PW
export ARCH

View File

@ -5,25 +5,18 @@ variable "TAG" {
variable "LINUX_DISTRO" {
// default = "alpine"
}
variable "BUILD_DIR" {
default = ""
}
variable "IMAGE_NAME" {
// default = "alpine"
}
variable "BASE_IMAGE" {
// default = "alpine"
}
variable "KEEP" {
default = ""
}
variable "SYSADMIN_PW" {
default = ""
}
variable "ARCH" {
default = ""
}
function "tag" {
params = [suffix]
result = [format("${IMAGE_NAME}%s:${TAG}", notequal("", suffix) ? "-${suffix}" : "")]
@ -54,8 +47,6 @@ target "amd64" {
LINUX_DISTRO = "${LINUX_DISTRO}"
BASE_IMAGE = "${BASE_IMAGE}"
TAG = "${TAG}"
SCRIPTS = "${BUILD_DIR}"
KEEP = "${KEEP}"
SYSADMIN_PW = "${SYSADMIN_PW}"
}
tags = tag("")

View File

@ -9,21 +9,24 @@
try_container () {
declare -A arch=( ["x86_64"]="" ["aarch64"]="-arm64")
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 efile
[[ $# -lt 1 ]] && echo "image name required to try" && return 1
local mp;local cuser; local hmp; local vname; local prod; local priv
local hostmp; local efile; local entrypoint; local evar
local envf; local image; local append_distro; local options
declare OPTION; declare OPTARG; declare OPTIND
OPTIND=0
while getopts 'f:o:dpr:t:u:m:h:ke:s:' OPTION; do
while getopts 'lf:o:dpr:t:u:m:h:kv:e:s:' OPTION; do
# echo processing: option:$OPTION argument:$OPTARG index:$OPTIND remaining:${@:$OPTIND}
case "$OPTION" in
s)
entrypoint="--entrypoint $OPTARG"
;;
e)
if ! source_env_file $OPTARG; then return 2; fi
efile=true
;;
f)
efile"--env-file $OPTARG"
envf="--env-file $OPTARG"
;;
d)
dryrun="echo "
@ -31,6 +34,10 @@ try_container () {
u)
cuser=$OPTARG
;;
l)
# append distro name to image name
append_distro=true
;;
k)
keep=true
;;
@ -46,7 +53,7 @@ try_container () {
h)
hmp=$OPTARG
;;
e)
v)
evar="-e $OPTARG"
;;
t)
@ -67,11 +74,13 @@ try_container () {
shift $((OPTIND - 1))
[[ ! $1 ]] && echo must supply an image to try && return 1
[[ ! $efile ]] && source_env_file
# user=${2:-$ruser}
image=$1
shift 1
image=$(make_image_name $@)
echo image name to try: $image
[[ ! $image ]] && echo must supply an image to try && return 1
if [[ $prod ]]; then
echo removing any local copy of image $image
@ -110,10 +119,9 @@ try_container () {
echo starting container with image: $image, and name $name
echo at container prompt type \'exit\' to exit from shell and remove trial container
# --entrypoint /opt/scripts/entrypoint.sh \
$dryrun docker run -i -t --rm $priv $evar $hostmap $options \
${entrypoint} ${efile} \
$dryrun docker run -i -t --rm $priv $evar $options \
${entrypoint} ${evnf} \
$([[ $cuser ]] && echo "--user $cuser") \
--name try-$name --hostname try-$host-$name \
$([[ $mp ]] && echo "-v $vname:/$mp") \

View File

@ -56,6 +56,7 @@ env_file () {
}
read_env_file() {
local evar
while read line; do
evar=$(echo $line | cut -d '=' -f1)
if [[ ! ${!evar} ]]; then
@ -74,7 +75,7 @@ read_env_file() {
}
source_env_file () {
local default
local default; local efile
if efile=$(env_file $1); then
[[ $efile == ".env" ]] && default=true
[[ ! $(isAbsPath $efile) ]] && efile=$(realpath $efile)
@ -114,4 +115,28 @@ echo $(cat $temp | tr [:upper:] [:lower:] | grep -Poi '(debian|ubuntu|red hat|ce
rm $temp
}
(return 0 2>/dev/null) || docker_image_distro $@
make_image_name () {
# echo making image name $@
# echo name: $NAME
# echo user: $RUSER
# echo distro $LINUX_DISTRO
if [[ $1 ]]; then
# [[ $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
fi
if [[ $NAME ]]; then
[[ $append_distro ]] && NAME=$NAME-${LINUX_DISTRO}
else
# echo no image name supplied using distro name ${LINUX_DISTRO:-alpine}
NAME=${LINUX_DISTRO:-alpine}
fi
echo $([[ $RUSER ]] && echo ${RUSER}/)${NAME}
}

View File

@ -7,3 +7,4 @@ for f in $libdir/cmds/*; do
source "$f"
done

View File

@ -1,11 +1,8 @@
#!/bin/bash
# echo arguments in start script: "$@"
source $SCRIPTS_DIR/container.env
source $SCRIPTS_DIR/host-id-map.sh
source /opt/env/run.env
source /opt/bin/host-id-map.sh
case "$1" in
shell_update)
echo updating shell repos
;;
cmd)
shift 1
"$@"
@ -13,7 +10,7 @@ case "$1" in
script)
shift 1
module_load path
script=$([[ $(isAbsPath $1) ]] && echo $1 || echo $SCRIPTS_DIR/$1)
script=$([[ $(isAbsPath $1) ]] && echo $1 || echo opt/bin/$1)
shift 1
/bin/bash $script "$@"
;;

1
src/init/bin/entrypoint Symbolic link
View File

@ -0,0 +1 @@
base-entrypoint

View File

@ -0,0 +1,7 @@
#!/bin/bash
if [[ $HOST_MAP ]]; then
echo changing ownership of $VOLUME_DIRS to $HOST_MAP
declare usesudo
[[ ! $EUID -eq 0 ]] && usesudo=sudo
$usesudo chown -R $HOST_MAP $VOLUME_DIRS
fi

1
src/init/bin/start Symbolic link
View File

@ -0,0 +1 @@
base-start

View File

@ -20,14 +20,12 @@ export SHELL=/bin/bash
mkdir -p /home/sysadmin/shell
/bin/bash /shell/base/install/install.sh sysadmin
echo "******** uci shell install complete **********"
/bin/bash -l -c "module_load dir; dir_copy scripts /opt"
echo installed scripts in /opt/scripts
ls -la /opt/scripts
source /opt/scripts/container.env
# make essential directories
mkdir -p $SHARED_DIRS /opt/scripts /opt/bin
chown -R host:host /home/sysadmin/shell $SHARED_DIRS /opt/scripts /opt/bin
chmod -R g+rw /home/sysadmin/shell $SHARED_DIRS /opt/scripts /opt/bin
mkdir -p $VOLUME_DIRS /opt/bin
chown -R :host /home/sysadmin/shell
chown -R host:host $VOLUME_DIRS
chmod -R g+rw /home/sysadmin/shell $VOLUME_DIRS
ls -la /home/sysadmin/shell $VOLUME_DIRS
echo installing and running image distro info
/bin/bash install-info.sh
/opt/scripts/info
/bin/bash install-os-info.sh
/opt/bin/os-info

View File

@ -1,8 +1,7 @@
#!/bin/bash
wget --quiet -O /opt/scripts/info https://git.io/vaHfR > /dev/null
if [[ -f /opt/scripts/info ]]; then
chmod +x /opt/scripts/info
ln -sf /opt/scripts/info /opt/bin/
wget --quiet -O /opt/bin/os-info https://git.io/vaHfR > /dev/null
if [[ -f /opt/bin/os-info ]]; then
chmod +x /opt/bin/os-info
else
echo Failed to download
echo "https://raw.githubusercontent.com/KittyKatt/screenFetch/master/screenfetch-dev"

View File

@ -1,2 +0,0 @@
export SHARED_DIRS="/shell /opt /data"
export INITIAL_DIR=/opt/scripts

View File

@ -1,6 +0,0 @@
#!/bin/bash
source $SCRIPTS_DIR/container.env
if [[ $HOST_MAP ]]; then
echo changing ownership of $SHARED_DIRS to $HOST_MAP
$([[ ! $EUID -eq 0 ]] && echo sudo) chown -R $HOST_MAP $SHARED_DIRS
fi

4
src/init/env/build.env vendored Normal file
View File

@ -0,0 +1,4 @@
# anything in here will be sourced during build
# allows easy custom environment variables
source /opt/env/run.env
# export SOMEVAR="something"

3
src/init/env/run.env vendored Normal file
View File

@ -0,0 +1,3 @@
# added /opt/run.env to the end of /etc/profile
export VOLUME_DIRS="/shell /opt /data"
export INITIAL_DIR=/opt

View File

@ -1,5 +1,5 @@
#!/bin/sh
echo "entry init.sh script in $BUILD_DIR"
echo "running base init.sh script in /build"
# remove other distro files
# find $PWD -maxdepth 1 -type d ! -path $PWD ! -name ${LINUX_DISTRO} ! -name common -exec rm -rf {} +
cd ${LINUX_DISTRO} || exit 1
@ -19,8 +19,3 @@
echo returned from ${LINUX_DISTRO} post common script
fi
cd ..
if [ -z $KEEP ]; then
echo removing $BUILD_DIR directory used for build
cd /opt && rm -rf $BUILD_DIR
fi

View File

@ -3,4 +3,5 @@ curl
git
rsync
sudo
nano
nano
coreutils