Skip to content

Commit

Permalink
add code for ECR
Browse files Browse the repository at this point in the history
Signed-off-by: arcolife <[email protected]>
  • Loading branch information
arcolife committed Aug 29, 2022
1 parent 0408e9a commit 3154fe5
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 0 deletions.
6 changes: 6 additions & 0 deletions sample-network/network
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,11 @@ context CONSOLE_IMAGE_LABEL latest
context DEPLOYER_IMAGE ghcr.io/ibm-blockchain/fabric-deployer
context DEPLOYER_IMAGE_LABEL latest-amd64

context AWS_PROFILE default
context AWS_ACCOUNT 999999999999
context AWS_ECR_REPO chaincodes
context CHAINCODE_REGISTRY default

export FABRIC_OPERATOR_IMAGE=${OPERATOR_IMAGE}:${OPERATOR_IMAGE_LABEL}
export FABRIC_CONSOLE_IMAGE=${CONSOLE_IMAGE}:${CONSOLE_IMAGE_LABEL}
export FABRIC_DEPLOYER_IMAGE=${DEPLOYER_IMAGE}:${DEPLOYER_IMAGE_LABEL}
Expand Down Expand Up @@ -141,6 +146,7 @@ function print_help() {
. scripts/test_network.sh
. scripts/channel.sh
. scripts/chaincode.sh
. scripts/aws_ecr.sh

# check for kind, kubectl, etc.
check_prereqs
Expand Down
30 changes: 30 additions & 0 deletions sample-network/scripts/aws_ecr.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
#!/bin/bash

aws_env() {
push_fn "Check AWS CLI access for ${ECR_RESOURCE}"

AWS_ACCESS_KEY_ID=$(aws configure get aws_access_key_id --profile ${AWS_PROFILE})
AWS_SECRET_ACCESS_KEY=$(aws configure get aws_secret_access_key --profile ${AWS_PROFILE})

ECR_USER=AWS
ECR_REGION=$(aws configure get region --profile ${AWS_PROFILE})

export ECR_RESOURCE=${AWS_ACCOUNT}.dkr.ecr.${ECR_REGION}.amazonaws.com

pop_fn
}

ecr_login() {
# exported variables used:
# AWS_PROFILE
# AWS_ACCOUNT

aws_env

push_fn "Login to AWS ECR ${ECR_RESOURCE}"

aws ecr get-login-password --region ${ECR_REGION} | \
$CONTAINER_CLI login --username ${ECR_USER} --password-stdin ${ECR_RESOURCE}

pop_fn
}
31 changes: 31 additions & 0 deletions sample-network/scripts/chaincode.sh
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,34 @@
# limitations under the License.
#

function set_ecr_image_tag() {
# converts local "/" separated image name to an appropriate ECR tag used in AWS_ECR_REPO
# Example: fabric-samples/asset-transfer-basic/chaincode-java:latest -> asset-transfer-basic_java_latest

local cc_local_image=$1
ECR_IMAGE_TAG=$(python -c 'import sys; p=sys.argv[1]; p=p.split("/")[-3:]; cc=p[1]; lang=p[-1].split("-")[-1]; tag="latest"; print(f"{cc}_{lang}_{tag}")' ${cc_local_image})
}

function ecr_load_image() {
local cc_local_image=$1

ecr_login ${AWS_PROFILE} ${AWS_ACCOUNT}

local aws_ecr="${ECR_RESOURCE}/${AWS_ECR_REPO}"

set_ecr_image_tag ${cc_local_image}

CHAINCODE_IMAGE="${aws_ecr}:${ECR_IMAGE_TAG}"

push_fn "Tag chaincode image for ECR"
docker tag ${cc_local_image} ${CHAINCODE_IMAGE}
pop_fn

push_fn "Load chaincode image into ECR"
docker push "${CHAINCODE_IMAGE}"
pop_fn
}

# Convenience routine to "do everything" required to bring up a sample CC.
function deploy_chaincode() {
local cc_name=$1
Expand All @@ -33,8 +61,11 @@ function deploy_chaincode() {

build_chaincode_image ${cc_folder} ${CHAINCODE_IMAGE}

# push to container registry
if [ "${CLUSTER_RUNTIME}" == "kind" ]; then
kind_load_image ${CHAINCODE_IMAGE}
elif [ "${CLUSTER_RUNTIME}" == "k3s" ] && [ "${CHAINCODE_REGISTRY}" == "ecr" ]; then
ecr_load_image ${CHAINCODE_IMAGE}
fi

launch_chaincode ${cc_name} ${CHAINCODE_ID} ${CHAINCODE_IMAGE}
Expand Down

0 comments on commit 3154fe5

Please sign in to comment.