Exemple #1
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);
        }