Exemplo n.º 1
0
def scalein(vmware_settings, *args):
    """
    Deletes an instance from vmware and removes it from the Pool
    :param vmware_settings: dictionary of vmware settings keys [vmware_access_key_id,
    vmware_secret_access_key, ec2_region, security_group_ids, instance_type,
    image_id]
    :param args: The args passed down as part of the alert.
    """
    autoscale_dump(*args)
    alert_info = json.loads(args[1])
    # Perform actual scaleout
    pool_name, pool_uuid, pool_obj, num_autoscale = \
        scaleout_params('scalein', alert_info)
    print (pool_name, ':', pool_uuid, ' num_scaleout', num_autoscale)
    scalein_server = pool_obj['servers'][-1]
    instance_ids = [scalein_server['hostname']]
    pool_obj['servers'] = pool_obj['servers'][:-1]
    # call controller API to update the pool
    print 'new pool obj', pool_obj
    api = getAviApiSession()
    resp = api.put('pool/%s' % pool_uuid, data=json.dumps(pool_obj))
    print 'updated pool', pool_obj['name'], resp.status_code
    if resp.status_code in (200, 201, 204):
        print 'deleting the instance from the vmware - ', instance_ids
        delete_vmware_instance(vmware_settings, instance_ids)
Exemplo n.º 2
0
def scalein(aws_settings, *args):
    """
    Deletes an instance from AWS and removes it from the Pool
    :param aws_settings: dictionary of aws settings keys [aws_access_key_id,
    aws_secret_access_key, ec2_region, security_group_ids, instance_type,
    image_id]
    :param args: The args passed down as part of the alert.
    """
    tenant = aws_settings.get('tenant', 'admin')
    api = getAviApiSession(tenant)
    autoscale_dump(*args)
    alert_info = json.loads(args[1])
    # Perform actual scaleout
    pool_name, pool_uuid, pool_obj, num_autoscale = \
        scaleout_params('scalein', alert_info, api=api, tenant=tenant)
    print((pool_name, ':', pool_uuid, ' num_scaleout', num_autoscale))
    scalein_server = pool_obj['servers'][-1]
    autoscaling_group = get_autoscaling_group(api, pool_obj)
    try:
        instance_ids = [scalein_server['external_uuid']]
    except KeyError:
        vm_ref = scalein_server['vm_ref']
        # https://10.130.129.34/api/vimgrvmruntime/i-08ddf0d2
        vm_uuid = vm_ref.split('/api/vimgrvmruntime/')[1].split('#')[0]
        instance_ids = [vm_uuid]
    pool_obj['servers'] = pool_obj['servers'][:-1]
    # call controller API to update the pool
    print('pool %s scalein server %s' % (pool_name, scalein_server))
    api = getAviApiSession()
    resp = api.put('pool/%s' % pool_uuid, tenant=tenant, data=pool_obj)
    print('updated pool', pool_obj['name'], resp.status_code)
    if resp.status_code in (200, 201, 204):
        print('deleting the instance from the aws - ', instance_ids)
        delete_aws_autoscaling_instance(
            aws_settings, autoscaling_group, instance_ids)
Exemplo n.º 3
0
def scaleout(vmware_settings, *args):
    """
    1. Creates an instance in vmware
    2. Registers that instance as a Pool Member
    :param vmware_settings: dictionary of vmware settings keys [vmware_access_key_id,
    vmware_secret_access_key, ec2_region, security_group_ids, instance_type,
    image_id]
    :param args: The args passed down as part of the alert.
    """
    # print all the args passed down
    autoscale_dump(*args)
    alert_info = json.loads(args[1])
    # Perform actual scaleout
    pool_name, pool_uuid, pool_obj, num_scaleout = \
        scaleout_params('scaleout', alert_info)
    # create vmware instance using these two ids.
    print pool_name, 'scaleout', num_scaleout
    hostname, ip_addr = create_vmware_instance(vmware_settings, pool_name)
    if not (hostname or ip_addr):
        print 'not performing scaleout as could not create vm'
    new_server = {
        'ip': {'addr': ip_addr, 'type': 'V4'},
        'port': 0,
        'hostname': hostname,
    }
    # add new server to the pool
    pool_obj['servers'].append(new_server)
    # call controller API to update the pool
    print 'new pool obj', pool_obj
    api = getAviApiSession()
    resp = api.put('pool/%s' % pool_uuid, data=json.dumps(pool_obj))
    print 'updated pool', pool_obj['name'], resp.status_code
