Beispiel #1
0
        //   private String protocol;


        public IOMulticastAndBroadcast(StreamSocket socket, String host, String port)
        {
            SocksParameters      = new HostParameters();
            SocksParameters.Host = host;
            SocksParameters.Port = port;
            this.socket          = socket;
        }
        /// <summary>
        /// Gets a json preprocessor for the given host parameters
        /// </summary>
        public static JsonPreprocessor GetHostJsonPreprocessor(HostParameters parameters)
        {
            return(new JsonPreprocessor(
                       constraintDefinitions: new Dictionary <string, string>()
            {
                { "Stamp", parameters.Stamp },
                { "MachineFunction", parameters.MachineFunction },
                { "Region", parameters.Region },
                { "Ring", parameters.Ring },
                { "Environment", parameters.Environment },
                { "Env", parameters.Environment },
                { "Machine", parameters.Machine },

                // Backward compatibility where machine function was not
                // its own constraint
                { "Feature", parameters.MachineFunction == null ? null : "MachineFunction_" + parameters.MachineFunction },
            }
                       .Where(e => !string.IsNullOrEmpty(e.Value))
                       .Select(e => new ConstraintDefinition(e.Key, new[] { e.Value })),
                       replacementMacros: new Dictionary <string, string>()
            {
                { "Env", parameters.Environment },
                { "Environment", parameters.Environment },
                { "Machine", parameters.Machine },
                { "Stamp", parameters.Stamp },
                { "StampId", parameters.Stamp },
                { "Region", parameters.Region },
                { "RegionId", parameters.Region },
                { "Ring", parameters.Ring },
                { "RingId", parameters.Ring },
                { "ServiceDir", parameters.ServiceDir },
            }
                       .Where(e => !string.IsNullOrEmpty(e.Value))
                       .ToDictionary(e => e.Key, e => e.Value)));
        }
