public static TemplateObjectTO ParseJson(dynamic json) { TemplateObjectTO result = null; dynamic templateObjectTOJson = json[CloudStackTypes.TemplateObjectTO]; if (templateObjectTOJson != null) { result = new TemplateObjectTO() { imageDataStore = templateObjectTOJson.imageDataStore, format = (string)templateObjectTOJson.format, name = (string)templateObjectTOJson.name, uuid = (string)templateObjectTOJson.uuid, path = (string)templateObjectTOJson.path, checksum = (string)templateObjectTOJson.checksum, size = (string)templateObjectTOJson.size, id = (string)templateObjectTOJson.id }; result.s3DataStoreTO = S3TO.ParseJson(templateObjectTOJson.imageDataStore); result.nfsDataStoreTO = NFSTO.ParseJson(templateObjectTOJson.imageDataStore); result.primaryDataStore = PrimaryDataStoreTO.ParseJson(templateObjectTOJson.imageDataStore); } return(result); }
public static DiskTO ParseJson(dynamic json) { DiskTO result = null; if (json != null) { result = new DiskTO() { templateObjectTO = TemplateObjectTO.ParseJson(json.data), type = (string)json.type, }; } return(result); }
public static TemplateObjectTO ParseJson(dynamic json) { TemplateObjectTO result = null; dynamic templateObjectTOJson = json[CloudStackTypes.TemplateObjectTO]; if (templateObjectTOJson != null) { result = new TemplateObjectTO() { imageDataStore = templateObjectTOJson.imageDataStore, format = (string)templateObjectTOJson.format, name = (string)templateObjectTOJson.name, uuid = (string)templateObjectTOJson.uuid, path = (string)templateObjectTOJson.path, checksum = (string)templateObjectTOJson.checksum, size = (string)templateObjectTOJson.size, id = (string)templateObjectTOJson.id }; result.s3DataStoreTO = S3TO.ParseJson(templateObjectTOJson.imageDataStore); result.nfsDataStoreTO = NFSTO.ParseJson(templateObjectTOJson.imageDataStore); result.primaryDataStore = PrimaryDataStoreTO.ParseJson(templateObjectTOJson.imageDataStore); } return result; }
public JContainer CopyCommand(dynamic cmd) { using (log4net.NDC.Push(Guid.NewGuid().ToString())) { // Log command *after* we've removed security details from the command. logger.Info(CloudStackTypes.CopyCommand + Utils.CleanString(cmd.ToString())); bool result = false; string details = null; object newData = null; TemplateObjectTO destTemplateObjectTO = null; VolumeObjectTO destVolumeObjectTO = null; VolumeObjectTO srcVolumeObjectTO = null; TemplateObjectTO srcTemplateObjectTO = null; try { dynamic timeout = cmd.wait; // TODO: Useful? srcTemplateObjectTO = TemplateObjectTO.ParseJson(cmd.srcTO); destTemplateObjectTO = TemplateObjectTO.ParseJson(cmd.destTO); srcVolumeObjectTO = VolumeObjectTO.ParseJson(cmd.srcTO); destVolumeObjectTO = VolumeObjectTO.ParseJson(cmd.destTO); string destFile = null; if (destTemplateObjectTO != null) { if (destTemplateObjectTO.primaryDataStore != null) { destFile = destTemplateObjectTO.FullFileName; if (!destTemplateObjectTO.primaryDataStore.isLocal) { PrimaryDataStoreTO primary = destTemplateObjectTO.primaryDataStore; Utils.ConnectToRemote(primary.UncPath, primary.Domain, primary.User, primary.Password); } } else if (destTemplateObjectTO.nfsDataStoreTO != null) { destFile = destTemplateObjectTO.FullFileName; NFSTO store = destTemplateObjectTO.nfsDataStoreTO; Utils.ConnectToRemote(store.UncPath, store.Domain, store.User, store.Password); } } // Template already downloaded? if (destFile != null && File.Exists(destFile) && !String.IsNullOrEmpty(destTemplateObjectTO.checksum)) { // TODO: checksum fails us, because it is of the compressed image. // ASK: should we store the compressed or uncompressed version or is the checksum not calculated correctly? logger.Debug(CloudStackTypes.CopyCommand + " calling VerifyChecksum to see if we already have the file at " + destFile); result = VerifyChecksum(destFile, destTemplateObjectTO.checksum); if (!result) { result = true; logger.Debug(CloudStackTypes.CopyCommand + " existing file has different checksum " + destFile); } } // Do we have to create a new one? if (!result) { // Create local copy of a template? if (srcTemplateObjectTO != null && destTemplateObjectTO != null) { // S3 download to primary storage? // NFS provider download to primary storage? if ((srcTemplateObjectTO.s3DataStoreTO != null || srcTemplateObjectTO.nfsDataStoreTO != null) && destTemplateObjectTO.primaryDataStore != null) { if (File.Exists(destFile)) { logger.Info("Deleting existing file " + destFile); File.Delete(destFile); } if (srcTemplateObjectTO.s3DataStoreTO != null) { // Download from S3 to destination data storage DownloadS3ObjectToFile(srcTemplateObjectTO.path, srcTemplateObjectTO.s3DataStoreTO, destFile); } else if (srcTemplateObjectTO.nfsDataStoreTO != null) { // Download from S3 to destination data storage Utils.DownloadCifsFileToLocalFile(srcTemplateObjectTO.path, srcTemplateObjectTO.nfsDataStoreTO, destFile); } // Uncompress, as required if (srcTemplateObjectTO.path.EndsWith(".bz2")) { String uncompressedFile = destFile + ".tmp"; String compressedFile = destFile; using (var uncompressedOutStrm = new FileStream(uncompressedFile, FileMode.CreateNew, FileAccess.Write)) { using (var compressedInStrm = new FileStream(destFile, FileMode.Open, FileAccess.Read)) { using (var bz2UncompressorStrm = new Ionic.BZip2.BZip2InputStream(compressedInStrm, true) /* outer 'using' statement will close FileStream*/ ) { int count = 0; int bufsize = 1024 * 1024; byte[] buf = new byte[bufsize]; // EOF returns -1, see http://dotnetzip.codeplex.com/workitem/16069 while (0 < (count = bz2UncompressorStrm.Read(buf, 0, bufsize))) { uncompressedOutStrm.Write(buf, 0, count); } } } } File.Delete(compressedFile); File.Move(uncompressedFile, compressedFile); if (File.Exists(uncompressedFile)) { String errMsg = "Extra file left around called " + uncompressedFile + " when creating " + destFile; logger.Error(errMsg); throw new IOException(errMsg); } } // assert if (!File.Exists(destFile)) { String errMsg = "Failed to create " + destFile + " , because the file is missing"; logger.Error(errMsg); throw new IOException(errMsg); } newData = cmd.destTO; result = true; } else { details = "Data store combination not supported"; } } // Create volume from a template? else if (srcTemplateObjectTO != null && destVolumeObjectTO != null) { // VolumeObjectTO guesses file extension based on existing files // this can be wrong if the previous file had a different file type var guessedDestFile = destVolumeObjectTO.FullFileName; if (File.Exists(guessedDestFile)) { logger.Info("Deleting existing file " + guessedDestFile); File.Delete(guessedDestFile); } destVolumeObjectTO.format = srcTemplateObjectTO.format; destFile = destVolumeObjectTO.FullFileName; if (File.Exists(destFile)) { logger.Info("Deleting existing file " + destFile); File.Delete(destFile); } string srcFile = srcTemplateObjectTO.FullFileName; if (!File.Exists(srcFile)) { details = "Local template file missing from " + srcFile; } else { // TODO: thin provision instead of copying the full file. File.Copy(srcFile, destFile); destVolumeObjectTO.path = destFile; JObject ansObj = Utils.CreateCloudStackObject(CloudStackTypes.VolumeObjectTO, destVolumeObjectTO); newData = ansObj; result = true; } } else if (srcVolumeObjectTO != null && destVolumeObjectTO != null) { var guessedDestFile = destVolumeObjectTO.FullFileName; if (File.Exists(guessedDestFile)) { logger.Info("Deleting existing file " + guessedDestFile); File.Delete(guessedDestFile); } destVolumeObjectTO.format = srcVolumeObjectTO.format; destFile = destVolumeObjectTO.FullFileName; if (File.Exists(destFile)) { logger.Info("Deleting existing file " + destFile); File.Delete(destFile); } string srcFile = srcVolumeObjectTO.FullFileName; if (!File.Exists(srcFile)) { details = "Local template file missing from " + srcFile; } else { // Create the directory before copying the files. CreateDirectory // doesn't do anything if the directory is already present. Directory.CreateDirectory(Path.GetDirectoryName(destFile)); File.Copy(srcFile, destFile); // Create volumeto object deserialize and send it destVolumeObjectTO.path = destFile; JObject ansObj = Utils.CreateCloudStackObject(CloudStackTypes.VolumeObjectTO, destVolumeObjectTO); newData = ansObj; result = true; } } else if (srcVolumeObjectTO != null && destTemplateObjectTO != null) { var guessedDestFile = destTemplateObjectTO.FullFileName; if (File.Exists(guessedDestFile)) { logger.Info("Deleting existing file " + guessedDestFile); File.Delete(guessedDestFile); } destTemplateObjectTO.format = srcVolumeObjectTO.format; destFile = destTemplateObjectTO.FullFileName; if (File.Exists(destFile)) { logger.Info("Deleting existing file " + destFile); File.Delete(destFile); } string srcFile = srcVolumeObjectTO.FullFileName; if (!File.Exists(srcFile)) { details = "Local template file missing from " + srcFile; } else { // Create the directory before copying the files. CreateDirectory // doesn't do anything if the directory is already present. Directory.CreateDirectory(Path.GetDirectoryName(destFile)); File.Copy(srcFile, destFile); FileInfo destFileInfo = new FileInfo(destFile); // Write the template.properties file PostCreateTemplate(Path.GetDirectoryName(destFile), destTemplateObjectTO.id, destTemplateObjectTO.name, destFileInfo.Length.ToString(), srcVolumeObjectTO.size.ToString(), destTemplateObjectTO.format); TemplateObjectTO destTemplateObject = new TemplateObjectTO(); destTemplateObject.size = srcVolumeObjectTO.size.ToString(); destTemplateObject.format = srcVolumeObjectTO.format; destTemplateObject.path = destFile; destTemplateObject.nfsDataStoreTO = destTemplateObjectTO.nfsDataStoreTO; destTemplateObject.checksum = destTemplateObjectTO.checksum; newData = destTemplateObject; JObject ansObj = Utils.CreateCloudStackObject(CloudStackTypes.TemplateObjectTO, destTemplateObject); newData = ansObj; result = true; } } else { details = "Data store combination not supported"; } } } catch (Exception ex) { // Test by providing wrong key details = CloudStackTypes.CopyCommand + " failed on exception, " + ex.Message; logger.Error(details, ex); } object ansContent = new { result = result, details = details, newData = newData, contextMap = contextMap }; return ReturnCloudStackTypedJArray(ansContent, CloudStackTypes.CopyCmdAnswer); } }
public static TemplateObjectTO ParseJson(dynamic json) { TemplateObjectTO result = null; dynamic templateObjectTOJson = json[CloudStackTypes.TemplateObjectTO]; if (templateObjectTOJson != null) { result = new TemplateObjectTO() { imageDataStore = templateObjectTOJson.imageDataStore, formatExtension = ((string)templateObjectTOJson.format), name = (string)templateObjectTOJson.name, uuid = (string)templateObjectTOJson.uuid }; result.formatExtension = !String.IsNullOrEmpty(result.formatExtension) ? result.formatExtension.ToLowerInvariant() : result.formatExtension; } return result; }