github.com/IBM-Blockchain/fabric-operator@v1.0.4/sample-network/scripts/chaincode.sh (about)

     1  #!/bin/bash
     2  #
     3  # Copyright contributors to the Hyperledger Fabric Operator project
     4  #
     5  # SPDX-License-Identifier: Apache-2.0
     6  #
     7  # Licensed under the Apache License, Version 2.0 (the "License");
     8  # you may not use this file except in compliance with the License.
     9  # You may obtain a copy of the License at:
    10  #
    11  # 	  http://www.apache.org/licenses/LICENSE-2.0
    12  #
    13  # Unless required by applicable law or agreed to in writing, software
    14  # distributed under the License is distributed on an "AS IS" BASIS,
    15  # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    16  # See the License for the specific language governing permissions and
    17  # limitations under the License.
    18  #
    19  
    20  # Convenience routine to "do everything" required to bring up a sample CC.
    21  function deploy_chaincode() {
    22    local cc_name=$1
    23    local cc_label=$2
    24    local cc_folder=$(absolute_path $3)
    25  
    26    local temp_folder=$(mktemp -d)
    27    local cc_package=${temp_folder}/${cc_name}.tgz
    28  
    29    package_chaincode       ${cc_label} ${cc_name} ${cc_package}
    30  
    31    set_chaincode_id        ${cc_package}
    32    set_chaincode_image     ${cc_folder}
    33  
    34    build_chaincode_image   ${cc_folder} ${CHAINCODE_IMAGE}
    35  
    36    if [ "${CLUSTER_RUNTIME}" == "kind" ]; then
    37      kind_load_image       ${CHAINCODE_IMAGE}
    38    fi
    39  
    40    launch_chaincode        ${cc_name} ${CHAINCODE_ID} ${CHAINCODE_IMAGE}
    41    activate_chaincode      ${cc_name} ${cc_package}
    42  }
    43  
    44  # Infer a reasonable name for the chaincode image based on the folder path conventions, or
    45  # allow the user to override with TEST_NETWORK_CHAINCODE_IMAGE.
    46  function set_chaincode_image() {
    47    local cc_folder=$1
    48  
    49    if [ -z "$TEST_NETWORK_CHAINCODE_IMAGE" ]; then
    50      # cc_folder path starting with first index of "fabric-samples"
    51      CHAINCODE_IMAGE=${cc_folder/*fabric-samples/fabric-samples}
    52    else
    53      CHAINCODE_IMAGE=${TEST_NETWORK_CHAINCODE_IMAGE}
    54    fi
    55  }
    56  
    57  # Convenience routine to "do everything other than package and launch" a sample CC.
    58  # When debugging a chaincode server, the process must be launched prior to completing
    59  # the chaincode lifecycle at the peer.  This routine provides a route for packaging
    60  # and installing the chaincode out of band, and a single target to complete the peer
    61  # chaincode lifecycle.
    62  function activate_chaincode() {
    63    local cc_name=$1
    64    local cc_package=$2
    65  
    66    set_chaincode_id    ${cc_package}
    67  
    68    install_chaincode   ${cc_package}
    69    approve_chaincode   ${cc_name} ${CHAINCODE_ID}
    70    commit_chaincode    ${cc_name}
    71  }
    72  
    73  function query_chaincode() {
    74    local cc_name=$1
    75    shift
    76  
    77    set -x
    78  
    79    export_peer_context 1 1
    80  
    81    peer chaincode query \
    82      -n  $cc_name \
    83      -C  $CHANNEL_NAME \
    84      -c  $@
    85  }
    86  
    87  function query_chaincode_metadata() {
    88    local cc_name=$1
    89    shift
    90  
    91    set -x
    92    local args='{"Args":["org.hyperledger.fabric:GetMetadata"]}'
    93  
    94    log ''
    95    log 'Org1-Peer1:'
    96    export_peer_context 1 1
    97    peer chaincode query -n $cc_name -C $CHANNEL_NAME -c $args
    98  #
    99  #  log ''
   100  #  log 'Org1-Peer2:'
   101  #  export_peer_context 1 2
   102  #  peer chaincode query -n $cc_name -C $CHANNEL_NAME -c $args
   103  }
   104  
   105  function invoke_chaincode() {
   106    local cc_name=$1
   107    shift
   108  
   109    export_peer_context 1 1
   110  
   111    peer chaincode invoke \
   112      -n              $cc_name \
   113      -C              $CHANNEL_NAME \
   114      -c              $@ \
   115      --orderer       ${NS}-org0-orderersnode1-orderer.${INGRESS_DOMAIN}:443 \
   116      --tls --cafile  ${TEMP_DIR}/channel-msp/ordererOrganizations/org0/orderers/org0-orderersnode1/tls/signcerts/tls-cert.pem \
   117      --connTimeout   ${ORDERER_TIMEOUT}
   118  
   119    sleep 2
   120  }
   121  
   122  function build_chaincode_image() {
   123    local cc_folder=$1
   124    local cc_image=$2
   125  
   126    push_fn "Building chaincode image ${cc_image}"
   127  
   128    $CONTAINER_CLI build ${CONTAINER_NAMESPACE} -t ${cc_image} ${cc_folder}
   129  
   130    pop_fn
   131  }
   132  
   133  function kind_load_image() {
   134    local cc_image=$1
   135  
   136    push_fn "Loading chaincode to kind image plane"
   137  
   138    kind load docker-image ${cc_image}
   139  
   140    pop_fn
   141  }
   142  
   143  function package_chaincode() {
   144    local cc_label=$1
   145    local cc_name=$2
   146    local cc_archive=$3
   147  
   148    local cc_folder=$(dirname $cc_archive)
   149    local archive_name=$(basename $cc_archive)
   150  
   151    push_fn "Packaging chaincode ${cc_label}"
   152  
   153    mkdir -p ${cc_folder}
   154  
   155    # Allow the user to override the service URL for the endpoint.  This allows, for instance,
   156    # local debugging at the 'host.docker.internal' DNS alias.
   157    local cc_default_address="{{.peername}}-ccaas-${cc_name}:9999"
   158    local cc_address=${TEST_NETWORK_CHAINCODE_ADDRESS:-$cc_default_address}
   159  
   160    cat << EOF > ${cc_folder}/connection.json
   161  {
   162    "address": "${cc_address}",
   163    "dial_timeout": "10s",
   164    "tls_required": false
   165  }
   166  EOF
   167  
   168    cat << EOF > ${cc_folder}/metadata.json
   169  {
   170    "type": "ccaas",
   171    "label": "${cc_label}"
   172  }
   173  EOF
   174  
   175    tar -C ${cc_folder} -zcf ${cc_folder}/code.tar.gz connection.json
   176    tar -C ${cc_folder} -zcf ${cc_archive} code.tar.gz metadata.json
   177  
   178    rm ${cc_folder}/code.tar.gz
   179  
   180    pop_fn
   181  }
   182  
   183  function launch_chaincode_service() {
   184    local org=$1
   185    local peer=$2
   186    local cc_name=$3
   187    local cc_id=$4
   188    local cc_image=$5
   189    push_fn "Launching chaincode container \"${cc_image}\""
   190  
   191    cat << EOF | envsubst | kubectl -n $NS apply -f -
   192  ---
   193  apiVersion: apps/v1
   194  kind: Deployment
   195  metadata:
   196    name: ${org}-${peer}-ccaas-${cc_name}
   197  spec:
   198    replicas: 1
   199    selector:
   200      matchLabels:
   201        app: ${org}-${peer}-ccaas-${cc_name}
   202    template:
   203      metadata:
   204        labels:
   205          app: ${org}-${peer}-ccaas-${cc_name}
   206      spec:
   207        containers:
   208          - name: main
   209            image: ${cc_image}
   210            imagePullPolicy: IfNotPresent
   211            env:
   212              - name: CHAINCODE_SERVER_ADDRESS
   213                value: 0.0.0.0:9999
   214              - name: CHAINCODE_ID
   215                value: ${cc_id}
   216              - name: CORE_CHAINCODE_ID_NAME
   217                value: ${cc_id}
   218            ports:
   219              - containerPort: 9999
   220  
   221  ---
   222  apiVersion: v1
   223  kind: Service
   224  metadata:
   225    name: ${org}-${peer}-ccaas-${cc_name}
   226  spec:
   227    ports:
   228      - name: chaincode
   229        port: 9999
   230        protocol: TCP
   231    selector:
   232      app: ${org}-${peer}-ccaas-${cc_name}
   233  EOF
   234  
   235    kubectl -n $NS rollout status deploy/${org}-${peer}-ccaas-${cc_name}
   236  
   237    pop_fn
   238  }
   239  
   240  function launch_chaincode() {
   241    local org=org1
   242    local cc_name=$1
   243    local cc_id=$2
   244    local cc_image=$3
   245  
   246    launch_chaincode_service ${org} peer1 ${cc_name} ${cc_id} ${cc_image}
   247  #  launch_chaincode_service ${org} peer2 ${cc_name} ${cc_id} ${cc_image}
   248  }
   249  
   250  function install_chaincode_for() {
   251    local org=$1
   252    local peer=$2
   253    local cc_package=$3
   254    push_fn "Installing chaincode for org ${org} peer ${peer}"
   255  
   256    export_peer_context $org $peer
   257  
   258    peer lifecycle chaincode install $cc_package
   259  
   260    pop_fn
   261  }
   262  
   263  # Package and install the chaincode, but do not activate.
   264  function install_chaincode() {
   265    local org=1
   266    local cc_package=$1
   267  
   268    install_chaincode_for ${org} 1 ${cc_package}
   269  #  install_chaincode_for ${org} 2 ${cc_package}
   270  }
   271  
   272  # approve the chaincode package for an org and assign a name
   273  function approve_chaincode() {
   274    local org=1
   275    local peer=1
   276    local cc_name=$1
   277    local cc_id=$2
   278    push_fn "Approving chaincode ${cc_name} with ID ${cc_id}"
   279  
   280    export_peer_context $org $peer
   281  
   282    peer lifecycle \
   283      chaincode approveformyorg \
   284      --channelID     ${CHANNEL_NAME} \
   285      --name          ${cc_name} \
   286      --version       1 \
   287      --package-id    ${cc_id} \
   288      --sequence      1 \
   289      --orderer       ${NS}-org0-orderersnode1-orderer.${INGRESS_DOMAIN}:443 \
   290      --tls --cafile  ${TEMP_DIR}/channel-msp/ordererOrganizations/org0/orderers/org0-orderersnode1/tls/signcerts/tls-cert.pem \
   291      --connTimeout   ${ORDERER_TIMEOUT}
   292  
   293    pop_fn
   294  }
   295  
   296  # commit the named chaincode for an org
   297  function commit_chaincode() {
   298    local org=1
   299    local peer=1
   300    local cc_name=$1
   301    push_fn "Committing chaincode ${cc_name}"
   302  
   303    export_peer_context $org $peer
   304  
   305    peer lifecycle \
   306      chaincode commit \
   307      --channelID     ${CHANNEL_NAME} \
   308      --name          ${cc_name} \
   309      --version       1 \
   310      --sequence      1 \
   311      --orderer       ${NS}-org0-orderersnode1-orderer.${INGRESS_DOMAIN}:443 \
   312      --tls --cafile  ${TEMP_DIR}/channel-msp/ordererOrganizations/org0/orderers/org0-orderersnode1/tls/signcerts/tls-cert.pem \
   313      --connTimeout   ${ORDERER_TIMEOUT}
   314  
   315    pop_fn
   316  }
   317  
   318  function set_chaincode_id() {
   319    local cc_package=$1
   320  
   321    cc_sha256=$(shasum -a 256 ${cc_package} | tr -s ' ' | cut -d ' ' -f 1)
   322    cc_label=$(tar zxfO ${cc_package} metadata.json | jq -r '.label')
   323  
   324    CHAINCODE_ID=${cc_label}:${cc_sha256}
   325  }
   326  
   327  # chaincode "group" commands.  Like "main" for chaincode sub-command group.
   328  function chaincode_command_group() {
   329    set -x
   330  
   331    COMMAND=$1
   332    shift
   333  
   334    if [ "${COMMAND}" == "deploy" ]; then
   335      log "Deploying chaincode"
   336      deploy_chaincode $@
   337      log "🏁 - Chaincode is ready."
   338  
   339    elif [ "${COMMAND}" == "activate" ]; then
   340      log "Activating chaincode"
   341      activate_chaincode $@
   342      log "🏁 - Chaincode is ready."
   343  
   344    elif [ "${COMMAND}" == "package" ]; then
   345      log "Packaging chaincode"
   346      package_chaincode $@
   347      log "🏁 - Chaincode package is ready."
   348  
   349    elif [ "${COMMAND}" == "id" ]; then
   350      set_chaincode_id $@
   351      log $CHAINCODE_ID
   352  
   353    elif [ "${COMMAND}" == "launch" ]; then
   354      log "Launching chaincode services"
   355      launch_chaincode $@
   356      log "🏁 - Chaincode services are ready"
   357  
   358    elif [ "${COMMAND}" == "install" ]; then
   359      log "Installing chaincode for org1"
   360      install_chaincode $@
   361      log "🏁 - Chaincode is installed"
   362  
   363    elif [ "${COMMAND}" == "approve" ]; then
   364      log "Approving chaincode for org1"
   365      approve_chaincode $@
   366      log "🏁 - Chaincode is approved"
   367  
   368    elif [ "${COMMAND}" == "commit" ]; then
   369      log "Committing chaincode for org1"
   370      commit_chaincode $@
   371      log "🏁 - Chaincode is committed"
   372  
   373    elif [ "${COMMAND}" == "invoke" ]; then
   374      invoke_chaincode $@ 2>> ${LOG_FILE}
   375  
   376    elif [ "${COMMAND}" == "query" ]; then
   377      query_chaincode $@ >> ${LOG_FILE}
   378  
   379    elif [ "${COMMAND}" == "metadata" ]; then
   380      query_chaincode_metadata $@ >> ${LOG_FILE}
   381  
   382    else
   383      print_help
   384      exit 1
   385    fi
   386  }