🚧 This instance is under construction; expect occasional downtime. Runners available in /repos. Questions? Ask in #wikimedia-gitlab on libera.chat, or under GitLab on Phabricator.

Commit 179e9c72 authored by Wolfgang Kandek's avatar Wolfgang Kandek
Browse files

Minikube files added, new testmode on server

Change-Id: Id0e3ef35e7711f90f3e1debc2399af6aa69ffb5a
parent 49c3f55a
......@@ -22,17 +22,24 @@ Or
To access:
- curl http://localhost:8080/api?op=2+7 # json answer
- curl -d "2+9" -X POST http://localhost:8080/api
- browser to http://localhost:8080 for a form based test interface
- browser to http://localhost:8080 for a form based test interface # only in testmode
- http://localhost:8080/healthz for some usage info
To test:
- python3 tests/test01.py
- requires service running on localhost:8080
File overview
Environment variables
CALC_VERSION to overwrite version string
CALC_TESTMODE to activate a testmode that has the form interface enabled
set to ON to turn on
Overview of files
- server.py - the python3 program that implements the service. Uses the built-in http server to serve a simple API. It also has a page that serves health and usage information at /healthz. There is also a form based access that can be used for some simple testing - this function only works in test builds.
Note that this implementation needs to generate local files in the working directory for the parser library used and so needs to run with the insecurely: true flag set in blubber.
- Dockerfile - a file that can be used for local testing - not WMF styled
- calcblubber-debian.yaml - the YAML input file for blubber to generate a valid WMF styled Dockerfile
- tests/test01.py - a basic test file for the calculator functionality
- minikube/ - deployment and service defintions to test locally on minikube
apiVersion: apps/v1
kind: Deployment
metadata:
name: calc
labels:
app: calc
spec:
replicas: 1
strategy:
type: RollingUpdate
selector:
matchLabels:
app: calc
template:
metadata:
labels:
app: calc
spec:
containers:
- name: calc
image: docker-registry.wikimedia.org/wikimedia/blubber-doc-example-calculator-service:stable
imagePullPolicy: IfNotPresent
resources:
requests:
memory: "64Mi"
cpu: "100m"
limits:
memory: "64Mi"
cpu: "100m"
kind: Service
apiVersion: v1
metadata:
name: calc
spec:
selector:
app: calc
type: LoadBalancer
ports:
- protocol: TCP
port: 80
targetPort: 8080
......@@ -9,12 +9,16 @@ from urllib.parse import unquote
import html
# time to measure runtime of a request
import time
import os
# the modules used to implment the gramamr
import ply.yacc as yacc
import ply.lex as lex
# version number
version = 0.2
global version
version = "0.2"
global testmode
testmode = "OFF"
# web server config
hostName = "0.0.0.0"
......@@ -156,6 +160,9 @@ def write_healthz(self):
self.wfile.write(bytes("CalcVersion=", "utf-8"))
self.wfile.write(bytes(str(version), "utf-8"))
self.wfile.write(bytes("<br>", "utf-8"))
self.wfile.write(bytes("testmode=", "utf-8"))
self.wfile.write(bytes(str(testmode), "utf-8"))
self.wfile.write(bytes("<br>", "utf-8"))
self.wfile.write(bytes("wellformed=", "utf-8"))
self.wfile.write(bytes(str(wellformed), "utf-8"))
self.wfile.write(bytes("<br>", "utf-8"))
......@@ -195,9 +202,16 @@ class CalcServer(BaseHTTPRequestHandler):
outstr = "{"+"\"operation\":\"{}\",\"result\":\"{}\"".format(html.escape(operation),result)+"}"
self.wfile.write(bytes(str(outstr), "utf-8"))
else:
write_form(self,"","")
if testmode and testmode.upper() == "ON":
write_form(self,"","")
else:
self.send_response(404)
self.send_header("Content-type", "text/html")
self.end_headers()
duration = duration + time.time() - start
def do_POST(self):
global duration
start = time.time()
......@@ -221,15 +235,20 @@ class CalcServer(BaseHTTPRequestHandler):
self.end_headers()
self.wfile.write(bytes(str(outstr), "utf-8"))
else:
logstr = "Body:\n{0}\n".format(post_data.decode('utf-8'))
if len(logstr) > MAXLEN:
self.send_response(414)
if testmode and testmode.upper() == "ON":
logstr = "Body:\n{0}\n".format(post_data.decode('utf-8'))
if len(logstr) > MAXLEN:
self.send_response(414)
else:
ulogstr = unquote(logstr)
operation = ulogstr.split("=",1)[1]
result = parser.parse(operation)
self.send_response(200)
write_form(self, operation, result)
else:
ulogstr = unquote(logstr)
operation = ulogstr.split("=",1)[1]
result = parser.parse(operation)
self.send_response(200)
write_form(self, operation, result)
self.send_response(404)
self.send_header("Content-type", "text/html")
self.end_headers()
duration = duration + time.time() - start
if __name__ == "__main__":
......@@ -237,6 +256,11 @@ if __name__ == "__main__":
print("CalcServer started http://%s:%s" % (hostName, serverPort))
parser = yacc.yacc()
if os.environ.get('CALC_VERSION'):
version = os.environ.get('CALC_VERSION')
if os.environ.get('CALC_TESTMODE'):
testmode = os.environ.get('CALC_TESTMODE')
try:
webServer.serve_forever()
except KeyboardInterrupt:
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment