Serve a temporary html page while tljh is building

This commit is contained in:
GeorgianaElena
2020-07-30 22:59:56 +03:00
parent e8136c06c3
commit 972b15b95a
3 changed files with 114 additions and 1 deletions

View File

@@ -13,10 +13,13 @@ Constraints:
- Use stdlib modules only
"""
import os
from http.server import SimpleHTTPRequestHandler, HTTPServer
import multiprocessing
import subprocess
import sys
import logging
import shutil
import urllib.request
logger = logging.getLogger(__name__)
@@ -90,7 +93,38 @@ def validate_host():
print("For local development, see http://tljh.jupyter.org/en/latest/contributing/dev-setup.html")
sys.exit(1)
class LoaderPageRequestHandler(SimpleHTTPRequestHandler):
def do_GET(self):
if self.path == "/logs":
with open("/opt/tljh/installer.log", "rb") as log_file:
content = log_file.read()
self.wfile.write(content)
else:
self.path = '/index.html'
return SimpleHTTPRequestHandler.do_GET(self)
def serve_forever(server):
try:
server.serve_forever()
except KeyboardInterrupt:
pass
def main():
# Serve the loading page until TLJH builds
url="https://raw.githubusercontent.com/jupyterhub/the-littlest-jupyterhub/master/bootstrap/index.html"
urllib.request.urlretrieve(url, "index.html")
# If the bootstrap is run to upgrade TLJH, then this will raise an "Address already in use" error
try:
loading_page_server = HTTPServer(("", 80), LoaderPageRequestHandler)
p = multiprocessing.Process(target=serve_forever, args=(loading_page_server,))
p.start()
with open('/loading.pid', 'w+') as f:
f.write(str(p.pid))
except OSError:
# Only serve the loading page when installing TLJH
pass
validate_host()
install_prefix = os.environ.get('TLJH_INSTALL_PREFIX', '/opt/tljh')
hub_prefix = os.path.join(install_prefix, 'hub')
@@ -178,6 +212,5 @@ def main():
] + sys.argv[1:]
)
if __name__ == '__main__':
main()

67
bootstrap/index.html Normal file
View File

@@ -0,0 +1,67 @@
<!DOCTYPE html>
<html lang="en-US">
<head>
<body>
<meta http-equiv="refresh" content="20"/>
<img style="width: 150px; height: auto;" src="https://raw.githubusercontent.com/jupyterhub/the-littlest-jupyterhub/master/docs/images/logo/logo.png">
<div class="loader"></div>
<div class="message">Please wait while your TLJH is building...</div>
<div class="logs_info">Click the button below to see the logs</div>
<button class="logs_button" onclick="window.location.href='/logs'">View logs</button>
</body>
<style>
button:hover {
background: grey;
}
.message {
position: absolute;
top: 35%;
left: 30%;
font-size: 30px;
font-weight: bold;
color: grey;
}
.loader {
position: absolute;
top: 11%;
left: 46%;
width: 150px;
height: 150px;
border-radius: 90%;
border: 7px solid transparent;
animation: spin 2s infinite ease;
animation-direction: alternate;
}
@keyframes spin {
0% {
transform: rotateZ(0deg);
border-top-color: #f17c0e
}
100% {
transform: rotateZ(360deg);
border-top-color: #fce5cf;
}
}
.logs_info {
position: absolute;
top: 45%;
left: 42%;
font-size: 15px;
color: grey;
}
.logs_button {
position: absolute;
top: 50%;
left: 46%;
border: 0;
color: white;
padding: 15px 32px;
font-size: 16px;
cursor: pointer;
background: #f5a252;
}
</style>
</head>

View File

@@ -6,6 +6,7 @@ import itertools
import logging
import os
import secrets
import signal
import subprocess
import sys
import time
@@ -499,6 +500,18 @@ def main():
ensure_node()
ensure_jupyterhub_package(HUB_ENV_PREFIX)
ensure_jupyterlab_extensions()
# Stop the http server with the loading page before traefik starts
try:
with open(pidfile, "r") as f:
pid = f.read()
os.kill(int(pid), signal.SIGINT)
# Remove the pid file and the temporary html page
os.remove('/loading.pid')
os.remove('/index.html')
except:
pass
ensure_jupyterhub_service(HUB_ENV_PREFIX)
ensure_jupyterhub_running()
ensure_symlinks(HUB_ENV_PREFIX)