Ejemplo n.º 1
0
def build_transport_zone(dir, context, type='transport_zone', alternate_template=None):

    # if found the matching transport zone (either given or default), return
    if check_transport_zone(context):
        return

    # Transport zone does not exist, lets create
    transport_zone_name = context['nsxmanager'].get('transport_zone')
    if not transport_zone_name:
        transport_zone_name = context['name'] + '-tz'

    transport_zone = {}
    transport_zone['name'] = transport_zone_name
    transport_zone['cluster_names'] = context['nsxmanager'].get('transport_zone_clusters').strip()
    if transport_zone['cluster_names'] == '':
        raise Exception('Error! No cluster members specified to create the Transport Zone...!!')
    
    cluster_ids = ''
    for cluster_name in transport_zone['cluster_names'].split(','):
        cluster_id = mobclient.lookup_moid(cluster_name.strip())
        if 'domain' in cluster_id:
            cluster_ids += cluster_id + ','

    cluster_ids = cluster_ids.strip(',')
    if cluster_ids == '':
        raise Exception('Error! No matching cluster members found to create the Transport Zone...!!')
    
    transport_zone['cluster_ids'] = cluster_ids
    tz_context = {
        'context': context,
        'transport_zone': transport_zone,
        'files': []
    }    

    transport_zones_dir = os.path.realpath(os.path.join(dir ))
    if os.path.isdir(transport_zones_dir):
        shutil.rmtree(transport_zones_dir)
    mkdir_p(transport_zones_dir)

    template_dir = '.'
    if alternate_template is not None:
        template_dir = os.path.join(template_dir, alternate_template)

    template.render(
        os.path.join(transport_zones_dir, transport_zone['name'] + '_payload.xml'),
        os.path.join(template_dir, 'vdn_scope_post_payload.xml' ),
        tz_context
    )

    post_response = client.post_xml(NSX_URLS['scope']['all'], 
            os.path.join(transport_zones_dir, transport_zone['name'] + '_payload.xml'))
    data = post_response.text
    if DEBUG:
        print('Transport Zone creation response:{}\n'.format(data))

    if post_response.status_code < 400: 
        print('Created Transport Zone : {}\n'.format(transport_zone['name']))
        context['nsxmanager']['transport_zone'] = transport_zone['name']
        context['nsxmanager']['transport_zone_id'] = data
