Skip to content

Commit

Permalink
Merged in release/0.1.0 (pull request #42)
Browse files Browse the repository at this point in the history
Fabkit v0.1.0 Release

Approved-by: George Paulose
  • Loading branch information
czar0 committed May 27, 2021
2 parents f803821 + d6addb6 commit 0c368f5
Show file tree
Hide file tree
Showing 9 changed files with 78 additions and 85 deletions.
7 changes: 2 additions & 5 deletions .env
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
FABKIT_VERSION=0.1.0-rc2
FABKIT_VERSION=0.1.0
########################################
# PATHS
########################################
Expand Down Expand Up @@ -68,7 +68,4 @@ FABKIT_PROMETHEUS_VERSION=v2.21.0
########################################
# THIRD PARTY
########################################
# customized golang image with git support for build and test
FABKIT_GOLANG_DOCKER_IMAGE=everledgerio/golang:1.15.5-alpine3.12
FABKIT_JQ_DOCKER_IMAGE=imega/jq:1.6
FABKIT_YQ_DOCKER_IMAGE=mikefarah/yq:4.2.0
FABKIT_DOCKER_IMAGE="everledgerio/fabkit:${FABKIT_VERSION}"
11 changes: 6 additions & 5 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -1,15 +1,16 @@
FROM alpine:3.13.0

RUN echo "https://dl-cdn.alpinelinux.org/alpine/edge/community" >> /etc/apk/repositories
RUN echo "@edgecommunity https://dl-cdn.alpinelinux.org/alpine/edge/community" >> /etc/apk/repositories

RUN apk --update add --no-cache \
bash \
RUN apk add --no-cache \
bash@edgecommunity \
gawk \
ncurses \
git \
zip \
rsync \
jq \
yq \
jq@edgecommunity \
yq@edgecommunity \
go \
npm \
openjdk11 \
Expand Down
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,13 @@ If your OS supports `bash` and you are running this code locally, then beware th

If you want to be able to use **test** and **build** functionalities for your chaincode, depending on the language which it is written in, you may need to install any of the following dependencies:

- [Go](https://golang.org/doc/install) [>= 1.11]
- [Go](https://golang.org/doc/install) [>= 1.12]
- [Node](https://nodejs.org/en/) [>= 10.20]
- [Java](https://adoptopenjdk.net/) [>= 8]

### For Windows users

Windows users will need to run Fabkit by using the provided Docker image and particularly caring about the syntax when defining the root installation directory.
Windows users will need to run Fabkit by using the provided Docker image and particularly caring about the syntax when defining the root installation directory.

We recommend to create a directory under your user's home directory, for example, in `C:\Users\me\...\fabkit` (where `C:` is the disk used by Windows, `me` is your current username and `...` means "any directory in between").

Expand Down
21 changes: 8 additions & 13 deletions fabkit
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ readonly FABKIT_DOCKER_COMPOSE_VERSION_SUPPORTED="1.24"
# shellcheck disable=SC2034
readonly FABKIT_BASH_VERSION_SUPPORTED="3.2"
# shellcheck disable=SC2034
readonly FABKIT_GO_VERSION_SUPPORTED="1.11"
readonly FABKIT_GO_VERSION_SUPPORTED="1.12"
# shellcheck disable=SC2034
readonly FABKIT_NODE_VERSION_SUPPORTED="10.20"
# shellcheck disable=SC2034
Expand Down Expand Up @@ -59,19 +59,14 @@ __log_setup() {
echo
}

__yq() {
if type -p yq &>/dev/null; then
yq "$@"
__run() {
local path="$1"
local cmd="$2"
shift 2 || true
if type -p "$cmd" &>/dev/null; then
cd "$path" && (eval "$cmd" "$@")
else
docker run --rm -i -v "${PWD}":/workdir "$FABKIT_YQ_DOCKER_IMAGE" "$@"
fi
}

__jq() {
if type -p jq &>/dev/null; then
jq "$@"
else
docker run --rm -i -v "${PWD}":/workdir "$FABKIT_JQ_DOCKER_IMAGE" "$@"
docker run --rm -i -v "$path":/workdir -w /workdir "$FABKIT_DOCKER_IMAGE" bash -c "$cmd $*"
fi
}

Expand Down
49 changes: 20 additions & 29 deletions scripts/chaincode.sh
Original file line number Diff line number Diff line change
Expand Up @@ -55,16 +55,14 @@ chaincode_test() {

__init_go_mod install "$chaincode_path"

if ! type -p go &>/dev/null; then
if ! docker run --rm -v "${FABKIT_CHAINCODE_PATH}:/usr/src/myapp" -w "/usr/src/myapp/${chaincode_name}" "$FABKIT_GOLANG_DOCKER_IMAGE" go test ./... &>/dev/null; then
logerr "Failed testing chaincode $chaincode_name"
exit 1
fi
else
if ! (cd "$chaincode_path" && go test ./... &>/dev/null); then
logerr "Failed testing chaincode $chaincode_name"
exit 1
fi
if ! (__run "$chaincode_path" go test ./... &>/dev/null); then
logerr "Failed testing chaincode $chaincode_name"
exit 1
fi
elif [ "$chaincode_language" = "node" ]; then
if ! (__run "$chaincode_path" npm run test &>/dev/null); then
logerr "Failed testing chaincode $chaincode_name"
exit 1
fi
fi

Expand All @@ -86,16 +84,9 @@ chaincode_build() {
__check_go_version
__init_go_mod install "$chaincode_path"

if ! type -p go &>/dev/null; then
if ! (docker run --rm -v "${FABKIT_CHAINCODE_PATH}:/usr/src/myapp" -w "/usr/src/myapp/${chaincode_name}" -e CGO_ENABLED=0 "$FABKIT_GOLANG_DOCKER_IMAGE" bash -c "go build -a -installsuffix nocgo ./... &>/dev/null && rm -rf ./\"${chaincode_name}\" &>/dev/null"); then
logerr "Failed building chaincode $chaincode_name"
exit 1
fi
else
if ! (cd "${chaincode_path}" && CGO_ENABLED=0 go build -a -installsuffix nocgo ./... &>/dev/null && rm -rf "./${chaincode_name}" &>/dev/null); then
logerr "Failed building chaincode $chaincode_name"
exit 1
fi
if ! (__run "$chaincode_path" go build ./... &>/dev/null && rm -rf "${chaincode_path}/${chaincode_name}" &>/dev/null); then
logerr "Failed building chaincode $chaincode_name"
exit 1
fi
fi

Expand Down Expand Up @@ -432,14 +423,14 @@ lifecycle_chaincode_package_id() {
logdebu "Chaincode label: $chaincode_label"

if [ "${FABKIT_TLS_ENABLED:-}" = "false" ]; then
cmd+="peer lifecycle chaincode queryinstalled --output json | __jq -r '.installed_chaincodes[] | select(.label == ${chaincode_label})' | __jq -r '.package_id'"
cmd+="peer lifecycle chaincode queryinstalled --output json"
else
cmd+="peer lifecycle chaincode queryinstalled --tls $FABKIT_TLS_ENABLED --cafile $ORDERER_CA --output json | __jq -r '.installed_chaincodes[] | select(.label == ${chaincode_label})' | __jq -r '.package_id'"
cmd+="peer lifecycle chaincode queryinstalled --tls $FABKIT_TLS_ENABLED --cafile $ORDERER_CA --output json"
fi

__clear_logdebu
logdebu "Excecuting command: ${cmd}"
export PACKAGE_ID=$(eval "$cmd")
export PACKAGE_ID=$(eval "$cmd" | __run "$FABKIT_ROOT" jq -r "'.installed_chaincodes[] | select(.label == ${chaincode_label})'" | __run "$FABKIT_ROOT" jq -r '.package_id')
if echo "$PACKAGE_ID" | grep -iE "erro|pani|fail|fatal" > >(__throw >&2); then
logerr "Error retrieving package id for chaincode $chaincode_name"
exit 1
Expand Down Expand Up @@ -735,29 +726,29 @@ __init_go_mod() {
cd "${chaincode_relative_path}" >/dev/null 2> >(__throw >&2) || exit 1

if [ ! -f "./go.mod" ]; then
if ! go mod init &>/dev/null; then
if ! __run "${chaincode_relative_path}" go mod init &>/dev/null; then
logerr "Error initializing go mod"
exit 1
fi
fi

if [ "${operation}" = "install" ]; then
if ! go get ./... &>/dev/null; then
if ! __run "${chaincode_relative_path}" go get ./... &>/dev/null; then
logerr "Error installing go modules"
exit 1
fi
elif [ "${operation}" = "update" ]; then
if ! go get -u=patch ./... &>/dev/null; then
if ! __run "${chaincode_relative_path}" go get -u=patch ./... &>/dev/null; then
logerr "Error updating go modules"
exit 1
fi
fi

if ! go mod tidy &>/dev/null; then
if ! __run "${chaincode_relative_path}" go mod tidy &>/dev/null; then
logerr "Error running go mod tidy"
exit 1
fi
if ! go mod vendor &>/dev/null; then
if ! __run "${chaincode_relative_path}" go mod vendor &>/dev/null; then
logerr "Error downloading go vendor"
exit 1
fi
Expand Down
13 changes: 6 additions & 7 deletions scripts/explorer.sh
Original file line number Diff line number Diff line change
@@ -1,11 +1,7 @@
#!/usr/bin/env bash

start_explorer() {
loginfo "Starting explorer"
echo

(stop_explorer) &
__spinner
loginfoln "Starting explorer"

if ! docker ps | grep -q "fabric"; then
logerr "No Fabric networks running. First launch fabkit start"
Expand All @@ -17,12 +13,15 @@ start_explorer() {
exit 1
fi

__clear_logdebu
(stop_explorer) &
__spinner

# replacing private key path in connection profile
config="${FABKIT_EXPLORER_PATH}/connection-profile/first-network"
admin_key_path="peerOrganizations/org1.example.com/users/[email protected]/msp/keystore"
private_key="/tmp/crypto/${admin_key_path}/$(ls ${FABKIT_CRYPTOS_PATH}/${admin_key_path})"
if (__jq <"${config}.base.json" -r --arg private_key "$private_key" '.organizations.Org1MSP.adminPrivateKey.path = $private_key' |
__jq -r --argjson FABKIT_TLS_ENABLED "$FABKIT_TLS_ENABLED" '.client.tlsEnable = $FABKIT_TLS_ENABLED' >"${config}.json") 2>&1 >/dev/null | grep -iE "erro|pani|fail|fatal" > >(__throw >&2); then
if (cat "${config}.base.json" | __run "$FABKIT_ROOT" jq -r "'.organizations.Org1MSP.adminPrivateKey.path = \"$private_key\" | .client.tlsEnable = $FABKIT_TLS_ENABLED'" >"${config}.json") 2>&1 >/dev/null | grep -iE "erro|pani|fail|fatal" > >(__throw >&2); then
logerr "Error in replacing private key in explorer configuration"
exit 1
fi
Expand Down
6 changes: 3 additions & 3 deletions scripts/loggers.sh
Original file line number Diff line number Diff line change
Expand Up @@ -32,11 +32,11 @@ logdebu() {
}

tostring() {
echo "$*" | __jq tostring 2>/dev/null || echo "${*//\"/\\\"}"
echo "$*" | __run "$FABKIT_ROOT" jq tostring 2>/dev/null || echo "${*//\"/\\\"}"
}

tojson() {
echo "$*" | __jq . 2>/dev/null || echo "${*//\\\"/\"}"
echo "$*" | __run "$FABKIT_ROOT" jq . 2>/dev/null || echo "${*//\\\"/\"}"
}

# used to simulate spinner formatting when running within sub-processes
Expand Down Expand Up @@ -83,7 +83,7 @@ __print_to_file() {
local tag="$3"
local timestamp=$(date -u +"%Y-%m-%d %H:%M:%S UTC")

echo -e "$timestamp $tag $message" >>"$file"
echo -e "$timestamp $tag $message" >>"$file" 2>/dev/null
}

# remove dangling spinner when running in debug mode
Expand Down
35 changes: 22 additions & 13 deletions scripts/network.sh
Original file line number Diff line number Diff line change
Expand Up @@ -30,29 +30,25 @@ __docker_fabric_pull() {
__docker_third_party_images_pull() {
loginfo "Pulling utilities images"
__clear_logdebu
logdebu "Pulling ${FABKIT_GOLANG_DOCKER_IMAGE}"
docker pull "$FABKIT_GOLANG_DOCKER_IMAGE" 1>/dev/null 2> >(__throw >&2)
logdebu "Pulling ${FABKIT_JQ_DOCKER_IMAGE}"
docker pull "$FABKIT_JQ_DOCKER_IMAGE" 1>/dev/null 2> >(__throw >&2)
logdebu "Pulling ${FABKIT_YQ_DOCKER_IMAGE}"
docker pull "$FABKIT_YQ_DOCKER_IMAGE" 1>/dev/null 2> >(__throw >&2)
logdebu "Pulling ${FABKIT_DOCKER_IMAGE}"
docker pull "$FABKIT_DOCKER_IMAGE" 1>/dev/null 2> >(__throw >&2)
}

start_network() {
loginfoln "Starting Fabric network"

if (docker volume ls | grep -q "$FABKIT_DOCKER_NETWORK") && [[ (-z "${FABKIT_RESET}" || "${FABKIT_RESET}" = "false") ]]; then
logwarn "Found volumes"
read -rp "Do you wish to restart the network and reuse this data? (y/N) " yn
read -rp "Do you wish to restart the network and reuse this data? (Y/n) " yn
case $yn in
[Yy]*)
[Nn]*) ;;
*)
__load_lastrun
__log_setup
restart_network &
__spinner
return 0
;;
*) ;;
esac
fi

Expand Down Expand Up @@ -178,6 +174,7 @@ stop_network() {
fi

if docker ps | grep -q "hyperledger/explorer"; then
echo -en "\n\033[3C→ "
stop_explorer
fi

Expand Down Expand Up @@ -221,27 +218,39 @@ initialize_network() {
(chaincode_instantiate "$FABKIT_CHAINCODE_NAME" "$FABKIT_CHAINCODE_VERSION" "$FABKIT_CHANNEL_NAME" 1 0) &
__spinner
fi

local key="mydiamond"
local value='{\"cut_grade\":\"excellent\",\"color_grade\":\"d\",\"clarity_grade\":\"vs1\",\"carat_weight\":0.31,\"origin\":\"russia\",\"certifier\":\"GIA\",\"certificate_no\":\"5363986006\",\"uri\":\"https://provenance.everledger.io/time-lapse/GIA/5363986006\"}'
echo
logsucc "Great! Your blockchain network is up and running 🚀 now you can try to run a couple of simple commands:"
echo
echo "Let's add a new diamond! ✨💎✨"
loghead "\tfabkit chaincode invoke mychannel mygocc 1 0 '{\"Args\":[\"put\",\"$key\",\"$value\"]}'\n"
echo "And then let's fetch it! 🤩 (tip: click on the uri link to explore its journey 🌎)"
loghead "\tfabkit chaincode query mychannel mygocc 1 0 '{\"Args\":[\"get\",\"$key\"]}'\n"
echo
echo "Find more available commands at: $(loginfo "https://github.com/everledger/fabkit/blob/master/docs/chaincode.md")"
}

__replace_config_capabilities() {
configtx=${FABKIT_CONFIG_PATH}/configtx
if [[ "${FABKIT_FABRIC_VERSION}" =~ 2.* ]]; then
if (__yq <"${configtx}.base.yaml" e '.Capabilities.Channel.V2_0 = true |
if (cat "${configtx}.base.yaml" | __run "$FABKIT_ROOT" yq "e '.Capabilities.Channel.V2_0 = true |
.Capabilities.Channel.V1_4_3 = false |
.Capabilities.Orderer.V2_0 = true |
.Capabilities.Orderer.V1_4_2 = false |
.Capabilities.Application.V2_0 = true |
.Capabilities.Application.V1_4_2 = false' - >"${configtx}.yaml") 2>&1 >/dev/null | grep -iE "erro|pani|fail|fatal" > >(__throw >&2); then
.Capabilities.Application.V1_4_2 = false' -" >"${configtx}.yaml") 2>&1 >/dev/null | grep -iE "erro|pani|fail|fatal" > >(__throw >&2); then
logerr "Error in replacing Fabric capabilities"
exit 1
fi
else
if (__yq <"${configtx}.base.yaml" e '.Capabilities.Channel.V2_0 = false |
if (cat "${configtx}.base.yaml" | __run "$FABKIT_ROOT" yq "e '.Capabilities.Channel.V2_0 = false |
.Capabilities.Channel.V1_4_3 = true |
.Capabilities.Orderer.V2_0 = false |
.Capabilities.Orderer.V1_4_2 = true |
.Capabilities.Application.V2_0 = false |
.Capabilities.Application.V1_4_2 = true' - >"${configtx}.yaml") 2>&1 >/dev/null | grep -iE "erro|pani|fail|fatal" > >(__throw >&2); then
.Capabilities.Application.V1_4_2 = true' -" >"${configtx}.yaml") 2>&1 >/dev/null | grep -iE "erro|pani|fail|fatal" > >(__throw >&2); then
logerr "Error in replacing Fabric capabilities"
exit 1
fi
Expand Down
17 changes: 9 additions & 8 deletions scripts/utils.sh
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,10 @@ __check_version() {
local installed_version=($(echo -e "${3//./\\n}"))

if ! type -p "$cmd" &>/dev/null; then
logerr "${cmd} is not installed. Version >= $2} is required"
exit 1
if (docker run --rm -i "$FABKIT_DOCKER_IMAGE" bash -c "type -p $cmd" 2>&1 >/dev/null | grep -iE "erro|pani|fail|fatal") > >(__throw >&2); then
logerr "${cmd} is not installed. Version >= $2 is required"
exit 1
fi
fi

for i in "${!supported_version[@]}"; do
Expand All @@ -34,26 +36,26 @@ __check_dep_version() {

__check_bash_version() {
# shellcheck disable=SC2155
local version="$(bash --version 2>/dev/null | cut -d ' ' -f 4 | head -n 1 | cut -d '(' -f 1)"
local version="$(__run "$FABKIT_ROOT" bash --version 2>/dev/null | cut -d ' ' -f 4 | head -n 1 | cut -d '(' -f 1)"
__check_version bash "$FABKIT_BASH_VERSION_SUPPORTED" "$version"
}

__check_go_version() {
# shellcheck disable=SC2155
local version=$(go version 2>/dev/null | cut -d ' ' -f 3 | cut -c 3-)
local version=$(__run "$FABKIT_ROOT" go version 2>/dev/null | cut -d ' ' -f 3 | cut -c 3-)
__check_version go "$FABKIT_GO_VERSION_SUPPORTED" "$version"
}

__check_node_version() {
# shellcheck disable=SC2155
local version=$(node -v 2>/dev/null | cut -c 2-)
local version=$(__run "$FABKIT_ROOT" node -v 2>/dev/null | cut -c 2-)
__check_version node "$FABKIT_NODE_VERSION_SUPPORTED" "$version"

}

__check_java_version() {
# shellcheck disable=SC2155
local version=$(java --version 2>/dev/null | cut -d ' ' -f 2 | head -n 1)
local version=$(__run "$FABKIT_ROOT" java --version 2>/dev/null | cut -d ' ' -f 2 | head -n 1)
__check_version java "$FABKIT_JAVA_VERSION_SUPPORTED" "$version"
}

Expand All @@ -66,7 +68,6 @@ __check_docker_version() {
# shellcheck disable=SC2155
local version=$(docker version --format '{{.Server.Version}}' 2>/dev/null)
__check_version docker "$FABKIT_DOCKER_VERSION_SUPPORTED" "$version"

# shellcheck disable=SC2155
version=$(docker-compose version --short 2>/dev/null)
__check_version docker-compose "$FABKIT_DOCKER_COMPOSE_VERSION_SUPPORTED" "$version"
Expand Down Expand Up @@ -187,7 +188,7 @@ __catch() {
((frame++)) || true
line="$(caller "$frame" 2>&1 | cut -d ' ' -f 1)"
done
logwarn "Check the log file for more details: cat $FABKIT_LOGFILE"
logwarn "Check the log file for more details: cat ${FABKIT_LOGFILE/$FABKIT_ROOT/$FABKIT_HOST_ROOT}"

exit "$1"
}
Expand Down

0 comments on commit 0c368f5

Please sign in to comment.