Exemple #1
0
        private void SaveHaproxy(string publicIp, List <NodeModel> nodes)
        {
            ConsoleLogger.Log("[cluster] init haproxy");
            CommandLauncher.Launch("haproxy-stop");
            if (File.Exists(_haproxyFileOutput))
            {
                File.Copy(_haproxyFileOutput, $"{_haproxyFileOutput}.bck", true);
            }
            ConsoleLogger.Log("[cluster] set haproxy file");
            var clusterInfo = ClusterConfiguration.GetClusterInfo();
            var ports       = clusterInfo.PortMapping;

            if (!ports.Any())
            {
                return;
            }
            var lines = new List <string> {
                "global",
                "    daemon",
                "    log 127.0.0.1   local0",
                "    log 127.0.0.1   local1 notice",
                "    maxconn 4096",
                "    user haproxy",
                "    group haproxy",
                "",
                "defaults",
                "    log     global",
                "    mode    http",
                "    option  httplog",
                "    option  dontlognull",
                "    retries 3",
                "    option  redispatch",
                "    maxconn 2000",
                "    timeout connect 5000",
                "    timeout client  50000",
                "    timeout server  50000",
                ""
            };
            var localport     = new AppConfiguration().Get().AntdUiPort;
            var localServices = new ApiConsumer().Get <List <RssdpServiceModel> >($"http://localhost:{localport}/device/services");

            foreach (var portMapping in ports)
            {
                portMapping.ServicePort = localServices.FirstOrDefault(_ => _.Name == portMapping.ServiceName)?.Port;
                if (string.IsNullOrEmpty(portMapping.ServicePort))
                {
                    continue;
                }
                var frontEndLabel = $"fe_in{portMapping.ServicePort}_out{portMapping.VirtualPort}";
                var backEndLabel  = $"be_in{portMapping.ServicePort}_out{portMapping.VirtualPort}";
                lines.Add($"frontend {frontEndLabel}");
                lines.Add("    mode http");
                lines.Add($"    bind {clusterInfo.VirtualIpAddress}:{portMapping.VirtualPort} transparent");
                lines.Add("    stats enable");
                lines.Add("    stats auth admin:Anthilla");
                lines.Add("    option httpclose");
                lines.Add("    option forwardfor");
                lines.Add($"    default_backend {backEndLabel}");
                lines.Add("");
                lines.Add($"backend {backEndLabel}");
                lines.Add("    balance roundrobin");
                lines.Add("    cookie JSESSIONID prefix");
                lines.Add("    option httpchk HEAD /check.txt HTTP/1.0");
                foreach (var node in nodes)
                {
                    lines.Add($"    server {node.Hostname} {node.PublicIp}:{portMapping.ServicePort} check");
                }
                lines.Add("");
            }

            File.WriteAllLines(_haproxyFileOutput, lines);
            CommandLauncher.Launch("haproxy-start", new Dictionary <string, string> {
                { "$file", _haproxyFileOutput }
            });
            ConsoleLogger.Log("[cluster] haproxy started");
        }