Exemplo n.º 4
0
def scalein(aws_settings, *args):
    """
    Deletes an instance from AWS and removes it from the Pool
    :param aws_settings: dictionary of aws settings keys [aws_access_key_id,
    aws_secret_access_key, ec2_region, security_group_ids, instance_type,
    image_id]
    :param args: The args passed down as part of the alert.
    """
    tenant = aws_settings.get('tenant', 'admin')
    api = getAviApiSession(tenant)
    autoscale_dump(*args)
    alert_info = json.loads(args[1])
    # Perform actual scaleout
    pool_name, pool_uuid, pool_obj, num_autoscale = \
        scaleout_params('scalein', alert_info, api=api, tenant=tenant)
    print((pool_name, ':', pool_uuid, ' num_scaleout', num_autoscale))
    scalein_server = pool_obj['servers'][-1]
    try:
        instance_ids = [scalein_server['external_uuid']]
    except KeyError:
        vm_ref = scalein_server['vm_ref']
        # https://10.130.129.34/api/vimgrvmruntime/i-08ddf0d2
        vm_uuid = vm_ref.split('/api/vimgrvmruntime/')[1].split('#')[0]
        instance_ids = [vm_uuid]
    pool_obj['servers'] = pool_obj['servers'][:-1]
    # call controller API to update the pool
    print('pool %s scalein server %s' % (pool_name, scalein_server))
    api = getAviApiSession()
    resp = api.put('pool/%s' % pool_uuid,
                   tenant=tenant,
                   data=json.dumps(pool_obj))
    print('updated pool', pool_obj['name'], resp.status_code)
    if resp.status_code in (200, 201, 204):
        print('deleting the instance from the aws - ', instance_ids)
        delete_aws_instance(aws_settings, instance_ids)
Exemplo n.º 5
0
def scale_out(vmware_settings, *args):
    alert_info = json.loads(args[1])
    api, tenant = getAviApiSession()
    (pool_name, pool_uuid, pool_obj,
     num_scaleout) = scaleout_params('scaleout',
                                     alert_info,
                                     api=api,
                                     tenant=tenant)
    print('Scaling out pool %s by %d...' % (pool_name, num_scaleout))

    new_instances = do_scale_out(vmware_settings, pool_name, num_scaleout)

    # Get pool object again in case it has been modified
    pool_obj = api.get('pool/%s' % pool_uuid, tenant=tenant).json()

    new_servers = pool_obj.get('servers', [])

    for new_instance in new_instances:
        new_server = {
            'ip': {
                'addr': new_instance[1],
                'type': 'V4'
            },
            'hostname': new_instance[0]
        }
        new_servers.append(new_server)
    pool_obj['servers'] = new_servers
    print('Updating pool with %s' % new_server)
    resp = api.put('pool/%s' % pool_uuid, data=json.dumps(pool_obj))
    print('API status: %d' % resp.status_code)
Exemplo n.º 6
0
def scalein(aws_settings, *args):
    """
    Deletes an instance from AWS and removes it from the Pool
    :param aws_settings: dictionary of aws settings keys [aws_access_key_id,
    aws_secret_access_key, ec2_region, security_group_ids, instance_type,
    image_id]
    :param args: The args passed down as part of the alert.
    """
    autoscale_dump(*args)
    alert_info = json.loads(args[1])
    # Perform actual scaleout
    pool_name, pool_uuid, pool_obj, num_autoscale = \
        scaleout_params('scalein', alert_info)
    print(pool_name, ':', pool_uuid, ' num_scaleout', num_autoscale)
    scalein_server = pool_obj['servers'][-1]
    instance_ids = [scalein_server['external_uuid']]
    pool_obj['servers'] = pool_obj['servers'][:-1]
    # call controller API to update the pool
    print 'new pool obj', pool_obj
    api = getAviApiSession()
    resp = api.put('pool/%s' % pool_uuid, data=json.dumps(pool_obj))
    print 'updated pool', pool_obj['name'], resp.status_code
    if resp.status_code in (200, 201, 204):
        print 'deleting the instance from the aws - ', instance_ids
        delete_aws_instance(aws_settings, instance_ids)
Exemplo n.º 7
0
def scaleout(aws_settings, *args):
    """
    1. Creates an instance in AWS
    2. Registers that instance as a Pool Member
    :param aws_settings: dictionary of aws settings keys [aws_access_key_id,
    aws_secret_access_key, ec2_region, security_group_ids, instance_type,
    image_id]
    :param args: The args passed down as part of the alert.
    """
    # print all the args passed down
    autoscale_dump(*args)
    alert_info = json.loads(args[1])
    # Perform actual scaleout
    pool_name, pool_uuid, pool_obj, num_scaleout = \
        scaleout_params('scaleout', alert_info)
    # create AWS instance using these two ids.
    print pool_name, 'scaleout', num_scaleout
    insid, ip_addr, hostname = create_aws_instance(aws_settings)
    new_server = {
        'ip': {
            'addr': ip_addr,
            'type': 'V4'
        },
        'port': 0,
        'hostname': hostname,
        'external_uuid': insid
    }
    # add new server to the pool
    pool_obj['servers'].append(new_server)
    # call controller API to update the pool
    print 'new pool obj', pool_obj
    api = getAviApiSession()
    resp = api.put('pool/%s' % pool_uuid, data=json.dumps(pool_obj))
    print 'updated pool', pool_obj['name'], resp.status_code
