From 16c41bf23d74625d7015c477506d46374a4a8d5e Mon Sep 17 00:00:00 2001 From: pali <6pali6@gmail.com> Date: Sat, 3 Apr 2021 23:06:01 +0200 Subject: [PATCH 01/11] support for arbitrary byond versions instead of fixed images --- Dockerfile | 8 -------- compile.sh | 4 ---- listener.py | 18 +++++++++++------- 3 files changed, 11 insertions(+), 19 deletions(-) delete mode 100644 Dockerfile delete mode 100644 compile.sh diff --git a/Dockerfile b/Dockerfile deleted file mode 100644 index a18c34b..0000000 --- a/Dockerfile +++ /dev/null @@ -1,8 +0,0 @@ -ARG BYOND_VERSION=513.1527 -FROM beestation/byond:${BYOND_VERSION} - -WORKDIR /app - -COPY compile.sh . - -ENTRYPOINT ["bash", "/app/compile.sh"] diff --git a/compile.sh b/compile.sh deleted file mode 100644 index 10b4be2..0000000 --- a/compile.sh +++ /dev/null @@ -1,4 +0,0 @@ -#!/bin/bash -cp /app/code/* . -DreamMaker test.dme -DreamDaemon test.dmb -close -verbose -ultrasafe | cat diff --git a/listener.py b/listener.py index fbcb173..0003355 100644 --- a/listener.py +++ b/listener.py @@ -49,12 +49,12 @@ def randomString(stringLength=24): def checkVersions(version:str): try: - image_list = client.images.list(name='test') + image_list = client.images.list(name='beestation/byond') except IndexError: return False for image in image_list: - if f"test:{version}" in image.tags: + if f"beestation/byond:{version}" in image.tags: return True return False @@ -66,7 +66,10 @@ def buildVersion(version:str): else: try: print(f"Attempting to build version: {version}") - return client.images.build(path=f'{Path.cwd()}', dockerfile='Dockerfile', rm=True, pull=True, tag=f'test:{version}', buildargs={'BYOND_VERSION':version}) + return client.images.build(path=f'https://github.com/BeeStation/byond-docker.git', rm=True, pull=True, tag=f'beestation/byond:{version}', buildargs={ + 'buildtime_BYOND_MAJOR': version.split('.')[0], + 'buildtime_BYOND_MINOR': version.split('.')[1] + }) except docker.errors.BuildError: raise @@ -88,12 +91,13 @@ def compileTest(codeText:str, version:str): fc.write(loadTemplate(codeText)) else: fc.write(loadTemplate(codeText, False)) + command = ['bash', '-c', 'cp code/* .; DreamMaker test.dme; DreamDaemon test.dmb -close -verbose -ultrasafe | cat'] if HOST_OS == "Windows": #To get cleaner outputs, we run docker as a subprocess rather than through the API - proc = subprocess.Popen(["docker", "run", "--name", f"{randomDir.name}", "--rm", "--network", "none", "-v", f"{randomDir}:/app/code:ro", f"test:{version}"], stdout=subprocess.PIPE, stderr=subprocess.PIPE) + proc = subprocess.Popen(["docker", "run", "--name", f"{randomDir.name}", "--rm", "--network", "none", "-v", f"{randomDir}:/app/code:ro", "-w", "/app", f"beestation/byond:{version}"] + command, stdout=subprocess.PIPE, stderr=subprocess.PIPE) else: #Expects the linux user to be running docker locally, not as root - proc = subprocess.Popen([f"{Path.home()}/bin/docker", "run", "--name", f"{randomDir.name}", "--rm", "--network", "none", "-v", f"{randomDir}:/app/code:ro", f"test:{version}"], stdout=subprocess.PIPE, stderr=subprocess.PIPE) + proc = subprocess.Popen([f"/usr/bin/docker", "run", "--name", f"{randomDir.name}", "--rm", "--network", "none", "-v", f"{randomDir}:/app/code:ro", "-w", "/app", f"beestation/byond:{version}"] + command, stdout=subprocess.PIPE, stderr=subprocess.PIPE) try: compile_log, run_log = proc.communicate(timeout=30) #A bit hacky, but provides exceptionally clean results. The main output will be captured as the compile_log while the "error" output is captured as run_log test_killed = False @@ -114,7 +118,7 @@ def compileTest(codeText:str, version:str): shutil.rmtree(randomDir) - if f"Unable to find image 'test:{version}' locally" in run_log: + if f"Unable to find image 'byond:{version}' locally" in run_log: results = { "build_error": True, "exception": run_log @@ -125,7 +129,7 @@ def compileTest(codeText:str, version:str): "run_log":run_log, "timeout":test_killed } - + return results if __name__=='__main__': From 6ca8bb74f2130bc085259469a54103567fb7f268 Mon Sep 17 00:00:00 2001 From: pali <6pali6@gmail.com> Date: Sat, 3 Apr 2021 23:06:38 +0200 Subject: [PATCH 02/11] minor cleanup of some of the compile and run output --- listener.py | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/listener.py b/listener.py index 0003355..2c985e1 100644 --- a/listener.py +++ b/listener.py @@ -113,6 +113,10 @@ def compileTest(codeText:str, version:str): compile_log = compile_log.decode('utf-8') run_log = run_log.decode('utf-8') run_log = re.sub(r'The BYOND hub reports that port \d* is not reachable.', '', run_log) #remove the network error message + run_log = re.sub(r'\n---------------\n', '\n', run_log) #remove the dashes + run_log = re.sub(r'World opened on network port \d*\.\n', '', run_log) + compile_log = re.sub(r'loading test.dme\n', '', compile_log) + compile_log = re.sub(r'saving test.dmb\n', '', compile_log) compile_log = (compile_log[:1200] + '...') if len(compile_log) > 1200 else compile_log run_log = (run_log[:1200] + '...') if len(run_log) > 1200 else run_log From a4be7de67bd306c786c08c980146416a46dea99f Mon Sep 17 00:00:00 2001 From: pali6 <6pali6@gmail.com> Date: Sat, 11 Sep 2021 23:04:32 +0200 Subject: [PATCH 03/11] I don't remember what I changed here, ok? I bet it improved things. --- Dockerfile | 3 +-- listener.py | 6 +++--- 2 files changed, 4 insertions(+), 5 deletions(-) diff --git a/Dockerfile b/Dockerfile index a1ede92..394ee5a 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,5 +1,4 @@ -ARG BYOND_VERSION=513.1542 -FROM beestation/byond:${BYOND_VERSION} +FROM beestation/byond WORKDIR /app diff --git a/listener.py b/listener.py index 2c985e1..9ad2dba 100644 --- a/listener.py +++ b/listener.py @@ -66,7 +66,7 @@ def buildVersion(version:str): else: try: print(f"Attempting to build version: {version}") - return client.images.build(path=f'https://github.com/BeeStation/byond-docker.git', rm=True, pull=True, tag=f'beestation/byond:{version}', buildargs={ + return client.images.build(path=f'https://github.com/BeeStation/byond-docker.git', platform='linux/386', rm=True, pull=True, tag=f'beestation/byond:{version}', buildargs={ 'buildtime_BYOND_MAJOR': version.split('.')[0], 'buildtime_BYOND_MINOR': version.split('.')[1] }) @@ -94,10 +94,10 @@ def compileTest(codeText:str, version:str): command = ['bash', '-c', 'cp code/* .; DreamMaker test.dme; DreamDaemon test.dmb -close -verbose -ultrasafe | cat'] if HOST_OS == "Windows": #To get cleaner outputs, we run docker as a subprocess rather than through the API - proc = subprocess.Popen(["docker", "run", "--name", f"{randomDir.name}", "--rm", "--network", "none", "-v", f"{randomDir}:/app/code:ro", "-w", "/app", f"beestation/byond:{version}"] + command, stdout=subprocess.PIPE, stderr=subprocess.PIPE) + proc = subprocess.Popen(["docker", "run", "--name", f"{randomDir.name}", "--platform", "linux/386", "--rm", "--network", "none", "-v", f"{randomDir}:/app/code:ro", "-w", "/app", f"beestation/byond:{version}"] + command, stdout=subprocess.PIPE, stderr=subprocess.PIPE) else: #Expects the linux user to be running docker locally, not as root - proc = subprocess.Popen([f"/usr/bin/docker", "run", "--name", f"{randomDir.name}", "--rm", "--network", "none", "-v", f"{randomDir}:/app/code:ro", "-w", "/app", f"beestation/byond:{version}"] + command, stdout=subprocess.PIPE, stderr=subprocess.PIPE) + proc = subprocess.Popen([f"/usr/bin/docker", "run", "--name", f"{randomDir.name}", "--platform", "linux/386", "--rm", "--network", "none", "-v", f"{randomDir}:/app/code:ro", "-w", "/app", f"beestation/byond:{version}"] + command, stdout=subprocess.PIPE, stderr=subprocess.PIPE) try: compile_log, run_log = proc.communicate(timeout=30) #A bit hacky, but provides exceptionally clean results. The main output will be captured as the compile_log while the "error" output is captured as run_log test_killed = False From a80afd817bda6aada5dcabbfb26a750d0854bcd7 Mon Sep 17 00:00:00 2001 From: pali <6pali6@gmail.com> Date: Sun, 12 Sep 2021 10:13:29 +0200 Subject: [PATCH 04/11] added indentation normalization --- listener.py | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/listener.py b/listener.py index 2c985e1..ff759a9 100644 --- a/listener.py +++ b/listener.py @@ -73,6 +73,22 @@ def buildVersion(version:str): except docker.errors.BuildError: raise +def normalizeCode(codeText:str): + indent_step = float('inf') + lines = codeText.split('\n') + for line in lines: + indent_level = len(line) - len(line.lstrip()) + if indent_level > 0: + indent_step = min(indent_step, indent_level) + if indent_step == float('inf'): + return codeText + result_lines = [] + for line in lines: + stripped = line.lstrip() + indent_level = len(line) - len(stripped) + result_lines.append('\t' * (indent_level // indent_step) + stripped) + return '\n'.join(result_lines) + def compileTest(codeText:str, version:str): try: buildVersion(version=version) @@ -83,6 +99,8 @@ def compileTest(codeText:str, version:str): } return results + codeText = normalizeCode(codeText) + randomDir = Path.cwd().joinpath(randomString()) randomDir.mkdir() shutil.copyfile(TEST_DME, randomDir.joinpath("test.dme")) From 7f4bd48b7b7c3701c812c9790d04b343084cc540 Mon Sep 17 00:00:00 2001 From: pali6 <6pali6@gmail.com> Date: Thu, 18 May 2023 17:00:18 +0000 Subject: [PATCH 05/11] path cleanup --- listener.py | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) mode change 100644 => 100755 listener.py diff --git a/listener.py b/listener.py old mode 100644 new mode 100755 index b6b8304..bb54fe3 --- a/listener.py +++ b/listener.py @@ -6,6 +6,7 @@ import platform import docker import re +import os from flask import Flask, jsonify, request, abort from pathlib import Path @@ -110,21 +111,20 @@ def compileTest(codeText:str, version:str): else: fc.write(loadTemplate(codeText, False)) command = ['bash', '-c', 'cp code/* .; DreamMaker test.dme; DreamDaemon test.dmb -close -verbose -ultrasafe | cat'] + docker_path = None if HOST_OS == "Windows": - #To get cleaner outputs, we run docker as a subprocess rather than through the API - proc = subprocess.Popen(["docker", "run", "--name", f"{randomDir.name}", "--platform", "linux/386", "--rm", "--network", "none", "-v", f"{randomDir}:/app/code:ro", "-w", "/app", f"beestation/byond:{version}"] + command, stdout=subprocess.PIPE, stderr=subprocess.PIPE) + docker_path = "docker" else: - #Expects the linux user to be running docker locally, not as root - proc = subprocess.Popen([f"/usr/bin/docker", "run", "--name", f"{randomDir.name}", "--platform", "linux/386", "--rm", "--network", "none", "-v", f"{randomDir}:/app/code:ro", "-w", "/app", f"beestation/byond:{version}"] + command, stdout=subprocess.PIPE, stderr=subprocess.PIPE) + docker_path = f"{Path.home()}/bin/docker" + if not os.path.isfile(docker_path): + docker_path = "/usr/bin/docker" + proc = subprocess.Popen([docker_path, "run", "--name", f"{randomDir.name}", "--platform", "linux/386", "--rm", "--network", "none", "-v", f"{randomDir}:/app/code:ro", "-w", "/app", f"beestation/byond:{version}"] + command, stdout=subprocess.PIPE, stderr=subprocess.PIPE) try: compile_log, run_log = proc.communicate(timeout=30) #A bit hacky, but provides exceptionally clean results. The main output will be captured as the compile_log while the "error" output is captured as run_log test_killed = False except subprocess.TimeoutExpired: proc.kill() - if HOST_OS == "Windows": - subprocess.run(["docker", "stop", f"{randomDir.name}"], capture_output=True) - else: - subprocess.run([f"{Path.home()}/bin/docker", "stop", f"{randomDir.name}"], capture_output=True) + subprocess.run([docker_path, "stop", f"{randomDir.name}"], capture_output=True) compile_log, run_log = proc.communicate() test_killed = True From 5aed7a48aa5be7f130f1378f5f07521e23e1b9af Mon Sep 17 00:00:00 2001 From: pali <6pali6@gmail.com> Date: Thu, 18 May 2023 21:49:57 +0200 Subject: [PATCH 06/11] remove /usr/bin/docker --- listener.py | 2 -- 1 file changed, 2 deletions(-) diff --git a/listener.py b/listener.py index 97beeaf..dbfbdcb 100755 --- a/listener.py +++ b/listener.py @@ -135,8 +135,6 @@ def compileTest(codeText: str, version: str): docker_path = "docker" else: docker_path = f"{Path.home()}/bin/docker" - if not os.path.isfile(docker_path): - docker_path = "/usr/bin/docker" proc = subprocess.Popen( [ docker_path, From f1168d0e95aac418384782064242f15ab2e91d46 Mon Sep 17 00:00:00 2001 From: pali6 <6pali6@gmail.com> Date: Thu, 18 May 2023 20:36:51 +0000 Subject: [PATCH 07/11] do it all properly --- Dockerfile | 3 ++- compile.sh | 4 ++++ listener.py | 28 +++++++++++++++------------- 3 files changed, 21 insertions(+), 14 deletions(-) create mode 100644 compile.sh diff --git a/Dockerfile b/Dockerfile index 394ee5a..f35a7f5 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,4 +1,5 @@ -FROM beestation/byond +ARG BYOND_VERSION +FROM beestation/byond:${BYOND_VERSION} WORKDIR /app diff --git a/compile.sh b/compile.sh new file mode 100644 index 0000000..10b4be2 --- /dev/null +++ b/compile.sh @@ -0,0 +1,4 @@ +#!/bin/bash +cp /app/code/* . +DreamMaker test.dme +DreamDaemon test.dmb -close -verbose -ultrasafe | cat diff --git a/listener.py b/listener.py index 97beeaf..0eb00ba 100755 --- a/listener.py +++ b/listener.py @@ -14,7 +14,7 @@ client = docker.from_env() CODE_FILE = Path.cwd().joinpath("templates/code.dm") -HOST = "127.0.0.1" +HOST = "0.0.0.0" PORT = 5000 HOST_OS = platform.system() MAIN_PROC = "proc/main()" @@ -58,12 +58,12 @@ def randomString(stringLength=24): def checkVersions(version: str): try: - image_list = client.images.list(name="beestation/byond") + image_list = client.images.list(name="test") except IndexError: return False for image in image_list: - if f"beestation/byond:{version}" in image.tags: + if f"test:{version}" in image.tags: return True return False @@ -76,7 +76,7 @@ def buildVersion(version: str): else: try: print(f"Attempting to build version: {version}") - return client.images.build( + client.images.build( path=f"https://github.com/BeeStation/byond-docker.git", platform="linux/386", rm=True, @@ -87,6 +87,14 @@ def buildVersion(version: str): "buildtime_BYOND_MINOR": version.split(".")[1], }, ) + return client.images.build( + path=f"{Path.cwd()}", + dockerfile="Dockerfile", + rm=True, + pull=True, + tag=f"test:{version}", + buildargs={"BYOND_VERSION": version}, + ) except docker.errors.BuildError: raise @@ -125,11 +133,6 @@ def compileTest(codeText: str, version: str): fc.write(loadTemplate(codeText)) else: fc.write(loadTemplate(codeText, False)) - command = [ - "bash", - "-c", - "cp code/* .; DreamMaker test.dme; DreamDaemon test.dmb -close -verbose -ultrasafe | cat", - ] docker_path = None if HOST_OS == "Windows": docker_path = "docker" @@ -152,9 +155,8 @@ def compileTest(codeText: str, version: str): f"{randomDir}:/app/code:ro", "-w", "/app", - f"beestation/byond:{version}", - ] - + command, + f"test:{version}", + ], stdout=subprocess.PIPE, stderr=subprocess.PIPE, ) @@ -185,7 +187,7 @@ def compileTest(codeText: str, version: str): shutil.rmtree(randomDir) - if f"Unable to find image 'byond:{version}' locally" in run_log: + if f"Unable to find image 'test:{version}' locally" in run_log: results = {"build_error": True, "exception": run_log} else: results = { From 30e8c3d2c5a11f9d0d8319658cf5dd0a68c5add1 Mon Sep 17 00:00:00 2001 From: pali6 <6pali6@gmail.com> Date: Thu, 18 May 2023 20:40:20 +0000 Subject: [PATCH 08/11] bad ip --- listener.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/listener.py b/listener.py index c668f64..8b965b0 100755 --- a/listener.py +++ b/listener.py @@ -14,7 +14,7 @@ client = docker.from_env() CODE_FILE = Path.cwd().joinpath("templates/code.dm") -HOST = "0.0.0.0" +HOST = "127.0.0.1" PORT = 5000 HOST_OS = platform.system() MAIN_PROC = "proc/main()" From 4ce3ad1d409334c4718008262474ca1ddfdec681 Mon Sep 17 00:00:00 2001 From: pali <6pali6@gmail.com> Date: Fri, 19 May 2023 10:20:55 +0200 Subject: [PATCH 09/11] this is not necessary anymore --- listener.py | 2 -- 1 file changed, 2 deletions(-) diff --git a/listener.py b/listener.py index 8b965b0..ffb35f8 100755 --- a/listener.py +++ b/listener.py @@ -151,8 +151,6 @@ def compileTest(codeText: str, version: str): "none", "-v", f"{randomDir}:/app/code:ro", - "-w", - "/app", f"test:{version}", ], stdout=subprocess.PIPE, From e68cdab5406d37c053c261a67dd91e2b0925823c Mon Sep 17 00:00:00 2001 From: pali <6pali6@gmail.com> Date: Fri, 19 May 2023 23:45:47 +0200 Subject: [PATCH 10/11] readd /usr/bin/docker path --- listener.py | 2 ++ 1 file changed, 2 insertions(+) diff --git a/listener.py b/listener.py index ffb35f8..d0ba478 100755 --- a/listener.py +++ b/listener.py @@ -138,6 +138,8 @@ def compileTest(codeText: str, version: str): docker_path = "docker" else: docker_path = f"{Path.home()}/bin/docker" + if not os.path.isfile(docker_path): + docker_path = "/usr/bin/docker" proc = subprocess.Popen( [ docker_path, From 0002ba2b973a308ca1f8b7525ec5915a21a691ea Mon Sep 17 00:00:00 2001 From: pali <6pali6@gmail.com> Date: Tue, 31 Oct 2023 01:21:41 +0100 Subject: [PATCH 11/11] pulling defeats the whole point of building locally lol --- listener.py | 1 - 1 file changed, 1 deletion(-) diff --git a/listener.py b/listener.py index d0ba478..b65bbf6 100755 --- a/listener.py +++ b/listener.py @@ -91,7 +91,6 @@ def buildVersion(version: str): path=f"{Path.cwd()}", dockerfile="Dockerfile", rm=True, - pull=True, tag=f"test:{version}", buildargs={"BYOND_VERSION": version}, )