Ejemplo n.º 1
0
        protected static void PopulateContextWithUploadableRanges(FileInfo vhdFile, UploadContext context, bool resume)
        {
            using (var vds = new VirtualDiskStream(vhdFile.FullName))
            {
                IEnumerable <IndexRange> ranges = vds.Extents.Select(e => e.Range).ToArray();

                var bs = new BufferedStream(vds);
                if (resume)
                {
                    var alreadyUploadedRanges = context.DestinationBlob.GetPageRangesAsync()
                                                .ConfigureAwait(false).GetAwaiter().GetResult()
                                                .Select(pr => new IndexRange(pr.StartOffset, pr.EndOffset));
                    ranges = IndexRange.SubstractRanges(ranges, alreadyUploadedRanges);
                    context.AlreadyUploadedDataSize = alreadyUploadedRanges.Sum(ir => ir.Length);
                }
                var uploadableRanges = IndexRangeHelper.ChunkRangesBySize(ranges, PageSizeInBytes).ToArray();
                if (vds.DiskType == DiskType.Fixed)
                {
                    var nonEmptyUploadableRanges = GetNonEmptyRanges(bs, uploadableRanges).ToArray();
                    context.UploadableDataSize = nonEmptyUploadableRanges.Sum(r => r.Length);
                    context.UploadableRanges   = nonEmptyUploadableRanges;
                }
                else
                {
                    context.UploadableDataSize = uploadableRanges.Sum(r => r.Length);
                    context.UploadableRanges   = uploadableRanges;
                }
            }
        }
        private void checkForCorruptedAndDynamicallySizedVhd()
        {
            // checking for corrupted vhd
            PathIntrinsics currentPath = SessionState.Path;
            var            filePath    = new FileInfo(currentPath.GetUnresolvedProviderPathFromPSPath(LocalFilePath.ToString()));

            using (var vds = new VirtualDiskStream(filePath.FullName))
            {
                if (vds.DiskType == DiskType.Fixed)
                {
                    long divisor = Convert.ToInt64(Math.Pow(2, 9));
                    long rem     = 0;
                    Math.DivRem(filePath.Length, divisor, out rem);
                    if (rem != 0)
                    {
                        throw new ArgumentOutOfRangeException("LocalFilePath", "Given vhd file is a corrupted fixed vhd");
                    }
                }
                else
                {
                    convertDynamicVhdToStatic();
                }
            }
            return;
        }
Ejemplo n.º 3
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.CurrentSize;
            }

            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.ClearPagesAsync(indexRange.StartIndex, indexRange.Length)
                    .ConfigureAwait(false).GetAwaiter().GetResult();
                }
            }

            using (var bmds = new BlobMetaDataScope(destinationBlob))
            {
                bmds.Current.RemoveBlobMd5Hash();
                bmds.Current.SetUploadMetaData(OperationMetaData);
                bmds.Complete();
            }
        }
Ejemplo n.º 4
0
        public UploadParameters ValidateParameters()
        {
            BlobUri destinationUri;

            if (!BlobUri.TryParseUri(Destination, out destinationUri))
            {
                throw new ArgumentOutOfRangeException("Destination", this.Destination.ToString());
            }

            BlobUri baseImageUri = null;

            if (this.BaseImageUriToPatch != null)
            {
                if (!BlobUri.TryParseUri(BaseImageUriToPatch, out baseImageUri))
                {
                    throw new ArgumentOutOfRangeException("BaseImageUriToPatch", this.BaseImageUriToPatch.ToString());
                }

                if (!String.IsNullOrEmpty(destinationUri.Uri.Query))
                {
                    var message = String.Format(Rsrc.AddAzureVhdCommandSASUriNotSupportedInPatchMode, destinationUri.Uri);
                    throw new ArgumentOutOfRangeException("Destination", message);
                }
            }

            var storageCredentialsFactory = CreateStorageCredentialsFactory();

            PathIntrinsics currentPath = SessionState.Path;
            var            filePath    = new FileInfo(currentPath.GetUnresolvedProviderPathFromPSPath(LocalFilePath.ToString()));

            using (var vds = new VirtualDiskStream(filePath.FullName))
            {
                if (vds.DiskType == DiskType.Fixed)
                {
                    long divisor = Convert.ToInt64(Math.Pow(2, 9));
                    long rem     = 0;
                    Math.DivRem(filePath.Length, divisor, out rem);
                    if (rem != 0)
                    {
                        throw new ArgumentOutOfRangeException("LocalFilePath", "Given vhd file is a corrupted fixed vhd");
                    }
                }
            }

            var parameters = new UploadParameters(
                destinationUri, baseImageUri, filePath, OverWrite.IsPresent,
                (NumberOfUploaderThreads) ?? DefaultNumberOfUploaderThreads)
            {
                Cmdlet            = this,
                BlobObjectFactory = new CloudPageBlobObjectFactory(storageCredentialsFactory, TimeSpan.FromMinutes(1))
            };

            return(parameters);
        }
