public async Task StartQueueListener()
        {
            await queueService.OnQueueMessageInit(async (message) =>
            {
                logger.LogInformation(message);

                var clusterResult = JsonSerializer.Deserialize <ClusterCreationResultMessage>(message);
                var cluster       = await clusterRepository.ReadAsync(c => c.OrderId == clusterResult.Data.OrderId);

                if (cluster != null && clusterResult.Pattern == "success")
                {
                    cluster.KubeConfig     = clusterResult.Data.KubeConfig.Replace($"server: https://{cluster.Ip}:6443", $"server: https://{cluster.Name}.{cluster.Domain}:6443");
                    cluster.KubeConfigJson = clusterResult.Data.KubeConfigAsJson;

                    if ((await clusterRepository.UpdateAsync(cluster)) > 0)
                    {
                        await hubContext.Clients.All.NotificationReceived("Cluster ready", "Cluster created and ready to use.", "success");
                    }
                }
            });

            await queueService.OnQueueDeleteMessageInit(async (message) =>
            {
                logger.LogInformation(message);

                var cluster = JsonSerializer.Deserialize <Cluster>(message);
                await StartCleanClusterQueueListener(cluster);
            });
        }
        public async Task <bool> LinkDomainToClusterAsync(DomainLinkingRequestContract request)
        {
            var domainName = await domainRepository.ReadAsync(d => d.Id == request.DomainId && d.ValidationDate.HasValue);

            var cluster = await clusterRepository.ReadAsync(c => c.Id == request.ClusterId && !c.DeleteAt.HasValue);

            var domainLink = await domainRepository.ReadAsync(d => d.Value == request.SubDomain);

            if (domainLink != null)
            {
                throw new DuplicateException();
            }

            if (domainName == null || cluster == null)
            {
                throw new NotFoundException();
            }

            if (domainName.ValidationDate.HasValue)
            {
                try
                {
                    if (request.Protocol == "HTTP")
                    {
                        await ProcessHttpDomainLink(request, cluster, domainName);
                    }
                    else if (request.Protocol == "TLS")
                    {
                        await ProcessTlsDomainLink(request, cluster, domainName);
                    }
                    else
                    {
                        await ProcessHttpsDomainLink(request, cluster, domainName);
                    }

                    return(true);
                }
                catch (Exception e)
                {
                    logger.LogError(e, "Error on linking.");
                }
            }

            return(false);
        }
        public async Task StartQueueListener()
        {
            await queueService.OnQueueMessageInit(async (message) =>
            {
                var clusterResult = JsonSerializer.Deserialize <ClusterCreationResultMessage>(message);
                var cluster       = await clusterRepository.ReadAsync(c => c.OrderId == clusterResult.Data.OrderId);

                if (cluster != null)
                {
                    cluster.KubeConfig     = clusterResult.Data.KubeConfig;
                    cluster.KubeConfigJson = clusterResult.Data.KubeConfigAsJson;

                    if ((await clusterRepository.UpdateClusterAsync(cluster)) > 0)
                    {
                        await hubContext.Clients.All.NotificationReceived("Cluster ready", "Cluster created and ready to use.", "success");
                    }
                }
            });
        }
Beispiel #4
0
        public async Task <bool> CreateClusterAsync(ClusterCreateRequest request)
        {
            if ((await clusterRepository.ReadAsync(c => c.Name == request.Name)) != null)
            {
                throw new DuplicateException();
            }

            var selectedSshKey = await sshKeyRepository.ReadAsync(request.SshKeyId);

            var selectedNode = await datacenterRepository.ReadAsync(f => f.Id == request.DeployNodeId);

            var template = await templateRepository.ReadAsync(t => t.Id == request.SelectedTemplate);

            var existingCluster = await clusterRepository.ReadsAsync(c => c.ProxmoxNodeId == request.DeployNodeId);

            var baseIp = existingCluster.Any() ? GetBasedRangeIp(existingCluster) : 10;
            var baseId = existingCluster.Any() ? ExtractBaseId(await clusterRepository.GetMaxOrder()) : 3000;

            if (baseIp > 0 && baseId > 0)
            {
                var newCluster = new Cluster()
                {
                    Cpu           = request.Cpu,
                    Name          = $"k3s-master-{request.Name}",
                    Node          = request.Node,
                    Storage       = request.Storage,
                    User          = "******",
                    ProxmoxNodeId = selectedNode.Id,
                    OrderId       = baseId,
                    Memory        = request.Memory,
                    Ip            = $"10.0.{selectedNode.Id}.{baseIp}",
                    SshKey        = selectedSshKey.Public
                };

                if ((await clusterRepository.InsertClusterAsync(newCluster)) > 0)
                {
                    var nodeList = new List <ClusterNode>();

                    for (int i = 0; i < request.Node; i++)
                    {
                        nodeList.Add(new ClusterNode()
                        {
                            ClusterId = newCluster.Id,
                            OrderId   = baseId + i + 1,
                            Name      = $"k3s-node{i + 1}-{request.Name}",
                            Ip        = $"10.0.{selectedNode.Id}.{baseIp + i + 1}",
                        });
                    }

                    if ((await clusterNodeRepository.InsertClusterNodesAsync(nodeList.ToArray()) == request.Node))
                    {
                        ClusterCreateMessage message = GenerateQueueMessage(request, newCluster, selectedSshKey, selectedNode, template, baseIp, baseId);
                        queueService.QueueClusterCreation(message);

                        await InjectClusterCacheConfigurationAsync(newCluster);
                    }

                    return(true);
                }
            }

            return(false);
        }