Files
versions-for-swarm/app.py

155 lines
4.4 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
import os
import random
import socket
from datetime import datetime
from flask import Flask, jsonify
app = Flask(__name__)
# 从环境变量读取版本号
VERSION = os.getenv("APP_VERSION", "v1.0")
BUGGY = os.getenv("BUGGY", "false").lower() == "true"
# 版本主题配置
THEMES = {
"v1.0": {"color": "#4A90E2", "name": "蓝色经典版", "features": "基础功能"},
"v2.0": {
"color": "#50C878",
"name": "绿色升级版",
"features": "新增健康检查、API接口",
},
"v2.1": {"color": "#50C878", "name": "绿色稳定版", "features": "性能优化、Bug修复"},
"v3.0-buggy": {
"color": "#E74C3C",
"name": "红色测试版",
"features": "⚠️ 此版本存在已知问题",
},
}
@app.route("/")
def hello():
# 模拟 v3.0-buggy 的问题
if BUGGY and random.random() < 0.5:
return "💥 服务异常:数据库连接失败", 500
theme = THEMES.get(VERSION, THEMES["v1.0"])
return f"""
<!DOCTYPE html>
<html>
<head>
<title>Docker Swarm 滚动更新演示</title>
<meta charset="utf-8">
<meta http-equiv="refresh" content="3">
<style>
body {{
font-family: Arial;
max-width: 700px;
margin: 50px auto;
padding: 30px;
background: linear-gradient(135deg, {theme["color"]}22 0%, {theme["color"]}44 100%);
}}
.header {{
background: {theme["color"]};
color: white;
padding: 20px;
border-radius: 10px 10px 0 0;
text-align: center;
}}
.info {{
background: white;
padding: 20px;
border-radius: 0 0 10px 10px;
box-shadow: 0 4px 6px rgba(0,0,0,0.1);
}}
.highlight {{
color: {theme["color"]};
font-weight: bold;
font-size: 1.2em;
}}
.version-badge {{
display: inline-block;
background: {theme["color"]};
color: white;
padding: 5px 15px;
border-radius: 20px;
font-size: 0.9em;
margin-top: 10px;
}}
table {{
width: 100%;
border-collapse: collapse;
margin-top: 15px;
}}
td {{
padding: 10px;
border-bottom: 1px solid #eee;
}}
td:first-child {{
color: #666;
width: 40%;
}}
.auto-refresh {{
color: #999;
font-size: 0.85em;
text-align: center;
margin-top: 15px;
}}
</style>
</head>
<body>
<div class="header">
<h1>🚀 {theme["name"]}</h1>
<div class="version-badge">版本 {VERSION}</div>
</div>
<div class="info">
<table>
<tr>
<td>📦 容器主机名</td>
<td class="highlight">{socket.gethostname()}</td>
</tr>
<tr>
<td>🎓 学号服务</td>
<td class="highlight">s{os.getenv("STUDENT_ID", "00")}</td>
</tr>
<tr>
<td>⏰ 当前时间</td>
<td class="highlight">{datetime.now().strftime("%Y-%m-%d %H:%M:%S")}</td>
</tr>
<tr>
<td>✨ 版本特性</td>
<td class="highlight">{theme["features"]}</td>
</tr>
</table>
<div class="auto-refresh">🔄 页面每3秒自动刷新观察容器变化</div>
</div>
</body>
</html>
"""
@app.route("/api/version")
def version():
return jsonify(
{
"version": VERSION,
"hostname": socket.gethostname(),
"student_id": os.getenv("STUDENT_ID", "00"),
"timestamp": datetime.now().isoformat(),
"buggy": BUGGY,
}
)
@app.route("/health")
def health():
if BUGGY and random.random() < 0.7:
return {"status": "unhealthy"}, 503
return {"status": "healthy", "version": VERSION}, 200
if __name__ == "__main__":
app.run(host="0.0.0.0", port=80)