Ejemplo n.º 5
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.º 6
0
 private static void AssertIfValidVhdSize(FileInfo fileInfo)
 {
     using (var stream = new VirtualDiskStream(fileInfo.FullName))
     {
         if (stream.Length > FourTeraByte)
         {
             var    lengthString         = stream.Length.ToString("N0", CultureInfo.CurrentCulture);
             var    expectedLengthString = FourTeraByte.ToString("N0", CultureInfo.CurrentCulture);
             string message = String.Format("VHD size is too large ('{0}'), maximum allowed size is '{1}'.", lengthString, expectedLengthString);
             throw new InvalidOperationException(message);
         }
     }
 }
        protected static void PopulateContextWithUploadableRanges(FileInfo vhdFile, UploadContextDisk context, bool resume)
        {
            using (var vds = new VirtualDiskStream(vhdFile.FullName))
            {
                IEnumerable <IndexRange> ranges = vds.Extents.Select(e => e.Range).ToArray();

                var bs = new BufferedStream(vds);
                // linear still
                var uploadableRanges = IndexRangeHelper.ChunkRangesBySize(ranges, PageSizeInBytes).ToArray();

                // detecting empty data blocks line. Takes long
                var nonEmptyUploadableRanges = GetNonEmptyRanges(bs, uploadableRanges).ToArray();
                context.UploadableDataSize = nonEmptyUploadableRanges.Sum(r => r.Length);
                context.UploadableRanges   = nonEmptyUploadableRanges;
            }
        }
Ejemplo n.º 8
0
        protected static IEnumerable <DataWithRange> GetDataWithRangesToUpload(FileInfo vhdFile, UploadContextDisk context)
        {
            var uploadableRanges = context.UploadableRanges;
            var manager          = BufferManager.CreateBufferManager(Int32.MaxValue, MaxBufferSize);

            using (var vds = new VirtualDiskStream(vhdFile.FullName))
            {
                foreach (var range in uploadableRanges)
                {
                    var localRange = range;
                    yield return(new DataWithRange(manager)
                    {
                        Data = ReadBytes(vds, localRange, manager),
                        Range = localRange
                    });
                }
            }
            yield break;
        }
Ejemplo n.º 9
0
        private void CheckForInvalidVhd()
        {
            PathIntrinsics currentPath = SessionState.Path;
            FileInfo       filePath    = new FileInfo(currentPath.GetUnresolvedProviderPathFromPSPath(LocalFilePath.ToString()));

            try
            {
                bool resizeNeeded = false;
                long resizeTo     = 0;
                using (VirtualDiskStream vds = new VirtualDiskStream(filePath.FullName))
                {
                    if (vds.Length < 20971520 || vds.Length > 4396972769280)
                    {
                        throw new InvalidOperationException("The VHD must be between 20 MB and 4095 GB.");
                    }

                    if (!this.SkipResizing.IsPresent && (vds.Length - 512) % 1048576 != 0)
                    {
                        resizeNeeded = true;
                        resizeTo     = Convert.ToInt64(1048576 * Math.Ceiling((vds.Length - 512) / 1048576.0));
                    }
                    else if (this.SkipResizing.IsPresent)
                    {
                        WriteVerbose("Skipping VHD resizing.");
                    }

                    FixedSize = vds.Length;
                }

                if (resizeNeeded)
                {
                    resizeVhdFile(resizeTo);
                }
            }
            catch (VhdParsingException)
            {
                throw new InvalidOperationException("The VHD file is corrupted.");
            }

            return;
        }
        public static FileMetaData Create(string filePath)
        {
            var fileInfo = new FileInfo(filePath);
            if(!fileInfo.Exists)
            {
                throw new FileNotFoundException(filePath);
            }

            using(var stream = new VirtualDiskStream(filePath))
            {
                return new FileMetaData
                {
                    FileFullName = fileInfo.FullName,
                    CreatedDateUtc = DateTime.SpecifyKind(fileInfo.CreationTimeUtc, DateTimeKind.Utc),
                    LastModifiedDateUtc = DateTime.SpecifyKind(fileInfo.LastWriteTimeUtc, DateTimeKind.Utc),
                    Size = fileInfo.Length,
                    VhdSize = stream.Length,
                    MD5Hash = CalculateMd5Hash(stream, filePath)
                };
            }
        }
