github.com/verrazzano/verrazzano@v1.7.1/tools/scripts/bom_utils.sh (about)

     1  #!/bin/bash
     2  # Copyright (c) 2021, 2022, Oracle and/or its affiliates.
     3  # Licensed under the Universal Permissive License v 1.0 as shown at https://oss.oracle.com/licenses/upl.
     4  #
     5  # Script utilities for interacting with the Verrazzano BOM
     6  #
     7  
     8  # variables
     9  BOM_FILE=${BOM_FILE:-./verrazzano-bom.json}
    10  
    11  # Get the global Docker registry specified in the BOM
    12  function get_bom_global_registry() {
    13    cat ${BOM_FILE} | jq -r '.registry'
    14  }
    15  
    16  # Get the list of component names in the BOM
    17  function list_components() {
    18    cat ${BOM_FILE} | jq -r '.components[].name'
    19  }
    20  
    21  # List all the subcomponents for a component in the BOM
    22  function list_subcomponent_names() {
    23    local compName=$1
    24    cat ${BOM_FILE} | jq -r --arg comp ${compName} '.components[] | select(.name == $comp) | .subcomponents[].name'
    25  }
    26  
    27  # Get the repository name for a subcomponent in the BOM
    28  function get_subcomponent_repo() {
    29    local compName=$1
    30    local subcompName=$2
    31    cat ${BOM_FILE} | jq -r -c --arg comp ${compName} --arg subcomp ${subcompName} '.components[] | select(.name == $comp) | .subcomponents[] | select(.name == $subcomp) | .repository'
    32  }
    33  
    34  # Get the repository override for an image in the BOM
    35  function get_image_repo_override() {
    36    local compName=$1
    37    local subcompName=$2
    38    local imageNameTag=$3
    39    local imageName=$(echo $imageNameTag | cut -d \: -f 1)
    40    local imageTag=$(echo $imageNameTag | cut -d \: -f 2)
    41    cat ${BOM_FILE} | jq -r -c --arg comp ${compName} --arg subcomp ${subcompName} --arg image ${imageName} --arg tag ${imageTag} \
    42      '.components[] | select(.name == $comp) | .subcomponents[] | select(.name == $subcomp) | .images[] | select(.image == $image and .tag == $tag) | .repository'
    43  }
    44  
    45  # Resolves the repo for an image in the BOM; precedence is the image definition, followed by the subcomponent definition
    46  function resolve_image_repo_from_bom() {
    47    local compName=$1
    48    local subcompName=$2
    49    local imageNameTag=$3
    50  
    51    local resolvedRepo=$(get_subcomponent_repo $compName $subcompName)
    52    local imageRepoOverride=$(get_image_repo_override $compName $subcompName $imageNameTag)
    53    if [ -n "${imageRepoOverride}" ] && [ "${imageRepoOverride}" != "null" ]; then
    54      resolvedRepo=${imageRepoOverride}
    55    fi
    56    echo $resolvedRepo
    57  }
    58  
    59  # Resolves the registry location for a subcomponent in the BOM; precedence is
    60  # - the subcomponent definition
    61  # - the global BOM definition
    62  function resolve_subcomponent_registry_from_bom() {
    63    local compName=$1
    64    local subcompName=$2
    65    local subcompRegistry=$(cat ${BOM_FILE} | jq -r -c --arg comp ${compName} --arg subcomp ${subcompName} '.components[] | select(.name == $comp) | .subcomponents[] | select(.name == $subcomp) | .registry')
    66    if [ -z "$subcompRegistry" ] || [ "$subcompRegistry" == "null" ]; then
    67      subcompRegistry=$(get_bom_global_registry)
    68    fi
    69    echo $subcompRegistry
    70  }
    71  
    72  # Get the registry override for an image in the BOM
    73  function get_image_registry_override() {
    74    local compName=$1
    75    local subcompName=$2
    76    local imageNameTag=$3
    77    local imageName=$(echo $imageNameTag | cut -d \: -f 1)
    78    local imageTag=$(echo $imageNameTag | cut -d \: -f 2)
    79    cat ${BOM_FILE} | jq -r -c --arg comp ${compName} --arg subcomp ${subcompName} --arg image ${imageName} --arg tag ${imageTag} \
    80      '.components[] | select(.name == $comp) | .subcomponents[] | select(.name == $subcomp) | .images[] | select(.image == $image and .tag == $tag) | .registry'
    81  }
    82  
    83  # Resolves the registry location for an image in the BOM; precedence is
    84  # - the image definition
    85  # - the subcomponent definition
    86  # - the global BOM definition
    87  function resolve_image_registry_from_bom() {
    88    local compName=$1
    89    local subcompName=$2
    90    local imageNameTag=$3
    91  
    92    local resolvedRegistry=$(resolve_subcomponent_registry_from_bom $compName $subcompName)
    93    local imageRegistryOverride=$(get_image_registry_override $compName $subcompName $imageNameTag)
    94    if [ -n "${imageRegistryOverride}" ] && [ "${imageRegistryOverride}" != "null" ]; then
    95      resolvedRegistry=${imageRegistryOverride}
    96    fi
    97    echo $resolvedRegistry
    98  }
    99  
   100  # List the base image names for a subcomponent of a component in the BOM, in the form <image-name>:<tag>
   101  function list_subcomponent_images() {
   102    local compName=$1
   103    local subcompName=$2
   104    cat ${BOM_FILE} | jq -r --arg comp ${compName} --arg subcomp ${subcompName} \
   105      '.components[] | select(.name == $comp) | .subcomponents[] | select(.name == $subcomp) | .images[] | "\(.image):\(.tag)"'
   106  }