Exemple #1
0
def _start_libnetwork_container(libnetwork_image, etcd_envs, etcd_volumes,
                                etcd_binds, no_pull):
    """
    Start the libnetwork driver container.

    :param etcd_envs: Etcd environment variables to pass into the container
    :param libnetwork_image: The name of the Calico libnetwork driver image to
    use.  None, if not using libnetwork.
    :param etcd_volumes: List of mount_paths for etcd files to mount on the
    container
    :param etcd_binds: Dictionary of host file and mount file pairs for etcd
    files to mount on the container
    :param no_pull: Boolean, True to prevent function from pulling the Calico
    node libnetwork Docker image.
    :return:  None
    """
    if not no_pull:
        # Make sure the required image is pulled before removing the old one.
        # This minimizes downtime during upgrade.
        _find_or_pull_node_image(libnetwork_image)

    try:
        docker_client.remove_container("calico-libnetwork", force=True)
    except docker.errors.APIError as err:
        if err.response.status_code != 404:
            raise

    environment = ["HOSTNAME=%s" % hostname] + etcd_envs

    binds = {
        "/run/docker/plugins":
            {
                "bind": "/run/docker/plugins",
                "ro": False
            }
    }
    binds.update(etcd_binds)

    host_config = docker_client.create_host_config(
        privileged=True, # Needed since the plugin does "ip link" commands.
        restart_policy={"Name": "always"},
        network_mode="host",
        binds=binds)

    volumes = ["/run/docker/plugins"] + etcd_volumes
    container = docker_client.create_container(
        libnetwork_image,
        name="calico-libnetwork",
        detach=True,
        environment=environment,
        host_config=host_config,
        volumes=volumes)
    cid = container["Id"]

    docker_client.start(container)
    print "Calico libnetwork driver is running with id: %s" % cid
Exemple #2
0
def _start_libnetwork_container(libnetwork_image, etcd_envs, etcd_volumes,
                                etcd_binds, no_pull):
    """
    Start the libnetwork driver container.

    :param etcd_envs: Etcd environment variables to pass into the container
    :param libnetwork_image: The name of the Calico libnetwork driver image to
    use.  None, if not using libnetwork.
    :param etcd_volumes: List of mount_paths for etcd files to mount on the
    container
    :param etcd_binds: Dictionary of host file and mount file pairs for etcd
    files to mount on the container
    :param no_pull: Boolean, True to prevent function from pulling the Calico
    node libnetwork Docker image.
    :return:  None
    """
    if not no_pull:
        # Make sure the required image is pulled before removing the old one.
        # This minimizes downtime during upgrade.
        _find_or_pull_node_image(libnetwork_image)

    try:
        docker_client.remove_container("calico-libnetwork", force=True)
    except docker.errors.APIError as err:
        if err.response.status_code != 404:
            raise

    environment = ["HOSTNAME=%s" % hostname] + etcd_envs

    binds = {
        "/run/docker/plugins": {
            "bind": "/run/docker/plugins",
            "ro": False
        }
    }
    binds.update(etcd_binds)

    host_config = docker_client.create_host_config(
        privileged=True,  # Needed since the plugin does "ip link" commands.
        restart_policy={"Name": "always"},
        network_mode="host",
        binds=binds)

    volumes = ["/run/docker/plugins"] + etcd_volumes
    container = docker_client.create_container(libnetwork_image,
                                               name="calico-libnetwork",
                                               detach=True,
                                               environment=environment,
                                               host_config=host_config,
                                               volumes=volumes)
    cid = container["Id"]

    docker_client.start(container)
    print "Calico libnetwork driver is running with id: %s" % cid
