github.com/verrazzano/verrazzano@v1.7.0/release/scripts/download_source_prt.sh (about)

     1  #!/bin/bash
     2  #
     3  # Copyright (c) 2022, 2023, Oracle and/or its affiliates.
     4  # Licensed under the Universal Permissive License v 1.0 as shown at https://oss.oracle.com/licenses/upl.
     5  #
     6  # A script to download the source code for the images build from source.
     7  
     8  set -o pipefail
     9  set -o errtrace
    10  
    11  # variables
    12  IMAGES_TO_PUBLISH=
    13  REPO_URL_PROPS=
    14  ADDITIONAL_REPO_URLS=
    15  SAVE_DIR=
    16  DRY_RUN=false
    17  CLEAN_SAVE_DIR=false
    18  
    19  # Verrazzano repository, which is prefix for each of the entries in IMAGES_TO_PUBLISH
    20  VZ_REPO_PREFIX="verrazzano/"
    21  
    22  function exit_trap() {
    23    local rc=$?
    24    local lc="$BASH_COMMAND"
    25  
    26    if [[ $rc -ne 0 ]]; then
    27      echo "Command exited with code [$rc]"
    28    fi
    29  }
    30  
    31  trap exit_trap EXIT
    32  
    33  function usage() {
    34    ec=${1:-0}
    35    echo """
    36  A script to download the source code for the images used by Verrazzano..
    37  
    38  Options:
    39  -i Verrazzano image list
    40  -r File containing the repository URLs to download the source
    41  -a Addition source repository URLs
    42  -s Directory to download the source
    43  -d Dry run to ensure file specified by -r flag contains the URLs for all the images listed in the file specified by -i flag
    44  -c Clean the directory to download the source
    45  
    46  Examples:
    47    # Download source to /tmp/source_dir by reading the components from mydir/verrazzano_images.txt and repository URLs from mydir/repo_url.properties
    48    $0 -i mydir/verrazzano_images.txt -r mydir/repo_url.properties -s mydir/source_dir
    49  
    50  """
    51    exit ${ec}
    52  }
    53  
    54  # Validate the input flags
    55  function validateFlags() {
    56    if [ -z "${ADDITIONAL_REPO_URLS}" ] || [ "${ADDITIONAL_REPO_URLS}" == "" ]; then
    57      if [ "${REPO_URL_PROPS}" == "" ]; then
    58        echo "The file containing the repository URLs to download the source is required, but not specified by flag -r"
    59        usage 1
    60      fi
    61      if [[ ! -f "${REPO_URL_PROPS}" ]]; then
    62        echo "The file containing the repository URLs specified by flag -r doesn't exist"
    63        usage 1
    64      fi
    65    fi
    66  
    67    if [ "${DRY_RUN}" == false ]; then
    68      if [ "${SAVE_DIR}" == "" ]; then
    69        echo "The directory to save the source is required, but not specified by flag -s"
    70        usage 1
    71      fi
    72    fi
    73  }
    74  
    75  # Initialize the source download
    76  function initDownload() {
    77    # Create SAVE_DIR, if it doesn't exist
    78    if [[ ! -d "${SAVE_DIR}" ]] && [[ "$DRY_RUN" == false ]]; then
    79      mkdir -p ${SAVE_DIR}
    80    fi
    81  
    82    # Clean SAVE_DIR when $CLEAN_SAVE_DIR is true
    83    if [[ "$CLEAN_SAVE_DIR" == true ]] && [[ "$DRY_RUN" == false ]]; then
    84      rm -rf ${SAVE_DIR}/*
    85    fi
    86  }
    87  
    88  # Read the components from IMAGES_TO_PUBLISH file and download the source from the corresponding repositories
    89  function processImagesToPublish() {
    90    local imagesToPublish=$1
    91    if [ -f "${imagesToPublish}" ];
    92    then
    93      while IFS=':' read -r key value
    94      do
    95        # Skip empty lines and comments starting with #
    96        case $key in
    97         ''|\#*) continue ;;
    98        esac
    99        key=$(echo $key | tr '.' '_')
   100        key=${key#$VZ_REPO_PREFIX}
   101  
   102        # Remove till last - from value to get the short commit
   103        value=${value##*-}
   104  
   105        downloadSourceCode "$key" "${value}"
   106      done < "${imagesToPublish}"
   107    else
   108      echo "$imagesToPublish here not found."
   109    fi
   110  }
   111  
   112  # Download source using git clone,
   113  function downloadSourceCode() {
   114    local compKey=$1
   115    local commitOrBranch=$2
   116    local repoUrl=$3
   117    if [ "${repoUrl}" = "" ]; then
   118      repoUrl=$(getRepoUrl "${key}")
   119      if [ "${repoUrl}" = "" ]; then
   120        if [ "$DRY_RUN" == true ]; then
   121          echo "The repository URL for the component ${compKey} is missing from file ${REPO_URL_PROPS}"
   122          exit 1
   123        else
   124          continue
   125        fi
   126      fi
   127    fi
   128  
   129    # When DRY_RUN is set to true, do not download the source
   130    if [ "$DRY_RUN" == true ] ; then
   131      continue
   132    fi
   133  
   134    # Consider the value SKIP_CLONE for a property, to ignore cloning the repository
   135    if [ "${repoUrl}" = "SKIP_CLONE" ]; then
   136      continue
   137    fi
   138  
   139    cd "${SAVE_DIR}"
   140    changeDir=$(getChangeDir "${repoUrl}")
   141    if [ -d "${SAVE_DIR}/${changeDir}" ]; then
   142      continue
   143    fi
   144  
   145    # Create a blobless clone, downloads all reachable commits and trees while fetching blobs on-demand.
   146    git clone --filter=blob:none "${repoUrl}"
   147    cd "${changeDir}"
   148  
   149    # -c advice.detachedHead=false is used to avoid the Git message for the detached HEAD
   150    git -c advice.detachedHead=false checkout "${commitOrBranch}"
   151  
   152    # Remove git history and other files
   153    rm -rf .git .gitignore .github .gitattributes
   154    printf "\n"
   155  }
   156  
   157  # Handle the examples repository as a special case and get the source from master/main branch
   158  function downloadSourceExamples() {
   159    if [ "$DRY_RUN" == true ]; then
   160      return
   161    fi
   162    local repoUrl=$(getRepoUrl "examples")
   163    if [ "${repoUrl}" = "" ]; then
   164      return
   165    fi
   166    cd "${SAVE_DIR}"
   167    if [ -d "${SAVE_DIR}/examples" ]; then
   168      continue
   169    fi
   170  
   171    git clone "${repoUrl}"
   172    cd examples
   173    # Remove git history and other files
   174    rm -rf .git .gitignore .github .gitattributes
   175    printf "\n"
   176  }
   177  
   178  # Fetch the URL for the repository based on the a given property
   179  function getRepoUrl {
   180      local propKey=$1
   181      grep "^\\s*${propKey}=" "${REPO_URL_PROPS}"|cut -d'=' -f2
   182  }
   183  
   184  # Derive the directory from where to do - git checkout
   185  function getChangeDir() {
   186    local repoUrl=$1
   187    repoUrl=${repoUrl##*/}
   188    echo "${repoUrl%%.git}"
   189  }
   190  
   191  # Download additional source for the component, which is not part of verrazzano-bom.json
   192  function downloadAdditionalSource() {
   193    local additionalSource=$1
   194    if [ -f "${additionalSource}" ];
   195    then
   196      while IFS='=' read -r key value
   197      do
   198        # Skip empty lines and comments starting with #
   199        case $key in
   200         ''|\#*) continue ;;
   201        esac
   202        key=$(echo $key | tr '.' '_')
   203        url=$(echo "${value}"|cut -d':' -f2-)
   204        branchInfo=$(echo "${value}"|cut -d':' -f1)
   205        downloadSourceCode "$key" ${branchInfo} "${url}"
   206      done < "${additionalSource}"
   207    else
   208      echo "$additionalSource here not found."
   209    fi
   210  }
   211  
   212  # Read input flags
   213  while getopts 'a:i:r:s:d:c:' flag; do
   214    case $flag in
   215    a)
   216      ADDITIONAL_REPO_URLS=$OPTARG
   217      ;;
   218    i)
   219      IMAGES_TO_PUBLISH=$OPTARG
   220      ;;
   221    r)
   222      REPO_URL_PROPS=$OPTARG
   223      ;;
   224    s)
   225      SAVE_DIR=$OPTARG
   226      ;;
   227    d)
   228      DRY_RUN=true
   229      ;;
   230    c)
   231      CLEAN_SAVE_DIR=true
   232      ;;
   233    h | ?)
   234      usage
   235      ;;
   236    esac
   237  done
   238  
   239  # Validate the command line flags
   240  validateFlags
   241  initDownload
   242  
   243  if [ "${ADDITIONAL_REPO_URLS}" == "" ]; then
   244    processImagesToPublish "${IMAGES_TO_PUBLISH}"
   245    downloadSourceExamples
   246  else
   247    downloadAdditionalSource "${ADDITIONAL_REPO_URLS}"
   248  fi
   249  
   250  if [ "$DRY_RUN" == true ] ; then
   251     echo "Completed running the script with DRY_RUN = true"
   252  fi