feature: adds server for host machine
This commit is contained in:
parent
513e951219
commit
b56f07be42
1 changed files with 103 additions and 0 deletions
103
update_server.py
Normal file
103
update_server.py
Normal file
|
|
@ -0,0 +1,103 @@
|
|||
#!/usr/bin/env python
|
||||
import json
|
||||
import logging
|
||||
import os
|
||||
import subprocess
|
||||
import time
|
||||
|
||||
RCSTACK_PATH_NAME = "RCSTACK_PATH"
|
||||
RCSTACK_PATH = os.environ.get(RCSTACK_PATH_NAME)
|
||||
RCSTACK_LOG = os.environ.get("RCSTACK_LOG", "./rcstack_status.log")
|
||||
|
||||
try:
|
||||
# Py3
|
||||
from http.server import BaseHTTPRequestHandler, HTTPServer
|
||||
except ImportError:
|
||||
# Py2
|
||||
from BaseHTTPServer import BaseHTTPRequestHandler, HTTPServer
|
||||
|
||||
|
||||
log = logging.getLogger("mini-server")
|
||||
|
||||
|
||||
class RcstackUpdater(BaseHTTPRequestHandler):
|
||||
def __init__(self, request, client_address, server):
|
||||
super().__init__(request, client_address, server)
|
||||
|
||||
def _send_json_ok(self):
|
||||
body = json.dumps({"status": "ok"}).encode("utf-8")
|
||||
self.send_response(200)
|
||||
self.send_header("Content-Type", "application/json")
|
||||
self.send_header("Content-Length", str(len(body)))
|
||||
self.end_headers()
|
||||
self.wfile.write(body)
|
||||
|
||||
def do_GET(self):
|
||||
if self.path == "/health":
|
||||
return self._send_json_ok()
|
||||
elif self.path == "/update-rcstack":
|
||||
ok, err_msg = self.spawn_rcstack_status()
|
||||
if not ok:
|
||||
self._send_http_error(err_msg.encode("utf-8"), status_code=500)
|
||||
return
|
||||
|
||||
self._send_http_error(b"not found", status_code=404)
|
||||
|
||||
def spawn_rcstack_status(self):
|
||||
if not RCSTACK_PATH:
|
||||
return False, f"{RCSTACK_PATH_NAME} env variable not set"
|
||||
|
||||
if not os.path.exists(RCSTACK_PATH) or not os.path.isfile(RCSTACK_PATH):
|
||||
return False, f"{RCSTACK_PATH} does not exist"
|
||||
|
||||
rcstack = os.path.expanduser(RCSTACK_PATH)
|
||||
|
||||
# Append mode; don't use PIPE unless you read it.
|
||||
out = open(RCSTACK_LOG, "ab")
|
||||
|
||||
stamp = time.strftime("%Y-%m-%d %H:%M:%S").encode("utf-8")
|
||||
out.write(b"[" + stamp + b"] spawn: " + rcstack.encode("utf-8") + b" status\n")
|
||||
out.flush()
|
||||
|
||||
# Detach from the request handler process group/session.
|
||||
# start_new_session=True is Py3; preexec_fn=os.setsid works in Py2/3 on Unix.
|
||||
try:
|
||||
rcstack_dir = os.path.dirname(rcstack)
|
||||
|
||||
subprocess.Popen(
|
||||
[rcstack, "status"],
|
||||
cwd=rcstack_dir,
|
||||
stdout=out,
|
||||
stderr=out,
|
||||
close_fds=True,
|
||||
preexec_fn=os.setsid, # Unix only
|
||||
)
|
||||
finally:
|
||||
out.close()
|
||||
|
||||
return True, None
|
||||
|
||||
def _send_http_error(self, msg, status_code):
|
||||
self.send_response(status_code)
|
||||
self.send_header("Content-Type", "text/plain")
|
||||
self.end_headers()
|
||||
self.wfile.write(msg)
|
||||
|
||||
def log_message(self, fmt, *args):
|
||||
log.info("%s - %s", self.address_string(), fmt % args)
|
||||
|
||||
|
||||
def main():
|
||||
logging.basicConfig(
|
||||
level=logging.INFO,
|
||||
format="%(asctime)s %(levelname)s %(name)s: %(message)s",
|
||||
)
|
||||
host = "127.0.0.1" # allow only run within the system, no external connections allowed
|
||||
port = 10025
|
||||
httpd = HTTPServer((host, port), RcstackUpdater)
|
||||
log.info("listening on http://%s:%d", host, port)
|
||||
httpd.serve_forever()
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
Loading…
Add table
Add a link
Reference in a new issue