Ejemplo n.º 11
0
        public static FileMetaData Create(string filePath)
        {
            var fileInfo = new FileInfo(filePath);

            if (!fileInfo.Exists)
            {
                throw new FileNotFoundException(filePath);
            }

            using (var stream = new VirtualDiskStream(filePath))
            {
                return(new FileMetaData
                {
                    FileFullName = fileInfo.FullName,
                    CreatedDateUtc = DateTime.SpecifyKind(fileInfo.CreationTimeUtc, DateTimeKind.Utc),
                    LastModifiedDateUtc = DateTime.SpecifyKind(fileInfo.LastWriteTimeUtc, DateTimeKind.Utc),
                    Size = fileInfo.Length,
                    VhdSize = stream.Length,
                    MD5Hash = CalculateMd5Hash(stream, filePath)
                });
            }
        }
Ejemplo n.º 12
0
        private long GetVirtualDiskStreamLength()
        {
            long length;

            try
            {
                using (VirtualDiskStream vds = new VirtualDiskStream(vhdFileToBeUploaded.FullName))
                {
                    length = vds.Length;
                    if (vds.Length < 20971520 || vds.Length > 4396972769280)
                    {
                        throw new InvalidOperationException("The VHD must be between 20 MB and 4095 GB.");
                    }
                }
            }
            catch (VhdParsingException)
            {
                throw new InvalidOperationException("The VHD file is corrupted.");
            }

            return(length);
        }
Ejemplo n.º 13
0
 private static void AssertIfValidVhdSize(FileInfo fileInfo)
 {
     using(var stream = new VirtualDiskStream(fileInfo.FullName))
     {
         if(stream.Length > OneTeraByte)
         {
             var lengthString = stream.Length.ToString("N0", CultureInfo.CurrentCulture);
             var expectedLengthString = OneTeraByte.ToString("N0", CultureInfo.CurrentCulture);
             string message = String.Format("VHD size is too large ('{0}'), maximum allowed size is '{1}'.", lengthString, expectedLengthString);
             throw new InvalidOperationException(message);
         }
     }
 }