Beispiel #3
0
        // This method gets called by the runtime. Use this method to add services to the container.
        public override void ConfigureServices(IServiceCollection services)
        {
            base.ConfigureServices(services);

            var configurationFile = Configuration["ConfigurationPath"];
            var configJson        = File.ReadAllText(configurationFile);

            var configuration = JsonSerializer.Deserialize <DeploymentServiceConfiguration>(configJson, DeploymentUtilities.ConfigurationSerializationOptions);

            var consoleLog = new ConsoleLog(useShortLayout: false, printSeverity: true);
            var arguments  = new LoggerFactoryArguments(new Logger(consoleLog), new EnvironmentVariableHost(), configuration.LoggingSettings)
            {
                TelemetryFieldsProvider = new HostTelemetryFieldsProvider(HostParameters.FromEnvironment())
            };

            var deploymentService = new DeploymentService(
                configuration: configuration,
                deploymentRoot: new AbsolutePath(Configuration["DeploymentRoot"]),
                secretsProviderFactory: keyVaultUri => new KeyVaultSecretsProvider(keyVaultUri),
                clock: SystemClock.Instance,
                uploadConcurrency: Environment.ProcessorCount);

            services.AddSingleton(deploymentService);
            services.AddSingleton <ILogger>(sp =>
            {
                var lifetime          = sp.GetRequiredService <IHostApplicationLifetime>();
                var replacementLogger = LoggerFactory.CreateReplacementLogger(arguments);
                lifetime.ApplicationStopped.Register(() =>
                {
                    replacementLogger.DisposableToken?.Dispose();
                });
                return(replacementLogger.Logger);
            });
        }
        /// <nodoc />
        public DeploymentProxyService(
            ProxyServiceConfiguration configuration,
            HostParameters hostParameters,
            IAbsFileSystem fileSystem = null,
            IClock clock = null,
            IDeploymentServiceClient client = null)
        {
            clock ??= SystemClock.Instance;
            Configuration        = configuration;
            Root                 = new AbsolutePath(configuration.RootPath);
            Clock                = clock;
            ContentCacheRequests = new VolatileMap <(string, string), AsyncLazy <BoolResult> >(Clock);
            ProxyAddress         = new VolatileMap <UnitValue, AsyncLazy <string> >(Clock);
            Client               = client ?? DeploymentLauncherHost.Instance.CreateServiceClient();
            HostParameters       = hostParameters;

            DownloadQueue = new ActionQueue(configuration.DownloadConcurrency ?? Environment.ProcessorCount);

            Store = new FileSystemContentStoreInternal(
                fileSystem ?? new PassThroughFileSystem(),
                Clock,
                DeploymentUtilities.GetCasRootPath(Root),
                new ConfigurationModel(new ContentStoreConfiguration(new MaxSizeQuota($"{Configuration.RetentionSizeGb}GB"))),
                settings: new ContentStoreSettings()
            {
                TraceFileSystemContentStoreDiagnosticMessages = true,
            });
        }
        public override void ConfigureServices(IServiceCollection services)
        {
            base.ConfigureServices(services);

            bool useExternalServices = Configuration.GetValue <bool>(UseExternalServicesKey, false);

            if (!useExternalServices)
            {
                var hostParameters = HostParameters.FromEnvironment();
                var consoleLog     = new ConsoleLog(useShortLayout: false, printSeverity: true);
                var logger         = new Logger(consoleLog);
                services.AddSingleton <ILogger>(logger);
                services.AddSingleton <BoxRef <OperationContext> >(new OperationContext(new Context(logger)));
                services.AddSingleton <HostParameters>(hostParameters);
            }

            // Add ProxyServiceConfiguration as a singleton in service provider
            services.AddSingleton(sp =>
            {
                var context           = sp.GetRequiredService <BoxRef <OperationContext> >().Value;
                var configurationPath = Configuration["ProxyConfigurationPath"];
                var hostParameters    = sp.GetService <HostParameters>();

                return(context.PerformOperation(
                           new Tracer(nameof(DeploymentProxyStartup)),
                           () =>
                {
                    var proxyConfiguration = CacheServiceRunner.LoadAndWatchPreprocessedConfig <DeploymentConfiguration, ProxyServiceConfiguration>(
                        context,
                        configurationPath,
                        configHash: out _,
                        hostParameters: hostParameters,
                        extractConfig: c => c.Proxy.ServiceConfiguration);

                    return Result.Success(proxyConfiguration);
                },
                           messageFactory: r => $"ConfigurationPath=[{configurationPath}], Port={r.GetValueOrDefault()?.Port}",
                           caller: "LoadConfiguration").ThrowIfFailure());
            });

            // Add DeploymentProxyService as a singleton in service provider
            services.AddSingleton(sp =>
            {
                var hostParameters = sp.GetService <HostParameters>();

                var context       = sp.GetRequiredService <BoxRef <OperationContext> >().Value;
                var configuration = sp.GetRequiredService <ProxyServiceConfiguration>();

                return(context.PerformOperation(
                           new Tracer(nameof(DeploymentProxyStartup)),
                           () =>
                {
                    return Result.Success(new DeploymentProxyService(
                                              configuration,
                                              hostParameters));
                },
                           caller: "CreateProxyService").ThrowIfFailure());
            });
        }
Beispiel #6
0
            /// <summary>
            /// Constructs the service host and takes command line arguments because
            /// ASP.Net core application host is used to parse command line.
            /// </summary>
            public ServiceHost(string[] commandLineArgs, DistributedCacheServiceConfiguration configuration, HostParameters hostParameters)
            {
                HostParameters       = hostParameters;
                ServiceConfiguration = configuration;
                WebHostBuilder       = Host.CreateDefaultBuilder(commandLineArgs)
                                       .ConfigureWebHostDefaults(webBuilder =>
                {
                    webBuilder.UseStartup <CacheServiceStartup>();
                });

                WebHostBuilder.ConfigureHostConfiguration(cb => cb.Add(ConfigurationSource));
            }
Beispiel #7
0
        public ActionResult GetProxyAddress(string contextId, string accessToken, [FromBody] HostParameters parameters)
        {
            OperationContext context = new OperationContext(new Context(contextId, _logger));

            //  Use download token to authenticate.
            if (!_service.TryGetDownloadUrl(context, accessToken: accessToken, traceInfo: parameters.ToString()).Succeeded)
            {
                return(Unauthorized());
            }

            return(Ok(_service.GetProxyBaseAddress(context, parameters)));
        }
