def on_cloud_event(message):
    logger.debug(message)
    logger.debug(Haproxy.cls_linked_services)
    try:
        event = json.loads(message)
    except ValueError:
        logger.info("event is not a valid json message")
        return

    # When service scale up/down or container start/stop/terminate/redeploy, reload the service
    if event.get("state", "") not in ["In progress", "Pending", "Terminating", "Starting", "Scaling", "Stopping"] and \
                    event.get("type", "").lower() in ["container", "service"] and \
                    len(Haproxy.cls_linked_services.intersection(set(event.get("parents", [])))) > 0:
        msg = "Docker Cloud Event: %s %s is %s" % (
            event["type"],
            get_uuid_from_resource_uri(event.get("resource_uri",
                                                 "")), event["state"].lower())
        run_haproxy(msg)

    # Add/remove services linked to haproxy
    if event.get("state",
                 "") == "Success" and config.HAPROXY_SERVICE_URI in event.get(
                     "parents", []):
        run_haproxy(
            "Docker Cloud Event: New action is executed on the Haproxy container"
        )
def listen_docker_events():
    try:

        try:
            docker = docker_client()
        except:
            docker = docker_client(os.environ)

        docker.ping()
        for event in docker.events(decode=True):
            logger.debug(event)
            attr = event.get("Actor", {}).get("Attributes")
            compose_project = attr.get("com.docker.compose.project", "")
            compose_service = attr.get("com.docker.compose.service", "")
            container_name = attr.get("name", "")
            event_action = event.get("Action", "")
            service = "%s_%s" % (compose_project, compose_service)
            if service in Haproxy.cls_linked_services and event_action in [
                    "start", "die"
            ]:
                msg = "Docker event: container %s %s" % (container_name,
                                                         event_action)
                run_haproxy(msg)
    except APIError as e:
        logger.info("Docker API error: %s" % e)
Example #3
0
def main():
    logging.basicConfig(stream=sys.stdout)
    logging.getLogger("haproxy").setLevel(logging.DEBUG if DEBUG else logging.INFO)

    signal.signal(signal.SIGUSR1, on_user_reload)
    signal.signal(signal.SIGTERM, sys.exit)

    autoreload = set_autoreload(HAPROXY_CONTAINER_URI, HAPROXY_SERVICE_URI, API_AUTH)

    pid = create_pid_file()
    logger.info("HAProxy PID: %s" % pid)

    if autoreload:
        listen_remote_events()
    else:
        run_haproxy("Initial start")
def listen_docker_events():
    try:
        docker = docker_client()
        docker.ping()
        for event in docker.events(decode=True):
            logger.debug(event)
            attr = event.get("Actor", {}).get("Attributes")
            compose_project = attr.get("com.docker.compose.project", "")
            compose_service = attr.get("com.docker.compose.service", "")
            container_name = attr.get("name", "")
            event_action = event.get("Action", "")
            service = "%s_%s" % (compose_project, compose_service)
            if service in Haproxy.cls_linked_services and event_action in ["start", "die"]:
                msg = "Docker event: container %s %s" % (container_name, event_action)
                run_haproxy(msg)
    except APIError as e:
        logger.info("Docker API error: %s" % e)
Example #5
0
def main():
    logging.basicConfig(stream=sys.stdout)
    logging.getLogger("haproxy").setLevel(
        logging.DEBUG if DEBUG else logging.INFO)

    signal.signal(signal.SIGUSR1, on_user_reload)
    signal.signal(signal.SIGTERM, sys.exit)

    autoreload = set_autoreload(HAPROXY_CONTAINER_URI, HAPROXY_SERVICE_URI,
                                API_AUTH)

    pid = create_pid_file()
    logger.info("HAProxy PID: %s" % pid)

    if autoreload:
        listen_remote_events()
    else:
        run_haproxy("Initial start")
Example #6
0
def on_cloud_event(message):
    logger.debug(message)
    logger.debug(Haproxy.cls_linked_services)
    try:
        event = json.loads(message)
    except ValueError:
        logger.info("event is not a valid json message")
        return

    # When service scale up/down or container start/stop/terminate/redeploy, reload the service
    if event.get("state", "") not in ["In progress", "Pending", "Terminating", "Starting", "Scaling", "Stopping"] and \
                    event.get("type", "").lower() in ["container", "service"] and \
                    len(Haproxy.cls_linked_services.intersection(set(event.get("parents", [])))) > 0:
        msg = "Docker Cloud Event: %s %s is %s" % (
            event["type"], get_uuid_from_resource_uri(event.get("resource_uri", "")), event["state"].lower())
        run_haproxy(msg)

    # Add/remove services linked to haproxy
    if event.get("state", "") == "Success" and config.HAPROXY_SERVICE_URI in event.get("parents", []):
        run_haproxy("Docker Cloud Event: New action is executed on the Haproxy container")
Example #7
0
def main():
    logging.basicConfig(stream=sys.stdout)
    logging.getLogger("haproxy").setLevel(
        logging.DEBUG if DEBUG else logging.INFO)
    if DEBUG:
        logging.getLogger("python-dockercloud").setLevel(logging.DEBUG)

    config.LINK_MODE = check_link_mode(HAPROXY_CONTAINER_URI,
                                       HAPROXY_SERVICE_URI, API_AUTH)
    gevent.signal(signal.SIGUSR1, on_user_reload)
    gevent.signal(signal.SIGTERM, sys.exit)

    pid = create_pid_file()
    logger.info("dockercloud/haproxy PID: %s" % pid)

    if config.LINK_MODE == "cloud":
        listen_dockercloud_events()
    elif config.LINK_MODE == "new":
        run_haproxy("Initial start")
        while True:
            listen_docker_events()
            run_haproxy("Reconnect docker events")

    elif config.LINK_MODE == "legacy":
        run_haproxy()
def main():
    logging.basicConfig(stream=sys.stdout)
    logging.getLogger("haproxy").setLevel(logging.DEBUG if DEBUG else logging.INFO)
    if DEBUG:
        logging.getLogger("python-dockercloud").setLevel(logging.DEBUG)

    config.LINK_MODE = check_link_mode(HAPROXY_CONTAINER_URI, HAPROXY_SERVICE_URI, API_AUTH)
    signal.signal(signal.SIGUSR1, on_user_reload)
    signal.signal(signal.SIGTERM, sys.exit)

    pid = create_pid_file()
    logger.info("dockercloud/haproxy PID: %s" % pid)

    if config.LINK_MODE == "cloud":
        listen_dockercloud_events()
    elif config.LINK_MODE == "new":
        run_haproxy("Initial start")
        while True:
            listen_docker_events()
            run_haproxy("Reconnect docker events")

    elif config.LINK_MODE == "legacy":
        run_haproxy()
Example #9
0
def on_user_reload():
    Haproxy.cls_cfg = None
    if config.LINK_MODE == "legacy":
        logger.info("User reload is not supported in legacy link mode")
    else:
        run_haproxy("User reload")
Example #10
0
def on_websocket_open():
    helper.cloud_link_helper.LINKED_CONTAINER_CACHE.clear()
    run_haproxy("Websocket open")
Example #11
0
def on_user_reload(signum, frame):
    run_haproxy("User reload")
Example #12
0
def on_websocket_open():
    Haproxy.LINKED_CONTAINER_CACHE.clear()
    run_haproxy("Websocket open")
def on_user_reload(signum, frame):
    run_haproxy("User reload")
def on_websocket_open():
    Haproxy.LINKED_CONTAINER_CACHE.clear()
    run_haproxy("Websocket open")