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!")
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)
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!")
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")
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"] # check if we have this type of image image_list = Image.objects.filter(type=ud["type"]) if len(image_list) == 0: # nope, bail out and let the user know what happened! 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') image = image_list[0] logger.debug(str(image.id)) 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) 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, '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')
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')