Skip to content

Commit e7b83d5

Browse files
author
Doug Davis
committed
fix cleanup logic
Signed-off-by: Doug Davis <[email protected]>
1 parent e96e9d2 commit e7b83d5

File tree

1 file changed

+17
-19
lines changed

1 file changed

+17
-19
lines changed

cos-event/run

Lines changed: 17 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -2,26 +2,26 @@
22

33
# Env Vars:
44
# REPOSITORY: name of the image registry/namespace to get the images
5-
# COS_ID: If set, specifies the full CRN of a Cloud Object Storage instance to use
5+
# COS_ID: If set, specifies the full CRN of a Cloud Object Storage instance to
6+
# use
67

78
export REPOSITORY=${REPOSITORY:-ibmcom}
89
export PROJECT_NAME=$(ibmcloud ce project current | \
910
awk -F: '/Project Name/{ print $2 }')
1011
export PROJECT_ID=$(ibmcloud ce project get --name ${PROJECT_NAME} | \
1112
awk '/^ID/{ print $2 }')
13+
export POLICY_ID=""
1214

1315
# Clean up previous run
1416
function clean() {
1517
set +x
16-
DCOS=$1
17-
POLICY=$2
1818
echo "Cleaning..."
1919

2020
# Delete the event subscription
2121
ibmcloud ce sub cos delete -n cos-sub -f --wait=true > /dev/null 2>&1 || true
2222

2323
# Delete the IAM auth policy
24-
if [[ $POLICY != "" ]]; then
24+
if [[ -n "$POLICY_ID" ]]; then
2525
ibmcloud iam authorization-policy-delete $POLICY --force \
2626
> /dev/null 2>&1 || true
2727
fi
@@ -33,45 +33,43 @@ function clean() {
3333
ibmcloud cos bucket-delete --bucket ${PROJECT_ID} --force \
3434
> /dev/null 2>&1 || true
3535

36-
# Delete the COS instance
37-
if [[ $DCOS -gt 0 ]]; then
36+
# Delete the COS instance unless the instance was given to us
37+
if [[ -z "$COS_ID" ]]; then
3838
ibmcloud resource service-instance-delete codeengine-cos -f -q \
3939
> /dev/null 2>&1 || true
4040
fi
4141

4242
rm -f out
4343
}
4444

45-
clean 0
45+
clean
4646
[[ "$1" == "clean" ]] && exit 0
4747

4848
set -ex
4949

50-
# Create a COS instance unless one has been specified for use, mark it for
51-
# deletion
52-
DELETE_COS=0
53-
if [[ $COS_ID == "" ]]; then
50+
CID=${COS_ID}
51+
52+
# Create a COS instance unless one has been specified for use
53+
if [[ $CID == "" ]]; then
5454
ibmcloud resource service-instance-create codeengine-cos \
5555
cloud-object-storage lite global
56-
COS_ID=$(ibmcloud resource service-instance codeengine-cos | \
56+
CID=$(ibmcloud resource service-instance codeengine-cos | \
5757
awk '/^ID/{ print $2 }')
58-
DELETE_COS=1
5958
fi
6059

6160
# Set the COS config to use this instance
62-
ibmcloud cos config crn --crn $COS_ID --force
61+
ibmcloud cos config crn --crn $CID --force
6362
ibmcloud cos config auth --method IAM
6463

65-
# Create the IAM authorization policy to allow us to receive notifications
66-
# from COS
64+
# Create IAM authorization policy so we can receive notifications from COS
6765
POLICY_ID=$(ibmcloud iam authorization-policy-create codeengine \
6866
cloud-object-storage "Notifications Manager" \
6967
--source-service-instance-name ${PROJECT_NAME} \
70-
--target-service-instance-id ${COS_ID} | awk '/^Authorization/{ print $3 }')
68+
--target-service-instance-id ${CID} | awk '/^Authorization/{ print $3 }')
7169

7270
# Create our bucket
7371
export BUCKET=${PROJECT_ID}
74-
ibmcloud cos bucket-create --bucket $BUCKET --ibm-service-instance-id $COS_ID
72+
ibmcloud cos bucket-create --bucket ${BUCKET} --ibm-service-instance-id $CID
7573

7674
# Create the app && save its URL for later
7775
ibmcloud ce app create -n cos-app --image ${REPOSITORY}/cos-listen \
@@ -107,4 +105,4 @@ echo "Event stats:"
107105
curl -Ls ${URL}/stats
108106

109107
# Clean up
110-
clean $DELETE_COS $POLICY_ID
108+
clean

0 commit comments

Comments
 (0)