public async Task <ActionResult> PowerOperation(string repoId, string operation)
        {
            var fileServer = await _assetRepoCoordinator.GetRepository(repoId) as NfsFileServer;

            if (fileServer == null)
            {
                return(NotFound($"No NFS File Server found with the name: {repoId}"));
            }

            if (operation != "start" && operation != "shutdown")
            {
                return(BadRequest("Action must be 'start' or 'shutdown'"));
            }

            using (var computeClient = await _managementClientProvider.CreateComputeManagementClient(fileServer.SubscriptionId))
            {
                if (operation == "start")
                {
                    await computeClient.VirtualMachines.BeginStartWithHttpMessagesAsync(fileServer.ResourceGroupName, fileServer.VmName);
                }
                else if (operation == "shutdown")
                {
                    await computeClient.VirtualMachines.BeginDeallocateAsync(fileServer.ResourceGroupName, fileServer.VmName);
                }
            }

            return(RedirectToAction("Overview", new { repoId = repoId }));
        }
Esempio n. 2
0
        public async Task <IReadOnlyList <VirtualMachineSize> > GetSizes(RenderingEnvironment environment)
        {
            bool IsSupportedSize(VirtualMachineSize vmSize)
            {
                var size = vmSize.Name.Substring(vmSize.Name.IndexOf('_') + 1);

                return
                    (size.StartsWith("D", StringComparison.Ordinal) ||
                     size.StartsWith("NC", StringComparison.Ordinal) ||
                     size.StartsWith("F", StringComparison.Ordinal) ||
                     size.StartsWith("H", StringComparison.Ordinal));
            }

            var sizes = new List <VirtualMachineSize>();

            using (var computeClient = await _managementClientProvider.CreateComputeManagementClient(environment.SubscriptionId))
            {
                var vmSizes = await computeClient.VirtualMachineSizes.ListAsync(environment.BatchAccount.Location);

                sizes.AddRange(vmSizes.Where(IsSupportedSize));
            }

            return(sizes);
        }
        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);
                }
        }
        private async Task <List <string> > GetAvereVMNames(AvereCluster avereCluster)
        {
            var controllerName = avereCluster.ControllerName;
            var vfxtPrefix     = avereCluster.ClusterName;

            using (var computeClient = await _clientProvider.CreateComputeManagementClient(avereCluster.SubscriptionId))
            {
                var vms = await computeClient.VirtualMachines.ListAsync(avereCluster.ResourceGroupName);

                var vmNames = vms.Select(vm => vm.Name).Where(name => IsAvereVM(avereCluster, name)).ToList();
                while (!string.IsNullOrEmpty(vms.NextPageLink))
                {
                    vms = await computeClient.VirtualMachines.ListAsync(avereCluster.ResourceGroupName);

                    vmNames.AddRange(vms.Select(vm => vm.Name).Where(name => IsAvereVM(avereCluster, name)));
                }
                return(vmNames);
            }
        }
        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);
                    }
        }