Esempio n. 1
0
def import_topology_json(request):

    logger.debug("---- import_topology_json ----")
    json_string = request.body

    # fixme - add some basic check to ensure we have the proper format here
    try:
        topology_json_string = wistarUtils.clone_topology(json_string)
        topology_json = json.loads(topology_json_string)
        for json_object in topology_json:
            if json_object["type"] == "wistar.info":
                name = json_object["name"]
                description = json_object["description"]
                break

        logger.debug("Creating new topology with name: %s" % name)
        t = Topology(name=name,
                     description=description,
                     json=topology_json_string)
        t.save()

        return apiUtils.return_json(True,
                                    "Topology Imported with id: %s" % t.id,
                                    topology_id=t.id)

    except Exception as e:
        logger.error(e)
        return apiUtils.return_json(False, "Topology Import Failed!")
Esempio n. 2
0
def create(request):
    logger.debug('---- topology create ----')
    url = '/topologies/'
    try:
        if request.POST.has_key('id'):
            topo_id = request.POST['id']
            topo = get_object_or_404(Topology, pk=topo_id)
            topo.json = request.POST['json']
            topo.name = request.POST['name']
            topo.description = request.POST['description']
            topo.save()
        else:
            json_string = request.POST['json']
            description = request.POST['description']
            name = request.POST['name']
            t = Topology(name=name, description=description, json=json_string)
            t.save()
            url += str(t.id)

    except KeyError:
        logger.error('Invalid data in POST')
        return render(request, 'error.html', {
            'error': "Invalid data in POST"
        })
    else:
        # Always return an HttpResponseRedirect after successfully dealing # with POST data.
        # This prevents data from being posted twice if a
        # user hits the Back button.
        # return HttpResponseRedirect(reverse('topologies:converted', args=(p.id,)))
        # context = { 'json': json, 'name': name, 'description': description }
        # return render(request, 'topologies/output.html', context)
        return HttpResponseRedirect(url)
Esempio n. 3
0
def import_topology_json(request):

    logger.debug("---- import_topology_json ----")
    json_string = request.body

    # fixme - add some basic check to ensure we have the proper format here

    topology_json_string = wistarUtils.clone_topology(json_string)
    if topology_json_string is None:
        return apiUtils.return_json(False, "Topology Import Failed!")

    try:
        topology_json = json.loads(topology_json_string)
    except ValueError as ve:
        logger.error('Could not parse topology json from Clone!')
        return apiUtils.return_json(False, "Topology Import Failed!")

    try:
        for json_object in topology_json:
            if json_object["type"] == "wistar.info":
                name = json_object["name"]
                description = json_object["description"]
                break

        if Topology.objects.filter(name=name).exists():
            logger.info('Not importing existing topology with this name!')
            return apiUtils.return_json(True,
                                        "Topology Exists with name: %s" % name)

        logger.debug("Creating new topology with name: %s" % name)
        t = Topology(name=name,
                     description=description,
                     json=topology_json_string)
        t.save()

        return apiUtils.return_json(True,
                                    "Topology Imported with id: %s" % t.id,
                                    topology_id=t.id)

    except Exception as e:
        logger.error(e)
        return apiUtils.return_json(False, "Topology Import Failed!")
Esempio n. 4
0
def start_topology_old(request):
    """
        DEPRECATED
        verify the topology exists and is started!
        required parameters: topology_name, id of which to clone, cloud_init data
        returns json { "status": "running|unknown|powered off", "topology_id": "0" }

    """
    context = {"status": "unknown"}

    required_fields = set(
        ['topology_name', 'clone_id', 'script_id', 'script_param'])
    if not required_fields.issubset(request.POST):
        context["status"] = "unknown"
        context["message"] = "Invalid parameters in POST"
        return HttpResponse(json.dumps(context),
                            content_type="application/json")

    topology_name = request.POST['topology_name']
    clone_id = request.POST['clone_id']
    script_id = request.POST['script_id']
    script_param = request.POST['script_param']

    try:
        # get the topology by name
        topo = Topology.objects.get(name=topology_name)

    except ObjectDoesNotExist:
        # uh-oh! it doesn't exist, let's clone it and keep going
        # clone the topology with the new name specified!
        topology = Topology.objects.get(pk=clone_id)

        # get a list of all the currently used IPs defined
        all_used_ips = wistarUtils.get_used_ips()
        logger.debug(str(all_used_ips))

        raw_json = json.loads(topology.json)
        for json_object in raw_json:
            if "userData" in json_object and "wistarVm" in json_object[
                    "userData"]:
                ud = json_object["userData"]
                ip = ud["ip"]
                ip_octets = ip.split('.')
                # get the next available ip
                next_ip = wistarUtils.get_next_ip(all_used_ips, 2)
                # mark it as used so it won't appear in the next iteration
                all_used_ips.append(next_ip)

                ip_octets[3] = str(next_ip)
                newIp = ".".join(ip_octets)
                ud["ip"] = newIp

                ud["configScriptId"] = script_id
                ud["configScriptParam"] = script_param

        description = "Clone from: %s\nScript Id: %s\nScript Param: %s" % (
            clone_id, script_id, script_param)
        topo = Topology(name=topology_name,
                        description=description,
                        json=json.dumps(raw_json))
        topo.save()

    try:

        # by this point, the topology already exists
        logger.debug("Got topo " + str(topo.id))
        domain_status = libvirtUtils.get_domains_for_topology("t" +
                                                              str(topo.id) +
                                                              "_")

        if len(domain_status) == 0:
            # it has not yet been deployed!
            logger.debug("not yet deployed!")

            # let's parse the json and convert to simple lists and dicts
            config = wistarUtils.load_config_from_topology_json(
                topo.json, topo.id)

            logger.debug("Deploying to hypervisor now")
            # FIXME - should this be pushed into another module?
            av.inline_deploy_topology(config)
            time.sleep(1)

    except Exception as e:
        logger.debug(str(e))
        context["status"] = "unknown"
        context["message"] = "Exception"
        return HttpResponse(json.dumps(context),
                            content_type="application/json")

    try:
        # at this point, the topology now exists and is deployed!
        network_list = libvirtUtils.get_networks_for_topology("t" +
                                                              str(topo.id) +
                                                              "_")
        domain_list = libvirtUtils.get_domains_for_topology("t" +
                                                            str(topo.id) + "_")

        for network in network_list:
            libvirtUtils.start_network(network["name"])

        time.sleep(1)
        for domain in domain_list:
            time.sleep(10)
            libvirtUtils.start_domain(domain["uuid"])

        context = {
            'status': 'booting',
            'topologyId': topo.id,
            'message': 'sandbox is booting'
        }

        logger.debug("returning")
        return HttpResponse(json.dumps(context),
                            content_type="application/json")

    except Exception as ex:
        logger.debug(str(ex))
        context["status"] = "unknown"
        context["message"] = "Caught Exception %s" % ex
        return HttpResponse(json.dumps(context),
                            content_type="application/json")