示例#1
0
def tutum_event_handler(event):
    logger.debug(event)
    # 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(set(Haproxy.cls_linked_services).intersection(set(event.get("parents", [])))) > 0:
        msg = "Tutum event: %s %s is %s" % (
            event["type"], parse_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 Haproxy.cls_service_uri in event.get("parents", []):
        service = Haproxy.fetch_tutum_obj(Haproxy.cls_service_uri)
        service_endpoints = [srv.get("to_service") for srv in service.linked_to_service]
        if Haproxy.cls_linked_services != service_endpoints:
            services_unlinked = ", ".join([parse_uuid_from_resource_uri(uri) for uri in
                                           set(Haproxy.cls_linked_services) - set(service_endpoints)])
            services_linked = ", ".join([parse_uuid_from_resource_uri(uri) for uri in
                                         set(service_endpoints) - set(Haproxy.cls_linked_services)])
            msg = "Tutum event:"
            if services_unlinked:
                msg += " service %s is unlinked from HAProxy" % services_unlinked
            if services_linked:
                msg += " service %s is linked to HAProxy" % services_linked

            run_haproxy(msg)
示例#2
0
文件: main.py 项目: timani/haproxy
def tutum_event_handler(event):
    logger.debug(event)
    # 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(set(Haproxy.cls_linked_services).intersection(set(event.get("parents", [])))) > 0:
        logger.info("Tutum event detected: %s %s is %s" %
                    (event["type"], parse_uuid_from_resource_uri(event.get("resource_uri", "")), event["state"]))

        run_haproxy()

    # Add/remove services linked to haproxy
    if event.get("state", "") == "Success" and Haproxy.cls_service_uri in event.get("parents", []):
        service = Haproxy.fetch_tutum_obj(Haproxy.cls_service_uri)
        service_endpoints = [srv.get("to_service") for srv in service.linked_to_service]
        if Haproxy.cls_linked_services != service_endpoints:
            removed = ", ".join(set(Haproxy.cls_linked_services) - set(service_endpoints))
            added = ", ".join(set(service_endpoints) - set(Haproxy.cls_linked_services))
            changes = "Tutum event detected:"
            if removed:
                changes += " linked removed: %s" % removed
            if added:
                changes += " linked added: %s" % added
            logger.info(changes)
            Haproxy.cls_linked_services = service_endpoints

            run_haproxy()
示例#3
0
文件: haproxy.py 项目: mba811/haproxy
 def __init__(self):
     Haproxy.extra_bind_settings = Haproxy._parse_extra_bind_settings(
         Haproxy.envvar_extra_bind_settings)
     self.ssl = None
     self.ssl_updated = False
     self.routes_added = []
     self.require_default_route = False
     if Haproxy.cls_container_uri and Haproxy.cls_service_uri and Haproxy.cls_tutum_auth:
         container = self.fetch_tutum_obj(Haproxy.cls_container_uri)
         service = self.fetch_tutum_obj(Haproxy.cls_service_uri)
         Haproxy.cls_linked_services = [
             srv.get("to_service") for srv in service.linked_to_service
         ]
         logger.info(
             "Current links: %s", ", ".join([
                 "%s(%s)" %
                 (srv.get("name"),
                  parse_uuid_from_resource_uri(srv.get("to_service")))
                 for srv in service.linked_to_service
             ]))
         self.specs = Specs(container, service)
     else:
         logger.info(
             "Loading HAProxy definition from environment variables")
         Haproxy.cls_linked_services = None
         Haproxy.specs = Specs()
示例#4
0
def tutum_event_handler(event):
    logger.debug(event)
    # 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(set(Haproxy.cls_linked_services).intersection(set(event.get("parents", [])))) > 0:
        logger.info("Tutum event detected: %s %s is %s" %
                    (event["type"],
                     parse_uuid_from_resource_uri(event.get(
                         "resource_uri", "")), event["state"]))
        run_haproxy()

    # Add/remove services linked to haproxy
    if event.get("state",
                 "") == "Success" and Haproxy.cls_service_uri in event.get(
                     "parents", []):
        service = Haproxy.fetch_tutum_obj(Haproxy.cls_service_uri)
        service_endpoints = [
            srv.get("to_service") for srv in service.linked_to_service
        ]
        if Haproxy.cls_linked_services != service_endpoints:
            removed = ", ".join(
                set(Haproxy.cls_linked_services) - set(service_endpoints))
            added = ", ".join(
                set(service_endpoints) - set(Haproxy.cls_linked_services))
            changes = "Tutum event detected:"
            if removed:
                changes += " linked removed: %s" % removed
            if added:
                changes += " linked added: %s" % added
            logger.info(changes)
            Haproxy.cls_linked_services = service_endpoints
            run_haproxy()
示例#5
0
def tutum_event_handler(event):
    logger.debug(event)
    logger.debug(Haproxy.cls_linked_services)
    # 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(set(Haproxy.cls_linked_services).intersection(set(event.get("parents", [])))) > 0:
        msg = "Tutum event: %s %s is %s" % (
            event["type"], parse_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 Haproxy.cls_service_uri in event.get("parents", []):
        run_haproxy()
示例#6
0
文件: main.py 项目: versus/haproxy
def tutum_event_handler(event):
    logger.debug(event)
    logger.debug(Haproxy.cls_linked_services)
    # 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(set(Haproxy.cls_linked_services).intersection(set(event.get("parents", [])))) > 0:
        msg = "Tutum event: %s %s is %s" % (
            event["type"], parse_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 Haproxy.cls_service_uri in event.get("parents", []):
        run_haproxy()
示例#7
0
 def __init__(self):
     Haproxy.extra_bind_settings = Haproxy._parse_extra_bind_settings(Haproxy.envvar_extra_bind_settings)
     self.ssl = None
     self.ssl_updated = False
     self.routes_added = []
     self.require_default_route = False
     if Haproxy.cls_container_uri and Haproxy.cls_service_uri and Haproxy.cls_tutum_auth:
         container = self.fetch_tutum_obj(Haproxy.cls_container_uri)
         service = self.fetch_tutum_obj(Haproxy.cls_service_uri)
         Haproxy.cls_linked_services = [srv.get("to_service") for srv in service.linked_to_service]
         logger.info("Current links: %s", ", ".join(
             ["%s(%s)" % (srv.get("name"), parse_uuid_from_resource_uri(srv.get("to_service"))) for srv in
              service.linked_to_service]))
         self.specs = Specs(container, service)
     else:
         logger.info("Loading HAProxy definition from environment variables")
         Haproxy.cls_linked_services = None
         Haproxy.specs = Specs()
示例#8
0
    def __init__(self):
        Haproxy.extra_bind_settings = Haproxy._parse_extra_bind_settings(Haproxy.envvar_extra_bind_settings)
        self.ssl = None
        self.ssl_updated = False
        self.routes_added = []
        self.require_default_route = False
        if Haproxy.cls_container_uri and Haproxy.cls_service_uri and Haproxy.cls_tutum_auth:
            haproxy_container = self.fetch_tutum_obj(Haproxy.cls_container_uri)

            links = {}
            for link in haproxy_container.linked_to_container:
                linked_container_uri = link["to_container"]
                linked_container_name = link["name"].upper().replace("-", "_")
                linked_container_service_name = linked_container_name

                match = Haproxy.cls_service_name_match.match(linked_container_name)
                if match:
                    linked_container_service_name = match.group(1)

                links[linked_container_uri] = {
                    "container_name": linked_container_name,
                    "container_uri": linked_container_uri,
                    "service_name": linked_container_service_name,
                    "endpoints": link["endpoints"],
                }

            new_container_object_uris = filter(lambda x: x not in Haproxy.cls_linked_container_object_cache, links)
            pool = ThreadPool(processes=10)
            new_container_objects = pool.map(Haproxy.fetch_tutum_obj, new_container_object_uris)
            for i, container_uri in enumerate(new_container_object_uris):
                Haproxy.cls_linked_container_object_cache[container_uri] = new_container_objects[i]

            linked_containers = [
                Haproxy.cls_linked_container_object_cache[link["to_container"]]
                for link in haproxy_container.linked_to_container
            ]

            for linked_container in linked_containers:
                linked_container_uri = linked_container.resource_uri
                linked_container_service_uri = linked_container.service
                linked_container_name = linked_container.name.upper().replace("-", "_")
                linked_container_envvars = {}

                for envvar in linked_container.container_envvars:
                    if "_ENV_" not in envvar["key"]:
                        linked_container_envvars["%s_ENV_%s" % (linked_container_name, envvar["key"])] = envvar["value"]

                links[linked_container_uri]["service_uri"] = linked_container_service_uri
                links[linked_container_uri]["container_envvars"] = linked_container_envvars

            linked_services = []
            for link in links.itervalues():
                if link["service_uri"] not in linked_services:
                    linked_services.append(link["service_uri"])

            logger.info(
                "Service links: %s",
                ", ".join(
                    sorted(
                        set(
                            [
                                "%s(%s)"
                                % (link.get("service_name"), parse_uuid_from_resource_uri(link.get("service_uri")))
                                for link in links.itervalues()
                            ]
                        )
                    )
                ),
            )
            logger.info(
                "Container links: %s",
                ", ".join(
                    sorted(
                        set(
                            [
                                "%s(%s)"
                                % (link.get("container_name"), parse_uuid_from_resource_uri(link.get("container_uri")))
                                for link in links.itervalues()
                            ]
                        )
                    )
                ),
            )

            Haproxy.cls_linked_services = linked_services
            self.specs = Specs(links)
        else:
            logger.info("Loading HAProxy definition from environment variables")
            Haproxy.cls_linked_services = None
            Haproxy.specs = Specs()
示例#9
0
    def __init__(self):
        Haproxy.extra_bind_settings = Haproxy._parse_extra_bind_settings(
            Haproxy.envvar_extra_bind_settings)
        self.ssl = None
        self.ssl_updated = False
        self.routes_added = []
        self.require_default_route = False
        if Haproxy.cls_container_uri and Haproxy.cls_service_uri and Haproxy.cls_tutum_auth:
            haproxy_container = self.fetch_tutum_obj(Haproxy.cls_container_uri)

            links = {}
            for link in haproxy_container.linked_to_container:
                linked_container_uri = link["to_container"]
                linked_container_name = link["name"].upper().replace("-", "_")
                linked_container_service_name = linked_container_name

                match = Haproxy.cls_service_name_match.match(
                    linked_container_name)
                if match:
                    linked_container_service_name = match.group(1)

                links[linked_container_uri] = {
                    "container_name": linked_container_name,
                    "container_uri": linked_container_uri,
                    "service_name": linked_container_service_name,
                    "endpoints": link["endpoints"]
                }

            new_container_object_uris = filter(
                lambda x: x not in Haproxy.cls_linked_container_object_cache,
                links)
            pool = ThreadPool(processes=10)
            new_container_objects = pool.map(Haproxy.fetch_tutum_obj,
                                             new_container_object_uris)
            for i, container_uri in enumerate(new_container_object_uris):
                Haproxy.cls_linked_container_object_cache[
                    container_uri] = new_container_objects[i]

            linked_containers = [
                Haproxy.cls_linked_container_object_cache[link["to_container"]]
                for link in haproxy_container.linked_to_container
            ]

            for linked_container in linked_containers:
                linked_container_uri = linked_container.resource_uri
                linked_container_service_uri = linked_container.service
                linked_container_name = links[linked_container_uri][
                    "container_name"]
                linked_container_envvars = {}

                for envvar in linked_container.container_envvars:
                    if "_ENV_" not in envvar['key']:
                        linked_container_envvars[
                            "%s_ENV_%s" % (linked_container_name,
                                           envvar['key'])] = envvar['value']

                links[linked_container_uri][
                    "service_uri"] = linked_container_service_uri
                links[linked_container_uri][
                    "container_envvars"] = linked_container_envvars

            linked_services = []
            for link in links.itervalues():
                if link["service_uri"] not in linked_services:
                    linked_services.append(link["service_uri"])

            logger.info(
                "Service links: %s", ", ".join(
                    sorted(
                        set([
                            "%s(%s)" % (link.get("service_name"),
                                        parse_uuid_from_resource_uri(
                                            link.get("service_uri")))
                            for link in links.itervalues()
                        ]))))
            logger.info(
                "Container links: %s", ", ".join(
                    sorted(
                        set([
                            "%s(%s)" % (link.get("container_name"),
                                        parse_uuid_from_resource_uri(
                                            link.get("container_uri")))
                            for link in links.itervalues()
                        ]))))

            Haproxy.cls_linked_services = linked_services
            self.specs = Specs(links)
        else:
            logger.info(
                "Loading HAProxy definition from environment variables")
            Haproxy.cls_linked_services = None
            Haproxy.specs = Specs()