1
1
"""
2
2
Flask app for lab/guide
3
3
"""
4
+ import os
5
+ import re
4
6
from flask import Flask , render_template , jsonify , request , redirect , make_response , flash , url_for
5
7
from flask_caching import Cache
6
8
import requests
7
9
import markdown
8
- import validators
9
- import os
10
10
from ce import get_ce_info , get_ce_state
11
11
12
12
app = Flask (__name__ )
15
15
if app .config ['udf' ]:
16
16
info = get_ce_info ()
17
17
app .config ['ce_info' ] = info
18
- app .config ['base_url' ] = "lab- mcn.f5demos.com"
18
+ app .config ['base_url' ] = "mcn-lab .f5demos.com"
19
19
app .config ['CACHE_TYPE' ] = 'SimpleCache'
20
20
cache = Cache (app )
21
21
app .secret_key = "blahblahblah"
22
22
23
+ def render_md (file : str ) -> str :
24
+ """render markdown w/ common extentions"""
25
+ with open (file , "r" ) as file :
26
+ content = file .read ()
27
+ html = markdown .markdown (
28
+ content ,
29
+ extensions = ['markdown.extensions.attr_list' ,'markdown.extensions.codehilite' ,'markdown.extensions.fenced_code' ]
30
+ )
31
+ return html
32
+
33
+ def validate_eph_ns (input_name ):
34
+ """validate ephemeral namespace name"""
35
+ pattern = r'^[a-zA-Z]+-[a-zA-Z]+$'
36
+ return bool (re .match (pattern , input_name ))
37
+
38
+ def eph_ns () -> str :
39
+ """check if ephemeral namespace is set"""
40
+ eph_ns = request .cookies .get ('eph_ns' , None )
41
+ return eph_ns
42
+
23
43
@app .errorhandler (404 )
24
44
@app .errorhandler (500 )
25
45
def return_err (err ):
@@ -32,92 +52,89 @@ def return_err(err):
32
52
33
53
@app .route ('/' )
34
54
def index ():
35
- with open ("markdown/overview.md" , "r" ) as file :
36
- content = file .read ()
37
- html = markdown .markdown (content )
55
+ """index page"""
56
+ html = render_md ("markdown/overview.md" )
38
57
return render_template ('overview.html' , content = html )
39
58
40
59
@app .route ('/setup' , methods = ['GET' , 'POST' ])
41
60
def setup ():
61
+ """setup page"""
42
62
if request .method == 'POST' :
43
63
action = request .form ['action' ]
44
64
if action == 'save' :
45
- base_url = request .form ['base_url' ].strip ()
46
- if not validators .domain (base_url ):
47
- flash ("Invalid domain format." , "info" )
48
- return redirect (url_for ('setup' ))
49
- if not base_url .endswith (app .config ['base_url' ]):
50
- flash (f"Domain must end with { app .config ['base_url' ]} ." , "info" )
65
+ eph_ns = request .form ['eph_ns' ].strip ()
66
+ print (eph_ns )
67
+ if not validate_eph_ns (eph_ns ):
68
+ flash ("Invalid ephemeral NS." , "danger" )
51
69
return redirect (url_for ('setup' ))
52
70
response = make_response (redirect ('/setup' ))
53
- response .set_cookie ('base_url ' , base_url , max_age = 60 * 60 * 24 )
54
- flash ("Domain successfully set." , "success" )
71
+ response .set_cookie ('eph_ns ' , eph_ns , max_age = 60 * 60 * 24 )
72
+ flash ("Ephemeral NS successfully set." , "success" )
55
73
return response
56
74
elif action == 'clear' :
57
75
response = make_response (redirect ('/setup' ))
58
- response .set_cookie ('base_url ' , '' , expires = 0 )
59
- flash ("Domain setting cleared." , "info" )
76
+ response .set_cookie ('eph_ns ' , '' , expires = 0 )
77
+ flash ("Ephemeral NS cleared." , "info" )
60
78
return response
61
- return render_template ('setup.html' , base_url = app .config ['base_url' ])
79
+ html = render_md ("markdown/setup.md" )
80
+ return render_template ('setup.html' , content = html )
81
+
82
+ @app .route ('/arch' )
83
+ def arch ():
84
+ """arch page"""
85
+ html = render_md ("markdown/arch.md" )
86
+ return render_template ('standard.html' , content = html , title = "MCN Practical: Architecture" )
62
87
63
88
@app .route ('/_ce_state' )
64
89
@cache .cached (timeout = 30 )
65
90
def ce_state ():
91
+ """get ce state (internal route)"""
66
92
data = get_ce_state (app .config ['ce_info' ])
67
93
return data
68
94
69
- @app .route ('/test' )
70
- def test ():
71
- base_url = request .cookies .get ('base_url' )
72
- url = f"https://echo.{ base_url } "
73
- try :
74
- response = requests .get (url )
75
- response .raise_for_status ()
76
- return jsonify (status = 'success' , data = response .json ())
77
- except requests .RequestException as e :
78
- return jsonify (status = 'fail' , error = str (e ))
79
-
80
95
@app .route ('/lb' )
81
96
def lb ():
82
- with open ("markdown/lb.md" , "r" ) as file :
83
- content = file .read ()
84
- html = markdown .markdown (
85
- content ,
86
- extensions = ['markdown.extensions.codehilite' ,'markdown.extensions.fenced_code' ]
87
- )
88
- return render_template ('lb.html' , content = html )
97
+ """lb page"""
98
+ ns = eph_ns ()
99
+ html = render_md ("markdown/lb.md" )
100
+ return render_template ('exercise_standard.html' , title = "MCN Practical: LB" , content = html , ns = ns )
89
101
90
102
@app .route ('/path' )
91
103
def path ():
92
- with open ("markdown/path.md" , "r" ) as file :
93
- content = file .read ()
94
- html = markdown .markdown (
95
- content ,
96
- extensions = ['markdown.extensions.codehilite' ,'markdown.extensions.fenced_code' ]
97
- )
98
- return render_template ('path.html' , content = html )
104
+ """path page"""
105
+ ns = eph_ns ()
106
+ html = render_md ("markdown/path.md" )
107
+ return render_template ('exercise_standard.html' , title = "MCN Practical: Path Routing" , content = html , ns = ns )
99
108
100
109
@app .route ('/header' )
101
110
def header ():
102
- with open ("markdown/header.md" , "r" ) as file :
103
- content = file .read ()
104
- html = markdown .markdown (
105
- content ,
106
- extensions = ['markdown.extensions.codehilite' ,'markdown.extensions.fenced_code' ]
107
- )
108
- return render_template ('header.html' , context = html )
111
+ """header page"""
112
+ ns = eph_ns ()
113
+ html = render_md ("markdown/header.md" )
114
+ return render_template ('exercise_standard.html' , title = "MCN Practical: Headers" , content = html , ns = ns )
109
115
110
- @app .route ('/appCon-aws' )
111
- def make_request_ac1_aws ():
116
+ @app .route ('/_lb_aws' )
117
+ def lb_aws ():
118
+ """AWS LB test"""
112
119
try :
113
- response = requests .get ('https://ifconfig.io/all.json' )
114
- response .raise_for_status ()
120
+ ns = eph_ns ()
121
+ if not ns :
122
+ raise Exception ("Ephemeral NS not set." )
123
+ url = f"https://{ ns } .{ app .config ['base_url' ]} /raw"
124
+ print (url )
125
+ response = requests .get (url , timeout = 5 )
126
+ print (response .text )
127
+ print (response .json ())
128
+ response .raise_for_status ()
129
+ if response .json ()['request_env' ] != "AWS" :
130
+ raise Exception ("Invalid request env." )
115
131
return jsonify (status = 'success' , data = response .json ())
116
- except requests . RequestException as e :
132
+ except Exception as e :
117
133
return jsonify (status = 'fail' , error = str (e ))
118
134
119
- @app .route ('/appCon-azure' )
120
- def make_request_ac1_azure ():
135
+ @app .route ('/_lb_azure' )
136
+ def lb_azure ():
137
+ """Azure LB test"""
121
138
try :
122
139
response = requests .get ('https://ifconfig1.io/all.json' )
123
140
response .raise_for_status ()
0 commit comments