Ejemplo n.º 1
0
        private void CreateRemoteBlob()
        {
            var baseBlob = this.blobObjectFactory.Create(baseVhdBlobUri);

            if (!baseBlob.Exists())
            {
                throw new InvalidOperationException(String.Format("Base image to patch doesn't exist in blob storage: {0}", baseVhdBlobUri.Uri));
            }
            var blobVhdFooter = baseBlob.GetVhdFooter();

            long               blobSize;
            VhdFilePath        localBaseVhdPath;
            IEnumerable <Guid> childrenVhdIds;

            using (var vhdFile = new VhdFileFactory().Create(localVhd.FullName))
            {
                localBaseVhdPath = vhdFile.GetFilePathBy(blobVhdFooter.UniqueId);
                childrenVhdIds   = vhdFile.GetChildrenIds(blobVhdFooter.UniqueId).ToArray();
                blobSize         = vhdFile.Footer.VirtualSize;
            }

            FileMetaData fileMetaData = GetFileMetaData(baseBlob, localBaseVhdPath);

            var md5Hash = baseBlob.GetBlobMd5Hash();

            if (!md5Hash.SequenceEqual(fileMetaData.MD5Hash))
            {
                var message = String.Format("Patching cannot proceed, MD5 hash of base image in blob storage ({0}) and base VHD file ({1}) does not match ",
                                            baseBlob.Uri,
                                            localBaseVhdPath);
                throw new InvalidOperationException(message);
            }

            Program.SyncOutput.MessageCreatingNewPageBlob(blobSize);

            CopyBaseImageToDestination();

            using (var vds = new VirtualDiskStream(localVhd.FullName))
            {
                var streamExtents = vds.Extents.ToArray();
                var enumerable    = streamExtents.Where(e => childrenVhdIds.Contains(e.Owner)).ToArray();
                foreach (var streamExtent in enumerable)
                {
                    var indexRange = streamExtent.Range;
                    destinationBlob.ClearPages(indexRange.StartIndex, indexRange.Length);
                }
            }

            using (var bmds = new BlobMetaDataScope(destinationBlob))
            {
                bmds.Current.RemoveBlobMd5Hash();
                bmds.Current.SetUploadMetaData(OperationMetaData);
                bmds.Complete();
            }
        }
Ejemplo n.º 2
0
            using (var bdms = new BlobMetaDataScope(destinationBlob))
            {
                bdms.Current.SetUploadMetaData(OperationMetaData);
                bdms.Complete();
            }
        }
    }
}
Ejemplo n.º 3
0
        private void CreateRemoteBlob()
        {
            Program.SyncOutput.MessageCreatingNewPageBlob(OperationMetaData.FileMetaData.VhdSize);

            destinationBlob.Create(OperationMetaData.FileMetaData.VhdSize);

            using (var bdms = new BlobMetaDataScope(destinationBlob))
            {
                bdms.Current.SetUploadMetaData(OperationMetaData);
                bdms.Complete();
            }
        }
Ejemplo n.º 4
0
        public bool Synchronize()
        {
            var uploadStatus = new ProgressStatus(alreadyUploadedData, alreadyUploadedData + dataToUpload, new ComputeStats());

            using (new ServicePointHandler(blob.Uri, this.maxParallelism))
                using (new ProgressTracker(uploadStatus))
                {
                    var loopResult = Parallel.ForEach(dataWithRanges,
                                                      () => new CloudPageBlob(blob.Uri, blob.ServiceClient.Credentials),
                                                      (dwr, b) =>
                    {
                        using (dwr)
                        {
                            var md5HashOfDataChunk = GetBase64EncodedMd5Hash(dwr.Data, (int)dwr.Range.Length);
                            using (var stream = new MemoryStream(dwr.Data, 0, (int)dwr.Range.Length))
                            {
                                b.Properties.ContentMD5 = md5HashOfDataChunk;
                                b.WritePages(stream, dwr.Range.StartIndex);
                            }
                        }
                        uploadStatus.AddToProcessedBytes((int)dwr.Range.Length);
                    }, this.maxParallelism);
                    if (loopResult.IsExceptional)
                    {
                        if (loopResult.Exceptions.Any())
                        {
                            Program.SyncOutput.ErrorUploadFailedWithExceptions(loopResult.Exceptions);
                            //TODO: throw an AggregateException
                            return(false);
                        }
                    }
                    else
                    {
                        using (var bdms = new BlobMetaDataScope(new CloudPageBlob(blob.Uri, blob.ServiceClient.Credentials)))
                        {
                            bdms.Current.SetBlobMd5Hash(md5Hash);
                            bdms.Current.CleanUpUploadMetaData();
                            bdms.Complete();
                        }
                    }
                }
            return(true);
        }
