Skip to content

Commit 99d0dd9

Browse files
committed
Initial.
0 parents  commit 99d0dd9

File tree

8 files changed

+233
-0
lines changed

8 files changed

+233
-0
lines changed

.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
*.swp

.travis.yml

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
sudo: required
2+
3+
services:
4+
- docker
5+
6+
script:
7+
- ./test.sh

Dockerfile

+43
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
FROM ubuntu:latest
2+
3+
ENV DEBIAN_FRONTEND noninteractive
4+
5+
RUN apt-get -y update
6+
7+
RUN apt-get -y install dosbox
8+
9+
RUN apt-get -y install curl
10+
RUN apt-get -y install zip unzip
11+
12+
RUN apt-get -y install python python-flask
13+
14+
ENV SDL_VIDEODRIVER dummy
15+
16+
# Install OLDDOS.EXE utils.
17+
RUN mkdir -p /root/dos
18+
19+
WORKDIR /root/dos
20+
21+
RUN curl \
22+
-L -s \
23+
-o "./olddos.exe" \
24+
"https://web.archive.org/web/20120215074213/http://download.microsoft.com/download/win95upg/tool_s/1.0/w95/en-us/olddos.exe"
25+
26+
RUN zip -J "./olddos.exe"
27+
RUN yes | unzip "./olddos.exe"
28+
RUN rm "./olddos.exe"
29+
30+
# DosBox config file
31+
RUN mkdir -p /root/.dosbox
32+
COPY dosbox.conf "/root/.dosbox/dosbox-0.74.conf"
33+
34+
# Program itself
35+
COPY MORSE.BAS "/root/dos/MORSE.BAS"
36+
37+
# The proxy
38+
RUN mkdir -p /root/proxy
39+
COPY proxy.py /root/proxy/proxy.py
40+
41+
WORKDIR /root/proxy
42+
43+
CMD [ "python", "proxy.py" ]

MORSE.BAS

+81
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
DECLARE FUNCTION MORSEC$ (CHAR$)
2+
3+
DIM MESSAGE AS STRING
4+
5+
OPEN "INPUT.STR" FOR INPUT AS 1
6+
LINE INPUT #1, MESSAGE$
7+
8+
LET RESULT$ = ""
9+
10+
FOR I = 1 TO LEN(MESSAGE$)
11+
LET C$ = UCASE$(MID$(MESSAGE$, I, 1))
12+
13+
IF I <> 1 THEN RESULT$ = RESULT$ + " "
14+
RESULT$ = RESULT$ + MORSEC$(C$)
15+
NEXT I
16+
17+
PRINT RESULT$
18+
19+
SYSTEM
20+
21+
FUNCTION MORSEC$ (CHAR$)
22+
SELECT CASE CHAR$
23+
CASE "A"
24+
MORSEC$ = ".-"
25+
CASE "B"
26+
MORSEC$ = "-..."
27+
CASE "C"
28+
MORSEC$ = "-.-."
29+
CASE "D"
30+
MORSEC$ = "-.."
31+
CASE "E"
32+
MORSEC$ = "."
33+
CASE "F"
34+
MORSEC$ = "..-."
35+
CASE "G"
36+
MORSEC$ = "--."
37+
CASE "H"
38+
MORSEC$ = "...."
39+
CASE "I"
40+
MORSEC$ = ".."
41+
CASE "J"
42+
MORSEC$ = ".---"
43+
CASE "K"
44+
MORSEC$ = "-.-"
45+
CASE "L"
46+
MORSEC$ = ".-.."
47+
CASE "M"
48+
MORSEC$ = "--"
49+
CASE "N"
50+
MORSEC$ = "-."
51+
CASE "O"
52+
MORSEC$ = "---"
53+
CASE "P"
54+
MORSEC$ = ".--."
55+
CASE "Q"
56+
MORSEC$ = "--.-"
57+
CASE "R"
58+
MORSEC$ = ".-."
59+
CASE "S"
60+
MORSEC$ = "..."
61+
CASE "T"
62+
MORSEC$ = "-"
63+
CASE "U"
64+
MORSEC$ = "..-"
65+
CASE "V"
66+
MORSEC$ = "...-"
67+
CASE "W"
68+
MORSEC$ = ".--"
69+
CASE "X"
70+
MORSEC$ = "-..-"
71+
CASE "Y"
72+
MORSEC$ = "-.--"
73+
CASE "Z"
74+
MORSEC$ = "--.."
75+
CASE " "
76+
MORSEC$ = "/"
77+
CASE ELSE
78+
MORSEC$ = ""
79+
END SELECT
80+
END FUNCTION
81+

