github.com/verrazzano/verrazzano@v1.7.1/tests/e2e/config/scripts/delete_ocir_repositories.sh (about)

     1  #!/bin/bash
     2  # Copyright (c) 2021, 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  set -u
     6  
     7  usage() {
     8    echo """
     9  Delete OCIR repos matching a specific pattern in the specified tenancy compartment.  You can provide either the
    10  full region name (e.g., "us-phoenix-1", or the region short name (e.g., "phx").
    11  
    12  See https://docs.oracle.com/en-us/iaas/Content/Registry/Tasks/registrycreatingarepository.htm for details on
    13  repository creation in an OCI tenancy.
    14  
    15  Usage:
    16  
    17  $0  -p <repo-name-pattern> -c <compartment-id> [-r <region> -f -w]
    18  
    19  -r Region name (e.g., "us-phoenix-1")
    20  -s Region short name (e.g., "phx", "lhr")
    21  -p Repository pattern
    22  -c Compartment ID
    23  -f Force (no prompt)
    24  -w Wait for delete
    25    """
    26  }
    27  
    28  REGION=
    29  WAIT_FOR_STATE=""
    30  FORCE=""
    31  REGION_SHORT_NAME=""
    32  
    33  while getopts ":fws:c:r:p:" opt; do
    34    case ${opt} in
    35      c ) # delete the backend
    36        COMPARTMENT_ID=${OPTARG}
    37        ;;
    38      r ) # drain the backend
    39        REGION=${OPTARG}
    40        ;;
    41      f ) # force delete, no prompt
    42        FORCE="--force"
    43        ;;
    44      p )
    45        MATCH_PATTERN="${OPTARG}"
    46        ;;
    47      s )
    48        REGION_SHORT_NAME="${OPTARG}"
    49        ;;
    50      w ) # wait for deletion
    51        WAIT_FOR_STATE="--wait-for-state DELETED"
    52        ;;
    53      \? )
    54        usage
    55        ;;
    56    esac
    57  done
    58  shift $((OPTIND -1))
    59  
    60  if [ -z "${REGION}" ]; then
    61    if [ -z "${REGION_SHORT_NAME}" ]; then
    62      echo "Must provide either the full or the short region name"
    63      usage
    64      exit 1
    65    fi
    66    REGION=$(oci --region us-phoenix-1 iam region list | jq -r  --arg regionAbbr ${REGION_SHORT_NAME} '.data[] | select(.key|test($regionAbbr;"i")) | .name')
    67    if [ -z "${REGION}" ] || [ "null" == "${REGION}" ]; then
    68      echo "Invalid short region name ${REGION_SHORT_NAME}"
    69      usage
    70      exit 1
    71    fi
    72  fi
    73  
    74  if [ -z "${MATCH_PATTERN}" ]; then
    75    echo "Repository pattern not provided"
    76    usage
    77    exit 1
    78  fi
    79  if [ -z "${COMPARTMENT_ID}" ]; then
    80    echo "Compartment ID not provided"
    81    usage
    82    exit 1
    83  fi
    84  
    85  repo_ids="$(oci --region ${REGION} artifacts container repository list --compartment-id  ${COMPARTMENT_ID} | \
    86    jq -r --arg pattern "${MATCH_PATTERN}" '.data.items[] | select(."display-name"|test($pattern)) | .id')"
    87  
    88  for id in ${repo_ids}; do
    89    repo_name=$(oci --region ${REGION} artifacts container repository get --repository-id ${id} | jq -r '.data."display-name"')
    90    echo "Deleting repository ${repo_name}, id: ${id}"
    91    oci --region ${REGION} artifacts container repository delete --repository-id ${id} ${FORCE} ${WAIT_FOR_STATE}
    92  done