github.com/IBM-Blockchain/fabric-operator@v1.0.4/sample-network/scripts/utils.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  
    21  function logging_init() {
    22    # Reset the output and debug log files
    23    printf '' > ${LOG_FILE} > ${DEBUG_FILE}
    24  
    25    # Write all output to the control flow log to STDOUT
    26    tail -f ${LOG_FILE} &
    27  
    28    # Call the exit handler when we exit.
    29    trap "exit_fn" EXIT
    30  
    31    # Send stdout and stderr from child programs to the debug log file
    32    exec 1>>${DEBUG_FILE} 2>>${DEBUG_FILE}
    33  
    34    # There can be a race between the tail starting and the next log statement
    35    sleep 0.5
    36  }
    37  
    38  function exit_fn() {
    39    rc=$?
    40    set +x
    41  
    42    # Write an error icon to the current logging statement.
    43    if [ "0" -ne $rc ]; then
    44      pop_fn $rc
    45    fi
    46  
    47    # always remove the log trailer when the process exits.
    48    pkill -P $$
    49  }
    50  
    51  function push_fn() {
    52    #echo -ne "   - entering ${FUNCNAME[1]} with arguments $@"
    53  
    54    echo -ne "   - $@ ..." >> ${LOG_FILE}
    55  }
    56  
    57  function log() {
    58    echo -e $@ >> ${LOG_FILE}
    59  }
    60  
    61  function pop_fn() {
    62  #  echo exiting ${FUNCNAME[1]}
    63  
    64    if [ $# -eq 0 ]; then
    65      echo -ne "\r✅"  >> ${LOG_FILE}
    66      echo "" >> ${LOG_FILE}
    67      return
    68    fi
    69  
    70    local res=$1
    71    if [ $res -eq 0 ]; then
    72      echo -ne "\r✅\n"  >> ${LOG_FILE}
    73  
    74    elif [ $res -eq 1 ]; then
    75      echo -ne "\r⚠️\n" >> ${LOG_FILE}
    76  
    77    elif [ $res -eq 2 ]; then
    78      echo -ne "\r☠️\n" >> ${LOG_FILE}
    79  
    80    elif [ $res -eq 127 ]; then
    81      echo -ne "\r☠️\n" >> ${LOG_FILE}
    82  
    83    else
    84      echo -ne "\r\n" >> ${LOG_FILE}
    85    fi
    86  
    87    if [ $res -ne 0 ]; then
    88      tail -${LOG_ERROR_LINES} network-debug.log >> ${LOG_FILE}
    89    fi
    90  
    91    #echo "" >> ${LOG_FILE}
    92  }
    93  
    94  function wait_for_deployment() {
    95    local name=$1
    96    push_fn "Waiting for deployment $name"
    97  
    98    kubectl -n $NS rollout status deploy $name
    99  
   100    pop_fn
   101  }
   102  
   103  function absolute_path() {
   104    local relative_path=$1
   105  
   106    local abspath="$( cd "${relative_path}" && pwd )"
   107  
   108    echo $abspath
   109  }
   110  
   111  function apply_kustomization() {
   112    $KUSTOMIZE_BUILD $1 | envsubst | kubectl -n $NS apply -f -
   113  }
   114  
   115  function undo_kustomization() {
   116    $KUSTOMIZE_BUILD $1 | envsubst | kubectl -n $NS delete --ignore-not-found=true -f -
   117  }
   118  
   119  function create_image_pull_secret() {
   120    local secret=$1
   121    local registry=$2
   122    local username=$3
   123    local password=$4
   124  
   125    push_fn "Creating $secret for access to $registry"
   126  
   127    kubectl -n $NS delete secret $secret --ignore-not-found
   128  
   129    # todo: can this be moved to a kustomization overlay?
   130    kubectl -n $NS \
   131      create secret docker-registry \
   132      $secret \
   133      --docker-server="$registry" \
   134      --docker-username="$username" \
   135      --docker-password="$password"
   136  
   137    pop_fn
   138  }
   139  
   140  function export_peer_context() {
   141    local orgnum=$1
   142    local peernum=$2
   143    local org=org${orgnum}
   144    local peer=peer${peernum}
   145  
   146  #  export FABRIC_LOGGING_SPEC=DEBUG
   147  
   148    export FABRIC_CFG_PATH=${PWD}/config
   149    export CORE_PEER_ADDRESS=${NS}-${org}-${peer}-peer.${INGRESS_DOMAIN}:443
   150    export CORE_PEER_LOCALMSPID=Org${orgnum}MSP
   151    export CORE_PEER_TLS_ENABLED=true
   152    export CORE_PEER_MSPCONFIGPATH=${TEMP_DIR}/enrollments/${org}/users/${org}admin/msp
   153    export CORE_PEER_TLS_ROOTCERT_FILE=${TEMP_DIR}/channel-msp/peerOrganizations/${org}/msp/tlscacerts/tlsca-signcert.pem
   154  
   155  #  export | egrep "CORE_|FABRIC_"
   156  }