Ejemplo n.º 5
0
        public bool Synchronize()
        {
            var uploadStatus = new ProgressStatus(alreadyUploadedData, alreadyUploadedData + dataToUpload, new ComputeStats());

            using(new ServicePointHandler(blob.Uri, this.maxParallelism))
            using(new ProgressTracker(uploadStatus))
            {
                var loopResult = Parallel.ForEach(dataWithRanges,
                                                  () => new CloudPageBlob(blob.Uri, blob.ServiceClient.Credentials),
                                                  (dwr, b) =>
                                                      {
                                                          using (dwr)
                                                          {
                                                              var md5HashOfDataChunk = GetBase64EncodedMd5Hash(dwr.Data, (int) dwr.Range.Length);
                                                              using (var stream = new MemoryStream(dwr.Data, 0, (int)dwr.Range.Length))
                                                              {
                                                                  b.Properties.ContentMD5 = md5HashOfDataChunk;
                                                                  b.WritePages(stream, dwr.Range.StartIndex);
                                                              }
                                                          }
                                                          uploadStatus.AddToProcessedBytes((int) dwr.Range.Length);
                                                      }, this.maxParallelism);
                if(loopResult.IsExceptional)
                {
                    if (loopResult.Exceptions.Any())
                    {
                        Program.SyncOutput.ErrorUploadFailedWithExceptions(loopResult.Exceptions);
                        //TODO: throw an AggregateException
                        return false;
                    }
                }
                else
                {
                    using(var bdms = new BlobMetaDataScope(new CloudPageBlob(blob.Uri, blob.ServiceClient.Credentials)))
                    {
                        bdms.Current.SetBlobMd5Hash(md5Hash);
                        bdms.Current.CleanUpUploadMetaData();
                        bdms.Complete();
                    }
                }
            }
            return true;
        }
Ejemplo n.º 6
0
        private void CreateRemoteBlob()
        {
            var baseBlob = this.blobObjectFactory.Create(baseVhdBlobUri);

            if(!baseBlob.Exists())
            {
                throw new InvalidOperationException(String.Format("Base image to patch doesn't exist in blob storage: {0}", baseVhdBlobUri.Uri));
            }
            var blobVhdFooter = baseBlob.GetVhdFooter();

            long blobSize;
            VhdFilePath localBaseVhdPath;
            IEnumerable<Guid> childrenVhdIds;
            using (var vhdFile = new VhdFileFactory().Create(localVhd.FullName))
            {
                localBaseVhdPath = vhdFile.GetFilePathBy(blobVhdFooter.UniqueId);
                childrenVhdIds = vhdFile.GetChildrenIds(blobVhdFooter.UniqueId).ToArray();
                blobSize = vhdFile.Footer.VirtualSize;
            }

            FileMetaData fileMetaData = GetFileMetaData(baseBlob, localBaseVhdPath);

            var md5Hash = baseBlob.GetBlobMd5Hash();
            if (!md5Hash.SequenceEqual(fileMetaData.MD5Hash))
            {
                var message = String.Format("Patching cannot proceed, MD5 hash of base image in blob storage ({0}) and base VHD file ({1}) does not match ",
                                            baseBlob.Uri,
                                            localBaseVhdPath);
                throw new InvalidOperationException(message);
            }

            Program.SyncOutput.MessageCreatingNewPageBlob(blobSize);

            CopyBaseImageToDestination();

            using (var vds = new VirtualDiskStream(localVhd.FullName))
            {
                var streamExtents = vds.Extents.ToArray();
                var enumerable = streamExtents.Where(e => childrenVhdIds.Contains(e.Owner)).ToArray();
                foreach (var streamExtent in enumerable)
                {
                    var indexRange = streamExtent.Range;
                    destinationBlob.ClearPages(indexRange.StartIndex, indexRange.Length);
                }
            }

            using(var bmds = new BlobMetaDataScope(destinationBlob))
            {
                bmds.Current.RemoveBlobMd5Hash();
                bmds.Current.SetUploadMetaData(OperationMetaData);
                bmds.Complete();
            }
        }