|
| 1 | +#!/bin/bash |
| 2 | +# Copyright 2022 Amazon.com, Inc. or its affiliates. All Rights Reserved. |
| 3 | +# SPDX-License-Identifier: Apache-2.0 |
| 4 | + |
| 5 | +readonly SUCCESS=0 |
| 6 | +readonly FAILURE=255 |
| 7 | + |
| 8 | +readonly SCRIPTS_DIR=$(dirname $(realpath $0)) |
| 9 | +readonly TOP_DIR=$(cd $SCRIPTS_DIR/.. && pwd) |
| 10 | +readonly ECR_CONFIG_FILE_PATH="$SCRIPTS_DIR/.ecr.uri" |
| 11 | +readonly RELEASE_FILE="RELEASE" |
| 12 | + |
| 13 | +readonly BUILDER_IMAGE=ne-k8s-device-plugin-build:latest |
| 14 | +readonly REPOSITORY_NAME=aws-nitro-enclaves-k8s-device-plugin |
| 15 | +readonly RELEASE=$(cat $TOP_DIR/$RELEASE_FILE) |
| 16 | +readonly TAG=$RELEASE-$(arch) |
| 17 | +readonly IMAGE=$REPOSITORY_NAME:$TAG |
| 18 | + |
| 19 | +say() { |
| 20 | + echo "$@" |
| 21 | +} |
| 22 | + |
| 23 | +die() { |
| 24 | + say "[ERROR] $@" |
| 25 | + exit $FAILURE |
| 26 | +} |
| 27 | + |
| 28 | +[[ -f $TOP_DIR/$RELEASE_FILE ]] || \ |
| 29 | + die "Cannot find $RELEASE_FILE file in $TOP_DIR directory." |
| 30 | + |
| 31 | +_set_config_item() { |
| 32 | + local var=$1; shift |
| 33 | + local prompt="$@" |
| 34 | + |
| 35 | + local value="" |
| 36 | + while [[ $value = "" ]]; |
| 37 | + do |
| 38 | + printf "$prompt" |
| 39 | + read value |
| 40 | + done |
| 41 | + |
| 42 | + echo "$var=$value" >> "$ECR_CONFIG_FILE_PATH" |
| 43 | +} |
| 44 | + |
| 45 | +_load_ecr_config() { |
| 46 | + [[ -f $ECR_CONFIG_FILE_PATH ]] || { |
| 47 | + printf "No configuration found!\n" |
| 48 | + _set_config_item ECR_URL "Please enter an ECR URL:" |
| 49 | + _set_config_item ECR_REGION "Please enter AWS region of the ECR repository:" |
| 50 | + } |
| 51 | + |
| 52 | + source "$ECR_CONFIG_FILE_PATH" |
| 53 | + [[ -z "$ECR_URL" || -z "$ECR_REGION" ]] && { |
| 54 | + say "$(basename $ECR_CONFIG_FILE_PATH) seems corrupted. Try using" \ |
| 55 | + "'rm -f $ECR_CONFIG_FILE_PATH' to remove this configuration." |
| 56 | + exit 1 |
| 57 | + } |
| 58 | + |
| 59 | + return 0 |
| 60 | +} |
| 61 | + |
| 62 | +_ecr_login() { |
| 63 | + is_a_public_ecr_registry |
| 64 | + |
| 65 | + if [[ $? -eq $SUCCESS ]]; then |
| 66 | + aws ecr-public get-login-password --region "$ECR_REGION" | docker login --username AWS --password-stdin $ECR_URL |
| 67 | + else |
| 68 | + aws ecr get-login-password --region "$ECR_REGION" | docker login --username AWS --password-stdin $ECR_URL |
| 69 | + fi |
| 70 | +} |
| 71 | + |
| 72 | +# Loads configuration and logs in to a registry. |
| 73 | +# |
| 74 | +ecr_login() { |
| 75 | + _load_ecr_config || die "Error while loading configuration file!" |
| 76 | + say "Using ECR registry url: $ECR_URL. (region: $ECR_REGION)." |
| 77 | + _ecr_login || die "Failed to log in to the ECR registry." |
| 78 | +} |
| 79 | + |
| 80 | +# Check if the current ECR URL is a public one or not. |
| 81 | +# |
| 82 | +is_a_public_ecr_registry() { |
| 83 | + [[ "$ECR_URL" =~ ^public.ecr.aws* ]] && { return $SUCCESS; } |
| 84 | + return $FAILURE |
| 85 | +} |
| 86 | + |
| 87 | +# Generic user confirmation function |
| 88 | +# |
| 89 | +confirm() { |
| 90 | + read -p "$@ (yes/no)" yn |
| 91 | + case yn in |
| 92 | + yes) ;; |
| 93 | + *) |
| 94 | + say "Aborting..." |
| 95 | + exit $FAILURE |
| 96 | + ;; |
| 97 | + esac |
| 98 | +} |
0 commit comments