Skip to content

Commit 3154fe5

Browse files
committed
add code for ECR
Signed-off-by: arcolife <[email protected]>
1 parent 0408e9a commit 3154fe5

File tree

3 files changed

+67
-0
lines changed

3 files changed

+67
-0
lines changed

sample-network/network

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,11 @@ context CONSOLE_IMAGE_LABEL latest
9090
context DEPLOYER_IMAGE ghcr.io/ibm-blockchain/fabric-deployer
9191
context DEPLOYER_IMAGE_LABEL latest-amd64
9292

93+
context AWS_PROFILE default
94+
context AWS_ACCOUNT 999999999999
95+
context AWS_ECR_REPO chaincodes
96+
context CHAINCODE_REGISTRY default
97+
9398
export FABRIC_OPERATOR_IMAGE=${OPERATOR_IMAGE}:${OPERATOR_IMAGE_LABEL}
9499
export FABRIC_CONSOLE_IMAGE=${CONSOLE_IMAGE}:${CONSOLE_IMAGE_LABEL}
95100
export FABRIC_DEPLOYER_IMAGE=${DEPLOYER_IMAGE}:${DEPLOYER_IMAGE_LABEL}
@@ -141,6 +146,7 @@ function print_help() {
141146
. scripts/test_network.sh
142147
. scripts/channel.sh
143148
. scripts/chaincode.sh
149+
. scripts/aws_ecr.sh
144150

145151
# check for kind, kubectl, etc.
146152
check_prereqs

sample-network/scripts/aws_ecr.sh

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
#!/bin/bash
2+
3+
aws_env() {
4+
push_fn "Check AWS CLI access for ${ECR_RESOURCE}"
5+
6+
AWS_ACCESS_KEY_ID=$(aws configure get aws_access_key_id --profile ${AWS_PROFILE})
7+
AWS_SECRET_ACCESS_KEY=$(aws configure get aws_secret_access_key --profile ${AWS_PROFILE})
8+
9+
ECR_USER=AWS
10+
ECR_REGION=$(aws configure get region --profile ${AWS_PROFILE})
11+
12+
export ECR_RESOURCE=${AWS_ACCOUNT}.dkr.ecr.${ECR_REGION}.amazonaws.com
13+
14+
pop_fn
15+
}
16+
17+
ecr_login() {
18+
# exported variables used:
19+
# AWS_PROFILE
20+
# AWS_ACCOUNT
21+
22+
aws_env
23+
24+
push_fn "Login to AWS ECR ${ECR_RESOURCE}"
25+
26+
aws ecr get-login-password --region ${ECR_REGION} | \
27+
$CONTAINER_CLI login --username ${ECR_USER} --password-stdin ${ECR_RESOURCE}
28+
29+
pop_fn
30+
}

sample-network/scripts/chaincode.sh

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,34 @@
1717
# limitations under the License.
1818
#
1919

20+
function set_ecr_image_tag() {
21+
# converts local "/" separated image name to an appropriate ECR tag used in AWS_ECR_REPO
22+
# Example: fabric-samples/asset-transfer-basic/chaincode-java:latest -> asset-transfer-basic_java_latest
23+
24+
local cc_local_image=$1
25+
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})
26+
}
27+
28+
function ecr_load_image() {
29+
local cc_local_image=$1
30+
31+
ecr_login ${AWS_PROFILE} ${AWS_ACCOUNT}
32+
33+
local aws_ecr="${ECR_RESOURCE}/${AWS_ECR_REPO}"
34+
35+
set_ecr_image_tag ${cc_local_image}
36+
37+
CHAINCODE_IMAGE="${aws_ecr}:${ECR_IMAGE_TAG}"
38+
39+
push_fn "Tag chaincode image for ECR"
40+
docker tag ${cc_local_image} ${CHAINCODE_IMAGE}
41+
pop_fn
42+
43+
push_fn "Load chaincode image into ECR"
44+
docker push "${CHAINCODE_IMAGE}"
45+
pop_fn
46+
}
47+
2048
# Convenience routine to "do everything" required to bring up a sample CC.
2149
function deploy_chaincode() {
2250
local cc_name=$1
@@ -33,8 +61,11 @@ function deploy_chaincode() {
3361

3462
build_chaincode_image ${cc_folder} ${CHAINCODE_IMAGE}
3563

64+
# push to container registry
3665
if [ "${CLUSTER_RUNTIME}" == "kind" ]; then
3766
kind_load_image ${CHAINCODE_IMAGE}
67+
elif [ "${CLUSTER_RUNTIME}" == "k3s" ] && [ "${CHAINCODE_REGISTRY}" == "ecr" ]; then
68+
ecr_load_image ${CHAINCODE_IMAGE}
3869
fi
3970

4071
launch_chaincode ${cc_name} ${CHAINCODE_ID} ${CHAINCODE_IMAGE}

0 commit comments

Comments
 (0)