4
4
# REPOSITORY: name of the image registry/namespace to get the images
5
5
# COS_ID: If set, specifies the full CRN of a Cloud Object Storage instance to use
6
6
7
- function project_name() {
8
- ibmcloud ce project current | awk -F: ' /Project Name/{ print $2 }'
9
- }
10
-
11
- function project_id() {
12
- set +x
13
- ibmcloud ce project get --name $( project_name) | awk ' /^ID/{ print $2 }'
14
- set -x
15
- }
7
+ export REPOSITORY=${REPOSITORY:- ibmcom}
8
+ export PROJECT_NAME=$( ibmcloud ce project current | \
9
+ awk -F: ' /Project Name/{ print $2 }' )
10
+ export PROJECT_ID=$( ibmcloud ce project get --name ${PROJECT_NAME} | \
11
+ awk ' /^ID/{ print $2 }' )
16
12
17
13
# Clean up previous run
18
14
function clean() {
19
15
set +x
20
16
DCOS=$1
21
17
POLICY=$2
22
- echo " Cleaning...be patient "
18
+ echo " Cleaning..."
23
19
24
20
# Delete the event subscription
25
21
ibmcloud ce sub cos delete -n cos-sub -f --wait=true > /dev/null 2>&1 || true
26
22
27
23
# Delete the IAM auth policy
28
24
if [[ $POLICY != " " ]]; then
29
- ibmcloud iam authorization-policy-delete $POLICY --force > /dev/null 2>&1 || true
25
+ ibmcloud iam authorization-policy-delete $POLICY --force \
26
+ > /dev/null 2>&1 || true
30
27
fi
31
28
32
29
# Delete the app
33
30
ibmcloud ce app delete --name cos-app --force > /dev/null 2>&1 || true
34
31
35
32
# Delete the COS bucket
36
- ibmcloud cos bucket-delete --bucket $( project_id) --force > /dev/null 2>&1 || true
33
+ ibmcloud cos bucket-delete --bucket ${PROJECT_ID} --force \
34
+ > /dev/null 2>&1 || true
37
35
38
36
# Delete the COS instance
39
37
if [[ $DCOS -gt 0 ]]; then
40
- ibmcloud resource service-instance-delete codeengine-cos -f -q > /dev/null 2>&1 || true
38
+ ibmcloud resource service-instance-delete codeengine-cos -f -q \
39
+ > /dev/null 2>&1 || true
41
40
fi
42
41
43
42
rm -f out
@@ -47,45 +46,47 @@ clean 0
47
46
[[ " $1 " == " clean" ]] && exit 0
48
47
49
48
set -ex
50
- export REPOSITORY=${REPOSITORY:- ibmcom}
51
49
52
- # Create a COS instance unless one has been specified for use, mark it for deletion
50
+ # Create a COS instance unless one has been specified for use, mark it for
51
+ # deletion
53
52
DELETE_COS=0
54
53
if [[ $COS_ID == " " ]]; then
55
- ibmcloud resource service-instance-create codeengine-cos cloud-object-storage lite global
56
- COS_ID=$( ibmcloud resource service-instance codeengine-cos | awk ' /^ID/{ print $2 }' )
54
+ ibmcloud resource service-instance-create codeengine-cos \
55
+ cloud-object-storage lite global
56
+ COS_ID=$( ibmcloud resource service-instance codeengine-cos | \
57
+ awk ' /^ID/{ print $2 }' )
57
58
DELETE_COS=1
58
59
fi
59
60
60
61
# Set the COS config to use this instance
61
62
ibmcloud cos config crn --crn $COS_ID --force
62
63
ibmcloud cos config auth --method IAM
63
64
64
- # Create the IAM authorization policy to allow us to receive notifications from COS
65
+ # Create the IAM authorization policy to allow us to receive notifications
66
+ # from COS
65
67
POLICY_ID=$( ibmcloud iam authorization-policy-create codeengine \
66
68
cloud-object-storage " Notifications Manager" \
67
- --source-service-instance-name $( project_name ) \
69
+ --source-service-instance-name ${PROJECT_NAME} \
68
70
--target-service-instance-id ${COS_ID} | awk ' /^Authorization/{ print $3 }' )
69
71
70
72
# Create our bucket
71
- BUCKET=$( project_id )
73
+ export BUCKET=${PROJECT_ID}
72
74
ibmcloud cos bucket-create --bucket $BUCKET --ibm-service-instance-id $COS_ID
73
75
74
- # Create the app
75
- ibmcloud ce app create -n cos-app --image ${REPOSITORY} /cos-listen --min-scale=1 --max-scale=1
76
+ # Create the app && save its URL for later
77
+ ibmcloud ce app create -n cos-app --image ${REPOSITORY} /cos-listen \
78
+ --min-scale=1 --max-scale=1
79
+ URL=$( ibmcloud ce app get --output jsonpath=' {.status.url}' --name cos-app)
76
80
77
81
# Setup the COS Event Source
78
82
ibmcloud ce sub cos create -n cos-sub -d cos-app -b ${BUCKET}
79
83
80
-
81
84
# Extract the instance name from `ibmcloud ce app get`
82
- INSTANCE=$( ibmcloud ce app get --name cos-app | awk ' /cos.*Running/{ print $1 }' )
85
+ INSTANCE=$( ibmcloud ce app get --name cos-app| awk ' /cos.*Running/{ print $1 }' )
83
86
echo Instance name: $INSTANCE
84
87
85
-
86
88
# Now wait until we get the event - shouldn't take more than a minute
87
89
while true ; do
88
-
89
90
# Upload a file to COS (this will generate an event)
90
91
ibmcloud cos object-put --bucket ${BUCKET} --key " README.md" --body README.md
91
92
@@ -103,8 +104,6 @@ cat out
103
104
echo " =============="
104
105
echo " Event stats:"
105
106
106
- URL=$( ibmcloud ce app get --output jsonpath=' {.status.url}' --name cos-app)
107
-
108
107
curl -Ls ${URL} /stats
109
108
110
109
# Clean up
0 commit comments