forked from IBM/CodeEngine
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrun
executable file
·94 lines (74 loc) · 2.7 KB
/
run
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
#!/bin/bash
# Env Vars:
# REGISTRY: name of the image registry/namespace to get the images
# COS_ID: If set, specifies the full CRN of a Cloud Object Storage instance to
# use
export REGISTRY=${REGISTRY:-ibmcom}
export PROJECT_NAME=$(ibmcloud ce project current | \
awk -F: '/Project Name/{ print $2 }' | sed "s/ *$//")
export PROJECT_ID=$(ibmcloud ce project get --name ${PROJECT_NAME} | \
awk '/^ID/{ print $2 }')
export POLICY_ID=""
export BUCKET=${PROJECT_NAME}-${PROJECT_ID}
# Clean up previous run
function clean() {
set +ex
echo "Cleaning..."
(
ibmcloud ce sub cos delete -n cos-sub -f --wait=true
if [[ -n "$POLICY_ID" ]]; then
ibmcloud iam authorization-policy-delete $POLICY --force
fi
ibmcloud ce app delete --name cos-app --force
ibmcloud cos bucket-delete --bucket ${BUCKET} --force
if [[ -z "$COS_ID" ]]; then
# Delete the COS instance unless the instance was given to us
ibmcloud resource service-instance-delete ce-cos-event -f -q
fi
rm -f out
) > /dev/null 2>&1
}
clean
[[ "$1" == "clean" ]] && exit 0
set -ex
CID=${COS_ID}
# Create a COS instance unless one has been specified for use
if [[ $CID == "" ]]; then
ibmcloud resource service-instance-create ce-cos-event \
cloud-object-storage lite global
CID=$(ibmcloud resource service-instance ce-cos-event | \
awk '/^ID/{ print $2 }')
fi
# Set the COS config to use this instance
ibmcloud cos config crn --crn $CID --force
ibmcloud cos config auth --method IAM
# Create IAM authorization policy so we can receive notifications from COS
POLICY_ID=$(ibmcloud iam authorization-policy-create codeengine \
cloud-object-storage "Notifications Manager" \
--source-service-instance-name ${PROJECT_NAME} \
--target-service-instance-id ${CID} | awk '/^Authorization/{ print $3 }')
# Create our bucket
ibmcloud cos bucket-create --bucket ${BUCKET} --ibm-service-instance-id $CID
# Create the app && save its URL for later
ibmcloud ce app create -n cos-app --image ${REGISTRY}/cos-listen \
--min-scale=1 --max-scale=1
URL=$(ibmcloud ce app get --output url --name cos-app)
# Setup the COS Event Source
ibmcloud ce sub cos create -n cos-sub -d cos-app -b ${BUCKET}
# Now wait until we get the event - shouldn't take more than a minute
while true ; do
# Upload a file to COS (this will generate an event)
ibmcloud cos object-put --bucket ${BUCKET} --key "README.md" --body README.md
# Delete the file from COS (another event)
ibmcloud cos object-delete --bucket ${BUCKET} --key "README.md" --force
ibmcloud ce app logs --name cos-app > out
grep "README.md" out > /dev/null 2>&1 && break
sleep 10
done
echo "Log from 'cos-app' app:"
cat out
echo "=============="
echo "Event stats:"
curl -fs ${URL}/stats
# Clean up
clean