Exemplo n.º 1
0
        public static async Task <PluginServer> RunService(Assembly pluginAssembly = null)
        {
            var tfRunId = Environment.GetEnvironmentVariable("TF_RUN_ID");

            _log.LogInformation("###############################################################");
            _log.LogInformation("TF Plugin Server Starting up...");
            if (!string.IsNullOrEmpty(tfRunId))
            {
                _log.LogInformation($"TF Run ID: {tfRunId}");
            }

            // For debugging/troubleshooting:
            // _log.LogInformation("ENV: " + string.Join("\n", Environment.GetEnvironmentVariables()
            //     .Cast<System.Collections.DictionaryEntry>()
            //     .Select(x => $"{x.Key}={x.Value}")));

            var magic = System.Environment.GetEnvironmentVariable(HandshakeMagicCookieName);

            if (HandshakeMagicCookieValue != magic)
            {
                throw new Exception("plugin should only be invoked by host");
            }

            if (!int.TryParse(Environment.GetEnvironmentVariable(MinPortName), out var minPort))
            {
                minPort = _listenPort;
            }

            if (!int.TryParse(Environment.GetEnvironmentVariable(MaxPortName), out var maxPort))
            {
                maxPort = _listenPort + 100;
            }

            _listenPort = FindFreePort(minPort, maxPort);
            if (_listenPort <= 0)
            {
                throw new Exception("could not find a free listening port in the requested range");
            }

            _tls = TLSConfigSimple.GenerateSelfSignedRSA();

            _log.LogInformation("listen host: {@listenHost}", _listenHost);
            _log.LogInformation("listen ports: {@minPort}-{@maxPort} : {@listenPort}",
                                minPort, maxPort, _listenPort);

            var server   = new PluginServer(_listenHost, _listenPort, _appProtoVersion, _tls);
            var provider = new ProviderImpl(pluginAssembly);

            server.Services.Add(ProviderImpl.BindService(provider));
            server.Health.SetStatus("plugin", HealthStatus.Serving);
            server.Start();
            await server.WriteHandshakeAsync();

            Console.WriteLine("Hit a key to exit...");
            Console.ReadKey();
            Console.WriteLine("...shutting down...");
            _log.LogInformation("Shutting down...");

            await server.ShutdownAsync();

            _log.LogInformation("Stopped.");
            _log.LogInformation("!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!");

            return(server);
        }
Exemplo n.º 2
0
 /// <summary>
 /// Creates a service definition that can be registered with the server.
 /// </summary>
 public static ServerServiceDefinition BindService(ProviderImpl impl) => Tfplugin5.Provider.BindService(impl);