-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathpublish-ecr
executable file
·162 lines (144 loc) · 4.64 KB
/
publish-ecr
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
#!/usr/bin/env bash
# Publish a container image to an ECR repository.
set -o errexit
set -o nounset
set -o pipefail
show_help() {
>&2 echo "Usage: publish-ecr SOURCE_IMAGE [-p PLATFORM] [-t TAG]"
>&2 echo ""
>&2 echo "Publish an XRd container image to ECR"
>&2 echo ""
>&2 echo "Required arguments:"
>&2 echo " SOURCE_IMAGE Either a path to a local image tarball or"
>&2 echo " a URL to an image repository (with tag)"
>&2 echo ""
>&2 echo "Optional arguments:"
>&2 echo " -h, --help Show this help"
>&2 echo " -p, --platform PLATFORM XRd platform the image is for"
>&2 echo " (defaults to checking the image name)"
>&2 echo " -t, --tag TAG Override the target image tag"
>&2 echo " (defaults to 'latest')"
>&2 echo ""
>&2 echo "This script requires skopeo when publishing an image archive."
>&2 echo "When publishing from a repository, at least one of skopeo,"
>&2 echo "docker, or podman must be available."
}
POSITIONAL_ARGS=()
# Parse the arguments
while [ $# -gt 0 ]; do
case $1 in
-p|--platform )
PLATFORM="$2"
shift
;;
-t|--tag )
TAG_OVERRIDE="$2"
shift
;;
-h|--help )
show_help
exit 255
;;
-* )
>&2 echo "Unknown option $1"
show_help
exit 1
;;
* )
POSITIONAL_ARGS+=("$1")
;;
esac
shift
done
if [ ${#POSITIONAL_ARGS[@]} -ne 1 ]; then
>&2 echo "Exactly one positional arg required: SOURCE_IMAGE"
show_help
exit 1
fi
SOURCE_IMAGE=${POSITIONAL_ARGS[0]}
if [ -n "${PLATFORM:-}" ]; then
if [ "$(echo "$PLATFORM" | tr "[:upper:]" "[:lower:]")" = "vrouter" ]; then
TARGET_IMAGE_NAME=xrd/xrd-vrouter
elif [ "$(echo "$PLATFORM" | tr "[:upper:]" "[:lower:]")" = "controlplane" ]; then
TARGET_IMAGE_NAME=xrd/xrd-control-plane
else
>&2 echo "Invalid platform: ${PLATFORM}"
>&2 echo "Must be either vrouter or controlplane (case-insensitive)"
>&2 echo ""
show_help
exit 1
fi
else
case $(tr "[:upper:]" "[:lower:]" <<< "$SOURCE_IMAGE") in
*vrouter* )
TARGET_IMAGE_NAME=xrd/xrd-vrouter
;;
*control-plane*|*controlplane* )
TARGET_IMAGE_NAME=xrd/xrd-control-plane
;;
* )
>&2 echo "Unable to get platform from source image name."
>&2 echo "Please specify -p or --platform with the XRd platform name (vrouter or controlplane)"
exit 2
esac
fi
# Check if an executable with the given name is in PATH.
# (without printing anything to console)
is_in_path () {
type -P "$1" &> /dev/null
}
# Check what tool to use.
# Ordering is skopeo preferred, then docker, then podman.
if is_in_path "skopeo"; then
TOOL=skopeo
elif is_in_path "docker"; then
TOOL=docker
elif is_in_path "podman"; then
TOOL=podman
else
>&2 echo "None of skopeo, docker, or podman available"
exit 3
fi
ACCOUNT_ID=$(aws sts get-caller-identity --query "Account" --output text)
AWS_REGION=$(aws configure list | grep region | awk '{print $2}')
TARGET_REGISTRY="${ACCOUNT_ID}.dkr.ecr.${AWS_REGION}.amazonaws.com"
# Log into the registry.
aws ecr get-login-password | $TOOL login --username AWS --password-stdin "${TARGET_REGISTRY}"
# Create the target repository if it doesn't exist.
aws ecr create-repository --repository-name "${TARGET_IMAGE_NAME}" || true
# Generate out the target image name.
if [ -f "$SOURCE_IMAGE" ]; then
# Local image archive. Assume the tag is 'latest'.
SOURCE_IMAGE_TAG="latest"
else
# Repository URL. Try and extract the tag, if not found assume 'latest'.
# Strip the largest prefix before (and including) ':'
# This means if no tag is specified, the tag will be set to the full
# image name, so check that after.
SOURCE_IMAGE_TAG=${SOURCE_IMAGE#*:}
if [ "$SOURCE_IMAGE_TAG" = "$SOURCE_IMAGE" ]; then
# No tag specified - assume it's 'latest'.
SOURCE_IMAGE_TAG="latest"
fi
fi
TARGET_IMAGE_TAG=${TAG_OVERRIDE:-$SOURCE_IMAGE_TAG}
TARGET_IMAGE="${TARGET_REGISTRY}/${TARGET_IMAGE_NAME}:${TARGET_IMAGE_TAG}"
# Publish the image to ECR.
if [ -f "$SOURCE_IMAGE" ]; then
# Local image archive - only skopeo supported.
if [ $TOOL != "skopeo" ]; then
>&2 "Copying local image only supported with skopeo"
exit 1
fi
$TOOL copy "docker-archive:${SOURCE_IMAGE}" "docker://${TARGET_IMAGE}"
else
# Container repo URL - all tools supported.
if [ $TOOL = "skopeo" ]; then
$TOOL copy "docker://${SOURCE_IMAGE}" "docker://${TARGET_IMAGE}"
else
$TOOL pull "${SOURCE_IMAGE}"
$TOOL tag "${SOURCE_IMAGE}" "${TARGET_IMAGE}"
$TOOL push "${TARGET_IMAGE}"
fi
fi
echo "Image is now available at ${TARGET_IMAGE}"