Skip to content

Commit

Permalink
updates: make safer, add upstream pull option (#15)
Browse files Browse the repository at this point in the history
Co-authored-by: ook37 <[email protected]>
  • Loading branch information
oklopfer and ook37 committed Jan 11, 2024
1 parent bb38d1a commit d2acd1f
Show file tree
Hide file tree
Showing 2 changed files with 100 additions and 45 deletions.
25 changes: 19 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,17 @@ Options:
-C/-c, --clean Use --no-cache during Docker image build
(default: disabled)
-F/-f, --file Promptless: Create only the Dockerfile, with instructions
-F/-f, --file Create only the Dockerfile, with instructions
(default: prompted)
-B/-b, --build Create both the Dockerfile and the Docker image
(default: prompted)
-T/-t, --test Start the Docker image up after build is complete
(default: disabled)
-P/-p, --pull Pull a Docker image from the upstream registry
(options: --version, default: always uses --arch auto)
-T/-t, --test Start the Docker image up after build or pull is complete
(default: disabled or prompted)
-W/-w, --wipe Hazardous: Delete all related Dockerfiles and Docker images
(default: always prompted)
Expand All @@ -33,13 +36,23 @@ Options:
Examples:
rhino-docker-builder -f
Creates the file Dockerfile-RhinoLinux-YYYYMMDD for building an image
Creates the file Dockerfile-RhinoLinux-YYYYMMDD for building the image
rhino-linux/docker:YYYYMMDD, with instructions on how to build and run it.
Note: if no options are passed, this is the default function, but users
will be asked if they would like to build and test the image.
rhino-docker-builder -b -t -c -v 2023.4 -a x86_64
Builds and boots amd64/rhino-linux/docker:2023.4 from scratch.
Builds and starts amd64/rhino-linux/docker:2023.4 from scratch.
Note: the version tag may not correlate with the actual Rhino Linux version.
This option is meant for easily publishing images for specific milestones.
rhino-docker-builder -p -t -v latest
Pulls and starts ghcr.io/rhino-linux/docker:latest.
```
120 changes: 81 additions & 39 deletions rhino-docker-builder
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,12 @@
dateiniso="$(date +%Y%m%d)"
shopt -s extglob

if [[ ${PWD} == @(/usr/bin|/usr/local/bin) ]]; then
dock_dir="/tmp"
else
dock_dir="${PWD}"
fi

# Colors
if [[ -z $NO_COLOR ]]; then
export RED=$'\033[0;31m'
Expand Down Expand Up @@ -41,8 +47,11 @@ ${BYellow}Options:${NC}
${BGreen}-B/-b, --build${NC} Create both the Dockerfile and the Docker image
(default: ${CYAN}prompted${NC})
${BGreen}-T/-t, --test${NC} Start the Docker image up after build is complete
(default: ${CYAN}disabled${NC})
${BGreen}-P/-p, --pull${NC} Pull a Docker image from the upstream registry
(options: ${CYAN}--version${NC}, default: ${CYAN}always uses --arch auto${NC})
${BGreen}-T/-t, --test${NC} Start the Docker image up after build or pull is complete
(default: ${CYAN}disabled or prompted${NC})
${BGreen}-W/-w, --wipe${NC} ${YELLOW}Hazardous:${NC} Delete all related Dockerfiles and Docker images
(default: ${CYAN}always prompted${NC})
Expand All @@ -56,19 +65,28 @@ ${BYellow}Description:${NC}
${BYellow}Examples:${NC}
${BPurple}${0##*/} -f${NC}
Creates the file ${BGreen}Dockerfile-RhinoLinux-${dateiniso}${NC} for building an image
Creates the file ${BGreen}Dockerfile-RhinoLinux-${dateiniso}${NC} for building the image
${BGreen}rhino-linux/docker:${dateiniso}${NC}, with instructions on how to build and run it.
${BYellow}Note:${NC} if no options are passed, this is the default function, but users
will be asked if they would like to build and test the image.
${BPurple}${0##*/} -b -t -c -v 2023.4 -a x86_64${NC}
Builds and boots ${BGreen}amd64/rhino-linux/docker:2023.4${NC} from scratch.
Builds and starts ${BGreen}amd64/rhino-linux/docker:2023.4${NC} from scratch.
${BYellow}Note:${NC} the version tag may not correlate with the actual Rhino Linux version.
This option is meant for easily publishing images for specific milestones.
${BPurple}${0##*/}${NC} ${BCyan}0.1.1${NC}
${BPurple}${0##*/} -p -t -v latest${NC}
Pulls and starts ${BGreen}ghcr.io/rhino-linux/docker:latest${NC}.
${BPurple}${0##*/}${NC} ${BCyan}0.1.2${NC}
${BYellow}Written by:${NC} Oren Klopfer <[email protected]>
EOF
Expand Down Expand Up @@ -139,7 +157,7 @@ function wipe_docker_bits() {
local_docker_images=($(docker image ls | grep rhino | awk '{print $1":"$2}'))
ldi_hashes=($(docker image ls | grep rhino | awk '{print $3}'))
local_docker_files=()
for i in *; do
for i in ${dock_dir}/*; do
if grep -q 'Dockerfile.RhinoLinux' <<< "${i}"; then
local_docker_files+=("${i}")
fi
Expand Down Expand Up @@ -168,7 +186,7 @@ function wipe_docker_bits() {
if ((answer == 1)); then
for ((i = 0; i < ${#local_docker_images[@]}; i++)); do
echo "${BRed}Removing:${NC} ${CYAN}${local_docker_images[i]}~${ldi_hashes[i]}${NC}"
docker image rm ${ldi_hashes[i]} --force
docker rmi ${ldi_hashes[i]} --force
done
fi
else
Expand All @@ -178,6 +196,7 @@ function wipe_docker_bits() {

test_mode=0
build_mode=0
pull_upstream=0
no_cache=""
file_trigger=0

Expand All @@ -194,6 +213,11 @@ while (($# > 0)); do
shift
shift
;;
-P | -p | --pull)
input_darch="off"
pull_upstream=1
shift
;;
-T | -t | --test)
test_mode=1
shift
Expand Down Expand Up @@ -261,14 +285,8 @@ if [[ -z ${imgver} ]]; then
imgver="${dateiniso}"
fi

built_dock="$(pwd)/Dockerfile-RhinoLinux-${imgver}${darcher}"
built_img="${base_darch}rhino-linux/docker:${imgver}"

if [[ -f ${built_dock} ]]; then
rm -f ${built_dock}
fi

cat > ${built_dock} << EOF
function cat_built_dock {
cat > ${built_dock} << EOF
FROM ${base_darch}ubuntu:devel
LABEL org.opencontainers.image.description "Contains Rhino Linux ${imgver}"
Expand Down Expand Up @@ -305,15 +323,36 @@ WORKDIR /home/rhino
# ENTRYPOINT ["/bin/bash"]
CMD ["bash"]
EOF
}

if ((pull_upstream == 1)); then
image_registry="ghcr.io/"
else
image_registry="${base_darch}"
fi
built_img="${image_registry}rhino-linux/docker:${imgver}"

if ((pull_upstream == 0)); then
built_dock="${dock_dir}/Dockerfile-RhinoLinux-${imgver}${darcher}"
if [[ -f ${built_dock} ]]; then
rm -f ${built_dock}
fi
cat_built_dock
fi

function build_image {
docker build -f ${built_dock} -t ${built_img} . ${no_cache} && \
echo "${BYellow}Built image${NC} ${BPurple}${built_img}${BYellow}.${NC}"
docker build -f ${built_dock} -t ${built_img} . ${no_cache} \
&& echo "${BYellow}Built image${NC} ${BPurple}${built_img}${BYellow}.${NC}"
}

function start_image {
echo "${BYellow}Starting...${NC}" && \
docker run -it --net=host ${built_img} bash
echo "${BYellow}Starting...${NC}" \
&& docker run -it --net=host ${built_img} bash
}

function pull_image {
echo "${BYellow}Pulling ${BPurple}${built_img}${BYellow}...${NC}" \
&& docker pull ${built_img}
}

function not_start_test {
Expand All @@ -330,33 +369,36 @@ function not_start_build {
echo "${BYellow}After the build has complete, run the following command to test the image:${NC}"
}

if ((build_mode == 1)); then
echo "${BYellow}Dockerfile built at${NC} ${BPurple}${built_dock}${BYellow}. Building${NC} ${BPurple}${built_img}${BYellow}...${NC}"
build_image
if ((test_mode == 1)); then
start_image
else
function test_image {
if ((test_mode == 1)); then
start_image
else
ask "Do you want to start the image to test?" N
if ((answer == 0)); then
not_start_test
how_to_start
else
start_image
fi
fi
}

if ((pull_upstream == 1)); then
pull_image \
&& test_image \
|| echo "${BYellow}Version ${BPurple}${imgver}${BYellow} does not exist in the registry.${NC}"
elif ((build_mode == 1)); then
echo "${BYellow}Dockerfile built at${NC} ${BPurple}${built_dock}${BYellow}. Building${NC} ${BPurple}${built_img}${BYellow}...${NC}"
build_image \
&& test_image
else
echo "${BYellow}Dockerfile built at${NC} ${BPurple}${built_dock}${BYellow}.${NC}"
ask "Start image build now?" N
if ((answer == 0)); then
not_start_build
how_to_start
else
build_image
if ((test_mode == 1)); then
start_image
else
ask "Do you want to start the image to test?" N
if ((answer == 0)); then
not_start_test
how_to_start
else
start_image
fi
fi
build_image \
&& test_image
fi
fi

0 comments on commit d2acd1f

Please sign in to comment.