def remove_old_instances_from_elb(group, instance_ids): logging.info("remove_old_instances_from_elb. Group elbs: {}. instance_ids: {}".format(group.load_balancers, instance_ids)) balancers = getELBConn().get_all_load_balancers(load_balancer_names=group.load_balancers) for elb in balancers: logging.info("ELB instances: {}".format(elb.instances)) elb.deregister_instances(instance_ids) time.sleep(10)
def scale_down(self, instid): ''' Stop instid. Note this does not stop an instance if its name is <KeyName>_base. If instid is '*', will attempt to stop the first running instance it encounters. ''' mgr = KooabaWorkerManager() try: if instid == '*': list = mgr.get_instances(key_name=os.environ['KAS_KEYFILTER']) for id in list: inst = list[id] if self.scale_down_instance(inst) != None: return inst.id note( 'No running instance to scale down. Hence terminating all stopped.' ) for id in list: inst = list[id] if inst.state == 'stopped': mgr.terminate_instance(inst.id) elb = self.connect_elb() elb.deregister_instances(os.environ['KAS_KEY'], [inst.id]) return 'terminated.all' else: inst = mgr.get_instance(instid) # if none was scaled down (probably base or ctl), shut down # any if self.scale_down_instance(inst) == None: instid = self.scale_down('*') return instid except Exception as e: note('Error stopping instance %s, %s', instid, e) return None
def scale_down(self, instid): ''' Stop instid. Note this does not stop an instance if its name is <KeyName>_base. If instid is '*', will attempt to stop the first running instance it encounters. ''' mgr = KooabaWorkerManager() try: if instid == '*': list = mgr.get_instances(key_name=os.environ['KAS_KEYFILTER']) for id in list: inst = list[id] if self.scale_down_instance(inst) != None: return inst.id note('No running instance to scale down. Hence terminating all stopped.') for id in list: inst = list[id] if inst.state == 'stopped': mgr.terminate_instance(inst.id) elb = self.connect_elb() elb.deregister_instances(os.environ['KAS_KEY'], [inst.id]) return 'terminated.all' else: inst = mgr.get_instance(instid) # if none was scaled down (probably base or ctl), shut down # any if self.scale_down_instance(inst) == None: instid = self.scale_down('*') return instid except Exception as e: note('Error stopping instance %s, %s', instid, e) return None
def remove_instance(self, instance_id, load_balancer_name): """ Removes the given instance from the ELB with the given name. """ elb = self._get_connection() try: elb.deregister_instances(load_balancer_name, [instance_id]) except boto.exception.BotoServerError: trace = sys.exc_info()[2] raise ELBInstanceMismatchError(), None, trace self._logger.info('Removed instance %s from load balancer %s', instance_id, load_balancer_name)
def _unassign_from_elb_(elb_name=None, nodes = None): """Removes nodes from the Elastic Load Balancer. :type: list :param: Nodes to assign or None for env.nodes :type: str :param: DNS name of ELB or None for env.aws_elb_name. """ nodes = nodes or env.nodes elb = _find_elb_(elb_name) elb_name = elb_name or env.aws_elb_name if elb: info("Removing %s from ELB %s" % ([pretty_instance(node) for node in nodes], elb_name)) elb.deregister_instances([node.id for node in nodes])
def __init__(self): ''' Where the magic begins ''' self.parse_cli_args() #set inventory self.group = os.environ['GROUP'] self.lbname = os.environ['LBNAME'] self.get_servers() #its all the same to here. elb = boto.ec2.elb.connect_to_region('us-west-2') if self.args.dereg: for instance in self.hosts['ec2_instances']: elb.deregister_instances(self.lbname,instance) print('De-Registered '+ instance) else: for instance in self.hosts['ec2_instances']: elb.register_instances(self.lbname,instance) print('Registered '+ instance)
def remove_this_instance_from_elb(): elb = get_elb() # get the aws ec2 instance id for the current machine instance_id = boto.utils.get_instance_metadata()['instance-id'] if instance_id in [i.id for i in elb.instances]: elb.deregister_instances(instance_id) print 'Removing %s from ELB %s' % (instance_id, elb.name) timeout = time.time() + 60*5 # 5 minutes while True: health = elb.get_instance_health([instance_id])[0] assert health.instance_id == instance_id assert time.time() < timeout if health.state == 'OutOfService': break print 'Waiting for removal...' time.sleep(1)
def scale_down_instance(self, inst): ''' Scale down a particular instance. Checks for _base and _ctl instances, which are never scaled down. Only instances in running state are scaled down after being removed from the ELB. TODO stopping should happen in background thread a few minutes after removing of ELB was successfull ''' if inst.name != "%s_base" % os.environ['KAS_KEY'].strip() and \ inst.name != "%s_ctl" % os.environ['KAS_KEY'].strip(): if inst.state == 'running': note('Removing instance %s form load balancer', inst.id) elb = self.connect_elb() elb.deregister_instances(os.environ['KAS_KEY'], [inst.id]) note('Stopping instance %s', inst.id) inst.stop() return inst.id else: note('Not scaling down instance %s which is the base', inst.id) return None
def main(): parser = optparse.OptionParser('Usage: %prog [options]') parser.add_option('-l', '--load-balancer', dest='lb', help='The name of the load balancer to deregister instances from.') (opts, args) = parser.parse_args() if 0 != len(args): parser.print_help() return 1 try: elb = boto.connect_elb() lb = elb.get_all_load_balancers([opts.lb]) if lb is None: raise Error('could not find \'{0}\''.format(opts.lb)) elb.deregister_instances(opts.lb, [i.id for i in lb.instances]) except Error, err: sys.stderr.write('[ERROR] {0}\n'.format(err)) return 1
def remove_instance(lbname, instance_id): "Given the name of a load balancer and an instance id, remove the instance from the load balancer." elb = connect() return elb.deregister_instances(lbname, [instance_id])
def modify(name, region, zones, listeners=None, subnets=None, security_groups=None, health_check=None, policies=None, instances=None, test=False, aws_key=None, aws_secret=None, elbconn=None, elb=None): """ Launch an ELB Most arguments are the same as :meth:`.launch_or_modify` Parameters ---------- elbconn : :class:`boto.ec2.elb.ELBConnection`, optional If present this function will not open a new elb connection elb : :class:`boto.ec2.elb.LoadBalancer`, optional If present this function will not make a call to fetch the load balancer """ if elbconn is None: elbconn = __salt__['aws_util.elbconn'](region, aws_key, aws_secret) if elb is None: elbs = elbconn.get_all_load_balancers(load_balancer_names=[name]) elb = elbs[0] changes = {} # Availability Zones if zones != elb.availability_zones: to_remove = set(elb.availability_zones) - set(zones) if to_remove: if not test: elb.disable_zones(list(to_remove)) for zone in to_remove: changes['Zone {0}'.format(zone)] = "Disabled" to_add = set(zones) - set(elb.availability_zones) if to_add: if not test: elb.enable_zones(list(to_add)) for zone in to_add: changes['Zone {0}'.format(zone)] = "Enabled" # Listeners elb_listeners = [] # Convert the boto listeners to tuples for listener in elb.listeners: tup_listener = ( listener.load_balancer_port, listener.instance_port, listener.protocol, ) if listener.instance_protocol is not None: tup_listener += (listener.instance_protocol, ) if listener.ssl_certificate_id is not None: tup_listener += (listener.ssl_certificate_id, ) elb_listeners.append(tup_listener) listeners = listeners or [] # Translate all protocols to uppercase for listener in listeners: listener[2] = listener[2].upper() listener[3] = listener[3].upper() listeners = [tuple(l) for l in listeners] if listeners != elb_listeners: to_delete = set(elb_listeners) - set(listeners) if to_delete: if not test: for listener in to_delete: elb.delete_listener(listener[0]) changes['Listeners Deleted'] = str(list(to_delete)) to_create = set(listeners) - set(elb_listeners) if to_create: if not test: elbconn.create_load_balancer_listeners( elb.name, complex_listeners=list(to_create)) changes['Listeners Created'] = str(list(to_create)) # Subnets # TODO: This is untested because my account doesn't have VPC elb_subnets = list(elb.subnets) subnets = subnets or [] if subnets != elb_subnets: to_detach = set(elb_subnets) - set(subnets) if to_detach: if not test: elb.detach_subnets(list(to_detach)) changes['Subnets Detached'] = str(list(to_detach)) to_attach = set(subnets) - set(elb_subnets) if to_attach: if not test: elb.attach_subnets(list(to_attach)) changes['Subnets Attached'] = str(list(to_attach)) # Security Groups # TODO: This is untested because my account doesn't have VPC elb_security_groups = list(elb.security_groups) security_groups = security_groups or [] if security_groups != elb_security_groups: to_remove = set(elb_security_groups) - set(security_groups) if to_remove: changes['Security Groups Removed'] = str(list(to_remove)) to_add = set(security_groups) - set(elb_security_groups) if to_add: changes['Security Groups Added'] = str(list(to_add)) if not test: elb.apply_security_groups(security_groups) # Health check if health_check is None: health_check = {} check = elb.health_check new_check = boto.ec2.elb.healthcheck.HealthCheck( access_point=name, interval=health_check.get('interval', 30), target=health_check.get('target'), healthy_threshold=health_check.get('healthy_threshold', 3), timeout=health_check.get('timeout', 5), unhealthy_threshold=health_check.get('unhealthy_threshold', 5)) modified = False if check is None: modified = True if not modified: for attr in ('interval', 'target', 'healthy_threshold', 'timeout', 'unhealthy_threshold'): if getattr(new_check, attr) != getattr(check, attr): modified = True break if modified: changes['Health check'] = "Modified" if not test: elb.configure_health_check(new_check) # Instances if instances is not None: _convert_server_names(instances, region, aws_key, aws_secret) elb_instances = [i.id for i in elb.instances] if instances != elb_instances: to_remove = set(elb_instances) - set(instances) if to_remove: changes['Instances Removed'] = list(to_remove) if not test: elb.deregister_instances(list(to_remove)) to_add = set(instances) - set(elb_instances) if to_add: changes['Instances Added'] = list(to_add) if not test: elb.register_instances(list(to_add)) # Policies policies = policies or {} elb_policies = [] for attr in ('app_cookie_stickiness_policies', 'lb_cookie_stickiness_policies'): for policy in getattr(elb.policies, attr): elb_policies.append(policy.policy_name) # Create any policies that don't exist yet for policy in policies.itervalues(): if policy['type'] == 'lb': policy['name'] = 'lb' + str(policy['cookie_expire']) elif policy['type'] == 'app': policy['name'] = 'app' + str(hash(policy['cookie_name'])) else: raise ValueError("Policy type '{0}' must be 'lb' or 'app'".format( policy['name'])) if policy['name'] not in elb_policies: if not test: if policy['type'] == 'lb': elb.create_cookie_stickiness_policy( policy['cookie_expire'], policy['name']) elif policy['type'] == 'app': elb.create_app_cookie_stickiness_policy( policy['cookie_name'], policy['name']) for listener in elb.listeners: if listener.load_balancer_port in policies: port = listener.load_balancer_port policy = policies[port] if len(listener.policy_names) == 0: if not test: elb.set_policies_of_listener(port, [policy['name']]) changes['Port {0:d}'.format(port)] = "Added policy" else: if policy['name'] != listener.policy_names[0]: if not test: elb.set_policies_of_listener(port, [policy['name']]) changes['Port {0:d}'.format(port)] = "Changed policy" else: if len(listener.policy_names) > 0: if not test: elb.set_policies_of_listener(port, []) changes['Port {0:d}'.format(port)] = "Removed policy" return changes
def modify( name, region, zones, listeners=None, subnets=None, security_groups=None, health_check=None, policies=None, instances=None, test=False, aws_key=None, aws_secret=None, elbconn=None, elb=None): """ Launch an ELB Most arguments are the same as :meth:`.launch_or_modify` Parameters ---------- elbconn : :class:`boto.ec2.elb.ELBConnection`, optional If present this function will not open a new elb connection elb : :class:`boto.ec2.elb.LoadBalancer`, optional If present this function will not make a call to fetch the load balancer """ if elbconn is None: elbconn = __salt__['aws_util.elbconn'](region, aws_key, aws_secret) if elb is None: elbs = elbconn.get_all_load_balancers(load_balancer_names=[name]) elb = elbs[0] changes = {} # Availability Zones if zones != elb.availability_zones: to_remove = set(elb.availability_zones) - set(zones) if to_remove: if not test: elb.disable_zones(list(to_remove)) for zone in to_remove: changes['Zone {0}'.format(zone)] = "Disabled" to_add = set(zones) - set(elb.availability_zones) if to_add: if not test: elb.enable_zones(list(to_add)) for zone in to_add: changes['Zone {0}'.format(zone)] = "Enabled" # Listeners elb_listeners = [] # Convert the boto listeners to tuples for listener in elb.listeners: tup_listener = ( listener.load_balancer_port, listener.instance_port, listener.protocol, ) if listener.instance_protocol is not None: tup_listener += (listener.instance_protocol,) if listener.ssl_certificate_id is not None: tup_listener += (listener.ssl_certificate_id,) elb_listeners.append(tup_listener) listeners = listeners or [] # Translate all protocols to uppercase for listener in listeners: listener[2] = listener[2].upper() listener[3] = listener[3].upper() listeners = [tuple(l) for l in listeners] if listeners != elb_listeners: to_delete = set(elb_listeners) - set(listeners) if to_delete: if not test: for listener in to_delete: elb.delete_listener(listener[0]) changes['Listeners Deleted'] = str(list(to_delete)) to_create = set(listeners) - set(elb_listeners) if to_create: if not test: elbconn.create_load_balancer_listeners( elb.name, complex_listeners=list(to_create)) changes['Listeners Created'] = str(list(to_create)) # Subnets # TODO: This is untested because my account doesn't have VPC elb_subnets = list(elb.subnets) subnets = subnets or [] if subnets != elb_subnets: to_detach = set(elb_subnets) - set(subnets) if to_detach: if not test: elb.detach_subnets(list(to_detach)) changes['Subnets Detached'] = str(list(to_detach)) to_attach = set(subnets) - set(elb_subnets) if to_attach: if not test: elb.attach_subnets(list(to_attach)) changes['Subnets Attached'] = str(list(to_attach)) # Security Groups # TODO: This is untested because my account doesn't have VPC elb_security_groups = list(elb.security_groups) security_groups = security_groups or [] if security_groups != elb_security_groups: to_remove = set(elb_security_groups) - set(security_groups) if to_remove: changes['Security Groups Removed'] = str(list(to_remove)) to_add = set(security_groups) - set(elb_security_groups) if to_add: changes['Security Groups Added'] = str(list(to_add)) if not test: elb.apply_security_groups(security_groups) # Health check if health_check is None: health_check = {} check = elb.health_check new_check = boto.ec2.elb.healthcheck.HealthCheck( access_point=name, interval=health_check.get('interval', 30), target=health_check.get('target'), healthy_threshold=health_check.get('healthy_threshold', 3), timeout=health_check.get('timeout', 5), unhealthy_threshold=health_check.get('unhealthy_threshold', 5)) modified = False if check is None: modified = True if not modified: for attr in ('interval', 'target', 'healthy_threshold', 'timeout', 'unhealthy_threshold'): if getattr(new_check, attr) != getattr(check, attr): modified = True break if modified: changes['Health check'] = "Modified" if not test: elb.configure_health_check(new_check) # Instances if instances is not None: _convert_server_names(instances, region, aws_key, aws_secret) elb_instances = [i.id for i in elb.instances] if instances != elb_instances: to_remove = set(elb_instances) - set(instances) if to_remove: changes['Instances Removed'] = list(to_remove) if not test: elb.deregister_instances(list(to_remove)) to_add = set(instances) - set(elb_instances) if to_add: changes['Instances Added'] = list(to_add) if not test: elb.register_instances(list(to_add)) # Policies policies = policies or {} elb_policies = [] for attr in ('app_cookie_stickiness_policies', 'lb_cookie_stickiness_policies'): for policy in getattr(elb.policies, attr): elb_policies.append(policy.policy_name) # Create any policies that don't exist yet for policy in policies.itervalues(): if policy['type'] == 'lb': policy['name'] = 'lb' + str(policy['cookie_expire']) elif policy['type'] == 'app': policy['name'] = 'app' + str(hash(policy['cookie_name'])) else: raise ValueError("Policy type '{0}' must be 'lb' or 'app'" .format(policy['name'])) if policy['name'] not in elb_policies: if not test: if policy['type'] == 'lb': elb.create_cookie_stickiness_policy( policy['cookie_expire'], policy['name']) elif policy['type'] == 'app': elb.create_app_cookie_stickiness_policy( policy['cookie_name'], policy['name']) for listener in elb.listeners: if listener.load_balancer_port in policies: port = listener.load_balancer_port policy = policies[port] if len(listener.policy_names) == 0: if not test: elb.set_policies_of_listener(port, [policy['name']]) changes['Port {0:d}'.format(port)] = "Added policy" else: if policy['name'] != listener.policy_names[0]: if not test: elb.set_policies_of_listener(port, [policy['name']]) changes['Port {0:d}'.format(port)] = "Changed policy" else: if len(listener.policy_names) > 0: if not test: elb.set_policies_of_listener(port, []) changes['Port {0:d}'.format(port)] = "Removed policy" return changes