def create_local_image(name, description, file_path, image_type): """ Register a local (server-side) image with Wistar :param name: name of the image :param description: Description of the image :param file_path: full path to the where the file exists on the server :param image_type: The wistar type of the image, this type must already be registered in Wistar :return: The local id of the image """ if image_exists(name): logger.info('Image with this name already exists!') try: existing_image = Image.objects.get(name=name) return existing_image.id except Image.DoesNotExist: logger.error('Image already exists but we cannot get it locally!') pass if osUtils.check_path(file_path): logger.debug("path exists") if settings.MEDIA_ROOT not in file_path: raise Exception("Image must in in path: %s" % settings.MEDIA_ROOT) else: logger.debug("removing media_root") file_path = file_path.replace(settings.MEDIA_ROOT + '/', '') logger.debug(file_path) else: raise Exception("Invalid image path") try: logger.debug(file_path) image = Image() image.description = description image.name = name image.filePath = file_path image.type = image_type image.save() full_path = image.filePath.path if re.match(".*\.vmdk$", full_path): # we need to convert this for KVM based deployments! converted_image_path = re.sub("\.vmdk$", ".qcow2", full_path) converted_image_file_name = converted_image_path.split('/')[-1] if osUtils.convert_vmdk_to_qcow2(full_path, converted_image_path): logger.info("Converted vmdk image to qcow2!") image.filePath = "user_images/%s" % converted_image_file_name image.save() logger.debug("Removing original vmdk") osUtils.remove_instance(full_path) else: logger.error("Could not convert vmdk!") if image_type == "junos_vre" and "jinstall64-vmx-15.1" in full_path: logger.debug("Creating RIOT image for Junos vMX 15.1") # lets replace the last "." with "_riot." if '.' in file_path: new_image_path = re.sub(r"(.*)\.(.*)$", r"\1_riot.\2", full_path) else: # if there is no '.', let's just add one new_image_path = full_path + "_riot.img" new_image_file_name = new_image_path.split('/')[-1] new_image_name = name + ' Riot PFE' if osUtils.copy_image_to_clone(full_path, new_image_path): logger.debug("Copied from %s" % full_path) logger.debug("Copied to %s" % new_image_path) n_image = Image() n_image.name = new_image_name n_image.type = "junos_riot" n_image.description = image.description + "\nRiot PFE" n_image.filePath = "user_images/" + new_image_file_name n_image.save() return image.id except Exception as e: logger.error("Caught Error in create_local_image") logger.error(str(e)) raise
def create_disk_instance(device, disk_params): """ Creates a disk according to the parameters specified. Can be blank, image, or config_drive :param device: device dictionary created from wistarUtils.loadJson() :param disk_params: parameters for the specific disk we are working with :return: the path to the created disk or "" """ domain_name = device["name"] disk_instance_path = "" if "type" in disk_params: if disk_params["type"] == "image" and "image_id" in disk_params: logger.debug("Creating secondary/tertiary Disk information") image_id = disk_params["image_id"] disk_image = Image.objects.get(pk=image_id) disk_base_path = settings.MEDIA_ROOT + "/" + disk_image.filePath.url disk_instance_path = osUtils.get_instance_path_from_image(disk_base_path, domain_name + "_secondary_image.img" ) if not osUtils.check_path(disk_instance_path): if not osUtils.create_thin_provision_instance(disk_base_path, domain_name + "_secondary_image.img" ): raise Exception("Could not create image instance for image: " + disk_base_path) elif disk_params["type"] == "blank": disk_instance_path = settings.MEDIA_ROOT \ + "/user_images/instances/" + domain_name + "_secondary_blank.img" disk_size = "16G" if "size" in disk_params: disk_size = disk_params["size"] if not osUtils.check_path(disk_instance_path): if not osUtils.create_blank_image(disk_instance_path, disk_size): raise Exception("Could not create image instance for image: " + disk_instance_path) elif disk_params["type"] == "config_drive": # let's check if config_drive is supported for this vm_type! # this is usually used for vMX in openstack, however, we can also use it here for KVM deployments disk_instance_path = '' if "configDriveSupport" in device and device["configDriveSupport"] is True: logger.debug("Lets create a config-drive!") # keep a dict of files with format: filename: filecontents files = dict() params = device["configDriveParams"] if "configDriveParamsFile" in device and device["configDriveParamsFile"]: logger.debug("Using inline config_drive format") # behavior change 12-28-2016 - allow passing a list of templates and destinations # instead of defining the params directly on the device object # if the configDriveParams is a dict, then this is an older topology, leave this code here # to still support them - otherwise fall through to the isinstance check for list type for # newer style configuration if isinstance(params, dict): name = device["configDriveParamsFile"] file_data = "" # config drive params are usually a dict - to make json serialization easier # for our purposes here, let's just make a file with a single key: value per line # note, we can add a serialization format to the vm_type.js if needed here # only currently used for /boot/loader.conf in vmx and riot for k in params: file_data += '%s="%s"\n' % (k, params[k]) files[name] = file_data # junos customization # let's also inject a default config here as well if possible! if "junos" in device["type"]: logger.debug("Creating Junos configuration template") junos_config = osUtils.get_junos_default_config_template(device["name"], device["label"], device["password"], device["ip"], device["managementInterface"]) if junos_config is not None: files["/juniper.conf"] = junos_config # check for new (12-28-2016) style config drive params definition if isinstance(params, list): logger.debug("params is a list") for p in params: if "template" in p and "destination" in p: file_data = None file_data = osUtils.compile_config_drive_params_template( p["template"], device["name"], device["label"], device["password"], device["ip"], device["managementInterface"] ) if file_data is not None: files[p["destination"]] = file_data disk_instance_path = osUtils.create_config_drive(device["name"], files) if disk_instance_path is None: disk_instance_path = '' logger.debug("Using %s" % disk_instance_path) return disk_instance_path
def create_local_image(name, description, file_path, image_type): if osUtils.check_path(file_path): logger.debug("path exists") if settings.MEDIA_ROOT not in file_path: raise Exception("Image must in in path: %s" % settings.MEDIA_ROOT) else: logger.debug("removing media_root") file_path = file_path.replace(settings.MEDIA_ROOT + '/', '') logger.debug(file_path) else: raise Exception("Invalid image path") try: logger.debug(file_path) image = Image() image.description = description image.name = name image.filePath = file_path image.type = image_type image.save() full_path = image.filePath.path if re.match(".*\.vmdk$", full_path): # we need to convert this for KVM based deployments! converted_image_path = re.sub("\.vmdk$", ".qcow2", full_path) converted_image_file_name = converted_image_path.split('/')[-1] if osUtils.convert_vmdk_to_qcow2(full_path, converted_image_path): logger.info("Converted vmdk image to qcow2!") image.filePath = "user_images/%s" % converted_image_file_name image.save() logger.debug("Removing original vmdk") osUtils.remove_instance(full_path) else: logger.error("Could not convert vmdk!") if image_type == "junos_vre" and "jinstall64-vmx-15.1" in full_path: logger.debug("Creating RIOT image for Junos vMX 15.1") # lets replace the last "." with "_riot." if '.' in file_path: new_image_path = re.sub(r"(.*)\.(.*)$", r"\1_riot.\2", full_path) else: # if there is no '.', let's just add one new_image_path = full_path + "_riot.img" new_image_file_name = new_image_path.split('/')[-1] new_image_name = name + ' Riot PFE' if osUtils.copy_image_to_clone(full_path, new_image_path): logger.debug("Copied from %s" % full_path) logger.debug("Copied to %s" % new_image_path) n_image = Image() n_image.name = new_image_name n_image.type = "junos_riot" n_image.description = image.description + "\nRiot PFE" n_image.filePath = "user_images/" + new_image_file_name n_image.save() return image.id except Exception as e: logger.error("Caught Error in create_local_image") logger.error(str(e)) raise