class ServoLoop(object): proxy_mgr = ProxyManager() hc_mgr = HealthCheckManager() log_listener = mon.LogListener(stat_instance) access_logger = AccessLogger() lb_updater = LoadBalancerUpdater() def __init__(self): # get the instance id from metadata service self.__instance_id = None self.__elb_host = config.get_clc_host() # TODO: should query user-data if self.__instance_id is None: self.__instance_id = config.get_servo_id() self.__redis = None self.__pubsub = None servo.log.debug('main loop running with elb_host=%s, instance_id=%s' % (self.__elb_host, self.__instance_id)) @staticmethod def get_redis(): return redis.StrictRedis(host='localhost', port=6379) @staticmethod def report_cloudwatch_metrics(msg): try: if not 'data' in msg or not msg['data']: raise Exception('No data field in the received redis message') msg = msg['data'] except Exception, err: servo.log.error('failed to parse get-cloudwatch-metrics message') metric_json = '{}' try: metric_json = stat_instance.get_json_and_clear_stat() except Exception, err: servo.log.error('failed to retrieve cw metrics in json format: %s' % str(err))
# along with this program. If not, see http://www.gnu.org/licenses/. # # Please contact Eucalyptus Systems, Inc., 6755 Hollister Ave., Goleta # CA 93117, USA or visit http://www.eucalyptus.com/licenses/ if you need # additional information or have any questions. from servo.haproxy import ProxyManager, Listener mytest = 1 if __name__ == "__main__": print "TEST -- CREATE NEW LISTENER" first = Listener(protocol='tcp', port=80, instance_port=80, loadbalancer='lb01') first.add_instance('192.168.0.100') proxy = ProxyManager() proxy.update_listeners([first]) current = proxy.listeners() if len(current) == 1 and first in current: print "PASSED: new listener added" else: print "ERROR: new listener not found" print "TEST -- ADD NEW LISTENER" second = Listener(protocol='http', port=82, instance_port=80, loadbalancer='lb02') second.add_instance('192.168.0.101') second.add_instance('192.168.0.102') proxy.update_listeners([first, second])
def start(self): if config.ENABLE_CLOUD_WATCH: hl = mon.LogListener(stat_instance) hl.start() self.__status = ServoLoop.RUNNING proxy_mgr = ProxyManager() hc_mgr = HealthCheckManager() while self.__status == ServoLoop.RUNNING: # call elb-describe-services lbs = None try: access_key_id = config.get_access_key_id() secret_access_key = config.get_secret_access_key() security_token = config.get_security_token() con = servo.ws.connect_elb(host_name=self.__elb_host, aws_access_key_id=access_key_id, aws_secret_access_key=secret_access_key, security_token=security_token) lbs = con.get_servo_load_balancers(self.__instance_id) except Exception, err: servo.log.error('failed to query the elb service: %s' % err) if lbs is None: servo.log.warning('failed to find the loadbalancers') else: # prepare Listener lists # call update_listeners received=[] try: for lb in lbs: if lb.health_check is not None: interval = lb.health_check.interval healthy_threshold = lb.health_check.healthy_threshold unhealthy_threshold = lb.health_check.unhealthy_threshold timeout = lb.health_check.timeout target = lb.health_check.target if interval is None or healthy_threshold is None or unhealthy_threshold is None or timeout is None or target is None: pass else: hc = HealthCheckConfig(interval, healthy_threshold, unhealthy_threshold, timeout, target) if health_check.health_check_config is None or health_check.health_check_config != hc: health_check.health_check_config = hc servo.log.info('new health check config: %s' % hc) hc_mgr.reset() instances = [] if lb.instances is not None and isinstance(lb.instances, Iterable): for inst in lb.instances: instances.append(str(inst.id)) hc_mgr.set_instances(instances) in_service_instances = [] for inst_id in instances: if hc_mgr.health_status(inst_id) is 'InService': in_service_instances.append(inst_id) if lb.listeners is not None and isinstance(lb.listeners, Iterable) : for listener in lb.listeners: protocol=listener.protocol port=listener.load_balancer_port instance_port=listener.instance_port instance_protocol=None # TODO: boto doesn't have the field ssl_cert=None # TODO: not supported cookie_expiration = ServoLoop.get_cookie_expiration(listener) cookie_name = ServoLoop.get_cookie_name(listener) l = Listener(protocol=protocol, port=port, instance_port=instance_port, instance_protocol=instance_protocol, ssl_cert=ssl_cert, loadbalancer=lb.name, cookie_name=cookie_name, cookie_expiration=cookie_expiration) for inst_id in in_service_instances: hostname = servo.hostname_cache.get_hostname(inst_id) if hostname is not None: l.add_instance(hostname) received.append(l) except Exception, err: servo.log.error('failed to receive listeners: %s' % err) try: proxy_mgr.update_listeners(received) servo.log.debug('listener updated') except Exception, err: servo.log.error('failed to update proxy listeners: %s' % err)
def start(self): log_listener = None access_logger = AccessLogger() access_logger.start() if config.ENABLE_CLOUD_WATCH: log_listener = mon.LogListener(stat_instance) log_listener.access_logger = access_logger log_listener.start() self.__status = ServoLoop.RUNNING proxy_mgr = ProxyManager() hc_mgr = HealthCheckManager() while self.__status == ServoLoop.RUNNING: # call elb-describe-services lbs = None try: access_key_id = config.get_access_key_id() secret_access_key = config.get_secret_access_key() security_token = config.get_security_token() con = servo.ws.connect_elb(aws_access_key_id=access_key_id, aws_secret_access_key=secret_access_key, security_token=security_token) lbs = con.get_servo_load_balancers(self.__instance_id) except Exception, err: servo.log.error('failed to query the elb service: %s' % err) if lbs is None: servo.log.warning('failed to find the loadbalancers') else: # prepare Listener lists # call update_listeners received=[] try: conn_idle_timeout = config.CONNECTION_IDLE_TIMEOUT for lb in lbs: try: if log_listener: # assume there is only one loadbalancer per servo log_listener.set_loadbalancer(lb.name) attr = lb.attributes conn_idle_timeout = attr.connecting_settings.idle_timeout if int(conn_idle_timeout) < 1: conn_idle_timeout = 1 elif int(conn_idle_timeout) > 3600: conn_idle_timeout = 3600 access_log_setting = attr.access_log access_logger.loadbalancer = lb.name if access_log_setting.s3_bucket_name != None: access_logger.bucket_name = access_log_setting.s3_bucket_name servo.log.debug('access log bucket name: %s' % access_logger.bucket_name) if access_log_setting.s3_bucket_prefix != None: access_logger.bucket_prefix = access_log_setting.s3_bucket_prefix servo.log.debug('access log bucket prefix: %s' % access_logger.bucket_prefix) if access_log_setting.emit_interval != None: access_logger.emit_interval = int(access_log_setting.emit_interval) servo.log.debug('access log emit interval: %d' % access_logger.emit_interval) if access_log_setting.enabled != None: access_logger.enabled = access_log_setting.enabled servo.log.debug('access log enabled?: %s' % access_logger.enabled) except Exception, err: servo.log.warning('failed to get connection idle timeout: %s' % str(err)) if lb.health_check is not None: interval = lb.health_check.interval healthy_threshold = lb.health_check.healthy_threshold unhealthy_threshold = lb.health_check.unhealthy_threshold timeout = lb.health_check.timeout target = lb.health_check.target if interval is None or healthy_threshold is None or unhealthy_threshold is None or timeout is None or target is None: pass else: hc = HealthCheckConfig(interval, healthy_threshold, unhealthy_threshold, timeout, target) if health_check.health_check_config is None or health_check.health_check_config != hc: health_check.health_check_config = hc servo.log.info('new health check config: %s' % hc) hc_mgr.reset() instances = [] if lb.instances is not None and isinstance(lb.instances, Iterable): instances.extend(lb.instances) instance_ids = [inst.instance_id for inst in instances] hc_mgr.set_instances(instances) in_service_instances = [] for inst_id in instance_ids: if hc_mgr.health_status(inst_id) is 'InService': in_service_instances.append(inst_id) if lb.listeners is not None and isinstance(lb.listeners, Iterable) : for listener in lb.listeners: protocol=listener.protocol port=listener.load_balancer_port instance_port=listener.instance_port instance_protocol=listener.instance_protocol ssl_cert=str(listener.ssl_certificate_id) policies = ServoLoop.get_listener_policies(lb, listener.policy_names) policies.extend(ServoLoop.get_backend_policies(lb, instance_port)) l = Listener(protocol=protocol, port=port, instance_port=instance_port, instance_protocol=instance_protocol, ssl_cert=ssl_cert, loadbalancer=lb.name, policies=policies, connection_idle_timeout=conn_idle_timeout) for inst_id in in_service_instances: hostname = servo.hostname_cache.get_hostname(inst_id) if hostname is not None: l.add_instance(hostname) received.append(l) except Exception, err: servo.log.error('failed to receive listeners: %s' % err) try: proxy_mgr.update_listeners(received) servo.log.debug('listener updated') except Exception, err: servo.log.error('failed to update proxy listeners: %s' % err)
def start(self): log_listener = None access_logger = AccessLogger() access_logger.start() if config.ENABLE_CLOUD_WATCH: log_listener = mon.LogListener(stat_instance) log_listener.access_logger = access_logger log_listener.start() self.__status = ServoLoop.RUNNING proxy_mgr = ProxyManager() hc_mgr = HealthCheckManager() while self.__status == ServoLoop.RUNNING: # call elb-describe-services lbs = None try: access_key_id = config.get_access_key_id() secret_access_key = config.get_secret_access_key() security_token = config.get_security_token() con = servo.ws.connect_elb( aws_access_key_id=access_key_id, aws_secret_access_key=secret_access_key, security_token=security_token) lbs = con.get_servo_load_balancers(self.__instance_id) except Exception, err: servo.log.error('failed to query the elb service: %s' % err) if lbs is None: servo.log.warning('failed to find the loadbalancers') else: # prepare Listener lists # call update_listeners received = [] try: conn_idle_timeout = config.CONNECTION_IDLE_TIMEOUT for lb in lbs: try: if log_listener: # assume there is only one loadbalancer per servo log_listener.set_loadbalancer(lb.name) attr = lb.attributes conn_idle_timeout = attr.connecting_settings.idle_timeout if int(conn_idle_timeout) < 1: conn_idle_timeout = 1 elif int(conn_idle_timeout) > 3600: conn_idle_timeout = 3600 access_log_setting = attr.access_log access_logger.loadbalancer = lb.name if access_log_setting.s3_bucket_name != None: access_logger.bucket_name = access_log_setting.s3_bucket_name servo.log.debug( 'access log bucket name: %s' % urllib2.quote(access_logger.bucket_name)) if access_log_setting.s3_bucket_prefix != None: access_logger.bucket_prefix = access_log_setting.s3_bucket_prefix servo.log.debug( 'access log bucket prefix: %s' % urllib2.quote(access_logger.bucket_prefix)) if access_log_setting.emit_interval != None: access_logger.emit_interval = int( access_log_setting.emit_interval) servo.log.debug( 'access log emit interval: %d' % access_logger.emit_interval) if access_log_setting.enabled != None: access_logger.enabled = access_log_setting.enabled servo.log.debug('access log enabled?: %s' % access_logger.enabled) except Exception, err: servo.log.warning( 'failed to get connection idle timeout: %s' % str(err)) if lb.health_check is not None: interval = lb.health_check.interval healthy_threshold = lb.health_check.healthy_threshold unhealthy_threshold = lb.health_check.unhealthy_threshold timeout = lb.health_check.timeout target = lb.health_check.target if interval is None or healthy_threshold is None or unhealthy_threshold is None or timeout is None or target is None: pass else: hc = HealthCheckConfig(interval, healthy_threshold, unhealthy_threshold, timeout, target) if health_check.health_check_config is None or health_check.health_check_config != hc: health_check.health_check_config = hc servo.log.info( 'new health check config: %s' % hc) hc_mgr.reset() instances = [] if lb.instances is not None and isinstance( lb.instances, Iterable): instances.extend(lb.instances) instance_ids = [inst.instance_id for inst in instances] hc_mgr.set_instances(instances) in_service_instances = [] for inst_id in instance_ids: if hc_mgr.health_status(inst_id) is 'InService': in_service_instances.append(inst_id) if lb.listeners is not None and isinstance( lb.listeners, Iterable): for listener in lb.listeners: protocol = listener.protocol port = listener.load_balancer_port instance_port = listener.instance_port instance_protocol = listener.instance_protocol ssl_cert = str(listener.ssl_certificate_id) policies = ServoLoop.get_listener_policies( lb, listener.policy_names) policies.extend( ServoLoop.get_backend_policies( lb, instance_port)) l = Listener( protocol=protocol, port=port, instance_port=instance_port, instance_protocol=instance_protocol, ssl_cert=ssl_cert, loadbalancer=lb.name, policies=policies, connection_idle_timeout=conn_idle_timeout) for inst_id in in_service_instances: hostname = servo.hostname_cache.get_hostname( inst_id) if hostname is not None: l.add_instance(hostname) received.append(l) except Exception, err: servo.log.error('failed to receive listeners: %s' % err) try: proxy_mgr.update_listeners(received) servo.log.debug('listener updated') except Exception, err: servo.log.error('failed to update proxy listeners: %s' % err)
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # GNU General Public License for more details. # # You should have received a copy of the GNU General Public License # along with this program. If not, see http://www.gnu.org/licenses/. # # Please contact Eucalyptus Systems, Inc., 6755 Hollister Ave., Goleta # CA 93117, USA or visit http://www.eucalyptus.com/licenses/ if you need # additional information or have any questions. from servo.haproxy import ProxyManager, Listener mytest=1 if __name__ == "__main__": print "TEST -- CREATE NEW LISTENER" first = Listener(protocol='tcp', port=80, instance_port=80, loadbalancer='lb01') first.add_instance('192.168.0.100') proxy = ProxyManager() proxy.update_listeners([first]) current = proxy.listeners() if len(current) == 1 and first in current: print "PASSED: new listener added" else: print "ERROR: new listener not found" print "TEST -- ADD NEW LISTENER" second = Listener(protocol='http', port=82, instance_port=80, loadbalancer='lb02') second.add_instance('192.168.0.101') second.add_instance('192.168.0.102') proxy.update_listeners([first, second]) current = proxy.listeners() if len(current) ==2 and first in current and second in current: print "PASSED: second listener added"
def start(self): if config.ENABLE_CLOUD_WATCH: hl = mon.LogListener(stat_instance) hl.start() self.__status = ServoLoop.RUNNING proxy_mgr = ProxyManager() hc_mgr = HealthCheckManager() while self.__status == ServoLoop.RUNNING: # call elb-describe-services lbs = None try: access_key_id = config.get_access_key_id() secret_access_key = config.get_secret_access_key() security_token = config.get_security_token() con = servo.ws.connect_elb( host_name=self.__elb_host, aws_access_key_id=access_key_id, aws_secret_access_key=secret_access_key, security_token=security_token) lbs = con.get_servo_load_balancers(self.__instance_id) except Exception, err: servo.log.error('failed to query the elb service: %s' % err) if lbs is None: servo.log.warning('failed to find the loadbalancers') else: # prepare Listener lists # call update_listeners received = [] try: for lb in lbs: if lb.health_check is not None: interval = lb.health_check.interval healthy_threshold = lb.health_check.healthy_threshold unhealthy_threshold = lb.health_check.unhealthy_threshold timeout = lb.health_check.timeout target = lb.health_check.target if interval is None or healthy_threshold is None or unhealthy_threshold is None or timeout is None or target is None: pass else: hc = HealthCheckConfig(interval, healthy_threshold, unhealthy_threshold, timeout, target) if health_check.health_check_config is None or health_check.health_check_config != hc: health_check.health_check_config = hc servo.log.info( 'new health check config: %s' % hc) hc_mgr.reset() instances = [] if lb.instances is not None and isinstance( lb.instances, Iterable): for inst in lb.instances: instances.append(str(inst.id)) hc_mgr.set_instances(instances) in_service_instances = [] for inst_id in instances: if hc_mgr.health_status(inst_id) is 'InService': in_service_instances.append(inst_id) if lb.listeners is not None and isinstance( lb.listeners, Iterable): for listener in lb.listeners: protocol = listener.protocol port = listener.load_balancer_port instance_port = listener.instance_port instance_protocol = None # TODO: boto doesn't have the field ssl_cert = None # TODO: not supported cookie_expiration = ServoLoop.get_cookie_expiration( listener) cookie_name = ServoLoop.get_cookie_name( listener) l = Listener( protocol=protocol, port=port, instance_port=instance_port, instance_protocol=instance_protocol, ssl_cert=ssl_cert, loadbalancer=lb.name, cookie_name=cookie_name, cookie_expiration=cookie_expiration) for inst_id in in_service_instances: hostname = servo.hostname_cache.get_hostname( inst_id) if hostname is not None: l.add_instance(hostname) received.append(l) except Exception, err: servo.log.error('failed to receive listeners: %s' % err) try: proxy_mgr.update_listeners(received) servo.log.debug('listener updated') except Exception, err: servo.log.error('failed to update proxy listeners: %s' % err)