go.mondoo.com/cnquery@v0.0.0-20231005093811-59568235f6ea/scripts/provider_bundler.sh (about)

     1  #!/bin/bash
     2  # Copyright (c) Mondoo, Inc.
     3  # SPDX-License-Identifier: BUSL-1.1
     4  
     5  ## Build the provider and bundle it into a single file
     6  
     7  REPOROOT=$(git rev-parse --show-toplevel)
     8  PROVIDER_NAME=$1
     9  PROVIDER_PATH=$REPOROOT/providers/$PROVIDER_NAME
    10  PROVIDER_DIST=$PROVIDER_PATH/dist
    11  BUNDLE_DIST=$REPOROOT/dist
    12  
    13  cd $REPOROOT
    14  
    15  if [ -z $PROVIDER_NAME ]; then
    16    echo "Please specify a provider name."
    17    exit 1
    18  fi
    19  
    20  # Check if the provider exists
    21  if [ ! -d $PROVIDER_PATH ]; then
    22    echo "The ${PROVIDER_NAME} provider does not exist.  Please create it first."
    23    exit 1
    24  fi
    25  
    26  # Clean up the dist directory
    27  if [ -d $PROVIDER_DIST ]; then
    28    echo "Previous build detected.  Cleaning up the ${PROVIDER_NAME} provider (${PROVIDER_PATH})..."
    29    rm -rf $PROVIDER_DIST
    30  fi
    31  
    32  # Create the dist directory
    33  mkdir -p $PROVIDER_DIST
    34  mkdir -p $BUNDLE_DIST
    35  
    36  # Record the starting timestamp
    37  START_TIME=$(date +%s)
    38  
    39  # Extract the version from the provider
    40  PROVIDER_VERSION=$(grep Version ${PROVIDER_PATH}/config/config.go | cut -f2 -d\")
    41  
    42  # Build the provider
    43  echo "Building the ${PROVIDER_NAME} provider (Version: ${PROVIDER_VERSION})..."
    44  
    45  # Build the non-binary files first
    46  echo "  - Generate the plugin..."
    47  cd ${PROVIDER_PATH} && go run gen/main.go .
    48  echo "  - Compile the resources..."
    49  ${REPOROOT}/lr go ${PROVIDER_PATH}/resources/${PROVIDER_NAME}.lr --dist ${PROVIDER_DIST}
    50  echo "  - Generate the resource docs..."
    51  ${REPOROOT}/lr docs json ${PROVIDER_PATH}/resources/${PROVIDER_NAME}.lr.manifest.yaml
    52  #echo "  - Build the provider binary..."
    53  #go build -o ${PROVIDER_DIST}/${PROVIDER_NAME} ${PROVIDER_PATH}/main.go
    54  
    55  build_bundle(){
    56    GOOS=$1
    57    GOARCH=$2
    58    GOARM=$3
    59  
    60    echo "Building ${PROVIDER_DIST}/${PROVIDER_NAME} for ${GOOS}/${GOARCH}/${GOARM} ..."
    61    # we switch into the path to use the local go.mods
    62    PROVIDER_EXECUTABLE="${PROVIDER_NAME}"
    63    if [[ "${GOOS}" == "windows" ]]; then
    64      PROVIDER_EXECUTABLE="${PROVIDER_EXECUTABLE}.exe"
    65    fi
    66    cd ${PROVIDER_PATH} && CGO_ENABLED=0 GOOS=${GOOS} GOARCH=${GOARCH} GOARM=${GOARM} go build -tags production -ldflags "-s -w" -o ${PROVIDER_DIST}/${PROVIDER_EXECUTABLE} main.go
    67  
    68    # set linux flags that do not work on macos
    69    TAR_FLAGS=""
    70    if uname -s | grep -q 'Linux'; then
    71      TAR_FLAGS="--owner=0 --group=0 --no-same-owner"
    72    fi
    73  
    74    tar -cf ${BUNDLE_DIST}/${PROVIDER_NAME}_${PROVIDER_VERSION}_${GOOS}_${GOARCH}.tar.xz \
    75      ${TAR_FLAGS} --use-compress-program='xz -9v' \
    76      -C ${PROVIDER_DIST} \
    77      ${PROVIDER_EXECUTABLE} ${PROVIDER_NAME}.json ${PROVIDER_NAME}.resources.json
    78  
    79    if [ $? -ne 0 ]; then
    80      echo "Failed to build the ${PROVIDER_NAME} provider."
    81      exit 1
    82    fi
    83  
    84    rm ${PROVIDER_DIST}/${PROVIDER_EXECUTABLE}
    85  }
    86  
    87  # Build Darwin Architectures
    88  build_bundle darwin amd64
    89  build_bundle darwin arm64
    90  
    91  # Build Linux Architectures
    92  build_bundle linux amd64
    93  build_bundle linux 386
    94  build_bundle linux arm64
    95  build_bundle linux arm 6
    96  build_bundle linux arm 7
    97  build_bundle linux ppc64le
    98  
    99  # Build Windows Architectures
   100  build_bundle windows amd64
   101  build_bundle windows arm64
   102  
   103  # Generate SHA256 checksums
   104  echo "  - Generating SHA256 checksums..."
   105  cd $BUNDLE_DIST
   106  shasum -a 256 ${PROVIDER_NAME}_${PROVIDER_VERSION}_*.xz > ${PROVIDER_NAME}_${PROVIDER_VERSION}_SHA256SUMS
   107  
   108  printf "\n\n"
   109  echo "  The ${PROVIDER_NAME} provider has been built and bundled successfully."
   110  echo "  The bundles can be found in ${BUNDLE_DIST}."
   111  echo "  Time Elapsed: $((($(date +%s)-$START_TIME)/60)) minutes"
   112  echo "-------------------------------------------------------"
   113  echo ""