Ejemplo n.º 14
0
        async Task StrategyExecuteCmdletAsync(IAsyncCmdlet asyncCmdlet)
        {
            var client = new Client(DefaultProfile.DefaultContext);

            ResourceGroupName   = ResourceGroupName ?? Name;
            VirtualNetworkName  = VirtualNetworkName ?? Name;
            SubnetName          = SubnetName ?? Name;
            PublicIpAddressName = PublicIpAddressName ?? Name;
            SecurityGroupName   = SecurityGroupName ?? Name;

            var resourceClient = AzureSession.Instance.ClientFactory.CreateArmClient <ResourceManagementClient>(
                DefaultProfile.DefaultContext,
                AzureEnvironment.Endpoint.ResourceManager);

            var parameters = new Parameters(this, client, resourceClient);


            if (DiskFile != null)
            {
                if (!resourceClient.ResourceGroups.CheckExistence(ResourceGroupName))
                {
                    Location = Location ?? parameters.DefaultLocation;
                    var st0 = resourceClient.ResourceGroups.CreateOrUpdate(
                        ResourceGroupName,
                        new ResourceGroup
                    {
                        Location = Location,
                        Name     = ResourceGroupName
                    });
                }
                parameters.ImageAndOsType = new ImageAndOsType(
                    Linux ? OperatingSystemTypes.Linux : OperatingSystemTypes.Windows,
                    null,
                    null);
                var storageClient = AzureSession.Instance.ClientFactory.CreateArmClient <StorageManagementClient>(
                    DefaultProfile.DefaultContext,
                    AzureEnvironment.Endpoint.ResourceManager);
                var st1 = storageClient.StorageAccounts.Create(
                    ResourceGroupName,
                    Name,
                    new StorageAccountCreateParameters
                {
                    Sku = new Microsoft.Azure.Management.Storage.Version2017_10_01.Models.Sku
                    {
                        Name = SkuName.PremiumLRS
                    },
                    Location = Location
                });
                var filePath = new FileInfo(SessionState.Path.GetUnresolvedProviderPathFromPSPath(DiskFile));
                using (var vds = new VirtualDiskStream(filePath.FullName))
                {
                    // 2 ^ 9 == 512
                    if (vds.DiskType == DiskType.Fixed && filePath.Length % 512 != 0)
                    {
                        throw new ArgumentOutOfRangeException(
                                  "filePath",
                                  string.Format("Given vhd file '{0}' is a corrupted fixed vhd", filePath));
                    }
                }
                var storageAccount = storageClient.StorageAccounts.GetProperties(ResourceGroupName, Name);
                // BlobUri destinationUri = null;
                BlobUri.TryParseUri(
                    new Uri(string.Format(
                                "{0}{1}/{2}{3}",
                                storageAccount.PrimaryEndpoints.Blob,
                                ResourceGroupName.ToLower(),
                                Name.ToLower(),
                                ".vhd")),
                    out parameters.DestinationUri);
                if (parameters.DestinationUri?.Uri == null)
                {
                    throw new ArgumentNullException("destinationUri");
                }
                var storageCredentialsFactory = new StorageCredentialsFactory(
                    ResourceGroupName, storageClient, DefaultContext.Subscription);
                var uploadParameters = new UploadParameters(parameters.DestinationUri, null, filePath, true, 2)
                {
                    Cmdlet            = this,
                    BlobObjectFactory = new CloudPageBlobObjectFactory(storageCredentialsFactory, TimeSpan.FromMinutes(1))
                };
                if (!string.Equals(
                        Environment.GetEnvironmentVariable("AZURE_TEST_MODE"), "Playback", StringComparison.OrdinalIgnoreCase))
                {
                    var st2 = VhdUploaderModel.Upload(uploadParameters);
                }
            }

            var result = await client.RunAsync(client.SubscriptionId, parameters, asyncCmdlet);

            if (result != null)
            {
                var fqdn     = PublicIPAddressStrategy.Fqdn(DomainNameLabel, Location);
                var psResult = ComputeAutoMapperProfile.Mapper.Map <PSVirtualMachine>(result);
                psResult.FullyQualifiedDomainName = fqdn;
                var connectionString = parameters.ImageAndOsType.GetConnectionString(
                    fqdn,
                    Credential?.UserName);
                asyncCmdlet.WriteVerbose(
                    Resources.VirtualMachineUseConnectionString,
                    connectionString);
                asyncCmdlet.WriteObject(psResult);
            }
        }
