Skip to content

Commit cba57da

Browse files
suryalnvsdenyeart
authored andcommitted
[FAB-8165] Adding upgrade function to byfn
Adding upgrade functionality to byfn.sh to upgrade the fabric network from v1.0.x to v1.1 and scripts for cli to enable /Channel, /Channel/Orderer, /Channel/Application capabilities Usage: git fetch origin git checkout origin/release ./byfn.sh -m up -i 1.0.6 git checkout origin/master ./byfn.sh upgrade Change-Id: I6f53a6db39501e2653dc4c325d3d42d78f463e87 Signed-off-by: Surya <suryalnvs@gmail.com> Signed-off-by: Jason Yellick <jyellick@us.ibm.com>
1 parent c6166d6 commit cba57da

File tree

5 files changed

+305
-31
lines changed

5 files changed

+305
-31
lines changed

first-network/byfn.sh

+67-1
Original file line numberDiff line numberDiff line change
@@ -34,13 +34,14 @@ export FABRIC_CFG_PATH=${PWD}
3434
# Print the usage message
3535
function printHelp () {
3636
echo "Usage: "
37-
echo " byfn.sh up|down|restart|generate [-c <channel name>] [-t <timeout>] [-d <delay>] [-f <docker-compose-file>] [-s <dbtype>] [-i <imagetag>]"
37+
echo " byfn.sh up|down|restart|generate|upgrade [-c <channel name>] [-t <timeout>] [-d <delay>] [-f <docker-compose-file>] [-s <dbtype>] [-i <imagetag>]"
3838
echo " byfn.sh -h|--help (print this message)"
3939
echo " <mode> - one of 'up', 'down', 'restart' or 'generate'"
4040
echo " - 'up' - bring up the network with docker-compose up"
4141
echo " - 'down' - clear the network with docker-compose down"
4242
echo " - 'restart' - restart the network"
4343
echo " - 'generate' - generate required certificates and genesis block"
44+
echo " - 'upgrade' - upgrade the network from v1.0.x to v1.1"
4445
echo " -c <channel name> - channel name to use (defaults to \"mychannel\")"
4546
echo " -t <timeout> - CLI timeout duration in seconds (defaults to 10)"
4647
echo " -d <delay> - delay duration in seconds (defaults to 3)"
@@ -58,6 +59,7 @@ function printHelp () {
5859
echo " byfn.sh up -c mychannel -s couchdb -i 1.1.0-alpha"
5960
echo " byfn.sh up -l node"
6061
echo " byfn.sh down -c mychannel"
62+
echo " byfn.sh upgrade -c mychannel"
6163
echo
6264
echo "Taking all defaults:"
6365
echo " byfn.sh generate"
@@ -141,6 +143,65 @@ function networkUp () {
141143
fi
142144
}
143145

146+
# Upgrade the network from v1.0.x to v1.1
147+
# Stop the orderer and peers, backup the ledger from orderer and peers, cleanup chaincode containers and images
148+
# and relaunch the orderer and peers with latest tag
149+
function upgradeNetwork () {
150+
if [ ! -d ledgers ]; then
151+
echo "ERROR !!!! There is no persisted ledgers directory, did you start your network with -p?"
152+
exit 1
153+
fi
154+
155+
LEDGERS_BACKUP=./ledgers-backup
156+
157+
# create ledger-backup directory
158+
mkdir -p $LEDGERS_BACKUP
159+
160+
export IMAGE_TAG=$IMAGETAG
161+
if [ "${IF_COUCHDB}" == "couchdb" ]; then
162+
COMPOSE_FILES="-f $COMPOSE_FILE -f $COMPOSE_FILE_PERSIST -f $COMPOSE_FILE_COUCH"
163+
else
164+
COMPOSE_FILES="-f $COMPOSE_FILE -f $COMPOSE_FILE_PERSIST"
165+
fi
166+
167+
# removing the cli container
168+
docker-compose $COMPOSE_FILES stop cli
169+
docker-compose $COMPOSE_FILES up -d --no-deps cli
170+
171+
echo "Upgrading orderer"
172+
docker-compose $COMPOSE_FILES stop orderer.example.com
173+
docker cp -a orderer.example.com:/var/hyperledger/production/orderer $LEDGERS_BACKUP/orderer.example.com
174+
docker-compose $COMPOSE_FILES up -d --no-deps orderer.example.com
175+
176+
for PEER in peer0.org1.example.com peer1.org1.example.com peer0.org2.example.com peer1.org2.example.com; do
177+
echo "Upgrading peer $PEER"
178+
179+
# Stop the peer and backup its ledger
180+
docker-compose $COMPOSE_FILES stop $PEER
181+
docker cp -a $PEER:/var/hyperledger/production $LEDGERS_BACKUP/$PEER/
182+
183+
# Remove any old containers and images for this peer
184+
CC_CONTAINERS=$(docker ps | grep dev-$PEER | awk '{print $1}')
185+
if [ -n "$CC_CONTAINERS" ] ; then
186+
docker rm -f $CC_CONTAINERS
187+
fi
188+
CC_IMAGES=$(docker images | grep dev-$PEER | awk '{print $1}')
189+
if [ -n "$CC_IMAGES" ] ; then
190+
docker rmi -f $CC_IMAGES
191+
fi
192+
193+
# Start the peer again
194+
docker-compose $COMPOSE_FILES up -d --no-deps $PEER
195+
done
196+
197+
docker exec cli scripts/upgrade_to_v11.sh $CHANNEL_NAME $CLI_DELAY $LANGUAGE $CLI_TIMEOUT
198+
if [ $? -ne 0 ]; then
199+
echo "ERROR !!!! Test failed"
200+
exit 1
201+
fi
202+
}
203+
204+
144205
# Tear down running network
145206
function networkDown () {
146207
docker-compose -f $COMPOSE_FILE down
@@ -149,6 +210,7 @@ function networkDown () {
149210
if [ "$MODE" != "restart" ]; then
150211
#Delete any persisted ledgers
151212
docker run -v $PWD:/tmp/first-network --rm hyperledger/fabric-tools:$IMAGETAG rm -Rf /tmp/first-network/ledgers
213+
docker run -v $PWD:/tmp/first-network --rm hyperledger/fabric-tools:$IMAGETAG rm -Rf /tmp/first-network/ledgers-backup
152214
#Cleanup the chaincode containers
153215
clearContainers
154216
#Cleanup images
@@ -356,6 +418,8 @@ elif [ "$MODE" == "restart" ]; then
356418
EXPMODE="Restarting"
357419
elif [ "$MODE" == "generate" ]; then
358420
EXPMODE="Generating certs and genesis block for"
421+
elif [ "$MODE" == "upgrade" ]; then
422+
EXPMODE="Upgrading the network"
359423
else
360424
printHelp
361425
exit 1
@@ -409,6 +473,8 @@ elif [ "${MODE}" == "generate" ]; then ## Generate Artifacts
409473
elif [ "${MODE}" == "restart" ]; then ## Restart the network
410474
networkDown
411475
networkUp
476+
elif [ "${MODE}" == "upgrade" ]; then ## Upgrade the network from v1.0.x to v1.1
477+
upgradeNetwork
412478
else
413479
printHelp
414480
exit 1
+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
{
2+
"mod_policy": "Admins",
3+
"value": {
4+
"capabilities": {
5+
"V1_1": {}
6+
}
7+
},
8+
"version": "0"
9+
}

first-network/scripts/step1org3.sh

+10-30
Original file line numberDiff line numberDiff line change
@@ -29,57 +29,37 @@ if [ "$LANGUAGE" = "node" ]; then
2929
CC_SRC_PATH="/opt/gopath/src/github.com/chaincode/chaincode_example02/node/"
3030
fi
3131

32+
# import utils
33+
. scripts/utils.sh
34+
3235
echo
3336
echo "========= Creating config transaction to add org3 to network =========== "
3437
echo
3538

3639
echo "Installing jq"
3740
apt-get -y update && apt-get -y install jq
3841

39-
echo "Fetching the most recent configuration block for the channel"
40-
peer channel fetch config config_block.pb -o orderer.example.com:7050 -c ${CHANNEL_NAME} --tls --cafile ${ORDERER_CA}
41-
42-
echo "Creating config transaction adding org3 to the network"
43-
# translate channel configuration block into JSON format
44-
configtxlator proto_decode --input config_block.pb --type common.Block --output config_block.json
42+
# Fetch the config for the channel, writing it to config.json
43+
fetchChannelConfig ${CHANNEL_NAME} config.json
4544

46-
# strip away all of the encapsulating wrappers
47-
jq .data.data[0].payload.data.config config_block.json > config.json
48-
49-
# append new org to the configuration
45+
# Modify the configuration to append the new org
5046
jq -s '.[0] * {"channel_group":{"groups":{"Application":{"groups": {"Org3MSP":.[1]}}}}}' config.json ./channel-artifacts/org3.json > modified_config.json
5147

52-
# translate json config files back to protobuf
53-
configtxlator proto_encode --input config.json --type common.Config --output config.pb
54-
configtxlator proto_encode --input modified_config.json --type common.Config --output modified_config.pb
55-
56-
# get delta between old and new configs
57-
configtxlator compute_update --channel_id ${CHANNEL_NAME} --original config.pb --updated modified_config.pb --output org3_update.pb
58-
59-
# translate protobuf delta to json
60-
configtxlator proto_decode --input org3_update.pb --type common.ConfigUpdate --output org3_update.json
61-
62-
# wrap delta in an envelope message
63-
echo '{"payload":{"header":{"channel_header":{"channel_id":"'${CHANNEL_NAME}'", "type":2}},"data":{"config_update":'$(cat org3_update.json)'}}}' | jq . > org3_update_in_envelope.json
64-
65-
# translate json back to protobuf
66-
configtxlator proto_encode --input org3_update_in_envelope.json --type common.Envelope --output org3_update_in_envelope.pb
48+
# Compute a config update, based on the differences between config.json and modified_config.json, write it as a transaction to org3_update_in_envelope.pb
49+
createConfigUpdate ${CHANNEL_NAME} config.json modified_config.json org3_update_in_envelope.pb
6750

6851
echo
6952
echo "========= Config transaction to add org3 to network created ===== "
7053
echo
7154

7255
echo "Signing config transaction"
7356
echo
74-
peer channel signconfigtx -f org3_update_in_envelope.pb
57+
signConfigtxAsPeerOrg 1 org3_update_in_envelope.pb
7558

7659
echo
7760
echo "========= Submitting transaction from a different peer (peer0.org2) which also signs it ========= "
7861
echo
79-
export CORE_PEER_LOCALMSPID="Org2MSP"
80-
export CORE_PEER_TLS_ROOTCERT_FILE=/opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/peerOrganizations/org2.example.com/peers/peer0.org2.example.com/tls/ca.crt
81-
export CORE_PEER_MSPCONFIGPATH=/opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/peerOrganizations/org2.example.com/users/Admin@org2.example.com/msp
82-
export CORE_PEER_ADDRESS=peer0.org2.example.com:7051
62+
setGlobals 0 2
8363
peer channel update -f org3_update_in_envelope.pb -c ${CHANNEL_NAME} -o orderer.example.com:7050 --tls --cafile ${ORDERER_CA}
8464

8565
echo
+168
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,168 @@
1+
#!/bin/bash
2+
3+
echo
4+
echo " ____ _____ _ ____ _____ "
5+
echo "/ ___| |_ _| / \ | _ \ |_ _|"
6+
echo "\___ \ | | / _ \ | |_) | | | "
7+
echo " ___) | | | / ___ \ | _ < | | "
8+
echo "|____/ |_| /_/ \_\ |_| \_\ |_| "
9+
echo
10+
echo "Upgrade your first network (BYFN) from v1.0.x to v1.1 end-to-end test"
11+
echo
12+
CHANNEL_NAME="$1"
13+
DELAY="$2"
14+
LANGUAGE="$3"
15+
TIMEOUT="$4"
16+
: ${CHANNEL_NAME:="mychannel"}
17+
: ${DELAY:="5"}
18+
: ${LANGUAGE:="golang"}
19+
: ${TIMEOUT:="10"}
20+
LANGUAGE=`echo "$LANGUAGE" | tr [:upper:] [:lower:]`
21+
COUNTER=1
22+
MAX_RETRY=5
23+
ORDERER_CA=/opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/ordererOrganizations/example.com/orderers/orderer.example.com/msp/tlscacerts/tlsca.example.com-cert.pem
24+
25+
CC_SRC_PATH="github.com/chaincode/chaincode_example02/go/"
26+
if [ "$LANGUAGE" = "node" ]; then
27+
CC_SRC_PATH="/opt/gopath/src/github.com/chaincode/chaincode_example02/node/"
28+
fi
29+
30+
echo "Channel name : "$CHANNEL_NAME
31+
32+
# import utils
33+
. scripts/utils.sh
34+
35+
# addCapabilityToChannel <channel_id> <capabilities_group>
36+
# This function pulls the current channel config, modifies it with capabilities
37+
# for the specified group, computes the config update, signs, and submits it.
38+
addCapabilityToChannel() {
39+
CH_NAME=$1
40+
GROUP=$2
41+
42+
setOrdererGlobals
43+
44+
# Get the current channel config, decode and write it to config.json
45+
fetchChannelConfig $CH_NAME config.json
46+
47+
# Modify the correct section of the config based on capabilities group
48+
if [ $GROUP == "orderer" ]; then
49+
jq -s '.[0] * {"channel_group":{"groups":{"Orderer": {"values": {"Capabilities": .[1]}}}}}' config.json ./scripts/capabilities.json > modified_config.json
50+
elif [ $GROUP == "channel" ]; then
51+
jq -s '.[0] * {"channel_group":{"values": {"Capabilities": .[1]}}}' config.json ./scripts/capabilities.json > modified_config.json
52+
elif [ $GROUP == "application" ]; then
53+
jq -s '.[0] * {"channel_group":{"groups":{"Application": {"values": {"Capabilities": .[1]}}}}}' config.json ./scripts/capabilities.json > modified_config.json
54+
fi
55+
56+
# Create a config updated for this channel based on the differences between config.json and modified_config.json
57+
# write the output to config_update_in_envelope.pb
58+
createConfigUpdate "$CH_NAME" config.json modified_config.json config_update_in_envelope.pb
59+
60+
# Sign, and set the correct identity for submission.
61+
if [ $CH_NAME != "testchainid" ] ; then
62+
if [ $GROUP == "orderer" ]; then
63+
# Modifying the orderer group requires only the Orderer admin to sign.
64+
# Prepare to sign the update as the OrdererOrg.Admin
65+
setOrdererGlobals
66+
elif [ $GROUP == "channel" ]; then
67+
# Modifying the channel group requires a majority of application admins and the orderer admin to sign.
68+
# Sign with PeerOrg1.Admin
69+
signConfigtxAsPeerOrg 1 config_update_in_envelope.pb
70+
# Sign with PeerOrg2.Admin
71+
signConfigtxAsPeerOrg 2 config_update_in_envelope.pb
72+
# Prepare to sign the update as the OrdererOrg.Admin
73+
setOrdererGlobals
74+
elif [ $GROUP == "application" ]; then
75+
# Modifying the application group requires a majority of application admins to sign.
76+
# Sign with PeerOrg1.Admin
77+
signConfigtxAsPeerOrg 1 config_update_in_envelope.pb
78+
# Prepare to sign the update as the PeerOrg2.Admin
79+
setGlobals 0 2
80+
fi
81+
else
82+
# For the orderer system channel, only the orderer admin needs sign
83+
# which will be attached during the update
84+
setOrdererGlobals
85+
fi
86+
87+
if [ -z "$CORE_PEER_TLS_ENABLED" -o "$CORE_PEER_TLS_ENABLED" = "false" ]; then
88+
peer channel update -f config_update_in_envelope.pb -c $CH_NAME -o orderer.example.com:7050 --cafile $ORDERER_CA
89+
else
90+
peer channel update -f config_update_in_envelope.pb -c $CH_NAME -o orderer.example.com:7050 --tls true --cafile $ORDERER_CA
91+
fi
92+
res=$?
93+
verifyResult $res "Config update for \"$GROUP\" on \"$CH_NAME\" failed"
94+
echo "===================== Config update for \"$GROUP\" on \"$CH_NAME\" is completed ===================== "
95+
96+
}
97+
98+
echo "Installing jq"
99+
apt-get update
100+
apt-get install -y jq
101+
102+
sleep $DELAY
103+
104+
#Config update for /Channel/Orderer on testchainid
105+
echo "Config update for /Channel/Orderer on testchainid"
106+
addCapabilityToChannel testchainid orderer
107+
108+
sleep $DELAY
109+
110+
#Config update for /Channel on testchainid
111+
echo "Config update for /Channel on testchainid"
112+
addCapabilityToChannel testchainid channel
113+
114+
sleep $DELAY
115+
116+
#Config update for /Channel/Orderer
117+
echo "Config update for /Channel/Orderer on \"$CHANNEL_NAME\""
118+
addCapabilityToChannel $CHANNEL_NAME orderer
119+
120+
sleep $DELAY
121+
122+
#Config update for /Channel/Application
123+
echo "Config update for /Channel/Application on \"$CHANNEL_NAME\""
124+
addCapabilityToChannel $CHANNEL_NAME application
125+
126+
sleep $DELAY
127+
128+
#Config update for /Channel
129+
echo "Config update for /Channel on \"$CHANNEL_NAME\""
130+
addCapabilityToChannel $CHANNEL_NAME channel
131+
132+
#Query on chaincode on Peer0/Org1
133+
echo "Querying chaincode on org1/peer0..."
134+
chaincodeQuery 0 1 90
135+
136+
#Invoke on chaincode on Peer0/Org1
137+
echo "Sending invoke transaction on org1/peer0..."
138+
chaincodeInvoke 0 1
139+
140+
sleep $DELAY
141+
142+
#Query on chaincode on Peer0/Org1
143+
echo "Querying chaincode on org1/peer0..."
144+
chaincodeQuery 0 1 80
145+
146+
##Invoke on chaincode on Peer0/Org2
147+
echo "Sending invoke transaction on org2/peer0..."
148+
chaincodeInvoke 0 2
149+
150+
sleep $DELAY
151+
152+
#Query on chaincode on Peer0/Org2
153+
echo "Querying chaincode on org2/peer0..."
154+
chaincodeQuery 0 2 70
155+
156+
echo
157+
echo "===================== All GOOD, End-2-End UPGRADE Scenario execution completed ===================== "
158+
echo
159+
160+
echo
161+
echo " _____ _ _ ____ _____ ____ _____ "
162+
echo "| ____| | \ | | | _ \ | ____| |___ \ | ____|"
163+
echo "| _| | \| | | | | | _____ | _| __) | | _| "
164+
echo "| |___ | |\ | | |_| | |_____| | |___ / __/ | |___ "
165+
echo "|_____| |_| \_| |____/ |_____| |_____| |_____|"
166+
echo
167+
168+
exit 0

0 commit comments

Comments
 (0)