Ejemplo n.º 2
0
def build_nsx_edge_gateways(dir, context, alternate_template=None):
    nsx_edges_dir = os.path.realpath(os.path.join(dir ))
    
    if os.path.isdir(nsx_edges_dir):
        shutil.rmtree(nsx_edges_dir)
    mkdir_p(nsx_edges_dir)

    template_dir = '.'
    if alternate_template is not None:
        template_dir = os.path.join(template_dir, alternate_template)

    logical_switches = context['logical_switches']
    map_logical_switches_id(logical_switches)
    if DEBUG:
        print('Logical Switches:{}\n'.format(str(logical_switches)))

    empty_logical_switches = xrange(len(logical_switches) + 1, 10) 
    vcenterMobMap = refresh_moid_map(context)
    vm_network_moid = mobclient.lookup_moid('VM Network')

    # Go with the VM Network for default uplink
    nsxmanager = context['nsxmanager']
    bosh_nsx_enabled = nsxmanager['bosh_nsx_enabled']

    uplink_port_switch = nsxmanager['uplink_details'].get('uplink_port_switch')
    if uplink_port_switch is None:
        uplink_port_switch = 'VM Network'
        nsxmanager['uplink_details']['uplink_port_switch'] = uplink_port_switch
        
    # if use_port_switch is set to 'VM Network' or port switch id could not be retreived.
    portSwitchId = mobclient.lookup_moid(uplink_port_switch) 
    if (portSwitchId is None):
        nsxmanager['uplink_details']['uplink_id'] = vm_network_moid
    else:
        nsxmanager['uplink_details']['uplink_id'] = portSwitchId
    
    for nsx_edge in  context['edge_service_gateways']:
    
        # Defaults routed components
        # FIX ME -- would have to update this 
        # for any new component that needs direct route via firewall
        opsmgr_routed_component = nsx_edge['routed_components'][0]
        ert_routed_component    = nsx_edge['routed_components'][1]
        diego_routed_component  = nsx_edge['routed_components'][2]
        tcp_routed_component    = nsx_edge['routed_components'][3]

        isozone_routed_components = []
        iso_zones = []


        for routed_component in nsx_edge['routed_components']:
            routed_component_name_upper = routed_component['name'].upper()

            if 'ISOZONE' in routed_component_name_upper:
                isozone_routed_components.append(routed_component)
                iso_zone = { 'name' : routed_component['switch']['given_name'] }

                if iso_zone not in iso_zones:
                    iso_zones.append(iso_zone)
            else:
                if 'OPS' in routed_component_name_upper:
                    opsmgr_routed_component = routed_component
                elif 'GO-ROUTER' in routed_component_name_upper:
                    ert_routed_component = routed_component
                elif 'DIEGO' in routed_component_name_upper:
                    diego_routed_component = routed_component
                elif 'TCP-ROUTER' in routed_component_name_upper:
                    tcp_routed_component = routed_component

        nsx_edge['iso_zones'] = iso_zones

        ertLogicalSwitch = {}
        infraLogicalSwitch = {}
        ospfLogicalSwitch = {}

        for name, lswitch in nsx_edge['global_switches'].iteritems():
            switch_name_upper = name.upper()
            if 'ERT' in switch_name_upper:
                ertLogicalSwitch = lswitch
            elif 'INFRA' in switch_name_upper:
                infraLogicalSwitch = lswitch
            elif 'OSPF' in switch_name_upper:
                ospfLogicalSwitch = lswitch             

        nsx_edge['bosh_nsx_enabled'] = bosh_nsx_enabled

        vcenter_ctx = context['vcenter']

        nsx_edge['datacenter_id'] = mobclient.lookup_moid(vcenter_ctx['datacenter']) 

        # Use the cluster name/id for resource pool...
        nsx_edge['datastore_id'] = mobclient.lookup_moid(vcenter_ctx['datastore']) 
        nsx_edge['cluster_id'] = mobclient.lookup_moid(vcenter_ctx['cluster'])   
        nsx_edge['resourcePool_id'] = mobclient.lookup_moid(vcenter_ctx['cluster'])
        
        # TODO: Ignore the vm folder for now...
        #nsx_edge['vmFolder_id'] = mobclient.lookup_moid(vcenter_ctx['vmFolder']) 

        # Get a large cidr (like 16) that would allow all networks to talk to each other
        cross_network_cidr = calculate_cross_network_cidr(infraLogicalSwitch)
        
        gateway_address = nsx_edge.get('gateway_ip')
        if not gateway_address:
            gateway_address = calculate_gateway(context['nsxmanager']['uplink_details']['uplink_ip'])   

        firewall_src_network_list = logical_switches
        firewall_destn_network_list = logical_switches

        cross_logical_network_combo = cross_combine_lists(firewall_src_network_list, firewall_destn_network_list)       

        if DEBUG:
            print('NSX Edge config: {}\n'.format(str(nsx_edge)))   

        nsx_edges_context = {
            'context': context,
            'defaults': context['defaults'],
            'nsxmanager': context['nsxmanager'],
            'static_routes': context['nsxmanager']['static_routes'],
            'edge': nsx_edge,
            'enable_dlr': nsx_edge['enable_dlr'],
            'logical_switches': logical_switches,
            'empty_logical_switches': empty_logical_switches,
            'global_switches': nsx_edge['global_switches'],
            'ospfLogicalSwitch': ospfLogicalSwitch,
            'infraLogicalSwitch': infraLogicalSwitch,
            'ertLogicalSwitch': ertLogicalSwitch,
            'routed_components':  nsx_edge['routed_components'],
            'opsmgr_routed_component': opsmgr_routed_component,
            'ert_routed_component': ert_routed_component,
            'diego_routed_component': diego_routed_component,
            'tcp_routed_component': tcp_routed_component,
            'isozone_routed_components': isozone_routed_components,
            'cross_network_cidr': cross_network_cidr,
            'cross_logical_network_combo': cross_logical_network_combo,
            'gateway_address': gateway_address,
            'files': []
        }    

        template.render(
            os.path.join(nsx_edges_dir, nsx_edge['name'] + '_post_payload.xml'),
            os.path.join(template_dir, 'edge_config_post_payload.xml' ),
            nsx_edges_context
        )
        
        """
        if True:
        """
        print('Creating NSX Edge instance: {}\n\n'.format(nsx_edge['name']))
        post_response = client.post_xml(NSX_URLS['esg']['all'] , 
                                os.path.join(nsx_edges_dir, nsx_edge['name'] + '_post_payload.xml'), 
                                check=False)
        data = post_response.text
        
        if post_response.status_code < 400: 
            print('Success!! Created NSX Edge :{}\n'.format(nsx_edge['name']))
            add_ert_certs_to_nsx_edge(nsx_edges_dir, nsx_edge)
            add_iso_certs_to_nsx_edge(nsx_edges_dir, nsx_edge)
            print('Finished adding certs to NSX Edge :{}!!\n'.format(nsx_edge['name']))

            print('Updating LBR config!!')
            add_lbr_to_nsx_edge(nsx_edges_dir, nsx_edge)
            print('Success!! Finished complete creation of NSX Edge instance: {}\n\n'.format(nsx_edge['name']))
        else:
            print('Creation of NSX Edge failed, details:\n{}\n'.format(data))
            raise Exception('Creation of NSX Edge failed, details:\n {}'.format(data))          
