From 93428f82fb4013566212f9aabe622abd53c2096a Mon Sep 17 00:00:00 2001 From: Kyle Squizzato Date: Tue, 22 Oct 2024 15:31:39 -0700 Subject: [PATCH] Add airgap-push script This script can be used to push images and Helm charts needed for an airgapped ManagedCluster deployment to a given image registry and chart repository. Signed-off-by: Kyle Squizzato --- scripts/airgap-push.sh | 132 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 132 insertions(+) create mode 100755 scripts/airgap-push.sh diff --git a/scripts/airgap-push.sh b/scripts/airgap-push.sh new file mode 100755 index 000000000..bf047b90e --- /dev/null +++ b/scripts/airgap-push.sh @@ -0,0 +1,132 @@ +#!/bin/bash +# Copyright 2024 +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +# +# This script can be used to help users re-tag and push images and Helm charts +# into a private registry for use when deploying HMC ManagedClusters into an +# air-gapped environment. This script is packaged as part of the airgap bundle +# for convenience. +# Usage: +# airgap-push.sh [OPTIONS] +# Options: +# -h, --help +# Print this help message +# -r, --repo +# The repository to push the images to +# -c, --chart-repo +# The repository to push the Helm charts to +# -a, --airgap-bundle +# The path to the airgap bundle +#!/bin/bash + +REPO="" +CHART_REPO="" +AIRGAP_BUNDLE="" +HELP="" + +# Parse the options +while [[ $# -gt 0 ]]; do + key="$1" + case $key in + -h|--help) + HELP="true" + shift + ;; + -r|--repo) + REPO="$2" + shift + shift + ;; + -c|--chart-repo) + CHART_REPO="$2" + shift + shift + ;; + -a|--airgap-bundle) + AIRGAP_BUNDLE="$2" + shift + shift + ;; + *) + echo "Unknown option: $1" + exit 1 + ;; + esac +done + +# Print the help message +if [ ! -z "$HELP" ]; then + echo "Usage:" + echo " airgap-push.sh [OPTIONS]" + echo "Options:" + echo " -h, --help" + echo " Print this help message" + echo " -r, --repo" + echo " The repository to push the images to" + echo " -c, --chart-repo" + echo " The repository to push the Helm charts to, for OCI prefix with oci://" + echo " -a, --airgap-bundle" + echo " The path to the airgap bundle" + exit 0 +fi + +if [ -z "$NEW_REPO" ]; then + echo "The repository must be set" + exit 1 +fi + +if [ -z "$CHART_REPO" ]; then + echo "The chart repository must be set" + exit 1 +fi + +if [ -z "$AIRGAP_BUNDLE" ]; then + echo "The airgap bundle must be set" + exit 1 +else + # Validate the airgap bundle + if [ ! -d "$AIRGAP_BUNDLE" ]; then + echo "The provided airgap bundle: $AIRGAP_BUNDLE does not exist" + exit 1 + fi +fi + +# Load the images into the local Docker daemon. +docker load -i $AIRGAP_BUNDLE + +# Extract the repositories json file from the airgap bundle. +tar xf $AIRGAP_BUNDLE "repositories" + +# Iterate over the images in the repositories json file and retag and push them +# to the given repository. +docker login $REPO -u $USERNAME -p $PASSWORD + +for IMAGE in $(cat repositories | jq -r 'to_entries[] | .key'); do + IMAGE_NAME=$(echo $IMAGE | grep -o '[^/]*$') + OLD_IMAGE=$(docker images -a | grep $IMAGE | awk '{print $1":"$2}') + TAG=$OLD_IMAGE | awk -F ":" '{print $2}' + + docker tag $OLD_IMAGE $REPO/$IMAGE_NAME:$TAG + docker push $REPO/$IMAGE_NAME:$TAG +done + +# Next, use Helm to push the charts to the given chart repository. +mkdir -p hmc_charts +tar xf $AIRGAP_BUNDLE "charts/extensions" -C hmc_charts/ + +helm registry login $CHART_REPO -u $USERNAME -p $PASSWORD + +for CHART in $(ls hmc_charts/extensions); do + helm push hmc_charts/extensions/$CHART $CHART_REPO +done