Skip to content

Commit

Permalink
Use bash substitution in place of cut, wrap vars in {}
Browse files Browse the repository at this point in the history
Signed-off-by: Kyle Squizzato <[email protected]>
  • Loading branch information
squizzi committed Nov 1, 2024
1 parent 66c07fa commit 3cdab0a
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 62 deletions.
20 changes: 10 additions & 10 deletions scripts/airgap-push.sh
Original file line number Diff line number Diff line change
Expand Up @@ -92,32 +92,32 @@ if [ -z "$AIRGAP_BUNDLE" ]; then
else
# Validate the airgap bundle
if [ ! -d "$AIRGAP_BUNDLE" ]; then
echo "The provided airgap bundle: $AIRGAP_BUNDLE does not exist"
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
docker load -i ${AIRGAP_BUNDLE}

# Extract the repositories json file from the airgap bundle.
tar xf $AIRGAP_BUNDLE "repositories"
tar xf ${AIRGAP_BUNDLE} "repositories"

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}'
image_name=$(echo ${image} | grep -o '[^/]*$')
old_image=$(docker images -a | grep ${IMAGE} | awk '{print $1":"$2}')
tag=${old_image#*:}

docker tag $old_image $repo/$image_name:$tag
docker push $repo/$image_name:$tag
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/
tar xf ${AIRGAP_BUNDLE} "charts/extensions" -C hmc_charts/

for chart in $(ls hmc_charts/extensions); do
helm push hmc_charts/extensions/$chart $CHART_REPO
${HELM} push hmc_charts/extensions/${chart} ${CHART_REPO}
done

# Clean up the extracted files.
Expand Down
86 changes: 43 additions & 43 deletions scripts/bundle-images.sh
Original file line number Diff line number Diff line change
Expand Up @@ -36,22 +36,22 @@ function wait_for_deploy_exist() {

start_time=$(date +%s)

echo "Verifying provider Deployment with label: \"$deployment_label\" exists in namespace: \"$NAMESPACE\"..."
echo "Verifying provider Deployment with label: \"${deployment_label}\" exists in namespace: \"${NAMESPACE}\"..."

while true; do
current_time=$(date +%s)
if (( (current_time - start_time) > max_wait_secs )); then
echo "Error: Waited for Deployment with label: \"$deployment_label\" in namespace: \"$NAMESPACE\" to exist for $max_wait_secs seconds and it still does not exist."
echo "Error: Waited for Deployment with label: \"${deployment_label}\" in namespace: \"${NAMESPACE}\" to exist for ${max_wait_secs} seconds and it still does not exist."
return 1
fi

output=$($KUBECTL -n "$NAMESPACE" get deploy -l $deployment_label)
output=$(${KUBECTL} -n "${NAMESPACE}" get deploy -l ${deployment_label})

if [[ $output != "" ]]; then
echo "Deployment in namespace: \"$NAMESPACE\" with label: \"$deployment_label\" exists."
echo "Deployment in namespace: \"${NAMESPACE}\" with label: \"${deployment_label}\" exists."
break
else
echo "Deployment with label: \"$deployment_label\" in namespace: \"$NAMESPACE\" does not exist yet. Waiting $interval_secs seconds..."
echo "Deployment with label: \"${deployment_label}\" in namespace: \"${NAMESPACE}\" does not exist yet. Waiting ${interval_secs} seconds..."
sleep $interval_secs
fi
done
Expand All @@ -61,10 +61,10 @@ function bundle_images() {
local images=$1
local tarball=$2

echo "Bundling images into $tarball..."
docker save -o $tarball $images
echo "Bundling images into ${tarball}..."
docker save -o ${tarball} ${images}
if [[ $? -ne 0 ]]; then
echo "Error: Failed to bundle images into $tarball"
echo "Error: Failed to bundle images into ${tarball}"
exit 1
fi
}
Expand All @@ -79,21 +79,21 @@ echo -e "\nVerifying provider Deployments are ready...\n"

# Verify each provider we support has deployed so we can get the images used
# across the deployments.
for template in $(find $TEMPLATES_DIR -name 'provider.yaml');
for template in $(find ${TEMPLATES_DIR} -name 'provider.yaml');
do
result=$(grep 'kind: .*Provider' $template)
provider_yaml=$(grep "$result" -A2 $template)
provider_kind=$(echo -e "$provider_yaml" | $YQ e '.kind' -)
provider_name=$(echo -e "$provider_yaml" | $YQ e '.metadata.name' -)
result=$(grep 'kind: .*Provider' ${template})
provider_yaml=$(grep "${result}" -A2 ${template})
provider_kind=$(echo -e "${provider_yaml}" | $YQ e '.kind' -)
provider_name=$(echo -e "${provider_yaml}" | $YQ e '.metadata.name' -)
provider_kind_tolower=$(echo ${provider_kind} | tr '[:upper:]' '[:lower:]')

if [[ $provider_name == "" ]]; then
echo "Error: Cannot determine provider Name from $template"
echo "Error: Cannot determine provider Name from ${template}"
exit 1
fi

if [[ $provider_kind_tolower == "" ]]; then
echo "Error: Cannot determine provider Kind from $template"
echo "Error: Cannot determine provider Kind from ${template}"
exit 1
fi

Expand All @@ -104,32 +104,32 @@ do

# coreprovider does not have a provider prefix.
if [[ $provider_kind_tolower == "coreprovider" ]]; then
label_value=$(echo $provider_name)
label_value=$(echo ${provider_name})
else
label_value=$(echo $(echo $provider_kind_tolower | sed -e 's/provider//g')-$provider_name)
label_value=$(echo $(echo ${provider_kind_tolower} | sed -e 's/provider//g')-${provider_name})
fi

wait_for_deploy_exist "$LABEL_KEY=$label_value"

$KUBECTL wait --for condition=available --timeout=2m deploy -l $LABEL_KEY=$label_value -n $NAMESPACE
${KUBECTL} wait --for condition=available --timeout=2m deploy -l ${LABEL_KEY}=${label_value} -n ${NAMESPACE}
if [[ $? -ne 0 ]]; then
echo "Error: Cannot wait for Deployment: Deployment with $LABEL_KEY=$label_value label not found"
echo "Error: Cannot wait for Deployment: Deployment with ${LABEL_KEY}=${label_value} label not found"
exit 1
fi
done


# Now that we know everything is deployed and ready, we can get all of images by
# execing into the KIND cluster.
control_plane=$($KUBECTL get nodes --no-headers -o custom-columns=":metadata.name")
control_plane=$(${KUBECTL} get nodes --no-headers -o custom-columns=":metadata.name")
if [[ $? -ne 0 ]] || [[ $control_plane == "" ]]; then
echo "Error: Cannot get control plane node"
exit 1
fi

echo -e "\nPulling images for HMC components...\n"

for image in $(docker exec -it $control_plane crictl images | sed 1,1d | awk '{print $1":"$2}' | grep -v 'kindest');
for image in $(docker exec -it ${control_plane} crictl images | sed 1,1d | awk '{print $1":"$2}' | grep -v 'kindest');
do
if [[ $image == "" ]]; then
echo "Error: Failed to get image from KIND cluster, image string should not be empty"
Expand All @@ -141,15 +141,15 @@ do
continue
fi

tag=$(echo $image | cut -d':' -f2)
tag=${image#*:}
if [[ $tag == "<none>" ]]; then
echo "Will not pull image: $image with tag <none>, continuing..."
echo "Will not pull image: ${image} with tag <none>, continuing..."
continue
fi

docker pull $image
if [[ $? -ne 0 ]]; then
echo "Error: Failed to pull $image"
echo "Error: Failed to pull ${image}"
exit 1
fi

Expand All @@ -160,36 +160,36 @@ echo -e "\nPulling images for HMC extensions...\n"

# Next, we need to build a list of images used by k0s extensions. Walk the
# templates directory and extract the images used by the extensions.
for template in $(find $templates_dir -name 'k0s*.yaml');
for template in $(find ${templates_dir} -name 'k0s*.yaml');
do
if [[ $template == *"k0smotron"* ]]; then
extensions_path=".spec.k0sConfig.spec.extensions.helm"
else
extensions_path=".spec.k0sConfigSpec.k0s.spec.extensions.helm"
fi

repos=$(grep -vw "{{" $template | $YQ e "${extensions_path}.repositories[] | [.url, .name] | join(\";\")")
repos=$(grep -vw "{{" ${template} | $YQ e "${extensions_path}.repositories[] | [.url, .name] | join(\";\")")
for repo in $repos
do
url=$(echo $repo | cut -d';' -f1)
chartname=$(echo $repo | cut -d';' -f2)
version=$(grep -vw "{{" $template |
$YQ e "${extensions_path}.charts[] | select(.chartname == \"*$chartname*\") | .version")
name=$(grep -vw "{{" $template |
$YQ e "${extensions_path}.charts[] | select(.chartname == \"*$chartname*\") | .name")
grep -vw "{{" $template | $YQ e "${extensions_path}.charts[] | select(.chartname == \"*$chartname*\") | .values" > $name-values.yaml
url=${repo%;*}
chartname=${repo#*;}
version=$(grep -vw "{{" ${template} |
$YQ e "${extensions_path}.charts[] | select(.chartname == \"*${chartname}*\") | .version")
name=$(grep -vw "{{" ${template} |
$YQ e "${extensions_path}.charts[] | select(.chartname == \"*${chartname}*\") | .name")
grep -vw "{{" $template | $YQ e "${extensions_path}.charts[] | select(.chartname == \"*$chartname*\") | .values" > ${name}-values.yaml

if [[ $url == "" ]] || [[ $name == "" ]] || [[ $version == "" ]]; then
echo "Error: Failed to get URL, name, or version from $template"
echo "Error: Failed to get URL, name, or version from ${template}"
exit 1
fi

# Use 'helm template' to get the images used by the extension.
for image in $($HELM template --repo $url --version $version $name --values $name-values.yaml | $YQ -N e .spec.template.spec.containers[].image);
for image in $(${HELM} template --repo ${url} --version ${version} ${name} --values ${name}-values.yaml | $YQ -N e .spec.template.spec.containers[].image);
do
docker pull $image
docker pull ${image}
if [[ $? -ne 0 ]]; then
echo "Error: Failed to pull $image"
echo "Error: Failed to pull ${image}"
exit 1
fi

Expand All @@ -201,11 +201,11 @@ do
done

echo -e "\nSaving images...\n"
images_bundled_uniq=$(echo "$IMAGES_BUNDLED" | tr ' ' '\n' | sort -u)
images_bundled_uniq=$(echo "${IMAGES_BUNDLED}" | tr ' ' '\n' | sort -u)
bundle_images "$images_bundled_uniq" $BUNDLE_TARBALL

if [[ $EXTENSION_IMAGES_BUNDLED != "" ]]; then
extension_images_bundled_uniq=$(echo "$EXTENSION_IMAGES_BUNDLED" | tr ' ' '\n' | sort -u)
extension_images_bundled_uniq=$(echo "${EXTENSION_IMAGES_BUNDLED}" | tr ' ' '\n' | sort -u)
bundle_images "$extension_images_bundled_uniq" $EXTENSIONS_BUNDLE_TARBALL
fi

Expand All @@ -214,13 +214,13 @@ echo -e "\nCleaning up all pulled images...\n"
all_images="$images_bundled_uniq $extension_images_bundled_uniq"
for image in $all_images;
do
echo "Removing $image from local image cache..."
docker rmi $image
echo "Removing ${image} from local image cache..."
docker rmi ${image}
if [ $? -ne 0 ]; then
# Note that we failed here but continue trying to remove the other
# images.
echo "Error: Failed to remove $image from local image cache"
echo "Error: Failed to remove ${image} from local image cache"
fi
done

echo "Done! Images bundled into $BUNDLE_TARBALL and $EXTENSIONS_BUNDLE_TARBALL"
echo "Done! Images bundled into ${BUNDLE_TARBALL} and ${EXTENSIONS_BUNDLE_TARBALL}"
18 changes: 9 additions & 9 deletions scripts/package-k0s-extensions-helm.sh
Original file line number Diff line number Diff line change
Expand Up @@ -15,26 +15,26 @@
# This script packages Helm charts affiliated with k0s extensions for airgap
# installations.
# This script should not be run directly. Use 'make airgap-package' instead.
for template in $(find $TEMPLATES_DIR -name 'k0s*.yaml'); do
for template in $(find ${TEMPLATES_DIR} -name 'k0s*.yaml'); do
if [[ $template == *"k0smotron"* ]]; then
extensions_path=".spec.k0sConfig.spec.extensions.helm"
else
extensions_path=".spec.k0sConfigSpec.k0s.spec.extensions.helm"
fi

repos=$(grep -vw "{{" $template | $YQ e "$extensions_path.repositories[] | [.url, .name] | join(\";\")")
repos=$(grep -vw "{{" ${template} | ${YQ} e "${extensions_path}.repositories[] | [.url, .name] | join(\";\")")
for repo in $repos; do
url=$(echo $repo | cut -d';' -f1)
chartname=$(echo $repo | cut -d';' -f2)
version=$(grep -vw "{{" $template | $YQ e "$extensions_path.charts[] | select(.chartname == \"*$chartname*\") | .version")
name=$(grep -vw "{{" $template | $YQ e "${extensions_path}.charts[] | select(.chartname == \"*$chartname*\") | .name")
url=${repo%;*}
chartname=${repo#*;}
version=$(grep -vw "{{" ${template} | $YQ e "${extensions_path}.charts[] | select(.chartname == \"*${chartname}*\") | .version")
name=$(grep -vw "{{" ${template} | $YQ e "${extensions_path}.charts[] | select(.chartname == \"*${chartname}*\") | .name")
if [[ $url == "" ]] || [[ $name == "" ]] || [[ $version == "" ]]; then
echo "Error: Cannot construct Helm pull command from url: $url, name: $name, version: $version: one or more vars is not populated"
exit 1
fi
if [[ ! $(find $EXTENSION_CHARTS_PACKAGE_DIR -name $name-$version*.tgz) ]]; then
echo "Pulling Helm chart $name from $url with version $version"
$HELM pull --repo $url --version $version $name -d $EXTENSION_CHARTS_PACKAGE_DIR
if [[ ! $(find ${EXTENSION_CHARTS_PACKAGE_DIR} -name ${name}-${version}*.tgz) ]]; then
echo "Pulling Helm chart ${name} from ${url} with version ${version}"
${HELM} pull --repo ${url} --version ${version} ${name} -d ${EXTENSION_CHARTS_PACKAGE_DIR}
fi
done
done

0 comments on commit 3cdab0a

Please sign in to comment.