-
Notifications
You must be signed in to change notification settings - Fork 7
/
simulate
executable file
·122 lines (92 loc) · 3.28 KB
/
simulate
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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
clear
CYAN='\033[0;36m'
BCYAN='\033[1;36m'
NC='\033[0m' # No Color
DIVIDER=$(printf %"$(tput cols)"s | tr " " "*")
DIVIDER+="\n"
# DISPLAY HELPERS
function section_open() {
section_description=$1
printf "$DIVIDER"
printf "${CYAN}$section_description${NC} \n"
printf "$DIVIDER"
}
function section_close() {
printf "$DIVIDER"
printf "${CYAN}$section_description ${BCYAN}- done${NC}\n"
printf "\n\n"
}
function print_title() {
printf "$DIVIDER"
printf "${BCYAN}$1${NC} \n"
printf "$2\n"
if [ $3 = 1 ]; then
printf "It's going to take around ${CYAN}1 min${NC} \n"
else
printf "It's going to take around ${CYAN}$3 mins${NC} \n"
fi
printf "$DIVIDER"
}
function projectDetails() {
local content
local raw=$(echo "${1//:/:${BCYAN}}")
IFS=$'\n' read -rd '' -a LINES <<<"$raw"
for line in "${LINES[@]}"; do
content=${content}"${NC}\n"${line}
done
linewidth=$(tput cols)
number=$(($linewidth - 15))
local SPACER=$(printf %"${number}"s | tr " " " ")
content=${content}${NC}
printf "\n"
printf "${ONCYAN}Project Details${SPACER}${NC}"
echo $content | column -s ',' -t
printf "\n"
}
input="terraform.tfvars"
while IFS= read -r line
do
lineArr=(${line//=/ })
key=$(echo "$lineArr[0]" | awk '{print toupper($0)}')
value=${lineArr[1]//\"/}
eval $key=$value
done < "$input"
DESC="This process will send a message to Pub/Sub simulating that a budget amount
has been exceeded. The following will happen:
* A message will be sent through Pub/Sub that indicates a budget has been exceeded
* The message uses example data that does not correspond to the actual budget
* Cloud Functions will respond to thie event by shutting down labeled resources
* This label was chosen when running the install script
* Compute Engine instances will be 'stopped'
* Cloud Run services will have 'allUsers' removed from access, rendering them private
Only resources that are configured with the label chosen when installing will be affected.
"
print_title "COSTSENTRY SIMULATE OVERAGE " "$DESC" "1"
projectDetails "
Project ID:,$PROJECT_ID
Region:,$REGION
Label:,$LABEL
Basename:,$BASENAME
"
section_open "Listing VMs to see status"
gcloud compute instances list --filter="labels.$LABEL=true"
section_close
section_open "Listing Services to see status"
gcloud run services list --filter="metadata.labels.$LABEL=true"
printf "Below should have 'allUsers' in role 'run.invoker'\n"
gcloud run services get-iam-policy costsentry-run-service --region $REGION --format="value(bindings)"
section_close
section_open "Sending test billing overage message"
MESSAGE=$(cat pubsub.test.json)
gcloud pubsub topics publish $BASENAME-billing-channel --message="$MESSAGE"
printf "Message sent to Pub/Sub: \n $MESSAGE \n"
sleep 10
section_close
section_open "Listing Services to see status"
gcloud run services list --filter="metadata.labels.$LABEL=true"
printf "Below should no longer have 'allUsers' have 'run.invoker'\n"
gcloud run services get-iam-policy costsentry-run-service --region $REGION --format="value(bindings)"
section_close
section_open "Listing VMs to see status"
gcloud compute instances list --filter="labels.$LABEL=true"
section_close