diff --git a/Dockerfile b/Dockerfile index a4bd315..32a9ba7 100644 --- a/Dockerfile +++ b/Dockerfile @@ -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 <> /etc/profile + tail /etc/profile + echo "%%%%%%%%%%%%%%%%%%%%%%%%%%%" + fi echo -e "\n ************* End Initialzation ************************" +eot # END INITIALIZATION # default command diff --git a/build b/build index 24357fa..4213537 100755 --- a/build +++ b/build @@ -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 diff --git a/docker-bake.hcl b/docker-bake.hcl index c53bba2..c5bd5af 100644 --- a/docker-bake.hcl +++ b/docker-bake.hcl @@ -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("") diff --git a/lib/cmds/try b/lib/cmds/try index 91c75e7..1b216fc 100755 --- a/lib/cmds/try +++ b/lib/cmds/try @@ -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") \ diff --git a/lib/helpers.lib b/lib/helpers.lib index 1432276..bbc02e7 100755 --- a/lib/helpers.lib +++ b/lib/helpers.lib @@ -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 $@ \ No newline at end of file +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} + +} \ No newline at end of file diff --git a/lib/load.sh b/lib/load.sh index 71296a6..1a1acde 100755 --- a/lib/load.sh +++ b/lib/load.sh @@ -7,3 +7,4 @@ for f in $libdir/cmds/*; do source "$f" done + diff --git a/src/init/common/scripts/entrypoint.sh b/src/init/bin/base-entrypoint similarity index 100% rename from src/init/common/scripts/entrypoint.sh rename to src/init/bin/base-entrypoint diff --git a/src/init/common/scripts/start.sh b/src/init/bin/base-start similarity index 54% rename from src/init/common/scripts/start.sh rename to src/init/bin/base-start index 39f7426..bff3855 100755 --- a/src/init/common/scripts/start.sh +++ b/src/init/bin/base-start @@ -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 "$@" ;; diff --git a/src/init/bin/entrypoint b/src/init/bin/entrypoint new file mode 120000 index 0000000..12ffe33 --- /dev/null +++ b/src/init/bin/entrypoint @@ -0,0 +1 @@ +base-entrypoint \ No newline at end of file diff --git a/src/init/bin/host-id-map.sh b/src/init/bin/host-id-map.sh new file mode 100644 index 0000000..cf4b1b8 --- /dev/null +++ b/src/init/bin/host-id-map.sh @@ -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 \ No newline at end of file diff --git a/src/init/bin/start b/src/init/bin/start new file mode 120000 index 0000000..1f0173b --- /dev/null +++ b/src/init/bin/start @@ -0,0 +1 @@ +base-start \ No newline at end of file diff --git a/src/init/common/init.sh b/src/init/common/init.sh index bc84358..74a2dbc 100644 --- a/src/init/common/init.sh +++ b/src/init/common/init.sh @@ -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 diff --git a/src/init/common/install-info.sh b/src/init/common/install-os-info.sh similarity index 50% rename from src/init/common/install-info.sh rename to src/init/common/install-os-info.sh index 184b2b5..9a87fcf 100644 --- a/src/init/common/install-info.sh +++ b/src/init/common/install-os-info.sh @@ -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" diff --git a/src/init/common/scripts/container.env b/src/init/common/scripts/container.env deleted file mode 100644 index 11e4fce..0000000 --- a/src/init/common/scripts/container.env +++ /dev/null @@ -1,2 +0,0 @@ -export SHARED_DIRS="/shell /opt /data" -export INITIAL_DIR=/opt/scripts \ No newline at end of file diff --git a/src/init/common/scripts/host-id-map.sh b/src/init/common/scripts/host-id-map.sh deleted file mode 100644 index 739bb93..0000000 --- a/src/init/common/scripts/host-id-map.sh +++ /dev/null @@ -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 \ No newline at end of file diff --git a/src/init/common/scripts/shell-update.sh b/src/init/common/scripts/shell-update.sh deleted file mode 100644 index e69de29..0000000 diff --git a/src/init/env/build.env b/src/init/env/build.env new file mode 100644 index 0000000..4bf0bd2 --- /dev/null +++ b/src/init/env/build.env @@ -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" diff --git a/src/init/env/run.env b/src/init/env/run.env new file mode 100644 index 0000000..4efb12d --- /dev/null +++ b/src/init/env/run.env @@ -0,0 +1,3 @@ +# added /opt/run.env to the end of /etc/profile +export VOLUME_DIRS="/shell /opt /data" +export INITIAL_DIR=/opt \ No newline at end of file diff --git a/src/init/init.sh b/src/init/init.sh index 3896c39..1e7c0ff 100644 --- a/src/init/init.sh +++ b/src/init/init.sh @@ -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 \ No newline at end of file diff --git a/src/packages/common/packages b/src/packages/common/packages index b1836d4..b6d1ee0 100644 --- a/src/packages/common/packages +++ b/src/packages/common/packages @@ -3,4 +3,5 @@ curl git rsync sudo -nano \ No newline at end of file +nano +coreutils \ No newline at end of file