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")
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')