From 5420c5f5bd23f19f040263b9172e83b6ce353d80 Mon Sep 17 00:00:00 2001 From: Chris Elder Date: Wed, 5 Jun 2024 12:36:43 -0400 Subject: [PATCH] Add external builders for golang and node chaincode to test-network-nano-bash The external builders will build and launch binary chaincode instead of docker containers. Signed-off-by: Chris Elder --- .gitignore | 4 +- test-network-nano-bash/README.md | 28 ++++++++++ .../configureExternalBuilders.sh | 6 ++ .../external_builders/core_yaml_change.yaml | 12 ++++ .../external_builders/golang/bin/detect | 11 ++++ .../external_builders/golang/bin/release | 12 ++++ .../external_builders/golang/bin/run | 55 +++++++++++++++++++ .../external_builders/node/bin/detect | 11 ++++ .../external_builders/node/bin/release | 11 ++++ .../external_builders/node/bin/run | 51 +++++++++++++++++ test-network-nano-bash/peer1.sh | 9 ++- test-network-nano-bash/peer2.sh | 9 ++- test-network-nano-bash/peer3.sh | 9 ++- test-network-nano-bash/peer4.sh | 9 ++- 14 files changed, 227 insertions(+), 10 deletions(-) create mode 100755 test-network-nano-bash/configureExternalBuilders.sh create mode 100644 test-network-nano-bash/external_builders/core_yaml_change.yaml create mode 100755 test-network-nano-bash/external_builders/golang/bin/detect create mode 100755 test-network-nano-bash/external_builders/golang/bin/release create mode 100755 test-network-nano-bash/external_builders/golang/bin/run create mode 100755 test-network-nano-bash/external_builders/node/bin/detect create mode 100755 test-network-nano-bash/external_builders/node/bin/release create mode 100755 test-network-nano-bash/external_builders/node/bin/run diff --git a/.gitignore b/.gitignore index 3fbf0f498e..6310950142 100644 --- a/.gitignore +++ b/.gitignore @@ -22,11 +22,11 @@ package-lock.json .settings # installed Fabric binaries etc. -bin/ +/bin/ builders/ config/ external-chaincode/ install-fabric.sh # override the ignore of all config/ folders -!full-stack-asset-transfer-guide/infrastructure/sample-network/config \ No newline at end of file +!full-stack-asset-transfer-guide/infrastructure/sample-network/config diff --git a/test-network-nano-bash/README.md b/test-network-nano-bash/README.md index 8c8540c028..455e4f6025 100644 --- a/test-network-nano-bash/README.md +++ b/test-network-nano-bash/README.md @@ -38,6 +38,34 @@ If you have [yq](https://mikefarah.gitbook.io/yq/) installed, run the following yq -i 'del(.chaincode.externalBuilders) | .chaincode.externalBuilders[0].name = "ccaas_builder" | .chaincode.externalBuilders[0].path = env(PWD) + "/builders/ccaas" | .chaincode.externalBuilders[0].propagateEnvironment[0] = "CHAINCODE_AS_A_SERVICE_BUILDER_CONFIG"' config/core.yaml ``` +## Run the chaincode without docker +You can run chaincode as binaries by enabling the external builders in the core.yaml. The external builders are configured to work with golang and node chaincode. This is accomplished by running the following command: + +``` +$ ./configureExternalBuilders.sh +``` + +This script copies the config files from `fabric-samples/config` to `fabric-samples/test-network-nano-bash/config` and adds the external builders to the core.yaml. The following is an example of the additions to core.yaml. + +``` + externalBuilders: + - name: golang + path: /Users/nanofab/fabric-samples/test-network-nano-bash/external_builders/golang + propagateEnvironment: + - HOME + - name: node + path: /Users/nanofab/fabric-samples/test-network-nano-bash/external_builders/node + propagateEnvironment: + - HOME + - npm_config_cache +``` + +Note: Golang chaincode will require at least Go 1.20 installed and in the path. Node chaincode will require at least Node 20 be installed. + + +The peer shell scripts detect the presence of the config directory in the `test-network-nano-bash/` directory and will use these config files if they exist. In order to revert to docker, simply delete the config directory in `test-network-nano-bash/`. + + # Instructions for starting network ## Running each component separately diff --git a/test-network-nano-bash/configureExternalBuilders.sh b/test-network-nano-bash/configureExternalBuilders.sh new file mode 100755 index 0000000000..b9f9a51d59 --- /dev/null +++ b/test-network-nano-bash/configureExternalBuilders.sh @@ -0,0 +1,6 @@ +mkdir -p config + +cp ../config/configtx.yaml ./config/configtx.yaml +cp ../config/orderer.yaml ./config/orderer.yaml +sed -e '/externalBuilders:/r ./external_builders/core_yaml_change.yaml' ../config/core.yaml | sed -e "s|_working_dir_|$PWD|g" > ./config/core.yaml + diff --git a/test-network-nano-bash/external_builders/core_yaml_change.yaml b/test-network-nano-bash/external_builders/core_yaml_change.yaml new file mode 100644 index 0000000000..3d4657e65d --- /dev/null +++ b/test-network-nano-bash/external_builders/core_yaml_change.yaml @@ -0,0 +1,12 @@ + - name: golang + path: _working_dir_/external_builders/golang + propagateEnvironment: + - GOCACHE + - GOENV + - HOME + - GOPROXY + - name: node + path: _working_dir_/external_builders/node + propagateEnvironment: + - HOME + - npm_config_cache diff --git a/test-network-nano-bash/external_builders/golang/bin/detect b/test-network-nano-bash/external_builders/golang/bin/detect new file mode 100755 index 0000000000..5743496d4d --- /dev/null +++ b/test-network-nano-bash/external_builders/golang/bin/detect @@ -0,0 +1,11 @@ +#!/usr/bin/env bash +# +# SPDX-License-Identifier: Apache-2.0 +# +set -euo pipefail +exec 1>&2 +CHAINCODE_METADATA_DIR="$2" +if [ "$(jq -r .type "${CHAINCODE_METADATA_DIR}/metadata.json" | tr '[:upper:]' '[:lower:]')" = "golang" ]; then + exit 0 +fi +exit 1 \ No newline at end of file diff --git a/test-network-nano-bash/external_builders/golang/bin/release b/test-network-nano-bash/external_builders/golang/bin/release new file mode 100755 index 0000000000..cd8979675b --- /dev/null +++ b/test-network-nano-bash/external_builders/golang/bin/release @@ -0,0 +1,12 @@ +#!/usr/bin/env bash +# +# SPDX-License-Identifier: Apache-2.0 +# +set -xeuo pipefail +exec 1>&2 + +BUILD_OUTPUT_DIR="$1" +RELEASE_OUTPUT_DIR="$2" +if [ -d "${BUILD_OUTPUT_DIR}/META-INF" ] ; then + cp -a "${BUILD_OUTPUT_DIR}/META-INF/"* "${RELEASE_OUTPUT_DIR}/" +fi diff --git a/test-network-nano-bash/external_builders/golang/bin/run b/test-network-nano-bash/external_builders/golang/bin/run new file mode 100755 index 0000000000..48697ab686 --- /dev/null +++ b/test-network-nano-bash/external_builders/golang/bin/run @@ -0,0 +1,55 @@ +#!/usr/bin/env bash +# +# SPDX-License-Identifier: Apache-2.0 +# +set -xeuo pipefail +exec 1>&2 +BUILD_OUTPUT_DIR="$1" +RUN_METADATA_DIR="$2" + +####################################### +# Export environment variables and extract certificate files from chaincode.json +# Globals: +# None +# Arguments: +# METADATA_DIR: Location of the chaincode.json file +# Returns: +# None +####################################### +function process_chaincode_metadata_json { + local METADATA_DIR=${RUN_METADATA_DIR} + + CORE_CHAINCODE_ID_NAME="$(jq -r .chaincode_id "$METADATA_DIR/chaincode.json")" + CORE_PEER_ADDRESS="$(jq -r .peer_address "$METADATA_DIR/chaincode.json")" + CORE_PEER_LOCALMSPID="$(jq -r .mspid "$METADATA_DIR/chaincode.json")" + export CORE_CHAINCODE_ID_NAME + export CORE_PEER_ADDRESS + export CORE_PEER_LOCALMSPID + + if [ -z "$(jq -r .client_cert "$METADATA_DIR/chaincode.json")" ]; then + CORE_PEER_TLS_ENABLED="false" + export CORE_PEER_TLS_ENABLED + else + CORE_PEER_TLS_ENABLED="true" + CORE_TLS_CLIENT_CERT_FILE="$BUILD_OUTPUT_DIR/client.crt" + CORE_TLS_CLIENT_KEY_FILE="$BUILD_OUTPUT_DIR/client.key" + CORE_PEER_TLS_ROOTCERT_FILE="$BUILD_OUTPUT_DIR/root.crt" + export CORE_PEER_TLS_ENABLED + export CORE_TLS_CLIENT_CERT_FILE + export CORE_TLS_CLIENT_KEY_FILE + export CORE_PEER_TLS_ROOTCERT_FILE + + jq -r .client_cert "$METADATA_DIR/chaincode.json" >"$CORE_TLS_CLIENT_CERT_FILE" + jq -r .client_key "$METADATA_DIR/chaincode.json" >"$CORE_TLS_CLIENT_KEY_FILE" + jq -r .root_cert "$METADATA_DIR/chaincode.json" >"$CORE_PEER_TLS_ROOTCERT_FILE" + fi +} + +# extract the required environment variables +process_chaincode_metadata_json + +# output for debug purposes +env | grep CORE | sort + + +exec "${BUILD_OUTPUT_DIR}/chaincode" -peer.address="${CORE_PEER_ADDRESS}" diff --git a/test-network-nano-bash/external_builders/node/bin/detect b/test-network-nano-bash/external_builders/node/bin/detect new file mode 100755 index 0000000000..96086926aa --- /dev/null +++ b/test-network-nano-bash/external_builders/node/bin/detect @@ -0,0 +1,11 @@ +#!/usr/bin/env bash +# +# SPDX-License-Identifier: Apache-2.0 +# +set -euo pipefail +exec 1>&2 +CHAINCODE_METADATA_DIR="$2" +if [ "$(jq -r .type "${CHAINCODE_METADATA_DIR}/metadata.json" | tr '[:upper:]' '[:lower:]')" = "node" ]; then + exit 0 +fi +exit 1 \ No newline at end of file diff --git a/test-network-nano-bash/external_builders/node/bin/release b/test-network-nano-bash/external_builders/node/bin/release new file mode 100755 index 0000000000..c241bd624e --- /dev/null +++ b/test-network-nano-bash/external_builders/node/bin/release @@ -0,0 +1,11 @@ +#!/usr/bin/env bash +# +# SPDX-License-Identifier: Apache-2.0 +# +set -euo pipefail +exec 1>&2 +BUILD_OUTPUT_DIR="$1" +RELEASE_OUTPUT_DIR="$2" +if [ -d "${BUILD_OUTPUT_DIR}/META-INF" ] ; then + cp -a "${BUILD_OUTPUT_DIR}/META-INF/"* "${RELEASE_OUTPUT_DIR}/" +fi \ No newline at end of file diff --git a/test-network-nano-bash/external_builders/node/bin/run b/test-network-nano-bash/external_builders/node/bin/run new file mode 100755 index 0000000000..139b4f499e --- /dev/null +++ b/test-network-nano-bash/external_builders/node/bin/run @@ -0,0 +1,51 @@ +#!/usr/bin/env bash +# +# SPDX-License-Identifier: Apache-2.0 +# +set -xeuo pipefail +exec 1>&2 + +BUILD_OUTPUT_DIR="$1" +RUN_METADATA_DIR="$2" + +cd "${BUILD_OUTPUT_DIR}" +ls -lart + +# extract the id, mspid, and peer address +CORE_CHAINCODE_ID_NAME="$(jq -r .chaincode_id "${RUN_METADATA_DIR}/chaincode.json")" +CORE_PEER_LOCALMSPID="$(jq -r .mspid "${RUN_METADATA_DIR}/chaincode.json")" +CORE_PEER_ADDRESS="$(jq -r .peer_address "${RUN_METADATA_DIR}/chaincode.json")" +export CORE_CHAINCODE_ID_NAME CORE_PEER_LOCALMSPID CORE_PEER_ADDRESS + +# process the TLS options if needed +if [ -z "$(jq -r .client_cert "$RUN_METADATA_DIR/chaincode.json")" ]; then + CORE_PEER_TLS_ENABLED="false" + export CORE_PEER_TLS_ENABLED +else + + export CORE_PEER_TLS_ENABLED="true" + export CORE_TLS_CLIENT_CERT_FILE="$BUILD_OUTPUT_DIR/client-pem.crt" + export CORE_TLS_CLIENT_KEY_FILE="$BUILD_OUTPUT_DIR/client-pem.key" + export CORE_TLS_CLIENT_CERT_PATH="$BUILD_OUTPUT_DIR/client.crt" + export CORE_TLS_CLIENT_KEY_PATH="$BUILD_OUTPUT_DIR/client.key" + + export CORE_PEER_TLS_ROOTCERT_FILE="$BUILD_OUTPUT_DIR/root.crt" + + jq -r .client_cert "$RUN_METADATA_DIR/chaincode.json" > "$CORE_TLS_CLIENT_CERT_FILE" + jq -r .client_key "$RUN_METADATA_DIR/chaincode.json" > "$CORE_TLS_CLIENT_KEY_FILE" + jq -r .root_cert "$RUN_METADATA_DIR/chaincode.json" > "$CORE_PEER_TLS_ROOTCERT_FILE" + + base64 -i "${CORE_TLS_CLIENT_CERT_FILE}" > "${CORE_TLS_CLIENT_CERT_PATH}" + base64 -i "${CORE_TLS_CLIENT_KEY_FILE}" > "${CORE_TLS_CLIENT_KEY_PATH}" + + #base64 -w 0 "${CORE_TLS_CLIENT_CERT_FILE}" > "${CORE_TLS_CLIENT_CERT_PATH}" + #base64 -w 0 "${CORE_TLS_CLIENT_KEY_FILE}" > "${CORE_TLS_CLIENT_KEY_PATH}" + + ls -lart "$BUILD_OUTPUT_DIR" +fi + +# output for debug purposes +env | grep CORE | sort + +# run the chaincode +exec npm start -- --peer.address="${CORE_PEER_ADDRESS}" diff --git a/test-network-nano-bash/peer1.sh b/test-network-nano-bash/peer1.sh index e6b147b58c..50b3bfd666 100755 --- a/test-network-nano-bash/peer1.sh +++ b/test-network-nano-bash/peer1.sh @@ -4,15 +4,20 @@ # set -eu -if [ "$(uname)" = "Linux" ] ; then +if [[ "$(uname)" == "Linux" || -d config ]] ; then CCADDR="127.0.0.1" else CCADDR="host.docker.internal" fi +if [ -d config ] ; then + export FABRIC_CFG_PATH="${PWD}"/config +else + export FABRIC_CFG_PATH="${PWD}"/../config +fi + # look for binaries in local dev environment /build/bin directory and then in local samples /bin directory export PATH="${PWD}"/../../fabric/build/bin:"${PWD}"/../bin:"$PATH" -export FABRIC_CFG_PATH="${PWD}"/../config export FABRIC_LOGGING_SPEC=debug:cauthdsl,policies,msp,grpc,peer.gossip.mcs,gossip,leveldbhelper=info export CORE_PEER_TLS_ENABLED=true diff --git a/test-network-nano-bash/peer2.sh b/test-network-nano-bash/peer2.sh index 6d1548df3e..e86bec8887 100755 --- a/test-network-nano-bash/peer2.sh +++ b/test-network-nano-bash/peer2.sh @@ -4,15 +4,20 @@ # set -eu -if [ "$(uname)" = "Linux" ] ; then +if [[ "$(uname)" == "Linux" || -d config ]] ; then CCADDR="127.0.0.1" else CCADDR="host.docker.internal" fi +if [ -d config ] ; then + export FABRIC_CFG_PATH="${PWD}"/config +else + export FABRIC_CFG_PATH="${PWD}"/../config +fi + # look for binaries in local dev environment /build/bin directory and then in local samples /bin directory export PATH="${PWD}"/../../fabric/build/bin:"${PWD}"/../bin:"$PATH" -export FABRIC_CFG_PATH="${PWD}"/../config export FABRIC_LOGGING_SPEC=debug:cauthdsl,policies,msp,grpc,peer.gossip.mcs,gossip,leveldbhelper=info export CORE_PEER_TLS_ENABLED=true diff --git a/test-network-nano-bash/peer3.sh b/test-network-nano-bash/peer3.sh index 7c4367a7ac..c70d3909a9 100755 --- a/test-network-nano-bash/peer3.sh +++ b/test-network-nano-bash/peer3.sh @@ -4,15 +4,20 @@ # set -eu -if [ "$(uname)" = "Linux" ] ; then +if [[ "$(uname)" == "Linux" || -d config ]] ; then CCADDR="127.0.0.1" else CCADDR="host.docker.internal" fi +if [ -d config ] ; then + export FABRIC_CFG_PATH="${PWD}"/config +else + export FABRIC_CFG_PATH="${PWD}"/../config +fi + # look for binaries in local dev environment /build/bin directory and then in local samples /bin directory export PATH="${PWD}"/../../fabric/build/bin:"${PWD}"/../bin:"$PATH" -export FABRIC_CFG_PATH="${PWD}"/../config export FABRIC_LOGGING_SPEC=debug:cauthdsl,policies,msp,grpc,peer.gossip.mcs,gossip,leveldbhelper=info export CORE_PEER_TLS_ENABLED=true diff --git a/test-network-nano-bash/peer4.sh b/test-network-nano-bash/peer4.sh index a78e9d6b81..d1f7d8f1f4 100755 --- a/test-network-nano-bash/peer4.sh +++ b/test-network-nano-bash/peer4.sh @@ -4,15 +4,20 @@ # set -eu -if [ "$(uname)" = "Linux" ] ; then +if [[ "$(uname)" == "Linux" || -d config ]] ; then CCADDR="127.0.0.1" else CCADDR="host.docker.internal" fi +if [ -d config ] ; then + export FABRIC_CFG_PATH="${PWD}"/config +else + export FABRIC_CFG_PATH="${PWD}"/../config +fi + # look for binaries in local dev environment /build/bin directory and then in local samples /bin directory export PATH="${PWD}"/../../fabric/build/bin:"${PWD}"/../bin:"$PATH" -export FABRIC_CFG_PATH="${PWD}"/../config export FABRIC_LOGGING_SPEC=debug:cauthdsl,policies,msp,grpc,peer.gossip.mcs,gossip,leveldbhelper=info export CORE_PEER_TLS_ENABLED=true