Skip to content

Commit b18cafd

Browse files
committed
scripts: update utility scripts
- Refactor the build and push scripts. - Add common.sh for shared functionality. - Add create_manifest.sh to facilitate multi-arch image release process. Signed-off-by: Erdem Meydanli <meydanli@amazon.com>
1 parent 05340cb commit b18cafd

File tree

6 files changed

+155
-11
lines changed

6 files changed

+155
-11
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
1+
.ecr.uri
12
k8s-ne-device-plugin
23
vendor/

RELEASE

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
0.1

scripts/build.sh

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
1-
#!/bin/bash -e
1+
#!/bin/bash
2+
# Copyright 2022 Amazon.com, Inc. or its affiliates. All Rights Reserved.
3+
# SPDX-License-Identifier: Apache-2.0
24

3-
TOP_DIR=$(dirname $(realpath $0))/..
4-
docker build --target builder -t ne-k8s-device-plugin-build:latest $TOP_DIR -f $TOP_DIR/container/Dockerfile
5-
docker build --target device_plugin -t aws-nitro-enclaves-k8s-device-plugin:latest $TOP_DIR -f $TOP_DIR/container/Dockerfile
5+
source "$(dirname $(realpath $0))/common.sh"
6+
7+
docker build --target builder -t $BUILDER_IMAGE $TOP_DIR -f $TOP_DIR/container/Dockerfile
8+
docker build --target device_plugin -t $IMAGE $TOP_DIR -f $TOP_DIR/container/Dockerfile

scripts/common.sh

Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
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+
}

scripts/create_manifest.sh

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
#!/bin/bash
2+
# Copyright 2022 Amazon.com, Inc. or its affiliates. All Rights Reserved.
3+
# SPDX-License-Identifier: Apache-2.0
4+
5+
source "$(dirname $(realpath $0))/common.sh"
6+
7+
main() {
8+
ecr_login
9+
10+
docker manifest create --amend $ECR_URL/$REPOSITORY_NAME \
11+
$ECR_URL/$REPOSITORY_NAME:$RELEASE-x86_64 \
12+
$ECR_URL/$REPOSITORY_NAME:$RELEASE-aarch64 || \
13+
die "Cannot create manifest for multiarch image." \
14+
" Please ensure that both x86_64 and aarch64 images" \
15+
" already exist in the repository."
16+
17+
docker manifest inspect $ECR_URL/$IMAGE
18+
19+
is_a_public_ecr_registry && {
20+
confirm "You are about to make changes on a" \
21+
" publicly available manifest. Are you sure want to continue? (yes/no)"
22+
}
23+
24+
docker manifest push $ECR_URL/$REPOSITORY_NAME:latest
25+
}
26+
27+
main

scripts/push.sh

Lines changed: 21 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,25 @@
11
#!/bin/bash
2-
# Temporary file for development purposes only. Will be removed.
2+
# Copyright 2022 Amazon.com, Inc. or its affiliates. All Rights Reserved.
3+
# SPDX-License-Identifier: Apache-2.0
34

4-
set -eu pipefail
5+
source "$(dirname $(realpath $0))/common.sh"
56

6-
URI=709843417989.dkr.ecr.eu-central-1.amazonaws.com
7-
REPOSITORY=709843417989.dkr.ecr.eu-central-1.amazonaws.com/plugin_devel:latest
7+
main() {
8+
ecr_login
89

9-
aws ecr get-login-password --region eu-central-1 | docker login --username AWS --password-stdin $URI
10-
docker tag aws-nitro-enclaves-k8s-device-plugin:latest $REPOSITORY
11-
docker push $REPOSITORY
10+
aws ecr --region $ECR_REGION describe-repositories \
11+
--repository-names "$REPOSITORY_NAME" > /dev/null || \
12+
die "There is no repository named $REPOSITORY_NAME in" \
13+
"$ECR_REGION region."
14+
15+
is_a_public_ecr_registry && {
16+
confirm "You are about to make changes on a public repository." \
17+
" Are you sure want to continue?"
18+
}
19+
20+
docker tag $IMAGE $ECR_URL/$IMAGE
21+
say "Pushing $IMAGE to $ECR_URL..."
22+
docker push $ECR_URL/$IMAGE
23+
}
24+
25+
main

0 commit comments

Comments
 (0)