def create(cls, group, port_name, protocol, options={}): """ :Parameters: - `options`: { 'securePort': 443, 'sslTermination': True, 'secureTrafficOnly': False, 'certificate': '<cert>', 'privatekey': '<key>' } """ lb_id = uuid.uuid4().hex lb = cls(id=lb_id, port_name=port_name, protocol=protocol, options=options) lb.group = group host, port = lb.backend.create_lb(lb) # Add key to config lb.group.environment.system.config_manager.set( lb.config_key, json.dumps({ 'host': host, 'port': port })) # Add group to endpoint supervisor endpoints = supervisors.endpoint_supervisor_client() endpoints.add_group(lb.group.pk, lb.group.config_key) lb.save() return lb
def safe_run(self, func): """ If the instance belongs to a load balancer, `func` is run while mirroring instance state to the load balancer. Restarting or removing any instance belonging to a load balancer uses a block/unblock pattern. This ensures that the endpoint manager does not remove the endpoint on its own. Since we must know the exact time the endpoint is removed from the load balancer, `_safe_run` removes it manually. :Parameters: - `func`: the function to safely run. """ if self.load_balancer: # Have endpoint manager block (ignore) this instance endpoints = supervisors.endpoint_supervisor_client() endpoints.block_instance(self.pk) # Manually remove the instance's endpoint from the load balancer config_manager = self.environment.system.config_manager endpoint = json.loads(config_manager.get(self.config_key)) self.load_balancer.remove_endpoint(endpoint) # Run `func` now that instance state is mirrored to its load balancer func(self) if self.load_balancer: # Have endpoint manager unblock this instance endpoints.unblock_instance(self.pk)
def create(cls, group, port_name, protocol, options={}): """ :Parameters: - `options`: { 'securePort': 443, 'sslTermination': True, 'secureTrafficOnly': False, 'certificate': '<cert>', 'privatekey': '<key>' } """ lb_id = uuid.uuid4().hex lb = cls(id=lb_id, port_name=port_name, protocol=protocol, options=options) lb.group = group host, port = lb.backend.create_lb(lb) # Add key to config lb.group.environment.system.config_manager.set(lb.config_key, json.dumps({'host': host, 'port': port})) # Add group to endpoint supervisor endpoints = supervisors.endpoint_supervisor_client() endpoints.add_group(lb.group.pk, lb.group.config_key) lb.save() return lb
def pre_delete(cls, sender, instance, **kwargs): lb = instance # Remove group from endpoint supervisor endpoints = supervisors.endpoint_supervisor_client() endpoints.remove_group(lb.group.pk) # Remove key from config lb.group.environment.system.config_manager.delete(lb.config_key) lb.backend.delete_lb(lb)