Allow users to run the scouting app with HTTPS/LDAP

I find myself needing to experiment with getting the username from the
LDAP login that happens in our HTTPS version of the scouting app. This
patch exposes a new `//scouting:https` target to let me do just that.

This patch also updates the README to let other folks know how to run
it under HTTPS/LDAP.

Signed-off-by: Philipp Schrader <philipp.schrader@gmail.com>
Change-Id: Ib9f4b8626cb9adfe178ded2b43677d1dcd30da4f
diff --git a/tools/build_rules/apache_runner.py b/tools/build_rules/apache_runner.py
index 3364216..3839cb0 100644
--- a/tools/build_rules/apache_runner.py
+++ b/tools/build_rules/apache_runner.py
@@ -16,9 +16,11 @@
 import os
 from pathlib import Path
 import signal
+import socket
 import subprocess
 import sys
 import tempfile
+import time
 
 import jinja2
 
@@ -32,6 +34,18 @@
 dummy@frc971.org
 """
 
+def wait_for_server(port: int):
+    """Waits for the server at the specified port to respond to TCP connections."""
+    while True:
+        try:
+            connection = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
+            connection.connect(("localhost", port))
+            connection.close()
+            break
+        except ConnectionRefusedError:
+            connection.close()
+            time.sleep(0.01)
+
 def main(argv):
   parser = argparse.ArgumentParser()
   parser.add_argument("--binary", type=str, required=True)
@@ -43,7 +57,7 @@
     help="JSON file containing 'ldap_bind_dn', 'ldap_url', and 'ldap_password' entries.",
     default="",
   )
-  args = parser.parse_args(argv[1:])
+  args, unknown_args = parser.parse_known_args(argv[1:])
 
   if not args.ldap_info:
     args.ldap_info = os.path.join(os.environ["BUILD_WORKSPACE_DIRECTORY"], "ldap.json")
@@ -102,20 +116,32 @@
     # Tell it via the environment what port to listen on.
     env = os.environ.copy()
     env["APACHE_WRAPPED_PORT"] = str(args.wrapped_port)
-    wrapped_binary = subprocess.Popen([args.binary], env=env)
+    wrapped_binary = subprocess.Popen([args.binary] + unknown_args, env=env)
 
     # Start the apache server.
     env = os.environ.copy()
     env["LD_LIBRARY_PATH"] = "external/apache2/usr/lib/x86_64-linux-gnu"
-    try:
-      subprocess.run(
-        ["external/apache2/usr/sbin/apache2", "-X", "-d", str(temp_dir)],
-        check=True,
-        env=env,
-      )
-    finally:
-      wrapped_binary.send_signal(signal.SIGINT)
-      wrapped_binary.wait()
+    apache = subprocess.Popen(
+      ["external/apache2/usr/sbin/apache2", "-X", "-d", str(temp_dir)],
+      env=env,
+    )
+
+    wait_for_server(args.https_port)
+    wait_for_server(args.wrapped_port)
+    # Sleep to attempt to get the HTTPS message after the webserver message.
+    time.sleep(1)
+    print(f"Serving HTTPS on port {args.https_port}")
+
+    # Wait until we see a request to shut down.
+    signal.signal(signal.SIGINT, lambda signum, frame: None)
+    signal.signal(signal.SIGTERM, lambda signum, frame: None)
+    signal.pause()
+
+    print("\nShutting down apache and wrapped binary.")
+    apache.terminate()
+    wrapped_binary.terminate()
+    apache.wait()
+    wrapped_binary.wait()
 
 if __name__ == "__main__":
   sys.exit(main(sys.argv))