Beispiel #1
0
        /// <summary>
        /// Rollback all imported cloud services.
        /// </summary>
        /// <param name="cloudServices">Cloud services to be deleted.</param>
        private void RollBackServices(List <string> cloudServices)
        {
            string methodName = System.Reflection.MethodBase.GetCurrentMethod().Name;

            Logger.Info(methodName, ProgressResources.ExecutionStarted, ResourceType.CloudService.ToString());
            Logger.Info(methodName, ProgressResources.RollbackCloudServices, ResourceType.CloudService.ToString());
            dcMigrationManager.ReportProgress(ProgressResources.RollbackCloudServices);
            Stopwatch swTotalServices = new Stopwatch();

            swTotalServices.Start();
            //string origServiceName = null;
            if (cloudServices.Count > 0)
            {
                using (var client = new ComputeManagementClient(importParameters.DestinationSubscriptionSettings.Credentials))
                {
                    Parallel.ForEach(cloudServices, cloudService =>
                    {
                        try
                        {
                            Stopwatch swService = new Stopwatch();
                            swService.Start();
                            string origServiceName = resourceImporter.GetSourceResourceName(ResourceType.CloudService, cloudService);
                            Retry.RetryOperation(() => client.HostedServices.DeleteAll(cloudService),
                                                 (BaseParameters)importParameters, ResourceType.CloudService, cloudService, ignoreResourceNotFoundEx: true);

                            CloudService service = subscription.DataCenters.FirstOrDefault().CloudServices.
                                                   Where(ser => (ser.CloudServiceDetails.ServiceName == origServiceName)).FirstOrDefault();
                            if (service.DeploymentDetails != null)
                            {
                                string deploymentName = resourceImporter.GetDestinationResourceName(
                                    ResourceType.Deployment, service.DeploymentDetails.Name, ResourceType.CloudService,
                                    resourceImporter.GetDestinationResourceName(ResourceType.CloudService, service.CloudServiceDetails.ServiceName)
                                    );
                                resourceImporter.UpdateMedatadaFile(ResourceType.Deployment, deploymentName, false,
                                                                    resourceImporter.GetDestinationResourceName(ResourceType.CloudService, service.CloudServiceDetails.ServiceName)
                                                                    );
                                Logger.Info(methodName, string.Format(ProgressResources.RollbackDeployment, service.DeploymentDetails.Name,
                                                                      service.DeploymentDetails.Name), ResourceType.Deployment.ToString(), service.DeploymentDetails.Name);

                                foreach (VirtualMachine vm in service.DeploymentDetails.VirtualMachines)
                                {
                                    string virtualmachineName = resourceImporter.GetDestinationResourceName(
                                        ResourceType.VirtualMachine, vm.VirtualMachineDetails.RoleName, ResourceType.CloudService,
                                        resourceImporter.GetDestinationResourceName(ResourceType.CloudService, service.CloudServiceDetails.ServiceName)
                                        );

                                    resourceImporter.UpdateMedatadaFile(ResourceType.VirtualMachine, virtualmachineName, false,
                                                                        resourceImporter.GetDestinationResourceName(ResourceType.CloudService, service.CloudServiceDetails.ServiceName)
                                                                        );
                                    Logger.Info(methodName, string.Format(ProgressResources.RollbackVirtualMachine, vm.VirtualMachineDetails.RoleName),
                                                ResourceType.VirtualMachine.ToString(), vm.VirtualMachineDetails.RoleName);
                                }
                            }
                            resourceImporter.UpdateMedatadaFile(ResourceType.CloudService, cloudService, false);
                            swService.Stop();
                            Logger.Info(methodName, string.Format(ProgressResources.RollbackCloudService, cloudService, swService.Elapsed.Days, swService.Elapsed.Hours,
                                                                  swService.Elapsed.Minutes, swService.Elapsed.Seconds), ResourceType.CloudService.ToString(), cloudService);
                        }
                        catch (AggregateException exAgg)
                        {
                            foreach (var ex in exAgg.InnerExceptions)
                            {
                                Logger.Error(methodName, exAgg, ResourceType.CloudService.ToString(), cloudService);
                            }
                            throw;
                        }
                    });
                    Logger.Info(methodName, ProgressResources.RollbackCloudServicesWaiting, ResourceType.CloudService.ToString());
                    dcMigrationManager.ReportProgress(ProgressResources.RollbackCloudServicesWaiting);
                    Task.Delay(Constants.DelayTimeInMilliseconds_Rollback).Wait();
                }
            }
            swTotalServices.Stop();
            Logger.Info(methodName, string.Format(ProgressResources.ExecutionCompletedWithTime, swTotalServices.Elapsed.Days, swTotalServices.Elapsed.Hours, swTotalServices.Elapsed.Minutes,
                                                  swTotalServices.Elapsed.Seconds), ResourceType.CloudService.ToString());
        }