README.md

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
# Serverless QBasic
2+
3+
This repository contains all code required to run a QBasic program as an [OpenWhisk](https://github.com/openwhisk/openwhisk) action.
4+
5+
## Building and deploying
6+
7+
1. Build the Docker image.
8+
2. Publish it to DockerHub, e.g. as `yourid/morse`.
9+
3. Create an OpenWhisk action: `wsk action create morse --docker "yourid/morse"
10+
4. Run it: `wsk action invoke -br morse -p input "les sanglots longs des violons de l'automne"`
11+
12+
[![Build Status](https://travis-ci.org/psuter/serverless-qbasic.svg?branch=master)](https://travis-ci.org/psuter/serverless-qbasic)

dosbox.conf

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
[mixer]
2+
nosound=true
3+
4+
[midi]
5+
mididevice=none
6+
7+
[sblaster]
8+
sbtype=none
9+
10+
[speaker]
11+
pcspeaker=false
12+
13+
[joystick]
14+
joysticktype=none
15+
16+
[autoexec]
17+
mount C /root/dos
18+
C:

proxy.py

+39
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
import flask
2+
import subprocess
3+
4+
proxy = flask.Flask(__name__)
5+
6+
@proxy.route("/init", methods=[ "POST" ])
7+
def init():
8+
return flask.jsonify(ok=True)
9+
10+
@proxy.route("/run", methods=[ "POST" ])
11+
def run():
12+
msg = flask.request.get_json(force=True, silent=True)
13+
14+
if not msg or not isinstance(msg, dict):
15+
r = flask.jsonify({ "error": "Invalid payload." })
16+
r.status_code = 400
17+
return r
18+
else:
19+
input_string = msg.get("value", {}).get("input", {})
20+
21+
with open("/root/dos/INPUT.STR", "w") as fp:
22+
fp.write("\"%s\"\n" % input_string)
23+
24+
subprocess.call([
25+
"dosbox", "./PRINT.EXE", "-c", "C:\\QBASIC.EXE /run C:\\MORSE.BAS > C:\\LOG.TXT", "-exit"
26+
], cwd="/root/dos")
27+
28+
output = "???"
29+
30+
try:
31+
with open("/root/dos/LOG.TXT", "r") as fp:
32+
output = fp.read().strip()
33+
except e:
34+
pass
35+
36+
return flask.jsonify(input=input_string, output=output)
37+
38+
if __name__ == "__main__":
39+
proxy.run(host='0.0.0.0', port=8080)

test.sh

+32
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
#!/usr/bin/env bash
2+
3+
set -e
4+
5+
# Build the image.
6+
docker build . -t serverless-qbasic
7+
8+
# Start the container.
9+
CONTAINER_ID=$(docker run -d -p 8080:8080 serverless-qbasic)
10+
echo $CONTAINER_ID
11+
12+
# Wait a little, give the container some time to come to life.
13+
sleep 10
14+
15+
# Send a test payload.
16+
RESULT=$(curl -s -X POST -H 'Content-Type: application/json' 'http://localhost:8080/run' -d '{ "value": { "input": "Hello world" } }')
17+
18+
echo $RESULT
19+
echo ""
20+
21+
# Clean up.
22+
docker stop ${CONTAINER_ID}
23+
docker rm -f ${CONTAINER_ID}
24+
25+
# Return proper status code.
26+
if [[ "$RESULT" == *".--"* ]]; then
27+
echo "Morse code found in answer. All good."
28+
exit 0
29+
else
30+
echo "Unexpected answer?"
31+
exit 1
32+
fi

0 commit comments

Comments
 (0)