|
1 | | -#!/bin/bash -l |
2 | | -# lightest possible entrypoint that ensures that |
3 | | -# we use a login shell to get a fully configured shell environment |
4 | | -# (e.g. sourcing /etc/profile.d, ~/.bashrc, and friends) |
5 | | - |
6 | | -# Setup a file descriptor (FD) that is connected to a tee process which |
7 | | -# writes its input to $REPO_DIR/.jupyter-server-log.txt |
8 | | -# We later use this FD as a place to redirect the output of the actual |
9 | | -# command to. We can't add `tee` to the command directly as that will prevent |
10 | | -# the container from exiting when `docker stop` is run. |
11 | | -# See https://stackoverflow.com/a/55678435 |
12 | | -exec {log_fd}> >(exec tee $REPO_DIR/.jupyter-server-log.txt) |
13 | | - |
14 | | -if [[ ! -z "${R2D_ENTRYPOINT:-}" ]]; then |
15 | | - if [[ ! -x "$R2D_ENTRYPOINT" ]]; then |
16 | | - chmod u+x "$R2D_ENTRYPOINT" |
17 | | - fi |
18 | | - exec "$R2D_ENTRYPOINT" "$@" 2>&1 >&"$log_fd" |
19 | | -else |
20 | | - exec "$@" 2>&1 >&"$log_fd" |
21 | | -fi |
22 | | - |
23 | | -# Close the logging output again |
24 | | -exec {log_fd}>&- |
| 1 | +#!/usr/local/bin/python3-login |
| 2 | +# note: must run on Python >= 3.5, which mainly means no f-strings |
| 3 | + |
| 4 | +# goals: |
| 5 | +# - load environment variables from a login shell (bash -l) |
| 6 | +# - preserve signal handling of subprocess (kill -TERM and friends) |
| 7 | +# - tee output to a log file |
| 8 | + |
| 9 | +import fcntl |
| 10 | +import os |
| 11 | +import select |
| 12 | +import signal |
| 13 | +import subprocess |
| 14 | +import sys |
| 15 | + |
| 16 | +# output chunk size to read |
| 17 | +CHUNK_SIZE = 1024 |
| 18 | + |
| 19 | +# signals to be forwarded to the child |
| 20 | +# everything catchable, excluding SIGCHLD |
| 21 | +SIGNALS = set(signal.Signals) - {signal.SIGKILL, signal.SIGSTOP, signal.SIGCHLD} |
| 22 | + |
| 23 | + |
| 24 | +def main(): |
| 25 | + |
| 26 | + # open log file to send output |
| 27 | + log_file = open( |
| 28 | + os.path.join(os.environ.get("REPO_DIR", "."), ".jupyter-server-log.txt"), |
| 29 | + "ab", |
| 30 | + ) |
| 31 | + |
| 32 | + # build the command |
| 33 | + # like `exec "$@"` |
| 34 | + command = sys.argv[1:] |
| 35 | + # load entrypoint override from env |
| 36 | + r2d_entrypoint = os.environ.get("R2D_ENTRYPOINT") |
| 37 | + if r2d_entrypoint: |
| 38 | + command.insert(0, r2d_entrypoint) |
| 39 | + |
| 40 | + # launch the subprocess |
| 41 | + child = subprocess.Popen( |
| 42 | + command, |
| 43 | + bufsize=1, |
| 44 | + stdout=subprocess.PIPE, |
| 45 | + stderr=subprocess.STDOUT, |
| 46 | + ) |
| 47 | + |
| 48 | + # hook up ~all signals so that every signal the parent gets, |
| 49 | + # the children also get |
| 50 | + |
| 51 | + def relay_signal(sig, frame): |
| 52 | + """Relay a signal to children""" |
| 53 | + # DEBUG: show signal |
| 54 | + child.send_signal(sig) |
| 55 | + |
| 56 | + for signum in SIGNALS: |
| 57 | + signal.signal(signum, relay_signal) |
| 58 | + |
| 59 | + # tee output from child to both our stdout and the log file |
| 60 | + def tee(chunk): |
| 61 | + """Tee output from child to both our stdout and the log file""" |
| 62 | + for f in [sys.stdout.buffer, log_file]: |
| 63 | + f.write(chunk) |
| 64 | + f.flush() |
| 65 | + |
| 66 | + # make stdout pipe non-blocking |
| 67 | + # this means child.stdout.read(nbytes) |
| 68 | + # will always return immediately, even if there's nothing to read |
| 69 | + flags = fcntl.fcntl(child.stdout, fcntl.F_GETFL) |
| 70 | + fcntl.fcntl(child.stdout, fcntl.F_SETFL, flags | os.O_NONBLOCK) |
| 71 | + poller = select.poll() |
| 72 | + poller.register(child.stdout) |
| 73 | + |
| 74 | + # while child is running, constantly relay output |
| 75 | + while child.poll() is None: |
| 76 | + chunk = child.stdout.read(CHUNK_SIZE) |
| 77 | + if chunk: |
| 78 | + tee(chunk) |
| 79 | + else: |
| 80 | + # empty chunk means nothing to read |
| 81 | + # wait for output on the pipe |
| 82 | + # timeout is in milliseconds |
| 83 | + poller.poll(1000) |
| 84 | + |
| 85 | + # child has exited, continue relaying any remaining output |
| 86 | + # At this point, read() will return an empty string when it's done |
| 87 | + chunk = child.stdout.read() |
| 88 | + while chunk: |
| 89 | + tee(chunk) |
| 90 | + chunk = child.stdout.read() |
| 91 | + |
| 92 | + # make our returncode match the child's returncode |
| 93 | + sys.exit(child.returncode) |
| 94 | + |
| 95 | + |
| 96 | +if __name__ == "__main__": |
| 97 | + main() |
0 commit comments