Ejemplo n.º 15
0
        async Task StrategyExecuteCmdletAsync(IAsyncCmdlet asyncCmdlet)
        {
            ResourceGroupName   = ResourceGroupName ?? Name;
            VirtualNetworkName  = VirtualNetworkName ?? Name;
            SubnetName          = SubnetName ?? Name;
            PublicIpAddressName = PublicIpAddressName ?? Name;
            SecurityGroupName   = SecurityGroupName ?? Name;

            var imageAndOsType = new ImageAndOsType(OperatingSystemTypes.Windows, null);

            var resourceGroup  = ResourceGroupStrategy.CreateResourceGroupConfig(ResourceGroupName);
            var virtualNetwork = resourceGroup.CreateVirtualNetworkConfig(
                name: VirtualNetworkName, addressPrefix: AddressPrefix);
            var subnet          = virtualNetwork.CreateSubnet(SubnetName, SubnetAddressPrefix);
            var publicIpAddress = resourceGroup.CreatePublicIPAddressConfig(
                name: PublicIpAddressName,
                getDomainNameLabel: () => DomainNameLabel,
                allocationMethod: AllocationMethod);
            var networkSecurityGroup = resourceGroup.CreateNetworkSecurityGroupConfig(
                name: SecurityGroupName,
                openPorts: OpenPorts,
                getOsType: () => imageAndOsType.OsType);
            var networkInterface = resourceGroup.CreateNetworkInterfaceConfig(
                Name, subnet, publicIpAddress, networkSecurityGroup);

            var availabilitySet = AvailabilitySetName == null
                ? null
                : resourceGroup.CreateAvailabilitySetConfig(name: AvailabilitySetName);

            ResourceConfig <VirtualMachine> virtualMachine = null;

            if (DiskFile == null)
            {
                virtualMachine = resourceGroup.CreateVirtualMachineConfig(
                    name: Name,
                    networkInterface: networkInterface,
                    getImageAndOsType: () => imageAndOsType,
                    adminUsername: Credential.UserName,
                    adminPassword: new NetworkCredential(string.Empty, Credential.Password).Password,
                    size: Size,
                    availabilitySet: availabilitySet);
            }
            else
            {
                var resourceClient =
                    AzureSession.Instance.ClientFactory.CreateArmClient <ResourceManagementClient>(DefaultProfile.DefaultContext,
                                                                                                   AzureEnvironment.Endpoint.ResourceManager);
                if (!resourceClient.ResourceGroups.CheckExistence(ResourceGroupName))
                {
                    var st0 = resourceClient.ResourceGroups.CreateOrUpdate(ResourceGroupName, new ResourceGroup
                    {
                        Location = Location,
                        Name     = ResourceGroupName
                    });
                }
                imageAndOsType = new ImageAndOsType(
                    Linux ? OperatingSystemTypes.Linux : OperatingSystemTypes.Windows,
                    null);
                var storageClient =
                    AzureSession.Instance.ClientFactory.CreateArmClient <StorageManagementClient>(DefaultProfile.DefaultContext,
                                                                                                  AzureEnvironment.Endpoint.ResourceManager);
                var st1 = storageClient.StorageAccounts.Create(
                    ResourceGroupName,
                    Name,
                    new StorageAccountCreateParameters
                {
#if !NETSTANDARD
                    AccountType = AccountType.PremiumLRS,
#else
                    Sku = new Microsoft.Azure.Management.Storage.Models.Sku
                    {
                        Name = SkuName.PremiumLRS
                    },
#endif
                    Location = Location
                });
                var filePath = new FileInfo(SessionState.Path.GetUnresolvedProviderPathFromPSPath(DiskFile));
                using (var vds = new VirtualDiskStream(filePath.FullName))
                {
                    if (vds.DiskType == DiskType.Fixed)
                    {
                        long divisor = Convert.ToInt64(Math.Pow(2, 9));
                        long rem     = 0;
                        Math.DivRem(filePath.Length, divisor, out rem);
                        if (rem != 0)
                        {
                            throw new ArgumentOutOfRangeException(
                                      "filePath",
                                      string.Format("Given vhd file '{0}' is a corrupted fixed vhd", filePath));
                        }
                    }
                }
                var     storageAccount = storageClient.StorageAccounts.GetProperties(ResourceGroupName, Name);
                BlobUri destinationUri = null;
                BlobUri.TryParseUri(
                    new Uri(string.Format(
                                "{0}{1}/{2}{3}",
                                storageAccount.PrimaryEndpoints.Blob,
                                ResourceGroupName.ToLower(),
                                Name.ToLower(),
                                ".vhd")),
                    out destinationUri);
                if (destinationUri == null || destinationUri.Uri == null)
                {
                    throw new ArgumentNullException("destinationUri");
                }
                var storageCredentialsFactory = new StorageCredentialsFactory(
                    this.ResourceGroupName, storageClient, DefaultContext.Subscription);
                var parameters = new UploadParameters(destinationUri, null, filePath, true, 2)
                {
                    Cmdlet            = this,
                    BlobObjectFactory = new CloudPageBlobObjectFactory(storageCredentialsFactory, TimeSpan.FromMinutes(1))
                };
                if (!string.Equals(
                        Environment.GetEnvironmentVariable("AZURE_TEST_MODE"), "Playback", StringComparison.OrdinalIgnoreCase))
                {
                    var st2 = VhdUploaderModel.Upload(parameters);
                }
                var disk = resourceGroup.CreateManagedDiskConfig(
                    name: Name,
                    sourceUri: destinationUri.Uri.ToString()
                    );
                virtualMachine = resourceGroup.CreateVirtualMachineConfig(
                    name: Name,
                    networkInterface: networkInterface,
                    osType: imageAndOsType.OsType,
                    disk: disk,
                    size: Size,
                    availabilitySet: availabilitySet);
            }

            var client = new Client(DefaultProfile.DefaultContext);

            // get current Azure state
            var current = await virtualMachine.GetStateAsync(client, new CancellationToken());

            Location = current.UpdateLocation(Location, virtualMachine);

            // generate a domain name label if it's not specified.
            DomainNameLabel = await PublicIPAddressStrategy.UpdateDomainNameLabelAsync(
                domainNameLabel : DomainNameLabel,
                name : Name,
                location : Location,
                client : client);

            var fqdn = PublicIPAddressStrategy.Fqdn(DomainNameLabel, Location);

            if (DiskFile == null)
            {
                imageAndOsType = await client.UpdateImageAndOsTypeAsync(ImageName, Location);
            }

            // create target state
            var target = virtualMachine.GetTargetState(current, client.SubscriptionId, Location);

            if (target.Get(availabilitySet) != null)
            {
                throw new InvalidOperationException("Availability set doesn't exist.");
            }

            // apply target state
            var newState = await virtualMachine
                           .UpdateStateAsync(
                client,
                target,
                new CancellationToken(),
                new ShouldProcess(asyncCmdlet),
                asyncCmdlet.ReportTaskProgress);

            var result = newState.Get(virtualMachine);

            if (result == null)
            {
                result = current.Get(virtualMachine);
            }
            if (result != null)
            {
                var psResult = ComputeAutoMapperProfile.Mapper.Map <PSVirtualMachine>(result);
                psResult.FullyQualifiedDomainName = fqdn;
                asyncCmdlet.WriteVerbose(imageAndOsType.OsType == OperatingSystemTypes.Windows
                    ? "Use 'mstsc /v:" + fqdn + "' to connect to the VM."
                    : "Use 'ssh " + Credential.UserName + "@" + fqdn + "' to connect to the VM.");
                asyncCmdlet.WriteObject(psResult);
            }
        }
