Example #1
0
def node_stop(force):
    """
    Stop the Calico node.  This stops the containers (calico/node and
    calico/node-libnetwork) that are started by calicoctl node.
    """
    endpoints = len(client.get_endpoints(hostname=hostname))
    if endpoints:
        if not force:
            print_paragraph("Current host has active endpoints so can't be "
                "stopped.  Force with --force")
            print_paragraph("Note that stopping the node while there are "
                            "active endpoints may make it difficult to clean "
                            "up the endpoints: for example, Docker containers "
                            "networked using libnetwork with Calico will not "
                            "invoke network cleanup during the normal "
                            "container lifecycle.")
            sys.exit(1)
        else:
            print_paragraph("Stopping node while host has active endpoints.  "
                            "If this in error, restart the node using the "
                            "'calicoctl node' command.")

    try:
        docker_client.stop("calico-node")
    except docker.errors.APIError as err:
        if err.response.status_code != 404:
            raise
    try:
        docker_client.stop("calico-libnetwork")
    except docker.errors.APIError as err:
        if err.response.status_code != 404:
            raise

    print "Node stopped"
Example #2
0
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)
Example #3
0
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)
Example #4
0
def node_stop(force):
    """
    Stop the Calico node.  This stops the containers (calico/node and
    calico/node-libnetwork) that are started by calicoctl node.
    """
    endpoints = len(client.get_endpoints(hostname=hostname))
    if endpoints:
        if not force:
            print_paragraph("Current host has active endpoints so can't be "
                            "stopped.  Force with --force")
            print_paragraph("Note that stopping the node while there are "
                            "active endpoints may make it difficult to clean "
                            "up the endpoints: for example, Docker containers "
                            "networked using libnetwork with Calico will not "
                            "invoke network cleanup during the normal "
                            "container lifecycle.")
            sys.exit(1)
        else:
            print_paragraph("Stopping node while host has active endpoints.  "
                            "If this in error, restart the node using the "
                            "'calicoctl node' command.")

    try:
        docker_client.stop("calico-node")
    except docker.errors.APIError as err:
        if err.response.status_code != 404:
            raise
    try:
        docker_client.stop("calico-libnetwork")
    except docker.errors.APIError as err:
        if err.response.status_code != 404:
            raise

    print "Node stopped"
Example #5
0
def node_stop(force):
    if force or len(client.get_endpoints(hostname=hostname,
                                         orchestrator_id=DOCKER_ORCHESTRATOR_ID)) == 0:
        client.remove_host(hostname)
        try:
            docker_client.stop("calico-node")
        except docker.errors.APIError as err:
            if err.response.status_code != 404:
                raise

        print "Node stopped and all configuration removed"
    else:
        print "Current host has active endpoints so can't be stopped." + \
              " Force with --force"
Example #6
0
def node_stop(force):
    if force or len(client.get_endpoints(hostname=hostname,
                                         orchestrator_id=DOCKER_ORCHESTRATOR_ID)) == 0:
        client.remove_host(hostname)
        try:
            docker_client.stop("calico-node")
        except docker.errors.APIError as err:
            if err.response.status_code != 404:
                raise

        print "Node stopped and all configuration removed"
    else:
        print "Current host has active endpoints so can't be stopped." + \
              " Force with --force"
Example #7
0
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)
Example #8
0
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)
Example #9
0
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)
Example #10
0
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)
Example #11
0
 def handle_sigterm(sig, frame):
     print "Got SIGTERM"
     docker_client.stop(container)
     sys.exit(0)
Example #12
0
 def handle_sigterm(sig, frame):
     print "Got SIGTERM"
     docker_client.stop(container)
     sys.exit(0)