Exemple #1
0
        /// <summary>
        /// Gets a list of VMs in the target subscription.
        /// </summary>
        /// <param name="client">The <see cref="ComputeManagementClient"/> that is performing the operation.</param>
        /// <returns>The full list of VMs in the subscription.</returns>
        /// <remarks>
        /// While debugging the enumeration might not be visible if tasks haven't all completed.
        /// To change this behaviour just force the enumeration after calling this by using the <see cref="Enumerable.ToList{T}(IEnumerable{T})"/>.
        /// </remarks>
        public static async Task <IEnumerable <string> > ListVmsAsync(this ComputeManagementClient client)
        {
            Contract.Requires(client != null);

            return((await client.HostedServices.ListAsync())
                   .Select(async s => await client.GetAzureDeyploymentAsync(s.ServiceName, DeploymentSlot.Production))
                   .Select(t => t.Result)
                   .Where(d => d != null && d.Roles.Count > 0)
                   .SelectMany(d => d.Roles.Where(r => r.RoleType == VirtualMachineRoleType.PersistentVMRole.ToString()))
                   .Select(r => r.RoleName));
        }
        private async Task DeployCloudServiceAsync(
            ComputeManagementClient computeClient,
            CloudBlobContainer container,
            string serviceName,
            string packagePath,
            string configurationPath,
            string diagnosticsConfigurationPath,
            DeploymentSlot targetSlot)
        {
            ThreadAdapter.QueueObject(new BuildEvent(BuildEventType.Information, BuildEventImportance.Medium,
                                                     $"[{serviceName}] Checking the existence of an existing deployment"));

            var deployment = await computeClient.GetAzureDeyploymentAsync(serviceName, targetSlot);

            if (ForceDelete && deployment != null)
            {
                ThreadAdapter.QueueObject(new BuildEvent(BuildEventType.Information, BuildEventImportance.Medium,
                                                         $"[{serviceName}] ForceDelete is true and found an existing deployment: Deleting it."));

                await computeClient.Deployments.DeleteBySlotAsync(serviceName, targetSlot);

                deployment = null;
            }

            var blob = container.GetBlockBlobReference(DateTime.Now.ToString("yyyyMMdd_HHmmss_") + Path.GetFileName(packagePath));

            ThreadAdapter.QueueObject(new BuildEvent(BuildEventType.Information, BuildEventImportance.Medium,
                                                     $"[{serviceName}] Uploading the cloud service package to storage account {StorageAccountName} in the {StorageContainer} container."));

            await blob.UploadFromFileAsync(packagePath, FileMode.Open);

            if (diagnosticsConfigurationPath != null)
            {
                ThreadAdapter.QueueObject(new BuildEvent(BuildEventType.Information, BuildEventImportance.Medium,
                                                         $"[{serviceName}] Checking the Cloud Service for the PaaS Diagnostics extension -> Creating it if it doesn't exist."));

                var diagnosticsConfiguration = File.ReadAllText(diagnosticsConfigurationPath);
                var diagnosticsCreated       = await computeClient.AddDiagnosticsExtensionIfNotExistsAsync(serviceName, diagnosticsConfiguration);

                if (!diagnosticsCreated)
                {
                    diagnosticsConfigurationPath = null;
                }
            }

            if (deployment == null)
            {
                ThreadAdapter.QueueObject(new BuildEvent(BuildEventType.Information, BuildEventImportance.Medium,
                                                         $"[{serviceName}] Found no previous deployments -> Creating a new deployment into {targetSlot.GetEnumDescription()}."));

                var createParams = new DeploymentCreateParameters
                {
                    Label           = serviceName,
                    Name            = $"{serviceName}{targetSlot.GetEnumDescription()}",
                    PackageUri      = blob.Uri,
                    Configuration   = File.ReadAllText(configurationPath),
                    StartDeployment = true
                };

                if (diagnosticsConfigurationPath != null)
                {
                    createParams.ExtensionConfiguration = new ExtensionConfiguration {
                        AllRoles = new[] { new ExtensionConfiguration.Extension {
                                               Id = FlexConfiguration.FlexDiagnosticsExtensionId
                                           } }
                    };
                }

                await computeClient.Deployments.CreateAsync(serviceName, targetSlot, createParams);
            }
            else
            {
                ThreadAdapter.QueueObject(new BuildEvent(BuildEventType.Information, BuildEventImportance.Medium,
                                                         $"[{serviceName}] Found a previous deployment -> Updating the current deployment in {targetSlot.GetEnumDescription()}."));

                await computeClient.Deployments.UpgradeBySlotAsync(
                    serviceName,
                    targetSlot,
                    new DeploymentUpgradeParameters
                {
                    Label         = serviceName,
                    PackageUri    = blob.Uri,
                    Configuration = File.ReadAllText(configurationPath)
                });
            }

            if (VipSwap)
            {
                ThreadAdapter.QueueObject(new BuildEvent(BuildEventType.Information, BuildEventImportance.Medium,
                                                         $"[{serviceName}] Swapping the deployments."));

                await computeClient.Deployments.SwapAsync(
                    serviceName,
                    new DeploymentSwapParameters
                {
                    SourceDeployment = $"{serviceName}{targetSlot.GetEnumDescription()}"
                });

                if (DeleteStaging)
                {
                    ThreadAdapter.QueueObject(new BuildEvent(BuildEventType.Information, BuildEventImportance.Medium,
                                                             $"[{serviceName}] Deleting the staging deployment after the swap."));

                    await computeClient.Deployments.DeleteBySlotAsync(serviceName, DeploymentSlot.Staging);
                }
            }

            ThreadAdapter.QueueObject(new BuildEvent(BuildEventType.Information, BuildEventImportance.Medium,
                                                     $"[{serviceName}] Deployment complete."));
        }