Exemplo n.º 1
0
 public DeploymentCoordinator(
     ITemplateProvider templateProvider,
     IManagementClientProvider managementClientProvider)
 {
     _templateProvider         = templateProvider;
     _managementClientProvider = managementClientProvider;
 }
 public PoolCoordinator(
     IHttpContextAccessor httpContextAccessor,
     IManagementClientProvider managementClient,
     BatchClientMsiProvider batchClient)
     : base(httpContextAccessor)
 {
     _managementClient    = managementClient;
     _batchClient         = batchClient;
     _httpContextAccessor = httpContextAccessor;
 }
        public async Task BeginDeleteRepositoryAsync(AssetRepository repository, IManagementClientProvider managementClientProvider)
        {
            repository.ProvisioningState = ProvisioningState.Deleting;
            await UpdateRepository(repository);

            await _deploymentQueue.Add(new ActiveDeployment
            {
                FileServerName = repository.Name,
                StartTime      = DateTime.UtcNow,
                Action         = "DeleteVM",
            });
        }
 public BackgroundDeploymentHost(
     IAssetRepoCoordinator assetRepoCoordinator,
     IManagementClientProvider managementClientProvider,
     IDeploymentQueue deploymentQueue,
     ILeaseMaintainer leaseMaintainer,
     ILogger <BackgroundDeploymentHost> logger)
 {
     _assetRepoCoordinator     = assetRepoCoordinator;
     _managementClientProvider = managementClientProvider;
     _deploymentQueue          = deploymentQueue;
     _leaseMaintainer          = leaseMaintainer;
     _logger = logger;
 }
Exemplo n.º 5
0
 public ScaleUpProcessorHost(
     IScaleUpRequestStore requestStore,
     IEnvironmentCoordinator envs,
     ManagementClientMsiProvider batchClientProvider,
     AsyncAutoResetEvent trigger,
     ILogger <ScaleUpProcessorHost> logger)
 {
     _requestStore   = requestStore;
     _clientProvider = batchClientProvider;
     _trigger        = trigger;
     _logger         = logger;
     _envs           = envs;
 }
 public StorageController(
     IConfiguration configuration,
     IManagementClientProvider managementClientProvider,
     CloudBlobClient cloudBlobClient,
     IAssetRepoCoordinator assetRepoCoordinator,
     IEnvironmentCoordinator environmentCoordinator,
     IPackageCoordinator packageCoordinator,
     IAzureResourceProvider azureResourceProvider) : base(environmentCoordinator, packageCoordinator, assetRepoCoordinator)
 {
     _configuration            = configuration;
     _azureResourceProvider    = azureResourceProvider;
     _managementClientProvider = managementClientProvider;
 }
        private async Task DeployFileServer(NfsFileServer repository, IManagementClientProvider managementClientProvider)
        {
            try
            {
                using (var client = await managementClientProvider.CreateResourceManagementClient(repository.SubscriptionId))
                {
                    await client.ResourceGroups.CreateOrUpdateAsync(repository.ResourceGroupName,
                                                                    new ResourceGroup { Location = repository.Subnet.Location });

                    var templateParams = GetTemplateParameters(repository);

                    var properties = new Deployment
                    {
                        Properties = new DeploymentProperties
                        {
                            Template   = await _templateProvider.GetTemplate("linux-file-server.json"),
                            Parameters = _templateProvider.GetParameters(templateParams),
                            Mode       = DeploymentMode.Incremental
                        }
                    };

                    // Start the ARM deployment
                    await client.Deployments.BeginCreateOrUpdateAsync(
                        repository.ResourceGroupName,
                        repository.DeploymentName,
                        properties);

                    // Queue a request for the background host to monitor the deployment
                    // and update the state and IP address when it's done.
                    await _deploymentQueue.Add(new ActiveDeployment
                    {
                        FileServerName = repository.Name,
                        StartTime      = DateTime.UtcNow,
                    });

                    repository.ProvisioningState = ProvisioningState.Running;
                    repository.InProgress        = false;

                    await UpdateRepository(repository);
                }
            }
            catch (CloudException ex)
            {
                _logger.LogError(ex, $"Failed to deploy NFS server: {ex.Message}.");
                throw;
            }
        }
 public AssetRepoCoordinator(
     IConfigRepository <AssetRepository> configCoordinator,
     ITemplateProvider templateProvider,
     IIdentityProvider identityProvider,
     IDeploymentQueue deploymentQueue,
     IManagementClientProvider clientProvider,
     IAzureResourceProvider azureResourceProvider,
     ILogger <AssetRepoCoordinator> logger)
 {
     _configCoordinator     = configCoordinator;
     _templateProvider      = templateProvider;
     _identityProvider      = identityProvider;
     _deploymentQueue       = deploymentQueue;
     _clientProvider        = clientProvider;
     _azureResourceProvider = azureResourceProvider;
     _logger = logger;
 }
