Exemplo n.º 1
0
def insert_inpath_service(tenant_id, service_image_id, management_net_name,
                          northbound_net_name, southbound_net_name, *args):
    """Inserting a network service between two networks"""
    print("Creating Network for Services and Servers")
    service_logic = servlogcs.ServicesLogistics()
    net_list = {}
    multiport_net_list = []
    networks_name_list = [
        management_net_name, northbound_net_name, southbound_net_name
    ]
    client = Client(HOST, PORT, USE_SSL, format='json', tenant=tenant_id)
    for net in networks_name_list:
        data = {servconts.NETWORK: {servconts.NAME: net}}
        net_list[net] = client.create_network(data)
        net_list[net][servconts.PORTS] = []
        LOG.debug("Network %s Created with ID: %s " %
                  (net, net_list[net][servconts.NETWORK][servconts.ID]))
    print "Completed"
    print("Creating Ports on Services and Server Networks")
    LOG.debug("Operation 'create_port' executed.")
    if not service_logic.verify_plugin(const.UCS_PLUGIN):
        for net in networks_name_list:
            net_list[net][servconts.PORTS].append
            (client.create_port(
                net_list[net][servconts.NETWORK][servconts.ID]))
        LOG.debug("Operation 'create_port' executed.")
    else:
        for net in networks_name_list:
            nets = net_list[net][servconts.NETWORK][servconts.ID]
            multiport_net_list.append(nets)
        data = create_multiport(tenant_id, multiport_net_list)
        net_idx = 0
        for net in networks_name_list:
            port_id = data[servconts.PORTS][net_idx][servconts.ID]
            net_list[net][servconts.PORTS].append(port_id)
            LOG.debug("Port UUID: %s on network: %s" %
                      (data[servconts.PORTS][net_idx][servconts.ID], net))
            net_idx = net_idx + 1
    print "Completed"
    try:
        create_vm_args = []
        create_vm_args.append(servconts.CREATE_VM_CMD)
        create_vm_args.append(service_image_id)
        print("Creating VM with image: %s" % (service_image_id))
        process = subprocess.Popen(create_vm_args, stdout=subprocess.PIPE)
        result = process.stdout.readlines()
        tokens = re.search("i-[a-f0-9]*", str(result[1]))
        service_vm_name = tokens.group(0)
        print("Image: %s instantiated successfully" % (service_vm_name))

    except Exception as exc:
        print exc

    service_logic.image_status(service_vm_name)
    print "Completed"
    print "Attaching Ports To VM Service interfaces"
    try:
        idx = 0
        for net in networks_name_list:
            network_id = net_list[net][servconts.NETWORK][servconts.ID]
            port_id = net_list[net][servconts.PORTS][idx]
            attachment = client.show_port_attachment(network_id, port_id)
            attachment = attachment[servconts.ATTACHMENT][servconts.ID][:36]
            LOG.debug(("Plugging virtual interface: %s of VM %s"
                       "into port: %s on network: %s") %
                      (attachment, service_vm_name, port_id, net))
            attach_data = {
                servconts.ATTACHMENT: {
                    servconts.ID: '%s' % attachment
                }
            }
            client.attach_resource(network_id, port_id, attach_data)
    except Exception as exc:
        print exc
    print "Completed"
    try:
        LOG.debug("Registering Service in DB")
        l2db.initialize()
        for uuid_net in db.network_id(networks_name_list[0]):
            mngnet_id = str(uuid_net.uuid)
        for uuid_net in db.network_id(networks_name_list[1]):
            nbnet_id = str(uuid_net.uuid)
        for uuid_net in db.network_id(networks_name_list[2]):
            sbnet_id = str(uuid_net.uuid)
        sdb.add_services_binding(service_vm_name, mngnet_id, nbnet_id,
                                 sbnet_id)
    except Exception as exc:
        print exc
Exemplo n.º 2
0
def insert_inpath_service(tenant_id, service_image_id,
                          management_net_name, northbound_net_name,
                          southbound_net_name, *args):
    """Inserting a network service between two networks"""
    print ("Creating Network for Services and Servers")
    service_logic = servlogcs.ServicesLogistics()
    net_list = {}
    multiport_net_list = []
    networks_name_list = [management_net_name, northbound_net_name,
                          southbound_net_name]
    client = Client(HOST, PORT, USE_SSL, format='json', tenant=tenant_id)
    for net in networks_name_list:
        data = {servconts.NETWORK: {servconts.NAME: net}}
        net_list[net] = client.create_network(data)
        net_list[net][servconts.PORTS] = []
        LOG.debug("Network %s Created with ID: %s " % (
            net, net_list[net][servconts.NETWORK][servconts.ID]))
    print "Completed"
    print ("Creating Ports on Services and Server Networks")
    LOG.debug("Operation 'create_port' executed.")
    if not service_logic.verify_plugin(const.UCS_PLUGIN):
        for net in networks_name_list:
            net_list[net][servconts.PORTS].append
            (client.create_port
            (net_list[net][servconts.NETWORK][servconts.ID]))
        LOG.debug("Operation 'create_port' executed.")
    else:
        for net in networks_name_list:
            nets = net_list[net][servconts.NETWORK][servconts.ID]
            multiport_net_list.append(nets)
        data = create_multiport(tenant_id, multiport_net_list)
        net_idx = 0
        for net in networks_name_list:
            port_id = data[servconts.PORTS][net_idx][servconts.ID]
            net_list[net][servconts.PORTS].append(port_id)
            LOG.debug("Port UUID: %s on network: %s" %
                      (data[servconts.PORTS][net_idx][servconts.ID], net))
            net_idx = net_idx + 1
    print "Completed"
    try:
        create_vm_args = []
        create_vm_args.append(servconts.CREATE_VM_CMD)
        create_vm_args.append(service_image_id)
        print ("Creating VM with image: %s" % (service_image_id))
        process = utils.subprocess_popen(create_vm_args,
                                         stdout=subprocess.PIPE)
        result = process.stdout.readlines()
        tokens = re.search("i-[a-f0-9]*", str(result[1]))
        service_vm_name = tokens.group(0)
        print ("Image: %s instantiated successfully" % (service_vm_name))

    except Exception as exc:
        print exc

    service_logic.image_status(service_vm_name)
    print "Completed"
    print "Attaching Ports To VM Service interfaces"
    try:
        idx = 0
        for net in networks_name_list:
            network_id = net_list[net][servconts.NETWORK][servconts.ID]
            port_id = net_list[net][servconts.PORTS][idx]
            attachment = client.show_port_attachment(network_id, port_id)
            attachment = attachment[servconts.ATTACHMENT][servconts.ID][:36]
            LOG.debug(("Plugging virtual interface: %s of VM %s"
                       "into port: %s on network: %s") %
                      (attachment, service_vm_name, port_id, net))
            attach_data = {servconts.ATTACHMENT: {servconts.ID: '%s' %
                                                  attachment}}
            client.attach_resource(network_id, port_id, attach_data)
    except Exception as exc:
        print exc
    print "Completed"
    try:
        LOG.debug("Registering Service in DB")
        l2db.initialize()
        for uuid_net in db.network_id(networks_name_list[0]):
            mngnet_id = str(uuid_net.uuid)
        for uuid_net in db.network_id(networks_name_list[1]):
            nbnet_id = str(uuid_net.uuid)
        for uuid_net in db.network_id(networks_name_list[2]):
            sbnet_id = str(uuid_net.uuid)
        sdb.add_services_binding(service_vm_name, mngnet_id, nbnet_id,
                                 sbnet_id)
    except Exception as exc:
        print exc