Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

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 10 commits into from
May 1, 2019
Merged
120 changes: 120 additions & 0 deletions 120 compute/managed-instances/demo/app.py
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)
andrewsg marked this conversation as resolved.
Show resolved Hide resolved
else:
return ''


if __name__ == "__main__":
app.run(debug=False, port=PORT_NUMBER)
1 change: 1 addition & 0 deletions 1 compute/managed-instances/demo/requirements.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
flask==1.0
7 changes: 7 additions & 0 deletions 7 compute/managed-instances/demo/templates/health.html
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>
47 changes: 47 additions & 0 deletions 47 compute/managed-instances/demo/templates/index.html
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>
Morty Proxy This is a proxified and sanitized view of the page, visit original site.