|
| 1 | +# Licensed to the Apache Software Foundation (ASF) under one |
| 2 | +# or more contributor license agreements. See the NOTICE file |
| 3 | +# distributed with this work for additional information |
| 4 | +# regarding copyright ownership. The ASF licenses this file |
| 5 | +# to you under the Apache License, Version 2.0 (the |
| 6 | +# "License"); you may not use this file except in compliance |
| 7 | +# with the License. You may obtain a copy of the License at |
| 8 | +# |
| 9 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | +# |
| 11 | +# Unless required by applicable law or agreed to in writing, |
| 12 | +# software distributed under the License is distributed on an |
| 13 | +# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 14 | +# KIND, either express or implied. See the License for the |
| 15 | +# specific language governing permissions and limitations |
| 16 | +# under the License. |
| 17 | +# |
| 18 | +--- |
| 19 | +apiVersion: v1 |
| 20 | +kind: ConfigMap |
| 21 | +metadata: |
| 22 | + name: nuvolaris-milvus-minio |
| 23 | +data: |
| 24 | + initialize: |- |
| 25 | + #!/bin/sh |
| 26 | + set -e ; # Have script exit in the event of a failed command. |
| 27 | + MC_CONFIG_DIR="/etc/minio/mc/" |
| 28 | + MC="/usr/bin/mc --insecure --config-dir ${MC_CONFIG_DIR}" |
| 29 | + |
| 30 | + # connectToMinio |
| 31 | + # Use a check-sleep-check loop to wait for Minio service to be available |
| 32 | + connectToMinio() { |
| 33 | + SCHEME=$1 |
| 34 | + ATTEMPTS=0 ; LIMIT=29 ; # Allow 30 attempts |
| 35 | + set -e ; # fail if we can't read the keys. |
| 36 | + ACCESS=$(cat /config/accesskey) ; SECRET=$(cat /config/secretkey) ; |
| 37 | + set +e ; # The connections to minio are allowed to fail. |
| 38 | + echo "Connecting to Minio server: $SCHEME://$MINIO_ENDPOINT:$MINIO_PORT" ; |
| 39 | + MC_COMMAND="${MC} config host add myminio $SCHEME://$MINIO_ENDPOINT:$MINIO_PORT $ACCESS $SECRET" ; |
| 40 | + $MC_COMMAND ; |
| 41 | + STATUS=$? ; |
| 42 | + until [ $STATUS = 0 ] |
| 43 | + do |
| 44 | + ATTEMPTS=`expr $ATTEMPTS + 1` ; |
| 45 | + echo \"Failed attempts: $ATTEMPTS\" ; |
| 46 | + if [ $ATTEMPTS -gt $LIMIT ]; then |
| 47 | + exit 1 ; |
| 48 | + fi ; |
| 49 | + sleep 2 ; # 1 second intervals between attempts |
| 50 | + $MC_COMMAND ; |
| 51 | + STATUS=$? ; |
| 52 | + done ; |
| 53 | + set -e ; # reset `e` as active |
| 54 | + return 0 |
| 55 | + } |
| 56 | + |
| 57 | + # checkBucketExists ($bucket) |
| 58 | + # Check if the bucket exists, by using the exit code of `mc ls` |
| 59 | + checkBucketExists() { |
| 60 | + BUCKET=$1 |
| 61 | + CMD=$(${MC} ls myminio/$BUCKET > /dev/null 2>&1) |
| 62 | + return $? |
| 63 | + } |
| 64 | + |
| 65 | + # createBucket ($bucket, $policy, $purge) |
| 66 | + # Ensure bucket exists, purging if asked to |
| 67 | + createBucket() { |
| 68 | + BUCKET=$1 |
| 69 | + POLICY=$2 |
| 70 | + PURGE=$3 |
| 71 | + VERSIONING=$4 |
| 72 | + |
| 73 | + # Purge the bucket, if set & exists |
| 74 | + # Since PURGE is user input, check explicitly for `true` |
| 75 | + if [ $PURGE = true ]; then |
| 76 | + if checkBucketExists $BUCKET ; then |
| 77 | + echo "Purging bucket '$BUCKET'." |
| 78 | + set +e ; # don't exit if this fails |
| 79 | + ${MC} rm -r --force myminio/$BUCKET |
| 80 | + set -e ; # reset `e` as active |
| 81 | + else |
| 82 | + echo "Bucket '$BUCKET' does not exist, skipping purge." |
| 83 | + fi |
| 84 | + fi |
| 85 | + |
| 86 | + # Create the bucket if it does not exist |
| 87 | + if ! checkBucketExists $BUCKET ; then |
| 88 | + echo "Creating bucket '$BUCKET'" |
| 89 | + ${MC} mb myminio/$BUCKET |
| 90 | + else |
| 91 | + echo "Bucket '$BUCKET' already exists." |
| 92 | + fi |
| 93 | + |
| 94 | + |
| 95 | + # set versioning for bucket |
| 96 | + if [ ! -z $VERSIONING ] ; then |
| 97 | + if [ $VERSIONING = true ] ; then |
| 98 | + echo "Enabling versioning for '$BUCKET'" |
| 99 | + ${MC} version enable myminio/$BUCKET |
| 100 | + elif [ $VERSIONING = false ] ; then |
| 101 | + echo "Suspending versioning for '$BUCKET'" |
| 102 | + ${MC} version suspend myminio/$BUCKET |
| 103 | + fi |
| 104 | + else |
| 105 | + echo "Bucket '$BUCKET' versioning unchanged." |
| 106 | + fi |
| 107 | + |
| 108 | + # At this point, the bucket should exist, skip checking for existence |
| 109 | + # Set policy on the bucket |
| 110 | + echo "Setting policy of bucket '$BUCKET' to '$POLICY'." |
| 111 | + ${MC} policy set $POLICY myminio/$BUCKET |
| 112 | + } |
| 113 | + |
| 114 | + # Try connecting to Minio instance |
| 115 | + scheme=http |
| 116 | + connectToMinio $scheme |
0 commit comments