-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathgce.sh
173 lines (148 loc) · 5.06 KB
/
gce.sh
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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
#! /bin/bash
apt update -y
apt install -y wget tcpdump fping bind9-dnsutils apache2-utils python3-pip python3-dev
apt install -y python3-flask python3-requests
# web server
#---------------------------------------------------
mkdir -p /var/flaskapp/flaskapp/{static,templates}
cat <<EOF > /var/flaskapp/flaskapp/__init__.py
import socket
from flask import Flask, request
app = Flask(__name__)
@app.route("/")
def default():
hostname = socket.gethostname()
address = socket.gethostbyname(hostname)
data_dict = {}
data_dict['name'] = hostname
data_dict['address'] = address
data_dict['remote'] = request.remote_addr
data_dict['headers'] = dict(request.headers)
return data_dict
@app.route('/${WEB_SERVER.health_check_path}')
def ${WEB_SERVER.health_check_path}():
return '${WEB_SERVER.health_check_response}'
if __name__ == "__main__":
app.run(host= '0.0.0.0', port=${WEB_SERVER.port}, debug = True)
EOF
cat <<EOF > /etc/systemd/system/flaskapp.service
[Unit]
Description=Script for flaskapp service
[Service]
Type=simple
ExecStart=/usr/bin/python3 /var/flaskapp/flaskapp/__init__.py
ExecStop=/usr/bin/pkill -f /var/flaskapp/flaskapp/__init__.py
StandardOutput=journal
[Install]
WantedBy=multi-user.target
EOF
systemctl daemon-reload
systemctl enable flaskapp.service
systemctl restart flaskapp.service
# playz (curl scripts)
#---------------------------------------------------
cat <<'EOF' > /usr/local/bin/playz
echo -e "\n apps ...\n"
%{ for target in SCRIPTS.targets_curl_dns ~}
echo "$(curl -kL --max-time 2.0 -H 'Cache-Control: no-cache' -w "%%{http_code} (%%{time_total}s) - %%{remote_ip}" -s -o /dev/null ${target}) - ${target}"
%{ endfor ~}
echo -e "\n psc4 ...\n"
%{ for target in SCRIPTS.targets_psc ~}
echo "$(curl -kL --max-time 2.0 -H 'Cache-Control: no-cache' -w "%%{http_code} (%%{time_total}s) - %%{remote_ip}" -s -o /dev/null ${target}) - ${target}"
%{ endfor ~}
echo -e "\n apis ...\n"
%{ for target in SCRIPTS.targets_pga ~}
echo "$(curl -kL --max-time 2.0 -H 'Cache-Control: no-cache' -w "%%{http_code} (%%{time_total}s) - %%{remote_ip}" -s -o /dev/null ${target}) - ${target}"
%{ endfor ~}
echo ""
EOF
chmod a+x /usr/local/bin/playz
# pingz
#-----------------------------------
cat <<'EOF' > /usr/local/bin/pingz
echo -e "\n ping ...\n"
%{ for target in SCRIPTS.targets_ping_dns ~}
echo ${target} - $(ping -qc2 -W1 ${target} 2>&1 | awk -F'/' 'END{ print (/^rtt/? "OK "$5" ms":"NA") }')
%{ endfor ~}
EOF
chmod a+x /usr/local/bin/pingz
# bucketz (gcs bucket test script)
#---------------------------------------------------
cat <<'EOF' > /usr/local/bin/bucketz
echo ""
%{ for env,bucket in SCRIPTS.targets_bucket ~}
echo -e "${env} : $(gsutil cat gs://${bucket})\n"
%{ endfor ~}
EOF
chmod a+x /usr/local/bin/bucketz
# aiz (connect to aiplatform)
#---------------------------------------------------
cat <<EOF > /usr/local/bin/aiz
echo ""
%{ for item in SCRIPTS.targets_ai_project ~}
echo -e " ${item.project}...\n"
gcloud ai models list --project=${item.project} --region=${item.region}
%{ endfor ~}
EOF
chmod a+x /usr/local/bin/aiz
# api discovery script
#---------------------------------------------------
cat <<EOF > /usr/local/bin/discoverz.py
#!/usr/bin/env python3
import os
import json
import requests
import urllib.request
from socket import timeout
response = urllib.request.urlopen("https://www.googleapis.com/discovery/v1/apis")
content = response.read()
data = json.loads(content.decode("utf8"))
googleapis = data['items']
reachable = []
unreachable = []
print("\n scanning all api endpoints ...\n")
for api in googleapis:
name = api['name']
version = api['version']
title = api['title']
url = 'https://' + name + '.googleapis.com/generate_204'
try:
r = requests.get(url, timeout=1)
if r.status_code == 204:
reachable.append([r.status_code, name, url])
print("{} - {:<26s} {:<10s} {}".format(r.status_code, name, version, url))
else:
unreachable.append([r.status_code, name, url])
print("{} - {:<26s} {:<10s} {}".format(r.status_code, name, version, url))
except Exception as e:
print("{} - {:<26s} {:<10s} {}".format(r.status_code, name, version, url))
unreachable.append(['err', name, url])
print("\n reachable api endpoints ...\n")
for code, name, url in sorted(reachable):
print("{} - {:<26s} {:<10s} {}".format(code, name, version, url))
print("\n unreachable api endpoints ...\n")
for code, name, url in sorted(unreachable):
print("{} - {:<26s} {:<10s} {}".format(code, name, version, url))
EOF
%{ if ENABLE_PROBES ~}
# probe
#---------------------------------------------------
cat <<'EOF' > /usr/local/bin/probez
#! /bin/bash
i=0
while [ $i -lt 3 ]; do
%{ for target in SCRIPTS.targets_probe ~}
ab -n $1 -c $2 ${target} > /dev/null 2>&1
%{ endfor ~}
let i=i+1
sleep 3
done
EOF
chmod a+x /usr/local/bin/probez
cat <<EOF > /tmp/crontab.txt
*/1 * * * * /usr/local/bin/probez 15 1 2>&1 > /dev/null
*/2 * * * * /usr/local/bin/probez 6 2 2>&1 > /dev/null
*/5 * * * * /usr/local/bin/probez 5 1 2>&1 > /dev/null
EOF
crontab /tmp/crontab.txt
%{ endif ~}