Ejemplo n.º 16
0
        async Task StrategyExecuteCmdletAsync(IAsyncCmdlet asyncCmdlet)
        {
            ResourceGroupName   = ResourceGroupName ?? Name;
            VirtualNetworkName  = VirtualNetworkName ?? Name;
            SubnetName          = SubnetName ?? Name;
            PublicIpAddressName = PublicIpAddressName ?? Name;
            DomainNameLabel     = DomainNameLabel ?? (Name + '-' + ResourceGroupName).ToLower();
            SecurityGroupName   = SecurityGroupName ?? Name;

            bool isWindows;

            Commands.Common.Strategies.Compute.Image image = null;
            if (ImageName.Contains(':'))
            {
                var imageArray = ImageName.Split(':');
                if (imageArray.Length != 4)
                {
                    throw new Exception("Invalid ImageName");
                }
                image = new Commands.Common.Strategies.Compute.Image
                {
                    publisher = imageArray[0],
                    offer     = imageArray[1],
                    sku       = imageArray[2],
                    version   = imageArray[3],
                };
                isWindows = image.publisher.ToLower() == "MicrosoftWindowsServer".ToLower();
            }
            else if (!string.IsNullOrEmpty(DiskFile))
            {
                // disk file parameter set requires the OS type input
                isWindows = !Linux;
            }
            else
            {
                // get image
                var osTypeAndImage = Images
                                     .Instance
                                     .SelectMany(osAndMap => osAndMap
                                                 .Value
                                                 .Where(nameAndImage => nameAndImage.Key.ToLower() == ImageName.ToLower())
                                                 .Select(nameAndImage => new
                {
                    OsType = osAndMap.Key,
                    Image  = nameAndImage.Value
                }))
                                     .FirstOrDefault();
                image     = osTypeAndImage.Image;
                isWindows = osTypeAndImage.OsType == "Windows";
            }

            OpenPorts = OpenPorts ?? (isWindows ? new[] { 3389, 5985 } : new[] { 22 });

            var resourceGroup  = ResourceGroupStrategy.CreateResourceGroupConfig(ResourceGroupName);
            var virtualNetwork = resourceGroup.CreateVirtualNetworkConfig(
                name: VirtualNetworkName, addressPrefix: AddressPrefix);
            var subnet          = virtualNetwork.CreateSubnet(SubnetName, SubnetAddressPrefix);
            var publicIpAddress = resourceGroup.CreatePublicIPAddressConfig(
                name: PublicIpAddressName,
                domainNameLabel: DomainNameLabel,
                allocationMethod: AllocationMethod);
            var networkSecurityGroup = resourceGroup.CreateNetworkSecurityGroupConfig(
                name: SecurityGroupName,
                openPorts: OpenPorts);
            var networkInterface = resourceGroup.CreateNetworkInterfaceConfig(
                Name, subnet, publicIpAddress, networkSecurityGroup);
            ResourceConfig <VirtualMachine> virtualMachine = null;

            if (image != null)
            {
                virtualMachine = resourceGroup.CreateVirtualMachineConfig(
                    name: Name,
                    networkInterface: networkInterface,
                    isWindows: isWindows,
                    adminUsername: Credential.UserName,
                    adminPassword: new NetworkCredential(string.Empty, Credential.Password).Password,
                    image: image,
                    size: Size);
            }
            else
            {
                var storageClient =
                    AzureSession.Instance.ClientFactory.CreateArmClient <StorageManagementClient>(DefaultProfile.DefaultContext,
                                                                                                  AzureEnvironment.Endpoint.ResourceManager);
                var st1 = storageClient.StorageAccounts.Create(ResourceGroupName, Name, new StorageAccountCreateParameters
                {
#if !NETSTANDARD
                    AccountType = AccountType.PremiumLRS,
#else
                    Sku = new SM.Sku
                    {
                        Name = SkuName.PremiumLRS
                    },
#endif
                    Location = Location
                });
                var filePath = new FileInfo(SessionState.Path.GetUnresolvedProviderPathFromPSPath(DiskFile));
                using (var vds = new VirtualDiskStream(filePath.FullName))
                {
                    if (vds.DiskType == DiskType.Fixed)
                    {
                        long divisor = Convert.ToInt64(Math.Pow(2, 9));
                        long rem     = 0;
                        Math.DivRem(filePath.Length, divisor, out rem);
                        if (rem != 0)
                        {
                            throw new ArgumentOutOfRangeException("filePath", string.Format("Given vhd file '{0}' is a corrupted fixed vhd", filePath));
                        }
                    }
                }
                var     storageAccount = storageClient.StorageAccounts.GetProperties(ResourceGroupName, Name);
                BlobUri destinationUri = null;
                BlobUri.TryParseUri(new Uri(string.Format("{0}{1}/{2}{3}", storageAccount.PrimaryEndpoints.Blob, Name.ToLower(), Name.ToLower(), ".vhd")), out destinationUri);
                if (destinationUri == null || destinationUri.Uri == null)
                {
                    throw new ArgumentNullException("destinationUri");
                }
                var storageCredentialsFactory = new StorageCredentialsFactory(this.ResourceGroupName, storageClient, DefaultContext.Subscription);
                var parameters = new UploadParameters(destinationUri, null, filePath, true, 2)
                {
                    Cmdlet            = this,
                    BlobObjectFactory = new CloudPageBlobObjectFactory(storageCredentialsFactory, TimeSpan.FromMinutes(1))
                };
                if (!string.Equals(Environment.GetEnvironmentVariable("AZURE_TEST_MODE"), "Playback", StringComparison.OrdinalIgnoreCase))
                {
                    var st2 = VhdUploaderModel.Upload(parameters);
                }
                var disk = resourceGroup.CreateManagedDiskConfig(
                    name: Name,
                    sourceUri: destinationUri.Uri.ToString()
                    );
                virtualMachine = resourceGroup.CreateVirtualMachineConfig(
                    name: Name,
                    networkInterface: networkInterface,
                    isWindows: isWindows,
                    disk: disk,
                    size: Size);
            }

            var client = new Client(DefaultProfile.DefaultContext);

            // get current Azure state
            var current = await virtualMachine.GetStateAsync(client, new CancellationToken());

            if (Location == null)
            {
                Location = current.GetLocation(virtualMachine);
                if (Location == null)
                {
                    Location = "eastus";
                }
            }

            var fqdn = DomainNameLabel + "." + Location + ".cloudapp.azure.com";

            // create target state
            var target = virtualMachine.GetTargetState(current, client.SubscriptionId, Location);

            // apply target state
            var newState = await virtualMachine
                           .UpdateStateAsync(
                client,
                target,
                new CancellationToken(),
                new ShouldProcess(asyncCmdlet),
                asyncCmdlet.ReportTaskProgress);

            var result = newState.Get(virtualMachine);

            if (result == null)
            {
                result = current.Get(virtualMachine);
            }
            if (result != null)
            {
                var psResult = ComputeAutoMapperProfile.Mapper.Map <PSVirtualMachine>(result);
                psResult.FullyQualifiedDomainName = fqdn;
                asyncCmdlet.WriteVerbose(isWindows
                    ? "Use 'mstsc /v:" + fqdn + "' to connect to the VM."
                    : "Use 'ssh " + Credential.UserName + "@" + fqdn + "' to connect to the VM.");
                asyncCmdlet.WriteObject(psResult);
            }
        }