Exemplo n.º 9
0
        private static IAssetRepoCoordinator CreateAssetRepoCoordinator(
            IServiceProvider p,
            IManagementClientProvider clientProvider,
            IAzureResourceProvider resourceProvider)
        {
            var cbc   = p.GetRequiredService <CloudBlobClient>();
            var cache = p.GetRequiredService <IMemoryCache>();

            return(new AssetRepoCoordinator(
                       new CachingConfigRepository <AssetRepository>(
                           new GenericConfigRepository <AssetRepository>(cbc.GetContainerReference("storage")),
                           cache),
                       p.GetRequiredService <ITemplateProvider>(),
                       p.GetRequiredService <IIdentityProvider>(),
                       p.GetRequiredService <IDeploymentQueue>(),
                       clientProvider,
                       resourceProvider,
                       p.GetRequiredService <ILogger <AssetRepoCoordinator> >()));
        }
        //
        // Deployment operations
        //
        public async Task BeginRepositoryDeploymentAsync(AssetRepository repository, IManagementClientProvider managementClientProvider, IAzureResourceProvider azureResourceProvider)
        {
            using (var client = await managementClientProvider.CreateResourceManagementClient(repository.SubscriptionId))
            {
                await client.ResourceGroups.CreateOrUpdateAsync(
                    repository.ResourceGroupName,
                    new ResourceGroup(
                        repository.Subnet.Location, // The subnet location pins us to a region
                        tags : AzureResourceProvider.GetEnvironmentTags(repository.EnvironmentName)));

                await azureResourceProvider.AssignManagementIdentityAsync(
                    repository.SubscriptionId,
                    repository.ResourceGroupResourceId,
                    AzureResourceProvider.ContributorRole,
                    _identityProvider.GetPortalManagedServiceIdentity());

                repository.DeploymentName = "FileServerDeployment";
                await UpdateRepository(repository);

                await DeployFileServer(repository as NfsFileServer, managementClientProvider);
            }
        }
Exemplo n.º 11
0
 public StorageController(
     IConfiguration configuration,
     IManagementClientProvider managementClientProvider,
     IAssetRepoCoordinator assetRepoCoordinator,
     IEnvironmentCoordinator environmentCoordinator,
     IPackageCoordinator packageCoordinator,
     IAzureResourceProvider azureResourceProvider,
     IDeploymentCoordinator deploymentCoordinator,
     IIdentityProvider identityProvider,
     ITokenAcquisition tokenAcquisition,
     ILogger <StorageController> logger) : base(
         environmentCoordinator,
         packageCoordinator,
         assetRepoCoordinator,
         tokenAcquisition)
 {
     _configuration            = configuration;
     _azureResourceProvider    = azureResourceProvider;
     _managementClientProvider = managementClientProvider;
     _deploymentCoordinator    = deploymentCoordinator;
     _identityProvider         = identityProvider;
     _logger = logger;
 }
        public async Task <ProvisioningState> UpdateRepositoryFromDeploymentAsync(
            AssetRepository repository,
            IManagementClientProvider managementClientProvider)
        {
            var deployment = await GetDeploymentAsync(repository, managementClientProvider);

            if (deployment == null)
            {
                return(ProvisioningState.Failed);
            }

            var fileServer = repository as NfsFileServer;

            if (fileServer == null)
            {
                return(ProvisioningState.Failed);
            }

            string privateIp = null;
            string publicIp  = null;

            Enum.TryParse <ProvisioningState>(deployment.Properties.ProvisioningState, out var deploymentState);
            if (deploymentState == ProvisioningState.Succeeded)
            {
                (privateIp, publicIp) = await GetIpAddressesAsync(fileServer, managementClientProvider);
            }

            if (deploymentState == ProvisioningState.Succeeded || deploymentState == ProvisioningState.Failed)
            {
                fileServer.ProvisioningState = deploymentState;
                fileServer.PrivateIp         = privateIp;
                fileServer.PublicIp          = publicIp;
                await UpdateRepository(fileServer);
            }

            return(deploymentState);
        }
