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)
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()
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()
def main(): """Generate a haproxy config file based on etcd then gracefully reload haproxy""" # This is designed to work with containers, we expect to receive params via the environment template = os.environ.get('TEMPLATE', '/app/templates/haproxy.tpl') client_url = os.environ.get('CLIENT_URL', None) haproxy_binary = os.environ.get('HAPROXY_BINARY', '/usr/sbin/haproxy') haproxy_pid = os.environ.get('HAPROXY_PID', '/var/run/haproxy.pid') haproxy_service = None haproxy_config = os.environ.get('HAPROXY_CONFIG', '/etc/haproxy/haproxy.cfg') interval_check = os.environ.get('INTERVAL_CHECK', 5) cluster_name = os.environ.get('CLUSTER_NAME', 'cluster1') # Cant continue if we don't have an etcd endpoint if not client_url: sys.exit('CLIENT_URL has not been defined') etcd = Etcd(client_url) haproxy = Haproxy(haproxy_service, haproxy_binary, haproxy_pid) if template: haproxy.set_template(template) if haproxy_config: haproxy.set_config_file(haproxy_config) while True: data = [] services = etcd.fetch_services() for service in services: # Rename the service to match the template, if the cluster name in etcd matches our galera cluster. if service['name'] != cluster_name: continue service['name'] = 'galera' instances = etcd.fetch_instances_of(service) data.append({'service': service, 'instances': instances}) # TODO - if we don't have any cluster data, should we die? haproxy.reload(data) time.sleep(interval_check)
def main(template, client_url, haproxy_binary, haproxy_pid, haproxy_service, haproxy_config, interval_check): """Generate a haproxy config file based on etcd then gracefully reload haproxy""" # TODO: if haproxy_binary, make sure haproxy_pid is provided etcd = Etcd(client_url) haproxy = Haproxy(haproxy_service, haproxy_binary, haproxy_pid) if template: haproxy.set_template(template) if haproxy_config: haproxy.set_config_file(haproxy_config) while True: data = [] services = etcd.fetch_services() for service in services: instances = etcd.fetch_instances_of(service) data.append({'service': service, 'instances': instances}) haproxy.reload(data) time.sleep(interval_check)
def run_haproxy(msg=None): logger.info("==========BEGIN==========") if msg: logger.info(msg) haproxy = Haproxy() haproxy.update()
def run_haproxy(): flush_arp() haproxy = Haproxy() haproxy.update()
def run_haproxy(): haproxy = Haproxy() haproxy.update()