diff --git a/labapp/app/app.py b/labapp/app/app.py index 1455130..73066da 100644 --- a/labapp/app/app.py +++ b/labapp/app/app.py @@ -8,7 +8,7 @@ import requests import markdown from ce import get_ce_info, get_ce_state -from fetch import cloudapp_fetch, cloudapp_req_headers, cloudapp_res_headers +from fetch import get_runner_session, cloudapp_fetch, cloudapp_req_headers, cloudapp_res_headers app = Flask(__name__) app.config['ce_info'] = None @@ -21,6 +21,9 @@ cache = Cache(app) app.secret_key = "blahblahblah" +session = get_runner_session() +session.headers.update({"User-Agent": "MCN-Lab-Runner/1.0"}) + class LabException(Exception): """lab exception""" @@ -206,10 +209,8 @@ def score(): def ex_test(): """Example test""" try: - s = requests.Session() - s.headers.update({"User-Agent": "MCN-Lab-Runner/1.0"}) url = f"https://foo.{app.config['base_url']}/" - data = cloudapp_fetch(s, url, 7, 'info', {"foo": True}) + data = cloudapp_fetch(session, url, 7, 'info', {"foo": True}) return jsonify(status='success', data=data) except (LabException, requests.RequestException, ValueError) as e: return jsonify(status='fail', error=str(e)) @@ -218,10 +219,8 @@ def ex_test(): def ex_test2(): """Example test""" try: - s = requests.Session() - s.headers.update({"User-Agent": "MCN-Lab-Runner/1.0"}) url = f"https://bar.{app.config['base_url']}/" - data = cloudapp_fetch(s, url, 7, 'info', {"bar": True}) + data = cloudapp_fetch(session, url, 7, 'info', {"bar": True}) return jsonify(status='success', data=data) except (LabException, requests.RequestException, ValueError) as e: return jsonify(status='fail', error=str(e)) @@ -231,13 +230,11 @@ def ex_test2(): def lb_aws(): """Azure LB test""" try: - s = requests.Session() - s.headers.update({"User-Agent": "MCN-Lab-Runner/1.0"}) ns = eph_ns() if not ns: raise LabException("Ephemeral NS not set") url = f"https://{ns}.{app.config['base_url']}" - data = cloudapp_fetch(s, url, 7, 'env', 'AWS') + data = cloudapp_fetch(session, url, 7, 'env', 'AWS') return jsonify(status='success', data=data) except (LabException, requests.RequestException, ValueError) as e: return jsonify(status='fail', error=str(e)) @@ -246,13 +243,11 @@ def lb_aws(): def lb_azure(): """Azure LB test""" try: - s = requests.Session() - s.headers.update({"User-Agent": "MCN-Lab-Runner/1.0"}) ns = eph_ns() if not ns: raise LabException("Ephemeral NS not set") url = f"https://{ns}.{app.config['base_url']}" - data = cloudapp_fetch(s, url, 7, 'env', 'Azure') + data = cloudapp_fetch(session, url, 7, 'env', 'Azure') return jsonify(status='success', data=data) except (LabException, requests.RequestException, ValueError) as e: return jsonify(status='fail', error=str(e)) @@ -261,16 +256,14 @@ def lb_azure(): def route1(): """First Route Test""" try: - s = requests.Session() - s.headers.update({"User-Agent": "MCN-Lab-Runner/1.0"}) ns = eph_ns() if not ns: raise LabException("Ephemeral NS not set") base_url = app.config['base_url'] aws_url = f"https://{ns}.{base_url}/aws/raw" azure_url = f"https://{ns}.{base_url}/azure/raw" - aws_data = cloudapp_fetch(s, aws_url, 7, 'env', 'AWS') - azure_data = cloudapp_fetch(s, azure_url, 7, 'env', 'Azure') + aws_data = cloudapp_fetch(session, aws_url, 7, 'env', 'AWS') + azure_data = cloudapp_fetch(session, azure_url, 7, 'env', 'Azure') data = { "aws": aws_data, "azure": azure_data @@ -283,18 +276,16 @@ def route1(): def route2(): """First Route Test""" try: - s = requests.Session() - s.headers.update({"User-Agent": "MCN-Lab-Runner/1.0"}) ns = eph_ns() if not ns: raise LabException("Ephemeral NS not set") base_url = app.config['base_url'] aws_url = f"https://{ns}.{base_url}/" azure_url = f"https://{ns}.{base_url}/" - s.headers["X-MCN-lab"] = "aws" - aws_data = cloudapp_fetch(s, aws_url, 7, 'env', 'AWS') - s.headers["X-MCN-lab"] = "azure" - azure_data = cloudapp_fetch(s, azure_url, 7, 'env', 'Azure') + session.headers["X-MCN-lab"] = "aws" + aws_data = cloudapp_fetch(session, aws_url, 7, 'env', 'AWS') + session.headers["X-MCN-lab"] = "azure" + azure_data = cloudapp_fetch(session, azure_url, 7, 'env', 'Azure') data = { "aws": aws_data, "azure": azure_data @@ -307,14 +298,12 @@ def route2(): def manip1(): """First Manip Test""" try: - s = requests.Session() - s.headers.update({"User-Agent": "MCN-Lab-Runner/1.0"}) ns = eph_ns() if not ns: raise LabException("Ephemeral NS not set") base_url = app.config['base_url'] url = f"https://{ns}.{base_url}/aws/raw" - r_data = cloudapp_fetch(s, url, 5, 'info', {"method": "GET", "path": "/raw"}) + r_data = cloudapp_fetch(session, url, 5, 'info', {"method": "GET", "path": "/raw"}) return jsonify(status='success', data=r_data) except (LabException, requests.RequestException, ValueError) as e: return jsonify(status='fail', error=str(e)) @@ -323,15 +312,13 @@ def manip1(): def manip2(): """Second Manip Test""" try: - s = requests.Session() - s.headers.update({"User-Agent": "MCN-Lab-Runner/1.0"}) ns = eph_ns() if not ns: raise LabException("Ephemeral NS not set") base_url = app.config['base_url'] url = f"https://{ns}.{base_url}/" t_headers = { "x-mcn-namespace": ns, "x-mcn-src-site": app.config["ce_info"]["site_name"]} - r_data = cloudapp_req_headers(s, url, 7, t_headers) + r_data = cloudapp_req_headers(session, url, 7, t_headers) return jsonify(status='success', data=r_data) except (LabException, requests.RequestException, ValueError) as e: return jsonify(status='fail', error=str(e)) @@ -340,8 +327,6 @@ def manip2(): def manip3(): """Third Manip Test""" try: - s = requests.Session() - s.headers.update({"User-Agent": "MCN-Lab-Runner/1.0"}) ns = eph_ns() if not ns: raise LabException("Ephemeral NS not set") @@ -350,8 +335,8 @@ def manip3(): azure_url = f"https://{ns}.{base_url}/azure/" aws_headers = { "x-mcn-dest-site": "student-awsnet" } azure_headers = { "x-mcn-dest-site": "student-azurenet" } - aws_data = cloudapp_res_headers(s, aws_url, 7, aws_headers) - azure_data = cloudapp_res_headers(s, azure_url, 7, azure_headers) + aws_data = cloudapp_res_headers(session, aws_url, 7, aws_headers) + azure_data = cloudapp_res_headers(session, azure_url, 7, azure_headers) data = { "aws": aws_data, "azure": azure_data @@ -364,13 +349,11 @@ def manip3(): def port1(): """Friend test""" try: - s = requests.Session() - s.headers.update({"User-Agent": "MCN-Lab-Runner/1.0"}) ns = eph_ns() if not ns: raise LabException("Ephemeral NS not set") url = f"https://{ns}.{app.config['base_url']}/" - data = cloudapp_fetch(s, url, 7, 'info', {"method": "GET", "path": "/"}) + data = cloudapp_fetch(session, url, 7, 'info', {"method": "GET", "path": "/"}) return jsonify(status='success', data=data) except (LabException, requests.RequestException, ValueError) as e: return jsonify(status='fail', error=str(e)) @@ -379,13 +362,11 @@ def port1(): def port2(): """Friend test""" try: - s = requests.Session() - s.headers.update({"User-Agent": "MCN-Lab-Runner/1.0"}) data = request.get_json() print(data) eph_ns = data['userInput'] url = f"https://{eph_ns}.{app.config['base_url']}/" - data = cloudapp_fetch(s, url, 7, 'info', {"method": "GET", "path": "/"}) + data = cloudapp_fetch(session, url, 7, 'info', {"method": "GET", "path": "/"}) return jsonify(status='success', data=data) except (LabException, requests.RequestException, ValueError) as e: return jsonify(status='fail', error=str(e)) diff --git a/labapp/app/fetch.py b/labapp/app/fetch.py index 6244ba7..3c803ae 100644 --- a/labapp/app/fetch.py +++ b/labapp/app/fetch.py @@ -1,4 +1,26 @@ from requests.structures import CaseInsensitiveDict +import requests +from requests.adapters import HTTPAdapter +from urllib3.util.retry import Retry + +def get_runner_session( + retries=3, + backoff_factor=0.3, + status_forcelist=(500, 502, 504), + session=None +): + session = session or requests.Session() + retry = Retry( + total=retries, + read=retries, + connect=retries, + backoff_factor=backoff_factor, + status_forcelist=status_forcelist, + ) + adapter = HTTPAdapter(max_retries=retry) + session.mount('http://', adapter) + session.mount('https://', adapter) + return session def headers_cleaner(headers): """