Ejemplo n.º 17
0
        protected static void PopulateContextWithUploadableRanges(FileInfo vhdFile, UploadContext context, bool resume)
        {
            using (var vds = new VirtualDiskStream(vhdFile.FullName))
            {
                IEnumerable<IndexRange> ranges = vds.Extents.Select(e => e.Range).ToArray();

                var bs = new BufferedStream(vds);
                if (resume)
                {
                    var alreadyUploadedRanges = context.DestinationBlob.GetPageRanges().Select(pr => new IndexRange(pr.StartOffset, pr.EndOffset));
                    ranges = IndexRange.SubstractRanges(ranges, alreadyUploadedRanges);
                    context.AlreadyUploadedDataSize = alreadyUploadedRanges.Sum(ir => ir.Length);
                }
                var uploadableRanges = IndexRangeHelper.ChunkRangesBySize(ranges, PageSizeInBytes).ToArray();
                if(vds.DiskType == DiskType.Fixed)
                {
                    var nonEmptyUploadableRanges = GetNonEmptyRanges(bs, uploadableRanges).ToArray();
                    context.UploadableDataSize = nonEmptyUploadableRanges.Sum(r => r.Length);
                    context.UploadableRanges = nonEmptyUploadableRanges;
                }
                else
                {
                    context.UploadableDataSize = uploadableRanges.Sum(r => r.Length);
                    context.UploadableRanges = uploadableRanges;
                }
            }
        }
Ejemplo n.º 18
0
 protected static IEnumerable<DataWithRange> GetDataWithRangesToUpload(FileInfo vhdFile, UploadContext context)
 {
     var uploadableRanges = context.UploadableRanges;
     var manager = BufferManager.CreateBufferManager(Int32.MaxValue, MaxBufferSize);
     using (var vds = new VirtualDiskStream(vhdFile.FullName))
     {
         foreach (var range in uploadableRanges)
         {
             var localRange = range;
             yield return new DataWithRange(manager)
                              {
                                  Data = ReadBytes(vds, localRange, manager),
                                  Range = localRange
                              };
         }
     }
     yield break;
 }