Beispiel #8
0
            static Result <CacheServiceWrapperConfiguration> tryCreateConfiguration(DistributedCacheServiceArguments configuration)
            {
                var outOfProcSettings = configuration.Configuration.DistributedContentSettings.OutOfProcCacheSettings;

                if (outOfProcSettings is null)
                {
                    return(Result.FromErrorMessage <CacheServiceWrapperConfiguration>($"{nameof(configuration.Configuration.DistributedContentSettings.OutOfProcCacheSettings)} should not be null."));
                }

                if (outOfProcSettings.Executable is null)
                {
                    return(Result.FromErrorMessage <CacheServiceWrapperConfiguration>($"{nameof(outOfProcSettings.Executable)} is null."));
                }

                if (!File.Exists(outOfProcSettings.Executable))
                {
                    // This is not a bullet proof check, but if the executable is not found we should not even trying to create an out of proc cache service.
                    return(Result.FromErrorMessage <CacheServiceWrapperConfiguration>($"the executable is not found at '{outOfProcSettings.Executable}'."));
                }

                if (outOfProcSettings.CacheConfigPath is null)
                {
                    return(Result.FromErrorMessage <CacheServiceWrapperConfiguration>($"{nameof(outOfProcSettings.CacheConfigPath)} is null."));
                }

                if (!File.Exists(outOfProcSettings.CacheConfigPath))
                {
                    // This is not a bullet proof check, but if the executable is not found we should not even trying to create an out of proc cache service.
                    return(Result.FromErrorMessage <CacheServiceWrapperConfiguration>($"the cache configuration is not found at '{outOfProcSettings.CacheConfigPath}'."));
                }

                // The next layout should be in sync with CloudBuild.
                AbsolutePath executable       = getExecutingPath() / outOfProcSettings.Executable;
                var          workingDirectory = getRootPath(configuration.Configuration);

                var hostParameters = HostParameters.FromTelemetryProvider(configuration.TelemetryFieldsProvider);

                var resultingConfiguration = new CacheServiceWrapperConfiguration(
                    serviceId: "OutOfProcCache",
                    executable: executable,
                    workingDirectory: workingDirectory,
                    hostParameters: hostParameters,
                    cacheConfigPath: new AbsolutePath(outOfProcSettings.CacheConfigPath),
                    // DataRootPath is set in CloudBuild and we need to propagate this configuration to the launched process.
                    dataRootPath: new AbsolutePath(configuration.Configuration.DataRootPath),
                    useInterProcSecretsCommunication: outOfProcSettings.UseInterProcSecretsCommunication);

                outOfProcSettings.ServiceLifetimePollingIntervalSeconds.ApplyIfNotNull(v => resultingConfiguration.ServiceLifetimePollingInterval = TimeSpan.FromSeconds(v));
                outOfProcSettings.ShutdownTimeoutSeconds.ApplyIfNotNull(v => resultingConfiguration.ShutdownTimeout = TimeSpan.FromSeconds(v));

                return(resultingConfiguration);
            }
Beispiel #9
0
        /// <summary>
        /// Loads a configuration object from preprocessed json
        /// </summary>
        public static TConfig LoadPreprocessedConfig <TConfig>(string configurationPath, out string configHash, HostParameters hostParameters = null)
        {
            hostParameters ??= HostParameters.FromEnvironment();
            var configJson = File.ReadAllText(configurationPath);

            configHash = HashInfoLookup.GetContentHasher(HashType.Murmur).GetContentHash(Encoding.UTF8.GetBytes(configJson)).ToHex();

            var preprocessor = DeploymentUtilities.GetHostJsonPreprocessor(hostParameters);

            var preprocessedConfigJson = preprocessor.Preprocess(configJson);

            var config = JsonSerializer.Deserialize <TConfig>(preprocessedConfigJson, DeploymentUtilities.ConfigurationSerializationOptions);

            return(config);
        }
Beispiel #10
0
        private static HostParameters Parse()
        {
            var hostParameters = new HostParameters {
                Modprobes        = GetModprobesList(),
                Rmmod            = GetRmmodList(),
                ModulesBlacklist = GetModulesBlacklistList(),
                OsParameters     = GetOsParametersList(),
                ServicesStart    = GetServicesStartList(),
                ServicesStop     = GetServicesStopList(),
                StartCommands    = GetStartCommandsList(),
                EndCommands      = GetEndCommandsList()
            };

            return(hostParameters);
        }
