-
Notifications
You must be signed in to change notification settings - Fork 6.6k
Demo webserver for managed instances tutorial #2077
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
fc7f24e
Add compute/managed-instances/demo/webserver.py as tutorial sample code
djmailhot 9a8116d
Update managed-instances demo webserver to run with Python3
djmailhot b58da3f
Lint fixes for managed-instances demo webserver
djmailhot fbc0262
Python3 fixes for managed-instances demo webserver
djmailhot 5e2b7c1
Update managed-instances demo webserver to use Flask
djmailhot 35fec9c
Add copywrite header
djmailhot 5ed945d
managed instances demo Flask app refactor long lines
djmailhot f94d30b
managed instances demo app actually uses PORT_NUMBER global
djmailhot 2d64909
managed instances demo app fix gethostname reference
djmailhot 0c3b19f
Add comments to managed instances demo app
djmailhot File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,120 @@ | ||
# Copyright 2019, Google, Inc. | ||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
""" | ||
Flask app to be used as an interactive demonstration of autohealing. | ||
|
||
Exposes a simple UI showing basic server stats and a toggle button to | ||
simulate a healthy/unhealthy server status for any attached health check. | ||
|
||
Attached health checks should query the '/health' path. | ||
""" | ||
|
||
from flask import Flask, make_response, render_template | ||
from re import sub | ||
from requests import get | ||
from socket import gethostname | ||
|
||
PORT_NUMBER = 80 | ||
|
||
app = Flask(__name__) | ||
healthy = True | ||
|
||
|
||
@app.route('/') | ||
def index(): | ||
"""Returns the demo UI.""" | ||
global healthy | ||
return render_template('index.html', | ||
hostname=gethostname(), | ||
zone=_get_zone(), | ||
template=_get_template(), | ||
healthy=healthy) | ||
|
||
|
||
@app.route('/health') | ||
def health(): | ||
"""Returns the simulated 'healthy'/'unhealthy' status of the server. | ||
|
||
Returns: | ||
HTTP status 200 if 'healthy', HTTP status 500 if 'unhealthy' | ||
""" | ||
global healthy | ||
template = render_template('health.html', healthy=healthy) | ||
return make_response(template, 200 if healthy else 500) | ||
|
||
|
||
@app.route('/makeHealthy') | ||
def make_healthy(): | ||
"""Sets the server to simulate a 'healthy' status.""" | ||
global healthy | ||
healthy = True | ||
template = render_template('index.html', | ||
hostname=gethostname(), | ||
zone=_get_zone(), | ||
template=_get_template(), | ||
healthy=True) | ||
response = make_response(template, 302) | ||
response.headers['Location'] = '/' | ||
return response | ||
|
||
|
||
@app.route('/makeUnhealthy') | ||
def make_unhealthy(): | ||
"""Sets the server to simulate an 'unhealthy' status.""" | ||
global healthy | ||
healthy = False | ||
template = render_template('index.html', | ||
hostname=gethostname(), | ||
zone=_get_zone(), | ||
template=_get_template(), | ||
healthy=False) | ||
response = make_response(template, 302) | ||
response.headers['Location'] = '/' | ||
return response | ||
|
||
|
||
def _get_zone(): | ||
"""Gets the GCE zone of this instance. | ||
|
||
Returns: | ||
str: The name of the zone if the zone was successfully determined. | ||
Empty string otherwise. | ||
""" | ||
r = get('http://metadata.google.internal/' | ||
'computeMetadata/v1/instance/zone', | ||
headers={'Metadata-Flavor': 'Google'}) | ||
if r.status_code == 200: | ||
return sub(r'.+zones/(.+)', r'\1', r.text) | ||
else: | ||
return '' | ||
|
||
|
||
def _get_template(): | ||
"""Gets the GCE instance template of this instance. | ||
|
||
Returns: | ||
str: The name of the template if the template was successfully | ||
determined and this instance was built using an instance template. | ||
Empty string otherwise. | ||
""" | ||
r = get('http://metadata.google.internal/' | ||
'computeMetadata/v1/instance/attributes/instance-template', | ||
headers={'Metadata-Flavor': 'Google'}) | ||
if r.status_code == 200: | ||
return sub(r'.+instanceTemplates/(.+)', r'\1', r.text) | ||
else: | ||
return '' | ||
|
||
|
||
if __name__ == "__main__": | ||
app.run(debug=False, port=PORT_NUMBER) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
flask==1.0 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
<div style="font-family: verdana; font-weight: bold; font-size: 40px"> | ||
{% if healthy %} | ||
<span>HTTP/1.0 200 OK</span> | ||
{% else %} | ||
<span>HTTP/1.0 500 Internal Server Error</span> | ||
{% endif %} | ||
</div> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
<html> | ||
<head> | ||
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/materialize/1.0.0-rc.2/css/materialize.min.css"> | ||
<script src="https://cdnjs.cloudflare.com/ajax/libs/materialize/1.0.0-rc.2/js/materialize.min.js"></script> | ||
</head> | ||
<body> | ||
<table class="striped"> | ||
<colgroup> | ||
<col width="200"> | ||
</colgroup> | ||
<tbody> | ||
<tr> | ||
<td>Hostname:</td> | ||
<td><b>{{ hostname }}</b></td> | ||
</tr> | ||
<tr> | ||
<td>Zone:</td> | ||
<td><b>{{ zone }}</b></td> | ||
</tr> | ||
<tr> | ||
<td>Template:</td> | ||
<td><b>{{ template }}</b></td> | ||
</tr> | ||
<tr> | ||
<td>Health status:</td> | ||
{% if healthy %} | ||
<td><span class="btn green">healthy</span></td> | ||
{% else %} | ||
<td><span class="btn red">unhealthy</span></td> | ||
{% endif %} | ||
</tr> | ||
<tr> | ||
<td>Actions:</td> | ||
<td> | ||
{% if healthy %} | ||
<a class="btn blue" href="/makeUnhealthy">Make unhealthy</a> | ||
{% else %} | ||
<a class="btn blue" href="/makeHealthy">Make healthy</a> | ||
{% endif %} | ||
|
||
<a class="btn blue" href="/health">Check health</a> | ||
</td> | ||
</tr> | ||
</tbody> | ||
</table> | ||
</body> | ||
</html> |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.