Exemple #3
0
def _start_node_container_docker(ip, ip6, as_num, log_dir, node_image, detach,
                                 etcd_envs, etcd_volumes, etcd_binds, no_pull):
    """
    Start the main Calico node container.

    :param ip:  The IPv4 address of the host.
    :param ip6:  The IPv6 address of the host (or None if not configured)
    :param as_num: The AS number for the host
    :param log_dir:  The log directory to use.
    :param node_image:  The calico-node image to use.
    :param detach: True to run in Docker's "detached" mode, False to run
    attached.
    :param etcd_envs: Etcd environment variables to pass into the container
    :param etcd_volumes: List of mount_paths for etcd files to mount on the
    container
    :param etcd_binds: Dictionary of host file and mount file pairs for etcd
    files to mount on the container
    :param no_pull: Boolean, True to prevent function from pulling the Calico
    node Docker image.
    :return: None.
    """
    calico_networking = os.getenv(CALICO_NETWORKING_ENV,
                                  CALICO_NETWORKING_DEFAULT)

    no_default_pools = os.getenv(NO_DEFAULT_POOLS_ENV)

    if not no_pull:
        # Make sure the required image is pulled before removing the old one.
        # This minimizes downtime during upgrade.
        _find_or_pull_node_image(node_image)

    try:
        docker_client.remove_container("calico-node", force=True)
    except docker.errors.APIError as err:
        if err.response.status_code != 404:
            raise

    environment = [
        "HOSTNAME=%s" % hostname,
        "IP=%s" % (ip or ""),
        "IP6=%s" % (ip6 or ""),
        "CALICO_NETWORKING=%s" % calico_networking,
        "AS=%s" % (as_num or ""),
        "NO_DEFAULT_POOLS=%s" % (no_default_pools or "")
    ] + etcd_envs

    binds = {
        log_dir: {
            "bind": "/var/log/calico",
            "ro": False
        },
        "/var/run/calico": {
            "bind": "/var/run/calico",
            "ro": False
        },
        "/lib/modules": {
            "bind": "/lib/modules",
            "ro": False
        }
    }
    binds.update(etcd_binds)

    host_config = docker_client.create_host_config(
        privileged=True,
        restart_policy={"Name": "always"},
        network_mode="host",
        binds=binds)

    volumes = ["/var/log/calico", "/var/run/calico", "/lib/modules"
               ] + etcd_volumes
    container = docker_client.create_container(node_image,
                                               name="calico-node",
                                               detach=True,
                                               environment=environment,
                                               host_config=host_config,
                                               volumes=volumes)
    cid = container["Id"]

    env_string = ""
    for an_env in environment:
        env_string += " -e " + an_env

    vol_string = ""
    for a_vol in binds:
        vol_string += " -v %s:%s" % (a_vol, binds[a_vol]["bind"])

    detach_string = " -d" if detach else ""

    print "Running Docker container with the following command:\n"
    print "docker run%s --restart=always --net=host --privileged --name=calico-node%s%s %s\n" % \
          (detach_string, env_string, vol_string, node_image)
    docker_client.start(container)
    print "Calico node is running with id: %s" % cid
    print "Waiting for successful startup"
    _attach_and_stream(container, detach)
Exemple #4
0
def _start_node_container_docker(ip, ip6, as_num, log_dir, node_image, detach, etcd_envs,
                                 etcd_volumes, etcd_binds, no_pull):
    """
    Start the main Calico node container.

    :param ip:  The IPv4 address of the host.
    :param ip6:  The IPv6 address of the host (or None if not configured)
    :param as_num: The AS number for the host
    :param log_dir:  The log directory to use.
    :param node_image:  The calico-node image to use.
    :param detach: True to run in Docker's "detached" mode, False to run
    attached.
    :param etcd_envs: Etcd environment variables to pass into the container
    :param etcd_volumes: List of mount_paths for etcd files to mount on the
    container
    :param etcd_binds: Dictionary of host file and mount file pairs for etcd
    files to mount on the container
    :param no_pull: Boolean, True to prevent function from pulling the Calico
    node Docker image.
    :return: None.
    """
    calico_networking = os.getenv(CALICO_NETWORKING_ENV,
                                  CALICO_NETWORKING_DEFAULT)

    no_default_pools = os.getenv(NO_DEFAULT_POOLS_ENV)

    if not no_pull:
        # Make sure the required image is pulled before removing the old one.
        # This minimizes downtime during upgrade.
        _find_or_pull_node_image(node_image)

    try:
        docker_client.remove_container("calico-node", force=True)
    except docker.errors.APIError as err:
        if err.response.status_code != 404:
            raise

    environment = [
        "HOSTNAME=%s" % hostname,
        "IP=%s" % (ip or ""),
        "IP6=%s" % (ip6 or ""),
        "CALICO_NETWORKING=%s" % calico_networking,
        "AS=%s" % (as_num or ""),
        "NO_DEFAULT_POOLS=%s" % (no_default_pools or "")
    ] + etcd_envs

    binds = {
        log_dir:
            {
                "bind": "/var/log/calico",
                "ro": False
            },
        "/var/run/calico":
            {
                "bind": "/var/run/calico",
                "ro": False
            },
        "/lib/modules":
            {
                "bind": "/lib/modules",
                "ro": False
            }
    }
    binds.update(etcd_binds)

    host_config = docker_client.create_host_config(
        privileged=True,
        restart_policy={"Name": "always"},
        network_mode="host",
        binds=binds)

    volumes = ["/var/log/calico", "/var/run/calico", "/lib/modules"] + etcd_volumes
    container = docker_client.create_container(
        node_image,
        name="calico-node",
        detach=True,
        environment=environment,
        host_config=host_config,
        volumes=volumes)
    cid = container["Id"]

    env_string = ""
    for an_env in environment:
        env_string += " -e " + an_env

    vol_string = ""
    for a_vol in binds:
        vol_string += " -v %s:%s" % (a_vol, binds[a_vol]["bind"])

    detach_string = " -d" if detach else ""

    print "Running Docker container with the following command:\n"
    print "docker run%s --restart=always --net=host --privileged --name=calico-node%s%s %s\n" % \
          (detach_string, env_string, vol_string, node_image)
    docker_client.start(container)
    print "Calico node is running with id: %s" % cid
    print "Waiting for successful startup"
    _attach_and_stream(container, detach)