def scale_down(config, instance_id): """Remove one instance from the environment and put it to the pool of stopped machines""" logger = logging.getLogger(__name__) if not scaling_down_possible(config): logger.warning("Scaling down not possible, minimum number of instances is running") else: logger.info("Downscaling started.") # Deregister it from load balancer elb = aws_ec2_elb.get_load_balancer(config.get("region"), config.get("elb_name")) elb.deregister_instances([instance_id]) # stop it time.sleep(static.AUTOSCALE_DELAY_BEFORE_REMOVING_INSTANCE) # let instance respond to pending requests instance = aws_ec2.get_instance(config, instance_id) instance.stop() # Update config try: instances = config.get_list("stopped_instances") except ConfigParser.NoOptionError: instances = [] instances.append(instance_id) config.set_list("stopped_instances", instances) instances = config.get_list("instances") instances.remove(instance_id) config.set_list("instances", instances) logger.info("Downscaling completed. 1 instance stopped and is available in the pool.") if len(instances) > 1: logger.info("%d instances are currently on duty." % len(instances)) else: logger.info("1 instance is currently on duty.")
def process_instance_state(config, state): if state.instance_id in config.get_list('instances') and \ not state.state == 'InService' and \ not state.reason_code == 'Instance registration is still in progress.' and \ not state.reason_code == 'ELB': id = state.instance_id if unhealthy_instances.has_key(id): num_checks = unhealthy_instances.get(id) if num_checks + 1 == static.HEALTH_CHECKS: logger.info('Stopping unhealty instance %s for investigation' %id) unhealthy_instances.pop(id) #Instance is considered failed and should be stopped for investigation #Deregister it from load balancer elb = aws_ec2_elb.get_load_balancer(config.get('region'),config.get('elb_name')) elb.deregister_instances([id]) #stop it instance = aws_ec2.get_instance(config,id) instance.stop() #Update config instances = config.get_list('instances') instances.remove(id) config.set_list('instances',instances) logger.info('Unhealthy instance is successfully stopped. ') logger.info('A new instance instead of stopped one will be allocated automatically.') else: unhealthy_instances[id] = num_checks + 1 else: unhealthy_instances[id] = 1 logger.warning('Instance %s is unhealthy with state %s and code %s' %(id, state.state, state.reason_code)) logger.warning('After %s checks it will be stopped.' % static.HEALTH_CHECKS)
def get_monitoring_information_from_start(config_id,instanceID,metric_q,statistic,unit): config = Config(config_id) cw = boto.ec2.cloudwatch.connect_to_region(config.get('region')) metrics = cw.list_metrics(dimensions={'InstanceId': instanceID}, metric_name=metric_q) if metrics: inst = aws_ec2.get_instance(config, instanceID) end_time = datetime.datetime.utcnow() start_time=boto.utils.parse_ts(inst.launch_time) #get nr of seconds diff = end_time - start_time seconds = diff.total_seconds() #adjust the period period=get_adjusted_period(seconds) result = metrics[0].query(start_time,end_time,statistic,unit,period) return sorted(result, key = operator.itemgetter(u'Timestamp'))
def scale_up_from_pool(config): """Scale up an instance from an available pool of previously stopped instances""" logger = logging.getLogger(__name__) # Launch instance logger.info("Upscaling from the pool started.") stopped_instance_id = config.get_list("stopped_instances")[-1] instance = aws_ec2.get_instance(config, stopped_instance_id) instance.start() time.sleep(static.AUTOSCALE_DELAY_AFTER_START) # Start httpd service # Do not send instance directly as it does not have public DNS attached now, need to get it again from EC2 aws_ec2.start_httpd(config, instance.id) # Register instance at load balancer logger.info("Registering an instance at the load balancer") elb = aws_ec2_elb.get_load_balancer(config.get("region"), config.get("elb_name")) elb.register_instances([instance.id]) # Update config instances = config.get_list("stopped_instances") instances.remove(instance.id) config.set_list("stopped_instances", instances) instances = config.get_list("instances") instances.append(instance.id) config.set_list("instances", instances)
def test(): config = Config('100') instance_id = 'i-3029b47b' instance = aws_ec2.get_instance(config,instance_id) pprint(vars(instance))