Ejemplo n.º 3
0
def build_nsx_dlrs(dir, context, alternate_template=None):
    nsx_dlrs_dir = os.path.realpath(os.path.join(dir ))
    
    if os.path.isdir(nsx_dlrs_dir):
        shutil.rmtree(nsx_dlrs_dir)
    mkdir_p(nsx_dlrs_dir)

    template_dir = '.'
    if alternate_template is not None:
        template_dir = os.path.join(template_dir, alternate_template)

    logical_switches = context['logical_switches']
    map_logical_switches_id(logical_switches)
    if DEBUG:
        print('Logical Switches:{}\n'.format(str(logical_switches)))

    empty_logical_switches = xrange(len(logical_switches) + 1, 10) 
    vcenterMobMap = refresh_moid_map(context)
    vm_network_moid = mobclient.lookup_moid('VM Network')

    # Go with the VM Network for default uplink
    nsxmanager = context['nsxmanager']
    enable_dlr = nsxmanager['enable_dlr']
    if enable_dlr:
        nsxmanager['distributed_portgroup_id'] =  mobclient.lookup_moid(nsxmanager['distributed_portgroup'])

    uplink_port_switch = nsxmanager['uplink_details'].get('uplink_port_switch')
    if uplink_port_switch is None:
        uplink_port_switch = 'VM Network'
        nsxmanager['uplink_details']['uplink_port_switch'] = uplink_port_switch
    
    # if use_port_switch is set to 'VM Network' or port switch id could not be retreived.
    portSwitchId = mobclient.lookup_moid(uplink_port_switch) 
    if (portSwitchId is None):
        #nsxmanager['uplink_details']['uplink_id'] = vm_network_moid
        raise Exception('Error! Uplink Port Group not defined...!!')
    
    nsxmanager['uplink_details']['uplink_id'] = portSwitchId

    dlr_instances = []
    
    for nsx_edge in  context['edge_service_gateways']:
    
        enable_dlr = nsx_edge['enable_dlr']
        if not enable_dlr:
            print('DLR disabled!! Not creating DLR for NSX Edge: ' + nsx_edge['name'])
            continue

        nsx_dlr = copy.deepcopy(nsx_edge)

        vcenter_ctx = context['vcenter']

        nsx_dlr['name'] = nsx_dlr['name'] + '-dlr'
        print('Name of DLR: ' + nsx_dlr['name'])
        nsx_dlr['datacenter_id'] = mobclient.lookup_moid(vcenter_ctx['datacenter']) 

        # Use the cluster name/id for resource pool...
        nsx_dlr['datastore_id'] = mobclient.lookup_moid(vcenter_ctx['datastore']) 
        nsx_dlr['cluster_id'] = mobclient.lookup_moid(vcenter_ctx['cluster'])   
        nsx_dlr['resourcePool_id'] = mobclient.lookup_moid(vcenter_ctx['cluster'])

        
        gateway_address = nsx_dlr.get('gateway_ip')
        if not gateway_address:
            gateway_address = calculate_gateway(context['nsxmanager']['uplink_details']['uplink_ip'])
            nsx_dlr['gateway_ip'] = gateway_address 

        nsx_dlrs_context = {
            'context': context,
            'defaults': context['defaults'],
            'nsxmanager': context['nsxmanager'],
            'dlr': nsx_dlr,
            'logical_switches': logical_switches,
            'empty_logical_switches': empty_logical_switches,
            'gateway_address': gateway_address,
            'files': []
        }    

        template.render(
            os.path.join(nsx_dlrs_dir, nsx_dlr['name'] + '_dlr_post_payload.xml'),
            os.path.join(template_dir, 'dlr_config_post_payload.xml' ),
            nsx_dlrs_context
        )
        
        print('Creating NSX DLR instance: {}\n\n'.format(nsx_dlr['name']))

        post_response = client.post_xml(NSX_URLS['esg']['all'] , 
                                os.path.join(nsx_dlrs_dir, nsx_dlr['name'] + '_dlr_post_payload.xml'), 
                                check=False)
        data = post_response.text
        
        if post_response.status_code < 400: 
            print('Created NSX DLR :{}\n'.format(nsx_dlr['name']))
            print('Success!! Finished creation of NSX DLR instance: {}\n\n'.format(nsx_dlr['name']))
            add_ospf_to_nsx_dlr(nsx_dlrs_dir, context, nsx_dlr)
            print('Success!! Finished adding OSPF & Interfaces for NSX DLR instance: {}\n\n'.format(nsx_dlr['name']))
        else:
            print('Creation of NSX DLR failed, details:\n{}\n'.format(data))
            raise Exception('Creation of NSX DLR failed, details:\n {}'.format(data))           

        dlr_instances.append(nsx_dlr)

    context['nsx_dlrs'] = dlr_instances