-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
updates: make safer, add upstream pull option (#15)
Co-authored-by: ook37 <[email protected]>
- Loading branch information
Showing
2 changed files
with
100 additions
and
45 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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' | ||
|
@@ -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}) | ||
|
@@ -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 | ||
|
@@ -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 | ||
|
@@ -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 | ||
|
@@ -178,6 +196,7 @@ function wipe_docker_bits() { | |
|
||
test_mode=0 | ||
build_mode=0 | ||
pull_upstream=0 | ||
no_cache="" | ||
file_trigger=0 | ||
|
||
|
@@ -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 | ||
|
@@ -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}" | ||
|
@@ -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 { | ||
|
@@ -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 |