Skip to content

Instantly share code, notes, and snippets.

@awendland
Created June 12, 2018 19:09
Show Gist options
  • Save awendland/e8ae7d557469d724cb5308880a7182e0 to your computer and use it in GitHub Desktop.
Save awendland/e8ae7d557469d724cb5308880a7182e0 to your computer and use it in GitHub Desktop.
Convenience script for installing a pre-compiled version of https://github.com/awslabs/amazon-ecr-credential-helper on macOS and 64-bit linux
#!/bin/bash
set -e
##################################################################################
# Configure the ECR credential helper to make interacting with the ECR
# repo eaiser from the Docker client.
#
# Usage: `./setup_ecr_credential.sh`
#
# $OS (defaults to autodetection using OSTYPE) - macos OR linux, which
# OS environment is getting installed to.
# $TARGET_DIR (defaults to /usr/local/bin) - where the docker-credential-ecr-login
# binary will get installed to.
# $DOCKER_CONFIG_DIR (no default) - provide the path to the docker config,
# usually ~/.docker/, to automatically write a config.json that contains
# the correct setup to use the ecr-login. It will backup any existing config.
#
# NOTE: any additional instructions will be printed out by the script
# NOTE: expects shasum/sha256sum & curl & docker to be installed
##################################################################################
SRC_MACOS="https://s3.amazonaws.com/public-awendland/dev/docker-credential-ecr-login--7d73138a-macos"
HASH_MACOS="5cf1ce657a0ad7b0a9e7d24c23dc001e793e03ebc05ef8787fa5e945fb265790"
SRC_LINUX="https://s3.amazonaws.com/public-awendland/dev/docker-credential-ecr-login--7d73138a-linux"
HASH_LINUX="54f735345554ff19cb9ab82983324ffde6b3285729c5ad0e0df669e9394cfd10"
if [ -z ${OS+x} ]; then
if [[ "$OSTYPE" == "linux-gnu" ]]; then OS='linux'
elif [[ "$OSTYPE" == "darwin"* ]]; then OS='macos'
else
echo "Unknown OS. \$OS must be 'linux' or 'macos'"
exit 1
fi
fi
TARGET_DIR=${TARGET_DIR:-"/usr/local/bin"}
# Copy the creds binary into the PATH
ecr_file=docker-credential-ecr-login
echo "Download $ecr_file and placing in $TARGET_DIR"
cd "$TARGET_DIR"
if [[ "$OS" == "macos" ]]; then
curl "$SRC_MACOS" > $ecr_file
echo "$HASH_MACOS $ecr_file" | shasum -a 256 -c -
else
curl "$SRC_LINUX" > $ecr_file
echo "$HASH_LINUX $ecr_file" | sha256sum -c -
fi
chmod a+x $ecr_file
if [ -z ${DOCKER_CONFIG_DIR+x} ]; then
# Alert user to modifications that must occur in the docker config
cat <<EOT
You will need to modify ~/.docker/config.json to include this line:
{
"credsStore": "ecr-login"
}
You will also need to ensure that either that the ~/.aws/credentials
file exists or that AWS_ACCESS_KEY_ID and AWS_SECRET_ACCESS_KEY are
set in the environment.
Then you should be able to docker pull/push without logging in!
EOT
else
# Configure docker to leverage the helper
mkdir -p "$DOCKER_CONFIG_DIR"
config_file="$DOCKER_CONFIG_DIR/config.json"
if [ -f "$config_file" ]; then
cp "$config_file" "$config_file.backup$(date -u +%Y%m%d%H%M%SZ)"
fi
cat <<EOT > "$config_file"
{
"credsStore": "ecr-login"
}
EOT
cat <<EOT
You will also need to ensure that either that the ~/.aws/credentials
file exists or that AWS_ACCESS_KEY_ID and AWS_SECRET_ACCESS_KEY are
set in the environment.
Then you should be able to docker pull/push without logging in!
EOT
fi
@awendland
Copy link
Author

This'll be less necessary once awslabs/amazon-ecr-credential-helper#80 happens and the binary lands in the various repos.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment