Пример #1
0
def new(request):
    logger.debug('---- topology new ----')

    script_list = Script.objects.all().order_by('name')
    vm_types = configuration.vm_image_types
    vm_types_string = json.dumps(vm_types)

    currently_allocated_ips = wistarUtils.get_used_ips()
    dhcp_reservations = wistarUtils.get_consumed_management_ips()

    if configuration.deployment_backend == "openstack":
        external_bridge = configuration.openstack_external_network
        image_list = Image.objects.filter(filePath='').order_by('name')
    else:
        external_bridge = configuration.kvm_external_bridge
        image_list = Image.objects.exclude(filePath='').order_by('name')

    image_list_json = serializers.serialize('json',
                                            image_list,
                                            fields=('name', 'type'))

    context = {
        'image_list': image_list,
        'script_list': script_list,
        'vm_types': vm_types_string,
        'image_list_json': image_list_json,
        'external_bridge': external_bridge,
        'allocated_ips': currently_allocated_ips,
        'dhcp_reservations': dhcp_reservations
    }
    return render(request, 'topologies/new.html', context)
Пример #2
0
def add_instance_form(request):
    logger.info('---------add_instance_form--------')

    image_list = Image.objects.all().order_by('name')
    script_list = Script.objects.all().order_by('name')
    vm_types = configuration.vm_image_types
    vm_types_string = json.dumps(vm_types)

    image_list_json = serializers.serialize('json',
                                            Image.objects.all(),
                                            fields=('name', 'type'))

    currently_allocated_ips = wistarUtils.get_used_ips()
    dhcp_reservations = wistarUtils.get_consumed_management_ips()

    if configuration.deployment_backend == "openstack":
        external_bridge = configuration.openstack_external_network
    else:
        external_bridge = configuration.kvm_external_bridge

    context = {
        'image_list': image_list,
        'script_list': script_list,
        'vm_types': vm_types_string,
        'image_list_json': image_list_json,
        'external_bridge': external_bridge,
        'allocated_ips': currently_allocated_ips,
        'dhcp_reservations': dhcp_reservations,
    }
    return render(request, 'topologies/overlay/add_instance.html', context)
Пример #3
0
def get_available_ip(request):
    # just grab the next available IP that is not currently
    # reserved via DHCP. This only get's called from topologies/new.html
    # when we've allocated all the IPs to various topologies
    # this allows new topologies to be built with overlapping
    # IP addresses. This makes the attempt to use 'old' ips that
    # are at least not still in use.
    logger.info("getting ips that are currently reserved via DHCP")
    all_used_ips = wistarUtils.get_consumed_management_ips()
    logger.debug(all_used_ips)
    next_ip = wistarUtils.get_next_ip(all_used_ips, 2)
    logger.debug(next_ip)
    response_data = {"result": next_ip}
    return HttpResponse(json.dumps(response_data),
                        content_type="application/json")
Пример #4
0
def clone(request, topo_id):
    logger.debug('---- topology clone ----')
    topology = get_object_or_404(Topology, pk=topo_id)
    orig_name = topology.name
    topology.name = orig_name + " clone"
    topology.json = wistarUtils.clone_topology(topology.json)
    topology.id = 0
    image_list = Image.objects.all().order_by('name')
    script_list = Script.objects.all().order_by('name')
    vm_types = configuration.vm_image_types
    vm_types_string = json.dumps(vm_types)

    image_list_json = serializers.serialize('json',
                                            Image.objects.all(),
                                            fields=('name', 'type'))

    # also grab all the ips from the currently cloned topology
    currently_allocated_ips = wistarUtils.get_used_ips()
    cloned_ips = wistarUtils.get_used_ips_from_topology_json(topology.json)

    dhcp_reservations = wistarUtils.get_consumed_management_ips()

    currently_allocated_ips += cloned_ips

    currently_allocated_ips.sort()

    if configuration.deployment_backend == "openstack":
        external_bridge = configuration.openstack_external_network
    else:
        external_bridge = configuration.kvm_external_bridge

    context = {
        'image_list': image_list,
        'script_list': script_list,
        'vm_types': vm_types_string,
        'image_list_json': image_list_json,
        'external_bridge': external_bridge,
        'allocated_ips': currently_allocated_ips,
        'dhcp_reservations': dhcp_reservations,
        'topo': topology
    }

    return render(request, 'topologies/new.html', context)
Пример #5
0
def import_topology(request):
    logger.debug('---- topology import ----')
    try:
        if request.method == "POST":
            logger.debug(str(request.FILES))

            json_file = request.FILES['file']
            logger.debug(str(json_file))
            json_string = json_file.read()
            json_data = json.loads(json_string)

            topology = Topology()
            topology.name = "Imported Topology"
            topology.id = 0

            # keep track of all the ips that are currently used
            # we will import this topology and ensure that any assigned ips are unique for this environment
            currently_allocated_ips = wistarUtils.get_used_ips()
            next_ip_floor = 2
            logger.debug("Iterating json objects in imported data")
            for json_object in json_data:
                if "userData" in json_object and "wistarVm" in json_object[
                        "userData"]:
                    # logger.debug("Found one")
                    ud = json_object["userData"]

                    # Tries to import with the same image index and type
                    # If it doesn't exist, we'll use the closest type
                    image_id = wistarUtils.get_same_or_similar_image(
                        ud["image"], ud["type"])

                    if image_id == None:
                        # Failed to find the same image or even one with a similar type
                        logger.error("Could not find image of type " +
                                     ud["type"])
                        return error(
                            request, 'Could not find a valid image of type ' +
                            ud['type'] +
                            '! Please upload an image of this type and try again'
                        )
                    else:
                        # Either we found a suitable one or even the same one
                        json_object["userData"]["image"] = image_id

                    valid_ip = wistarUtils.get_next_ip(currently_allocated_ips,
                                                       next_ip_floor)
                    next_ip_floor = valid_ip

                    json_object["userData"][
                        "ip"] = configuration.management_prefix + str(valid_ip)

                elif json_object["type"] == "wistar.info":
                    topology.name = json_object["name"]
                    topology.description = json_object["description"]

            topology.json = json.dumps(json_data)

            image_list = Image.objects.all().order_by('name')
            image_list_json = serializers.serialize('json',
                                                    Image.objects.all(),
                                                    fields=('name', 'type'))
            script_list = Script.objects.all().order_by('name')
            vm_types = configuration.vm_image_types
            vm_types_string = json.dumps(vm_types)

            dhcp_reservations = wistarUtils.get_consumed_management_ips()

            context = {
                'image_list': image_list,
                'image_list_json': image_list_json,
                'allocated_ips': currently_allocated_ips,
                'script_list': script_list,
                'vm_types': vm_types_string,
                'dhcp_reservations': dhcp_reservations,
                'topo': topology
            }

            return render(request, 'topologies/new.html', context)

        else:
            form = ImportForm()
            context = {'form': form}
            return render(request, 'topologies/import.html', context)
    except Exception as e:
        logger.error('Could not parse imported data!')
        logger.error(e)
        return error(request, 'Could not parse imported data')