Beispiel #11
0
        /// <summary>
        /// Run the cache service verb.
        ///
        /// NOTE: Currently, this is highly reliant on being launched by the launcher.
        /// TODO: Add command line args with HostParameters and ServiceLifetime args so that
        /// this can be used standalone.
        /// </summary>
        public static async Task RunCacheServiceAsync(
            OperationContext context,
            string configurationPath,
            Func <HostParameters, DistributedCacheServiceConfiguration, CancellationToken, IDistributedCacheServiceHost> createHost,
            HostParameters hostParameters    = null,
            bool requireServiceInterruptable = true)
        {
            try
            {
                hostParameters ??= HostParameters.FromEnvironment();

                using var cancellableContext = new CancellableOperationContext(context, default(CancellationToken));
                context = cancellableContext;

                var config = LoadAndWatchPreprocessedConfig <DistributedCacheServiceConfiguration, DistributedCacheServiceConfiguration>(
                    context,
                    configurationPath,
                    hostParameters,
                    out var configHash,
                    c => c);

                await ServiceLifetimeManager.RunDeployedInterruptableServiceAsync(context, async token =>
                {
                    var hostInfo = new HostInfo(hostParameters.Stamp, hostParameters.Ring, new List <string>());

                    var host = createHost(hostParameters, config, token);

                    await DistributedCacheServiceFacade.RunWithConfigurationAsync(
                        logger: context.TracingContext.Logger,
                        host: host,
                        hostInfo: hostInfo,
                        telemetryFieldsProvider: new HostTelemetryFieldsProvider(hostParameters)
                    {
                        ConfigurationId = configHash
                    },
                        config,
                        token: token);;

                    return(BoolResult.Success);
                },
                                                                                  requireServiceInterruptionEnabled : requireServiceInterruptable).ThrowIfFailure();
            }
            catch (Exception e)
            {
                Console.WriteLine(e);
                throw;
            }
        }
 public CacheServiceWrapperConfiguration(
     string serviceId,
     AbsolutePath executable,
     AbsolutePath workingDirectory,
     HostParameters hostParameters,
     AbsolutePath cacheConfigPath,
     AbsolutePath dataRootPath,
     bool useInterProcSecretsCommunication)
 {
     ServiceId        = serviceId;
     Executable       = executable;
     WorkingDirectory = workingDirectory;
     HostParameters   = hostParameters;
     CacheConfigPath  = cacheConfigPath;
     DataRootPath     = dataRootPath;
     UseInterProcSecretsCommunication = useInterProcSecretsCommunication;
 }
Beispiel #13
0
            /// <summary>
            /// Constructs the service host and takes command line arguments because
            /// ASP.Net core application host is used to parse command line.
            /// </summary>
            public ServiceHost(string[] commandLineArgs, DistributedCacheServiceConfiguration configuration, HostParameters hostParameters, Context context, CrossProcessSecretsCommunicationKind secretsCommunicationKind = CrossProcessSecretsCommunicationKind.Environment)
                : base(context, secretsCommunicationKind)
            {
                HostParameters       = hostParameters;
                ServiceConfiguration = configuration;
                WebHostBuilder       = Host.CreateDefaultBuilder(commandLineArgs)
                                       .ConfigureWebHostDefaults(webBuilder =>
                {
                    if (UseGrpc)
                    {
                        webBuilder.ConfigureLogging(l =>
                        {
                            l.ClearProviders();

                            // This is left for future reference if ASP.NET logging needs to be enabled
                            //l.AddProvider(new LoggingAdapter("ASPNET", context));
                        });

                        webBuilder.ConfigureKestrel(o =>
                        {
                            int?port = null;
                            var proxyConfiguration = WebHost.Services.GetService <ProxyServiceConfiguration>();
                            if (UseGrpc)
                            {
                                port = (int)ServiceConfiguration.LocalCasSettings.ServiceSettings.GrpcPort;
                            }
                            else if (proxyConfiguration == null)
                            {
                                port = proxyConfiguration.Port;
                            }

                            o.ConfigureEndpointDefaults(listenOptions =>
                            {
                                listenOptions.Protocols = HttpProtocols.Http2;
                            });

                            if (port.HasValue)
                            {
                                o.ListenAnyIP(port.Value);
                            }
                        });
                    }

                    webBuilder.UseStartup <CacheServiceStartup>();
                });
            }