Exemplo n.º 13
0
 public VMSizes(IManagementClientProvider managementClient)
 {
     _managementClientProvider = managementClient;
 }
        private async Task <DeploymentExtended> GetDeploymentAsync(AssetRepository assetRepo, IManagementClientProvider managementClientProvider)
        {
            using (var resourceClient = await managementClientProvider.CreateResourceManagementClient(assetRepo.SubscriptionId))
            {
                try
                {
                    return(await resourceClient.Deployments.GetAsync(
                               assetRepo.ResourceGroupName,
                               assetRepo.DeploymentName));
                }
                catch (CloudException e)
                {
                    if (ResourceNotFound(e))
                    {
                        return(null);
                    }

                    throw;
                }
            }
        }
        private async Task <(string privateIp, string publicIp)> GetIpAddressesAsync(NfsFileServer fileServer, IManagementClientProvider managementClientProvider)
        {
            using (var computeClient = await managementClientProvider.CreateComputeManagementClient(fileServer.SubscriptionId))
                using (var networkClient = await managementClientProvider.CreateNetworkManagementClient(fileServer.SubscriptionId))
                {
                    var vm = await computeClient.VirtualMachines.GetAsync(fileServer.ResourceGroupName, fileServer.VmName);

                    var networkIfaceName = vm.NetworkProfile.NetworkInterfaces.First().Id.Split("/").Last();
                    var net = await networkClient.NetworkInterfaces.GetAsync(fileServer.ResourceGroupName, networkIfaceName);

                    var firstConfiguration = net.IpConfigurations.First();

                    var privateIp  = firstConfiguration.PrivateIPAddress;
                    var publicIpId = firstConfiguration.PublicIPAddress?.Id;
                    var publicIp   =
                        publicIpId != null
                        ? await networkClient.PublicIPAddresses.GetAsync(
                            fileServer.ResourceGroupName,
                            publicIpId.Split("/").Last())
                        : null;

                    return(privateIp, publicIp?.IpAddress);
                }
        }
        public async Task DeleteRepositoryResourcesAsync(AssetRepository repository, IManagementClientProvider managementClientProvider)
        {
            var fileServer = repository as NfsFileServer;

            if (fileServer == null)
            {
                return;
            }

            using (var resourceClient = await managementClientProvider.CreateResourceManagementClient(repository.SubscriptionId))
                using (var computeClient = await managementClientProvider.CreateComputeManagementClient(repository.SubscriptionId))
                    using (var networkClient = await managementClientProvider.CreateNetworkManagementClient(repository.SubscriptionId))
                    {
                        try
                        {
                            var virtualMachine = await computeClient.VirtualMachines.GetAsync(fileServer.ResourceGroupName, fileServer.VmName);

                            var nicName   = virtualMachine.NetworkProfile.NetworkInterfaces[0].Id.Split("/").Last();;
                            var avSetName = virtualMachine.AvailabilitySet.Id?.Split("/").Last();
                            var osDisk    = virtualMachine.StorageProfile.OsDisk.ManagedDisk.Id.Split("/").Last();
                            var dataDisks = virtualMachine.StorageProfile.DataDisks.Select(dd => dd.ManagedDisk.Id.Split("/").Last()).ToList();

                            string pip = null;
                            string nsg = null;
                            try
                            {
                                var nic = await networkClient.NetworkInterfaces.GetAsync(fileServer.ResourceGroupName, nicName);

                                pip = nic.IpConfigurations[0].PublicIPAddress?.Id.Split("/").Last();
                                nsg = nic.NetworkSecurityGroup?.Id.Split("/").Last();
                            }
                            catch (CloudException ex) when(ResourceNotFound(ex))
                            {
                                // NIC doesn't exist
                            }

                            await IgnoreNotFound(async() =>
                            {
                                await computeClient.VirtualMachines.GetAsync(fileServer.ResourceGroupName, fileServer.VmName);
                                await computeClient.VirtualMachines.DeleteAsync(fileServer.ResourceGroupName, fileServer.VmName);
                            });

                            if (nicName != null)
                            {
                                await IgnoreNotFound(() => networkClient.NetworkInterfaces.DeleteAsync(fileServer.ResourceGroupName, nicName));
                            }

                            var tasks = new List <Task>();

                            if (nsg == "nsg")
                            {
                                tasks.Add(IgnoreNotFound(() => networkClient.NetworkSecurityGroups.DeleteAsync(fileServer.ResourceGroupName, nsg)));
                            }

                            if (pip != null)
                            {
                                tasks.Add(IgnoreNotFound(() => networkClient.PublicIPAddresses.DeleteAsync(fileServer.ResourceGroupName, pip)));
                            }

                            tasks.Add(IgnoreNotFound(() => computeClient.Disks.DeleteAsync(fileServer.ResourceGroupName, osDisk)));

                            tasks.AddRange(dataDisks.Select(
                                               dd => IgnoreNotFound(() => computeClient.Disks.DeleteAsync(fileServer.ResourceGroupName, dd))));

                            await Task.WhenAll(tasks);

                            if (avSetName != null)
                            {
                                await IgnoreNotFound(() => computeClient.AvailabilitySets.DeleteAsync(fileServer.ResourceGroupName, avSetName));
                            }
                        }
                        catch (CloudException ex) when(ResourceNotFound(ex))
                        {
                            // VM doesn't exist
                        }

                        try
                        {
                            await resourceClient.ResourceGroups.GetAsync(fileServer.ResourceGroupName);

                            var resources = await resourceClient.Resources.ListByResourceGroupAsync(fileServer.ResourceGroupName);

                            if (resources.Any())
                            {
                                _logger.LogDebug($"Skipping resource group deletion as it contains the following resources: {string.Join(", ", resources.Select(r => r.Id))}");
                            }
                            else
                            {
                                await resourceClient.ResourceGroups.DeleteAsync(fileServer.ResourceGroupName);
                            }
                        }
                        catch (CloudException ex) when(ResourceNotFound(ex))
                        {
                            // RG doesn't exist
                        }

                        await RemoveRepository(repository);
                    }
        }