From 312aaf964dd07fb065862963104c482840c5f361 Mon Sep 17 00:00:00 2001 From: Daniel Krol Date: Sun, 9 Feb 2025 22:49:09 -0500 Subject: [PATCH] Get sandstorm user id in launcher script --- .sandstorm/get-sandstorm-id.py | 46 ++++++++++++++++++++++++++++++++++ .sandstorm/launcher.sh | 5 +++- 2 files changed, 50 insertions(+), 1 deletion(-) create mode 100755 .sandstorm/get-sandstorm-id.py diff --git a/.sandstorm/get-sandstorm-id.py b/.sandstorm/get-sandstorm-id.py new file mode 100755 index 0000000..85da831 --- /dev/null +++ b/.sandstorm/get-sandstorm-id.py @@ -0,0 +1,46 @@ +#!/usr/bin/python3 +from http.server import BaseHTTPRequestHandler, HTTPServer +import os, tempfile, sys + +os.chdir(os.path.split(__file__)[0]) + +[_, serverPort, reloadDelay, envOutputPath] = sys.argv +serverPort = int(serverPort) + +hostName = "0.0.0.0" + +class MyServer(BaseHTTPRequestHandler): + def write_id(self, sandstorm_id): + dir_path = os.path.split(envOutputPath)[0] + f = tempfile.NamedTemporaryFile(mode='w', dir=dir_path, delete=False) + f.write(sandstorm_id) + f.close() + os.rename(f.name, envOutputPath) + + def do_GET(self): + self.write_id(self.headers['X-Sandstorm-User-Id']) + self.send_response(200) + self.send_header("Content-type", "text/html") + self.end_headers() + + # reloadDelay ms "hopefully" is enough time for the next server to start + # can make this marginally faster and more reliable by returning + # 503 and using Caddy's load balancer to wait for the service to start + self.wfile.write(bytes(f"", "utf-8")) + # Quit so that the service can start with the sandstorm id + exit(0) + +if __name__ == "__main__": + if os.path.exists(envOutputPath): + exit(0) + + webServer = HTTPServer((hostName, serverPort), MyServer) + print("Server started http://%s:%s" % (hostName, serverPort)) + + try: + webServer.serve_forever() + except KeyboardInterrupt: + pass + + webServer.server_close() + print("Server stopped.") diff --git a/.sandstorm/launcher.sh b/.sandstorm/launcher.sh index b3840f8..cef10c7 100644 --- a/.sandstorm/launcher.sh +++ b/.sandstorm/launcher.sh @@ -35,6 +35,9 @@ done redis-server --daemonize yes +./get-sandstorm-id.py 8000 1000 /var/sandstorm-id +echo "Sandstorm User ID: $(cat /var/sandstorm-id)" # whatever you need to do with it + cd /opt/paperless/src $VENV/bin/python3 manage.py migrate -$VENV/bin/python3 manage.py runserver \ No newline at end of file +$VENV/bin/python3 manage.py runserver