Beispiel #14
0
        /// <summary>
        /// Loads a configuration object from preprocessed json and watches files for changes.
        /// When result config value changes, teardown will be requested.
        /// </summary>
        public static TResultConfig LoadAndWatchPreprocessedConfig <TConfig, TResultConfig>(
            OperationContext context,
            string configurationPath,
            HostParameters hostParameters,
            out string configHash,
            Func <TConfig, TResultConfig> extractConfig,
            Action <Context, string> requestTeardown = null,
            TimeSpan?pollingInterval = null)
        {
            requestTeardown ??= (context, reason) => LifetimeManager.RequestTeardown(context, reason);
            pollingInterval ??= TimeSpan.FromSeconds(5);

            var config       = LoadPreprocessedConfig <TConfig>(configurationPath, out configHash, hostParameters);
            var resultConfig = extractConfig(config);

            var resultConfigString = JsonSerializer.Serialize(resultConfig);

            DeploymentUtilities.WatchFileAsync(
                configurationPath,
                context.Token,
                pollingInterval.Value,
                onChanged: () =>
            {
                var newConfig             = LoadPreprocessedConfig <TConfig>(configurationPath, out _, hostParameters);
                var newResultConfig       = extractConfig(newConfig);
                var newResultConfigString = JsonSerializer.Serialize(resultConfig);
                if (newResultConfigString != resultConfigString)
                {
                    resultConfigString = newResultConfigString;
                    requestTeardown(context, "Configuration changed: " + configurationPath);
                }
            },
                onError: ex =>
            {
                requestTeardown(context, "Error: " + ex.ToString());
            });

            return(resultConfig);
        }
            public TestCacheServerWrapper(TestHost testHost, DistributedCacheServiceArguments arguments)
            {
                string[] commandLineArguments = CreateCommandLine(arguments);

                var hostParameters = HostParameters.FromEnvironment();
                var hostInfo       = new HostInfo(hostParameters.Stamp, hostParameters.Ring, new List <string>());

                var serviceHost = new CacheServiceStartup.ServiceHost(
                    new string[0],
                    arguments.Configuration,
                    hostParameters,
                    new Context(TestGlobal.Logger));

                Host = new TestServiceHost(testHost, serviceHost);

                var _ = arguments.Cancellation.Register(() => Host.StartupCompletedSignal.TrySetCanceled());

                RunInBackground("RunCacheService", async context =>
                {
                    using (var cts = CancellationTokenSource.CreateLinkedTokenSource(context.Token, arguments.Cancellation))
                    {
                        arguments = arguments with
                        {
                            Host         = Host,
                            Cancellation = cts.Token
                        };

                        await DistributedCacheServiceFacade.RunAsync(arguments);

                        Assert.True(cts.IsCancellationRequested, "Cache service task shutdown prematurely");
                    }

                    return(BoolResult.Success);
                },
                                fireAndForget: false);
            }
Beispiel #16
0
 public Task <string> GetProxyBaseAddress(OperationContext context, string serviceUrl, HostParameters parameters, string token)
 {
     return(PostJsonAsync(context, GetProxyBaseAddressQueryUrl(context, serviceUrl, token), parameters));
 }
Beispiel #17
0
 /// <summary>
 /// Configures the host builder to use the given values as services rather than creating its own on
 /// application initialization.
 /// </summary>
 private static void UseExternalServices(IHostBuilder hostBuilder, OperationContext operationContext, HostParameters hostParameters)
 {
     hostBuilder
     .ConfigureWebHostDefaults(webBuilder =>
     {
         webBuilder.UseStartup <CacheServiceStartup>();
     })
     .ConfigureHostConfiguration(configBuilder =>
     {
         configBuilder.AddInMemoryCollection(new Dictionary <string, string>()
         {
             { UseExternalServicesKey, bool.TrueString }
         });
     })
     .ConfigureServices(services =>
     {
         services.AddSingleton <ILogger>(s => operationContext.TracingContext.Logger);
         services.AddSingleton <BoxRef <OperationContext> >(operationContext);
         services.AddSingleton <HostParameters>(hostParameters);
     });
 }
Beispiel #18
0
 public HostTelemetryFieldsProvider(HostParameters hostParameters)
 {
     _hostParmeters = hostParameters;
 }
Beispiel #19
0
 public Task <string> GetProxyBaseAddress(OperationContext context, string serviceUrl, HostParameters parameters, string token)
 {
     return(_inner.GetProxyBaseAddress(context, serviceUrl, parameters, token));
 }
 public Task <string> GetProxyBaseAddress(OperationContext context, string serviceUrl, HostParameters parameters, string token)
 {
     throw new NotImplementedException();
 }