Collectd operator utilties
[demo.git] / vnfs / DAaaS / deploy / minio / templates / _helper_create_bucket.txt
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   SCHEME=$1
8   ATTEMPTS=0 ; LIMIT=29 ; # Allow 30 attempts
9   set -e ; # fail if we can't read the keys.
10   ACCESS=$(cat /config/accesskey) ; SECRET=$(cat /config/secretkey) ;
11   set +e ; # The connections to minio are allowed to fail.
12   echo "Connecting to Minio server: $SCHEME://$MINIO_ENDPOINT:$MINIO_PORT" ;
13   MC_COMMAND="mc config host add myminio $SCHEME://$MINIO_ENDPOINT:$MINIO_PORT $ACCESS $SECRET" ;
14   $MC_COMMAND ;
15   STATUS=$? ;
16   until [ $STATUS = 0 ]
17   do
18     ATTEMPTS=`expr $ATTEMPTS + 1` ;
19     echo \"Failed attempts: $ATTEMPTS\" ;
20     if [ $ATTEMPTS -gt $LIMIT ]; then
21       exit 1 ;
22     fi ;
23     sleep 2 ; # 1 second intervals between attempts
24     $MC_COMMAND ;
25     STATUS=$? ;
26   done ;
27   set -e ; # reset `e` as active
28   return 0
29 }
30
31 # checkBucketExists ($bucket)
32 # Check if the bucket exists, by using the exit code of `mc ls`
33 checkBucketExists() {
34   BUCKET=$1
35   CMD=$(/usr/bin/mc ls myminio/$BUCKET > /dev/null 2>&1)
36   return $?
37 }
38
39 # createBucket ($bucket, $policy, $purge)
40 # Ensure bucket exists, purging if asked to
41 createBucket() {
42   BUCKET=$1
43   POLICY=$2
44   PURGE=$3
45
46   # Purge the bucket, if set & exists
47   # Since PURGE is user input, check explicitly for `true`
48   if [ $PURGE = true ]; then
49     if checkBucketExists $BUCKET ; then
50       echo "Purging bucket '$BUCKET'."
51       set +e ; # don't exit if this fails
52       /usr/bin/mc rm -r --force myminio/$BUCKET
53       set -e ; # reset `e` as active
54     else
55       echo "Bucket '$BUCKET' does not exist, skipping purge."
56     fi
57   fi
58
59   # Create the bucket if it does not exist
60   if ! checkBucketExists $BUCKET ; then
61     echo "Creating bucket '$BUCKET'"
62     /usr/bin/mc mb myminio/$BUCKET
63   else
64     echo "Bucket '$BUCKET' already exists."
65   fi
66
67   # At this point, the bucket should exist, skip checking for existence
68   # Set policy on the bucket
69   echo "Setting policy of bucket '$BUCKET' to '$POLICY'."
70   /usr/bin/mc policy $POLICY myminio/$BUCKET
71 }
72
73 # Try connecting to Minio instance
74 {{- if .Values.tls.enabled }}
75 scheme=https
76 {{- else }}
77 scheme=http
78 {{- end }}
79 connectToMinio $scheme
80
81 {{- if or .Values.defaultBucket.enabled }}
82 # Create the bucket
83 createBucket {{ .Values.defaultBucket.name }} {{ .Values.defaultBucket.policy }} {{ .Values.defaultBucket.purge }}
84 {{ else if .Values.buckets }}
85 # Create the buckets
86 {{- range .Values.buckets }}
87 createBucket {{ .name }} {{ .policy }} {{ .purge }} 
88 {{- end }}
89 {{- end }}