using System.Collections; using System.Runtime.InteropServices; using Anubis.Core; using Anubis.Platform; using Anubis.Supervision; // The guard (SPEC 9.3, ADR 0012): the main process of the container and the parent of the engine. Its arguments go to // the engine as they are. "drill" runs the guard drill in a temporary folder instead. if (args is ["drill", ..]) { return await GuardDrill.RunAsync(GuardLaunch.Server(), Console.Out, CancellationToken.None).ConfigureAwait(false); } var environment = Environment.GetEnvironmentVariables().Cast() .ToDictionary(e => (string)e.Key, e => e.Value as string, StringComparer.Ordinal); string root; try { root = DataRoot(args, environment); } catch (StartupRefusedException refused) { await Console.Error.WriteLineAsync(refused.Message).ConfigureAwait(false); return 2; } var paths = new GuardPaths(root); var logger = new LineLogger(Console.Out.WriteLine); using var http = Anubis.Brokers.Outgoing.KeyedClient(); http.Timeout = TimeSpan.FromSeconds(20); var clock = SystemClock.Instance; var guardian = new Guardian(paths, clock, new GuardOptions(), start => ChildEngine.Start(GuardLaunch.Dotnet(), [GuardLaunch.Server(), .. args], environment, start), (account, keys) => GuardBrokers.Create(http, clock, account, keys), (text, keys, ct) => GuardBrokers.AlertAsync(http, logger, keys, text, ct), logger); // The stop of the container (SIGTERM) or Ctrl+C: passed on to the engine, which closes in order and is not restarted. using var shutdown = new CancellationTokenSource(); using var terminate = PosixSignalRegistration.Create(PosixSignal.SIGTERM, Stop); using var interrupt = PosixSignalRegistration.Create(PosixSignal.SIGINT, Stop); return await guardian.RunAsync(shutdown.Token).ConfigureAwait(false); void Stop(PosixSignalContext context) { context.Cancel = true; shutdown.Cancel(); } // The engine's data folder, with the rule of the server (ADR 0022): --data-dir when given, /data inside a container. // A refused --platform ends here, with the server's message, instead of an engine restarted again and again. static string DataRoot(string[] args, Dictionary environment) { DataDirectory.RefuseOtherPlatforms(args); var index = Array.IndexOf(args, "--data-dir"); return DataDirectory.Locate(index >= 0 && index + 1 < args.Length ? args[index + 1] : null, environment); }