#传入第一个参数 up/down/start
UP_DOWN="$1"
#channel名字 下文可以看到默认为mychannel
CH_NAME="$2"
#CLI响应timeout
CLI_TIMEOUT="$3"
#是否使用couchdb
IF_COUCHDB="$4"
#默认为10000ms
: ${CLI_TIMEOUT:="10000"}
composefile的路径
COMPOSE_FILE=docker-compose-cli.yaml
COMPOSE_FILE_COUCH=docker-compose-couch.yaml
#对传入参数进行校验
function validateArgs () {
#up_down变量为空
if [ -z "${UP_DOWN}" ]; then
echo "Option up / down / restart not mentioned"
printHelp #上面有个printhelp函数 就不写了UP_DOWN
exit 1
fi
# channel名称为空
if [ -z "${CH_NAME}" ]; then
echo "setting to default channel 'mychannel'"UP_DOWN
CH_NAME=mychannel #改为mychannel
fi
}
#network down时候执行,清除容器
function clearContainers () {
#获取容器ID
#docker ps :列出正在运行的容器
#docker ps -a 列出所有容器(包括未运行的)、-q 静默模式,只显示ID -aq 所有容器ID
CONTAINER_IDS=$(docker ps -aq)
if [ -z "$CONTAINER_IDS" -o "$CONTAINER_IDS" = " " ]; then
echo "---- No containers available for deletion ----"
else
#docker rm -f 强制删除容器UP_DOWN
docker rm -f $CONTAINER_IDS
fi
}
UP_DOWN
#network down时候执行 移除镜像
function removeUnwantedImages() {
#获取不需要的镜像的ID,grep找TAG,awk打印第三个字段(就是ID)
DOCKER_IMAGE_IDS=$(docker images | grep "dev\|none\|test-vp\|peer[0-9]-" | awk '{print $3}')
if [ -z "$DOCKER_IMAGE_IDS" -o "$DOCKER_IMAGE_IDS" = " " ]; then
echo "---- No images available for deletion ----"
else
#docker rmi 移除镜像
docker rmi -f $DOCKER_IMAGE_IDS
fi
}
# 开启网络
function networkUp () {
#判断crypto-config目录是否存在,是
if [ -d "./crypto-config" ]; then
echo "crypto-config directory already exists."
#否: 调用generateArtifacts.sh 脚本 ,传入$CH_NAME(channelname,此脚本第二个传入的参数,默认为mychannel)
else
#Generate all the artifacts that includes org certs, orderer genesis block,
# channel configuration transaction
source generateArtifacts.sh $CH_NAME
fi
#是否使用couchdb
if [ "${IF_COUCHDB}" == "couchdb" ]; then
#变量赋值 docker-compose 通过一个配置文件来管理多个Docker容器 这里通过cli.yaml和couchdb.yaml进行配置 2>&1:错误也输出到屏幕
CHANNEL_NAME=$CH_NAME TIMEOUT=$CLI_TIMEOUT docker-compose -f $COMPOSE_FILE -f $COMPOSE_FILE_COUCH up -d 2>&1
else
CHANNEL_NAME=$CH_NAME TIMEOUT=$CLI_TIMEOUT docker-compose -f $COMPOSE_FILE up -d 2>&1
fi
#上一条命令不执行成功
if [ $? -ne 0 ]; then
echo "ERROR !!!! Unable to pull the images "
exit 1
fi
#查看名为cli的容器的日志
docker logs -f cli
}
#关闭网络,主要运行上面几个函数
function networkDown () {
docker-compose -f $COMPOSE_FILE -f $COMPOSE_FILE_COUCH down
#Cleanup the chaincode containers
clearContainers
#Cleanup images
removeUnwantedImages
# remove orderer block and other channel configuration transactions and certs 移除block等文件
rm -rf channel-artifacts/*.block channel-artifacts/*.tx crypto-config
}
#脚本主要逻辑 先验证参数,再通过UP_DOWN分别执行不同的函数
validateArgs
#Create the network using docker compose
if [ "${UP_DOWN}" == "up" ]; then
networkUp
elif [ "${UP_DOWN}" == "down" ]; then ## Clear the network
networkDown
elif [ "${UP_DOWN}" == "restart" ]; then ## Restart the network
networkDown
networkUp
else
printHelp
exit 1
fi
网友评论