def _attach_and_stream(container): """ Attach to a container and stream its stdout and stderr output to this process's stdout, until the container stops. If the user presses Ctrl-C or the process is killed, also stop the Docker container. Used to run the calico-node as a foreground attached service. :param container: Docker container to attach to. :return: None. """ # Register a SIGTERM handler, so we shut down the container if this # process is kill'd. def handle_sigterm(sig, frame): print "Got SIGTERM" docker_client.stop(container) sys.exit(0) signal.signal(signal.SIGTERM, handle_sigterm) output = docker_client.attach(container, stream=True) try: for raw_data in output: sys.stdout.write(raw_data) except KeyboardInterrupt: # mainline. someone press Ctrl-C. print "Stopping Calico node..." finally: # Could either be this process is being killed, or output generator # raises an exception. docker_client.stop(container)
def _attach_and_stream(container, startup_only): """ Attach to a container and stream its stdout and stderr output to this process's stdout. If the user presses Ctrl-C or the process is killed, also stop the Docker container. If startup_only is set, then only attach until the container starts up successfully. :param container: Docker container to attach to. :return: None. """ # Register a SIGTERM handler, so we shut down the container if this # process is kill'd. def handle_sigterm(sig, frame): print "Got SIGTERM" docker_client.stop(container) sys.exit(0) signal.signal(signal.SIGTERM, handle_sigterm) stop_container_on_exit = True exit_code = 1 output = docker_client.attach(container, stream=True) line_buf = "" try: for raw_data in output: sys.stdout.write(raw_data) if startup_only: # We've been asked to exit after the container has started, # look for the successful startup message. We buffer one line # of output in case we get a split line from the output stream. line_buf += raw_data if "Calico node started successfully" in line_buf: stop_container_on_exit = False break line_buf = line_buf.rsplit('\n')[-1] except KeyboardInterrupt: # Mainline. Someone pressed Ctrl-C. print "Stopping Calico node..." stop_container_on_exit = True exit_code = 130 finally: # Could either be this process is being killed, or output generator # raises an exception. if stop_container_on_exit: docker_client.stop(container) # If the container is stopped, some sort of error occurred. sys.exit(exit_code)
def _attach_and_stream(container, startup_only): """ Attach to a container and stream its stdout and stderr output to this process's stdout. If the user presses Ctrl-C or the process is killed, also stop the Docker container. If startup_only is set, then only attach until the container starts up successfully. :param container: Docker container to attach to. :return: None. """ # Register a SIGTERM handler, so we shut down the container if this # process is kill'd. def handle_sigterm(sig, frame): print "Got SIGTERM" docker_client.stop(container) sys.exit(0) signal.signal(signal.SIGTERM, handle_sigterm) stop_container_on_exit = True output = docker_client.attach(container, stream=True) try: for raw_data in output: sys.stdout.write(raw_data) if "Calico node started successfully" in raw_data and startup_only: stop_container_on_exit = False break except KeyboardInterrupt: # Mainline. Someone pressed Ctrl-C. print "Stopping Calico node..." finally: # Could either be this process is being killed, or output generator # raises an exception. if stop_container_on_exit: docker_client.stop(container)