Skip to content

Commit 3f2b797

Browse files
fixit: Clean up Python sample at compute/managed-instances/demo (#10112)
* fixit: Clean up Python sample at compute/managed-instances/demo * License fix * Fixing license year --------- Co-authored-by: Andrew Ferlitsch <aferlitsch@gmail.com>
1 parent 6110b59 commit 3f2b797

File tree

1 file changed

+78
-62
lines changed
  • compute/managed-instances/demo

1 file changed

+78
-62
lines changed

compute/managed-instances/demo/app.py

Lines changed: 78 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,17 @@
1-
# Copyright 2019 Google LLC
1+
# Copyright 2019 Google LLC
22
#
3-
# Licensed under the Apache License, Version 2.0 (the 'License');
4-
# you may not use this file except in compliance with the License.
5-
# You may obtain a copy of the License at
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
66
#
7-
# http://www.apache.org/licenses/LICENSE-2.0
7+
# http://www.apache.org/licenses/LICENSE-2.0
88
#
9-
# Unless required by applicable law or agreed to in writing, software
10-
# distributed under the License is distributed on an 'AS IS' BASIS,
11-
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12-
# See the License for the specific language governing permissions and
13-
# limitations under the License.
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
1415
"""
1516
Flask app to be used as an interactive demonstration of autohealing.
1617
@@ -40,99 +41,110 @@
4041

4142
@app.before_first_request
4243
def init():
44+
"""Initialize the application."""
4345
global _cpu_burner
4446
_cpu_burner = CpuBurner()
4547

4648

47-
@app.route('/')
49+
@app.route("/")
4850
def index():
4951
"""Returns the demo UI."""
5052
global _cpu_burner, _is_healthy
51-
return render_template('index.html',
52-
hostname=gethostname(),
53-
zone=_get_zone(),
54-
template=_get_template(),
55-
healthy=_is_healthy,
56-
working=_cpu_burner.is_running())
53+
return render_template(
54+
"index.html",
55+
hostname=gethostname(),
56+
zone=_get_zone(),
57+
template=_get_template(),
58+
healthy=_is_healthy,
59+
working=_cpu_burner.is_running(),
60+
)
5761

5862

59-
@app.route('/health')
63+
@app.route("/health")
6064
def health():
6165
"""Returns the simulated 'healthy'/'unhealthy' status of the server.
6266
6367
Returns:
6468
HTTP status 200 if 'healthy', HTTP status 500 if 'unhealthy'
6569
"""
6670
global _is_healthy
67-
template = render_template('health.html', healthy=_is_healthy)
71+
template = render_template("health.html", healthy=_is_healthy)
6872
return make_response(template, 200 if _is_healthy else 500)
6973

7074

71-
@app.route('/makeHealthy')
75+
@app.route("/makeHealthy")
7276
def make_healthy():
7377
"""Sets the server to simulate a 'healthy' status."""
7478
global _cpu_burner, _is_healthy
7579
_is_healthy = True
7680

77-
template = render_template('index.html',
78-
hostname=gethostname(),
79-
zone=_get_zone(),
80-
template=_get_template(),
81-
healthy=True,
82-
working=_cpu_burner.is_running())
81+
template = render_template(
82+
"index.html",
83+
hostname=gethostname(),
84+
zone=_get_zone(),
85+
template=_get_template(),
86+
healthy=True,
87+
working=_cpu_burner.is_running(),
88+
)
8389
response = make_response(template, 302)
84-
response.headers['Location'] = '/'
90+
response.headers["Location"] = "/"
8591
return response
8692

8793

88-
@app.route('/makeUnhealthy')
94+
@app.route("/makeUnhealthy")
8995
def make_unhealthy():
9096
"""Sets the server to simulate an 'unhealthy' status."""
9197
global _cpu_burner, _is_healthy
9298
_is_healthy = False
9399

94-
template = render_template('index.html',
95-
hostname=gethostname(),
96-
zone=_get_zone(),
97-
template=_get_template(),
98-
healthy=False,
99-
working=_cpu_burner.is_running())
100+
template = render_template(
101+
"index.html",
102+
hostname=gethostname(),
103+
zone=_get_zone(),
104+
template=_get_template(),
105+
healthy=False,
106+
working=_cpu_burner.is_running(),
107+
)
100108
response = make_response(template, 302)
101-
response.headers['Location'] = '/'
109+
response.headers["Location"] = "/"
102110
return response
103111

104112

105-
@app.route('/startLoad')
113+
@app.route("/startLoad")
106114
def start_load():
107115
"""Sets the server to simulate high CPU load."""
108116
global _cpu_burner, _is_healthy
109117
_cpu_burner.start()
110118

111-
template = render_template('index.html',
112-
hostname=gethostname(),
113-
zone=_get_zone(),
114-
template=_get_template(),
115-
healthy=_is_healthy,
116-
working=True)
119+
template = render_template(
120+
"index.html",
121+
hostname=gethostname(),
122+
zone=_get_zone(),
123+
template=_get_template(),
124+
healthy=_is_healthy,
125+
working=True,
126+
)
117127
response = make_response(template, 302)
118-
response.headers['Location'] = '/'
128+
response.headers["Location"] = "/"
119129
return response
120130

121131

122-
@app.route('/stopLoad')
132+
@app.route("/stopLoad")
123133
def stop_load():
124134
"""Sets the server to stop simulating CPU load."""
125135
global _cpu_burner, _is_healthy
126136
_cpu_burner.stop()
127137

128-
template = render_template('index.html',
129-
hostname=gethostname(),
130-
zone=_get_zone(),
131-
template=_get_template(),
132-
healthy=_is_healthy,
133-
working=False)
138+
template = render_template(
139+
"index.html",
140+
hostname=gethostname(),
141+
zone=_get_zone(),
142+
template=_get_template(),
143+
healthy=_is_healthy,
144+
working=False,
145+
)
134146
response = make_response(template, 302)
135-
response.headers['Location'] = '/'
147+
response.headers["Location"] = "/"
136148
return response
137149

138150

@@ -143,13 +155,14 @@ def _get_zone():
143155
str: The name of the zone if the zone was successfully determined.
144156
Empty string otherwise.
145157
"""
146-
r = get('http://metadata.google.internal/'
147-
'computeMetadata/v1/instance/zone',
148-
headers={'Metadata-Flavor': 'Google'})
158+
r = get(
159+
"http://metadata.google.internal/" "computeMetadata/v1/instance/zone",
160+
headers={"Metadata-Flavor": "Google"},
161+
)
149162
if r.status_code == 200:
150-
return sub(r'.+zones/(.+)', r'\1', r.text)
163+
return sub(r".+zones/(.+)", r"\1", r.text)
151164
else:
152-
return ''
165+
return ""
153166

154167

155168
def _get_template():
@@ -160,20 +173,23 @@ def _get_template():
160173
determined and this instance was built using an instance template.
161174
Empty string otherwise.
162175
"""
163-
r = get('http://metadata.google.internal/'
164-
'computeMetadata/v1/instance/attributes/instance-template',
165-
headers={'Metadata-Flavor': 'Google'})
176+
r = get(
177+
"http://metadata.google.internal/"
178+
"computeMetadata/v1/instance/attributes/instance-template",
179+
headers={"Metadata-Flavor": "Google"},
180+
)
166181
if r.status_code == 200:
167-
return sub(r'.+instanceTemplates/(.+)', r'\1', r.text)
182+
return sub(r".+instanceTemplates/(.+)", r"\1", r.text)
168183
else:
169-
return ''
184+
return ""
170185

171186

172187
class CpuBurner:
173188
"""
174189
Object to asynchronously burn CPU cycles to simulate high CPU load.
175190
Burns CPU in a separate process and can be toggled on and off.
176191
"""
192+
177193
def __init__(self):
178194
self._toggle = Value(c_bool, False, lock=True)
179195
self._process = Process(target=self._burn_cpu)
@@ -194,7 +210,7 @@ def is_running(self):
194210
def _burn_cpu(self):
195211
"""Burn CPU cycles if work is toggled, otherwise sleep."""
196212
while True:
197-
random()*random() if self._toggle.value else sleep(1)
213+
random() * random() if self._toggle.value else sleep(1)
198214

199215

200216
if __name__ == "__main__":

0 commit comments

Comments
 (0)