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 check_image_exists(request): logger.debug("---- check_image_exists ----") json_string = request.body json_body = json.loads(json_string) try: if "name" in json_body[0]: image_name = json_body[0]["name"] image_list = imageUtils.get_image_list() found = False image_id = 0 for image in image_list: if "file" in image and image_name in image["file"]: image_id = image["id"] found = True break if found: return apiUtils.return_json(True, "Image was found with id: %s" % image_id, image_id=image_id) else: return apiUtils.return_json(False, "Image was not found!") else: return apiUtils.return_json(False, "Malformed input data") except Exception as e: logger.error(e) return apiUtils.return_json(False, "Unknown Error checking image!")
def check_topology_exists(request): logger.debug("---- check_topology_exists ----") json_string = request.body json_body = json.loads(json_string) try: if "name" in json_body[0]: topology_name = json_body[0]["name"] try: # get the topology by name topology = Topology.objects.get(name=topology_name) return apiUtils.return_json( True, "Topology Already Exists with id: %s" % topology.id, topology_id=topology.id) except Topology.DoesNotExist: return apiUtils.return_json(False, 'Topology Does not Exist') else: return apiUtils.return_json(False, "Malformed input data") except Exception as e: logger.error(e) return apiUtils.return_json(False, "Unknown Error checking topology!")
def check_image_exists(request): """ Checks if an image already exists in Wistar :param request: JSON payload that contains a single object with the following properties: name :return: a JSON object with at least the following properties: status (boolean), message, and other properties may also return an HTTP Status of 500 in case of exceptions """ logger.debug("---- check_image_exists ----") json_string = request.body json_body = json.loads(json_string) try: if "name" in json_body[0]: image_name = json_body[0]["name"] image_list = imageUtils.get_image_list() found = False image_id = 0 for image in image_list: if "file" in image and image_name in image["file"]: image_id = image["id"] found = True break if found: return apiUtils.return_json(True, "Image was found with id: %s" % image_id, image_id=image_id) else: return apiUtils.return_json(False, "Image was not found!") else: return apiUtils.return_json(False, "Malformed input data") except Exception as e: logger.error(e) return apiUtils.return_json(False, "Unknown Error checking image!")
def start_topology(request): logger.debug("---- start_topology ---- ") json_string = request.body json_body = json.loads(json_string) try: if "name" in json_body[0]: topology_name = json_body[0]["name"] # get the topology by name topology = Topology.objects.get(name=topology_name) domain_list = libvirtUtils.get_domains_for_topology( "t" + str(topology.id) + "_") if len(domain_list) == 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( topology.json, topology.id) if config is None: return apiUtils( False, "Could not load config for topology: %s" % topology.id) logger.debug("Deploying to hypervisor now") # FIXME - should this be pushed into another module? av.inline_deploy_topology(config) # now, topology should be deployed and ready to go! network_list = libvirtUtils.get_networks_for_topology( "t" + str(topology.id) + "_") domain_list = libvirtUtils.get_domains_for_topology( "t" + str(topology.id) + "_") for network in network_list: logger.debug("starting network: %s" % network["name"]) libvirtUtils.start_network(network["name"]) time.sleep(.5) for domain in domain_list: # no sleep time? Just go ahead and melt the disks! time.sleep(.5) logger.debug("starting domain: %s" % domain["uuid"]) libvirtUtils.start_domain(domain["uuid"]) return apiUtils.return_json(True, 'Topology started!', topology_id=topology.id) except Topology.DoesNotExist: return apiUtils.return_json(False, 'Topology Does not Exist') except Exception as ex: logger.debug(str(ex)) return apiUtils.return_json(False, 'Could not start topology!')
def delete_topology(request): """ DEPRECATED :param request: :return: """ logger.debug("---- delete_topology ----") json_string = request.body json_body = json.loads(json_string) required_fields = set(['name']) if not required_fields.issubset(json_body[0]): logger.error("Invalid parameters in json body") return HttpResponse(status=500) topology_name = json_body[0]["name"] try: # get the topology by name topology = Topology.objects.get(name=topology_name) except ObjectDoesNotExist: return apiUtils.return_json(False, "Topology is already deleted or does not exist") try: topology_prefix = "t%s_" % topology.id if hasattr(configuration, "use_openvswitch") and configuration.use_openvswitch: use_ovs = True else: use_ovs = False network_list = libvirtUtils.get_networks_for_topology(topology_prefix) for network in network_list: logger.debug("undefining network: " + network["name"]) libvirtUtils.undefine_network(network["name"]) if use_ovs: ovsUtils.delete_bridge(network["name"]) domain_list = libvirtUtils.get_domains_for_topology(topology_prefix) for domain in domain_list: logger.debug("undefining domain: " + domain["name"]) source_file = libvirtUtils.get_image_for_domain(domain["uuid"]) if libvirtUtils.undefine_domain(domain["uuid"]): if source_file is not None: osUtils.remove_instance(source_file) # remove reserved mac addresses for all domains in this topology mac_address = libvirtUtils.get_management_interface_mac_for_domain(domain["name"]) libvirtUtils.release_management_ip_for_mac(mac_address) topology.delete() return apiUtils.return_json(True, "Topology deleted!") except Exception as e: logger.error(str(e)) return HttpResponse(status=500)
def import_glance_image(request): """ Imports Openstack Glance image into Wistar. :param request: JSON payload that contains a single object with the following properties: image_name, image_type, image_descr :return: a JSON object with at least the following properties: status (boolean) and message """ logger.debug("<---- API call import_glance_images ---->") if openstackUtils.connect_to_openstack(): json_string = request.body try: json_body = json.loads(json_string) except ValueError as ve: logger.error( 'Could not parse json payload with error <{0}>'.format( ve.message)) return apiUtils.return_json(False, "Could not parse json payload!") logger.debug(json_body) required_fields = {'image_name', 'image_type', 'image_descr'} if not required_fields.issubset(json_body): logger.error("Invalid parameters in json body") return apiUtils.return_json(False, "Invalid parameters in json payload") image_name = json_body["image_name"] image_type = json_body["image_type"] image_decr = json_body["image_descr"] logger.debug( "<---- API call import_glance_image: <{0}> START ---->".format( image_name)) if Image.objects.filter(name=image_name).exists(): logger.info('image with name %s already exists' % image_name) return apiUtils.return_json(True, "Glance images already imported") image = Image() image.description = image_decr + ' image imported from Glance' image.name = image_name image.type = image_type image.save() logger.debug( "<---- API call import_glance_image: <{0}> DONE ---->".format( image_name)) return apiUtils.return_json(True, "Glance images successfully imported") else: return apiUtils.return_json(False, "Failed to authenticate")
def create_local_image(request): logger.debug("---- create_local_image ----") json_string = request.body json_body = json.loads(json_string) logger.debug(json_string) logger.debug(json_body) required_fields = set(['name', 'description', 'image_type', 'file_name']) if not required_fields.issubset(json_body[0]): logger.error("Invalid parameters in json body") return HttpResponse(status=500) file_name = json_body[0]["file_name"] name = json_body[0]["name"] description = json_body[0]["description"] image_type = json_body[0]["image_type"] file_path = configuration.user_images_dir + "/" + file_name try: image_id = imageUtils.create_local_image(name, description, file_path, image_type) return apiUtils.return_json(True, "Image Created with id: %s" % image_id, image_id=image_id) except Exception as e: return HttpResponse(status=500)
def create_local_image(request): """ Creates an image from a file on the local filesystem. You should upload the image to the server first, then call this to register that image with Wistar! :param request: JSON payload that contains a single object with the following properties: name, description, image_type, and file_name :return: a JSON object with at least the following properties: status (boolean), message, and other properties may also return an HTTP Status of 500 in case of exceptions """ logger.debug("---- create_local_image ----") json_string = request.body try: json_body = json.loads(json_string) except ValueError as ve: logger.error('Could not parse json payload!') return apiUtils.return_json(False, "Could not parse json payload!") logger.debug(json_body) required_fields = set(['name', 'description', 'image_type', 'file_name']) if not required_fields.issubset(json_body[0]): logger.error("Invalid parameters in json body") return apiUtils.return_json(False, "Invalid parameters in json payload") file_name = json_body[0]["file_name"] name = json_body[0]["name"] description = json_body[0]["description"] image_type = json_body[0]["image_type"] file_path = configuration.user_images_dir + "/" + file_name try: image_id = imageUtils.create_local_image(name, description, file_path, image_type) return apiUtils.return_json(True, "Image Exists with id: %s" % image_id, image_id=image_id) except Exception as e: logger.error(str(e)) return apiUtils.return_json(False, "Unknown exception in create_local_image")
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 delete_image(request): logger.debug("---- delete_image ----") json_string = request.body json_body = json.loads(json_string) required_fields = set(['name']) if not required_fields.issubset(json_body[0]): logger.error("Invalid parameters in json body") return HttpResponse(status=500) name = json_body[0]["name"] imageUtils.delete_image_by_name(name) return apiUtils.return_json(True, "Image deleted")
def delete_image(request): """ Deletes an image from Wistar :param request: JSON payload that contains a single object with the following properties: name :return: a JSON object with at least the following properties: status (boolean) and message """ logger.debug("---- delete_image ----") json_string = request.body json_body = json.loads(json_string) required_fields = set(['name']) if not required_fields.issubset(json_body[0]): logger.error("Invalid parameters in json body") return HttpResponse(status=500) name = json_body[0]["name"] imageUtils.delete_image_by_name(name) return apiUtils.return_json(True, "Image deleted")
def start_topology(request): logger.debug("---- start_topology ---- ") json_string = request.body try: json_body = json.loads(json_string) except ValueError: return apiUtils(False, "Could not load json payload") try: if "name" in json_body[0]: topology_name = json_body[0]["name"] # do we have a specified delay between starting domains? if 'start_delay' in json_body[0]: delay = int(json_body[0]['start_delay']) else: delay = 0.5 # get the topology by name try: topology = Topology.objects.get(name=topology_name) except ObjectDoesNotExist: logger.error('Could not find topology with name: %s' % topology_name) return apiUtils( False, "Could not find topology with name: %s" % topology_name) domain_list = libvirtUtils.get_domains_for_topology( "t" + str(topology.id) + "_") if len(domain_list) == 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( topology.json, topology.id) if config is None: return apiUtils( False, "Could not load config for topology: %s" % topology.id) logger.debug("Deploying to hypervisor now") # FIXME - should this be pushed into another module? av.inline_deploy_topology(config) # now, topology should be deployed and ready to go! network_list = libvirtUtils.get_networks_for_topology( "t" + str(topology.id) + "_") domain_list = libvirtUtils.get_domains_for_topology( "t" + str(topology.id) + "_") for network in network_list: logger.debug("starting network: %s" % network["name"]) libvirtUtils.start_network(network["name"]) time.sleep(delay) for domain in domain_list: # no sleep time? Just go ahead and melt the disks! if domain["state"] != 'running': logger.debug("starting domain: %s" % domain["uuid"]) libvirtUtils.start_domain(domain["uuid"]) time.sleep(delay) return apiUtils.return_json(True, 'Topology started!', topology_id=topology.id) except Exception as ex: logger.debug(str(ex)) return apiUtils.return_json(False, 'Could not start topology!')