Exemplo n.º 8
0
def scale_in(vmware_settings, *args):
    alert_info = json.loads(args[1])
    api, tenant = getAviApiSession()
    (pool_name, pool_uuid, pool_obj,
     num_scaleout) = scaleout_params('scalein',
                                     alert_info,
                                     api=api,
                                     tenant=tenant)

    remove_instances = [
        instance['hostname']
        for instance in pool_obj['servers'][-num_scaleout:]
    ]
    pool_obj['servers'] = pool_obj['servers'][:-num_scaleout]
    print('Scaling in pool %s by %d...' % (pool_name, num_scaleout))
    resp = api.put('pool/%s' % pool_uuid, data=json.dumps(pool_obj))
    print('API status: %d' % resp.status_code)

    do_scale_in(vmware_settings, remove_instances)
Exemplo n.º 9
0
def scaleout(aws_settings, *args):
    """
    1. Creates an instance in AWS
    2. Registers that instance as a Pool Member
    :param aws_settings: dictionary of aws settings keys [aws_access_key_id,
    aws_secret_access_key, ec2_region, security_group_ids, instance_type,
    image_id]
    :param args: The args passed down as part of the alert.
    """
    # print all the args passed down
    tenant = aws_settings.get('tenant', 'admin')
    api = getAviApiSession(tenant)
    autoscale_dump(*args)
    alert_info = json.loads(args[1])
    # Perform actual scaleout
    pool_name, pool_uuid, pool_obj, num_scaleout = \
        scaleout_params('scaleout', alert_info, api=api, tenant=tenant)
    # create AWS instance using these two ids.
    print(pool_name, 'scaleout', num_scaleout)
    autoscaling_group = get_autoscaling_group(api, pool_obj)
    desired_capacity = len(pool_obj['servers']) + num_scaleout
    new_instances = aws_autoscaling_scaleout(
        aws_settings, pool_obj, autoscaling_group, desired_capacity,
        num_scaleout)
    for instance in new_instances:
        insid, ip_addr, hostname = instance
        new_server = {
            'ip': {'addr': ip_addr, 'type': 'V4'},
            'port': 0,
            'hostname': hostname,
            'external_uuid': insid,
            'vm_uuid': insid,
        }
        # add new server to the pool
        pool_obj['servers'].append(new_server)
    # call controller API to update the pool
    print('new pool obj', pool_obj)
    api = getAviApiSession()
    resp = api.put('pool/%s' % pool_uuid, tenant=tenant,
                   data=json.dumps(pool_obj))
    print('updated pool', pool_obj['name'], resp.status_code)
Exemplo n.º 10
0
def scaleout(vmware_settings, *args):
    """
    1. Creates an instance in vmware
    2. Registers that instance as a Pool Member
    :param vmware_settings: dictionary of vmware settings keys
        [vmware_access_key_id, vmware_secret_access_key, ec2_region,
        security_group_ids, instance_type, image_id]
    :param args: The args passed down as part of the alert.
    """
    # print all the args passed down
    autoscale_dump(*args)
    alert_info = json.loads(args[1])
    # Perform actual scaleout
    api = getAviApiSession()
    pool_name, pool_uuid, pool_obj, num_scaleout = \
        scaleout_params('scaleout', alert_info, api=api)
    # create vmware instance using these two ids.
    print(pool_name, 'scaleout', num_scaleout)
    hostname, ip_addr = create_vmware_instance(vmware_settings, pool_name)
    if not (hostname or ip_addr):
        print('not performing scaleout as could not create vm')
    new_server = {
        'ip': {
            'addr': ip_addr,
            'type': 'V4'
        },
        'port': 0,
        'hostname': hostname,
    }
    # refresh the pool object
    pool_obj = api.get('pool/%s' % pool_uuid).json()
    # add new server to the pool
    pool_obj['servers'].append(new_server)
    # call controller API to update the pool
    print('new pool obj', pool_obj)
    resp = api.put('pool/%s' % pool_uuid, data=json.dumps(pool_obj))
    print(
        'updated pool %s num servers %s rcode %s' %
        (pool_obj['name'], len(pool_obj.get('servers', [])), resp.status_code))