From bf162ac7259ca0b8a654cba9c11b63db000b4e35 Mon Sep 17 00:00:00 2001
From: Starbeamrainbowlabs <sbrl@starbeamrainbowlabs.com>
Date: Mon, 27 Sep 2021 02:27:57 +0100
Subject: [PATCH] Add ability to import images from another Docker registry

.....though we should refrain from using this unless absolutely
necessary
---
 README.md       | 11 ++++++++---
 imagebuilder.sh | 31 +++++++++++++++++++++++++++++++
 2 files changed, 39 insertions(+), 3 deletions(-)

diff --git a/README.md b/README.md
index d15d0cb..baaba48 100644
--- a/README.md
+++ b/README.md
@@ -29,10 +29,10 @@ Multiple reasons:
 4. Compatibility:
 	- I use [Hashicorp Nomad](https://www.nomadproject.io/), so some of these Dockerfiles are written explicitly with Hashicorp Nomad in mind - e.g. the [`NOMAD_PORT_*` environment variables](https://www.nomadproject.io/docs/job-specification/network#port-parameters).
 	- My Hashicorp Nomad cluster is comprised chiefly of Raspberry Pis (currently running armv7l, but an upgrade to arm64 is planned eventually), and many Docker containers on the Docker Hub are built by default for amd64
-5. Maintainability: I want to ensure I keep my Docker images up-to-date, so I rebuild them myself regularly via my [Continuous Integration server](https://laminar.ohwg.net/)
+5. Maintainability: I want to ensure I keep my Docker images up-to-date, so I rebuild them myself regularly via my [Continuous Integration server](https://laminar.ohwg.net/) ([see also](https://starbeamrainbowlabs.com/blog/article.php?article=posts/392-own-your-code-series-list.html), and [also this](https://starbeamrainbowlabs.com/blog/article.php?article=posts/451-cluster-11-lock-and-key.html))
 
 ### Why do I need to run a private Docker registry for `imagebuilder.sh` to work?
-`imagebuild.sh` is designed to automatically build the specified Docker image and then push it to a private Docker registry because then the hosts in my Hashicorp Nomad 
+`imagebuilder.sh` is designed to automatically build the specified Docker image and then push it to a private Docker registry because then the hosts in my Hashicorp Nomad 
 
 ### These Dockerfiles don't work for me!
 These Dockerfiles are specific to my environment. They depend on a patched version of `minideb` as a base image, which this package is also responsible for building. The key changes to `minideb` include:
@@ -96,8 +96,9 @@ UID     | GID	| Container				| Notes
 At present, 3 image types are present:
 
  - **`base`:** A base image - a script is called with an auto-generated output directory as the 1st and only argument. When the script exits the directory is checked for a `.tar.gz` file
- - **`base-nopush`:** A variant of the above that doesn't automatically much to the docker registry on completion.
+ - **`base-nopush`:** A variant of the above that doesn't automatically push to the docker registry on completion.
  - **`docker`:** A `Dockerfile` is is built with `docker build` before being pushed to the docker registry.
+ - **`import`:** Imports a Docker image from another Docker registry.
 
 ### Creating a new image
 Each image should have it's own subdirectory inside the `image` directory. The following files should be present for a `docker` image type:
@@ -112,9 +113,13 @@ For `base` and `base-nopush` image types, the following files should be present:
  - `IMAGE_NAME.sh`: A `.sh` file named after the name of the parent directory. For example, the `minideb` image contains the script `minideb.sh`
  - `type.txt`: Should contain either the word `base` or `base-nopush`
 
+For `import` image types, the file `imagetype.txt` should be present and contain the (fully qualified) name of the image to import.
+
 Optionally, any image type can contain the following files:
 
  - `dependents.txt`: The names of images that depend on this image - 1 image name per file. This is read by my continuous integration system to queue rebuilds of dependent Docker containers once the current image has finished building & pushing automatically.
 
+
+
 ## Licence
 The contents of this repository is licenced under the _Mozilla Public License 2.0_ (MPL-2.0). This license can be found in the _LICENSE_ file in this repository.
diff --git a/imagebuilder.sh b/imagebuilder.sh
index 478229f..82268e1 100755
--- a/imagebuilder.sh
+++ b/imagebuilder.sh
@@ -211,6 +211,37 @@ case "${subcommand}" in
 				task_end "$?";
 				;;
 			
+			import)
+				task_begin "Finding remote image name";
+				if [[ ! -r "${imagedir}/imagename.txt" ]]; then
+					echo "Error: 'import' image type specified, but no imagename.txt file was found containing the (fully qualified) image name to pull from.";
+					exit 8;
+				fi
+				
+				imagename_remote="$(tr -d "[:blank:]" <"${imagedir}/imagename.txt")"
+				
+				if [[ -z "${imagename_remote}" ]]; then
+					echo "Error: An empty image name to pull isn't valid.";
+					exit 7;
+				fi
+				
+				echo -e "[${HOSTNAME}:imagebuilder] Remote image name is ${FGRN}${HC}${imagename_remote}${RS}";
+				task_end "$?";
+				
+				task_begin "Downloading image";
+				execute docker pull "${imagename_remote}";
+				task_end "$?" "Error: Failed to download image";
+				
+				task_begin "Retagging image";
+				execute docker tag "${imagename_remote}" "${docker_tag}";
+				task_end "$?";
+				
+				task_begin "Pushing image";
+				execute docker push "${docker_tag}";
+				task_end "$?";
+				
+				;;
+			
 			
 			# ██    ██ ███    ██ ██   ██ ███    ██  ██████  ██     ██ ███    ██
 			# ██    ██ ████   ██ ██  ██  ████   ██ ██    ██ ██     ██ ████   ██
