github.com/argoproj/argo-cd@v1.8.7/util/helm/testdata/minio/templates/_helper_create_bucket.txt (about)

     1  #!/bin/sh
     2  set -e ; # Have script exit in the event of a failed command.
     3  
     4  # connectToMinio
     5  # Use a check-sleep-check loop to wait for Minio service to be available
     6  connectToMinio() {
     7    ATTEMPTS=0 ; LIMIT=29 ; # Allow 30 attempts
     8    set -e ; # fail if we can't read the keys.
     9    ACCESS=$(cat /config/accesskey) ; SECRET=$(cat /config/secretkey) ;
    10    set +e ; # The connections to minio are allowed to fail.
    11    echo "Connecting to Minio server: http://$MINIO_ENDPOINT:$MINIO_PORT" ;
    12    MC_COMMAND="mc config host add myminio http://$MINIO_ENDPOINT:$MINIO_PORT $ACCESS $SECRET" ;
    13    $MC_COMMAND ;
    14    STATUS=$? ;
    15    until [ $STATUS = 0 ]
    16    do
    17      ATTEMPTS=`expr $ATTEMPTS + 1` ;
    18      echo \"Failed attempts: $ATTEMPTS\" ;
    19      if [ $ATTEMPTS -gt $LIMIT ]; then
    20        exit 1 ;
    21      fi ;
    22      sleep 2 ; # 1 second intervals between attempts
    23      $MC_COMMAND ;
    24      STATUS=$? ;
    25    done ;
    26    set -e ; # reset `e` as active
    27    return 0
    28  }
    29  
    30  # checkBucketExists ($bucket)
    31  # Check if the bucket exists, by using the exit code of `mc ls`
    32  checkBucketExists() {
    33    BUCKET=$1
    34    CMD=$(/usr/bin/mc ls myminio/$BUCKET > /dev/null 2>&1)
    35    return $?
    36  }
    37  
    38  # createBucket ($bucket, $policy, $purge)
    39  # Ensure bucket exists, purging if asked to
    40  createBucket() {
    41    BUCKET=$1
    42    POLICY=$2
    43    PURGE=$3
    44  
    45    # Purge the bucket, if set & exists
    46    # Since PURGE is user input, check explicitly for `true`
    47    if [ $PURGE = true ]; then
    48      if checkBucketExists $BUCKET ; then
    49        echo "Purging bucket '$BUCKET'."
    50        set +e ; # don't exit if this fails
    51        /usr/bin/mc rm -r --force myminio/$BUCKET
    52        set -e ; # reset `e` as active
    53      else
    54        echo "Bucket '$BUCKET' does not exist, skipping purge."
    55      fi
    56    fi
    57  
    58    # Create the bucket if it does not exist
    59    if ! checkBucketExists $BUCKET ; then
    60      echo "Creating bucket '$BUCKET'"
    61      /usr/bin/mc mb myminio/$BUCKET
    62    else
    63      echo "Bucket '$BUCKET' already exists."
    64    fi
    65  
    66    # At this point, the bucket should exist, skip checking for existence
    67    # Set policy on the bucket
    68    echo "Setting policy of bucket '$BUCKET' to '$POLICY'."
    69    /usr/bin/mc policy $POLICY myminio/$BUCKET
    70  }
    71  
    72  # Try connecting to Minio instance
    73  connectToMinio
    74  # Create the bucket
    75  createBucket {{ .Values.defaultBucket.name }} {{ .Values.defaultBucket.policy }} {{ .Values.defaultBucket.purge }}