Example #1
0
        /// <summary>
        /// Called for interactive console process model which is NOT daemon and NOT governed
        /// </summary>
        public static int InteractiveConsoleMain(BootArgs args)
        {
            var result = interactiveConsoleMainBody(args);

            if (System.Diagnostics.Debugger.IsAttached)
            {
                Console.WriteLine("Press any key to stop the debugging...");
                Console.ReadKey(true);
            }

            return(result);
        }
Example #2
0
        private static int governedConsoleMainBody(BootArgs args)
        {
            Console.CancelKeyPress += (_, e) => {
                var app = s_Application;//capture
                if (app != null)
                {
                    app.Stop();
                    e.Cancel = true;
                }
            };

            try
            {
                try
                {
                    Start(args);

                    //blocks until application is running
                    while (true)
                    {
                        var stopped = s_Application.WaitForStopOrShutdown(5000);
                        if (stopped)
                        {
                            break;
                        }
                    }
                }
                finally
                {
                    Stop();
                }

                return(0);
            }
            catch (Exception error)
            {
                var wrap         = new WrappedExceptionData(error, true);
                var errorContent = "App Root exception, details: \n" + wrap.ToJson(JsonWritingOptions.PrettyPrintRowsAsMap);
                var crashFile    = "{0:yyyyMMdd-HHmmssff}-{1}-{2}.crash.log".Args(
                    DateTime.Now,
                    System.Reflection.Assembly.GetEntryAssembly().GetName().Name,
                    s_AppId.Default("unset"));

                try
                {
                    System.IO.File.WriteAllText(crashFile, errorContent);
                    Console.WriteLine(errorContent);
                }
                catch { }

                return(-100);
            }
        }
Example #3
0
        /// <summary>
        /// Starts the application container, used by daemons directly
        /// </summary>
        public static void Start(BootArgs args)
        {
            s_Args = args;
            if (args.IsGoverned)
            {
                s_Sipc = new GovernorSipcClient(args.GovPort, args.GovApp, () => s_Application);
                s_Sipc.Start();
            }

            s_Application = new AzosApplication(args.ForApplication, null);
            s_AppId       = s_Application.AppId.ToString();
            var nBoot = s_Application.ConfigRoot[CONFIG_BOOT_SECTION];

            s_Server = FactoryUtils.MakeAndConfigureComponent <Daemon>(s_Application, nBoot, typeof(WaveServer));
            s_Server.Start();
        }
Example #4
0
        private static int interactiveConsoleMainBody(BootArgs args)
        {
            Console.CancelKeyPress += (_, e) => {
                var app = s_Application;//capture
                if (app != null)
                {
                    app.Stop();
                    e.Cancel = true;
                }
            };

            try
            {
                try
                {
                    Console.WriteLine("Azos Sky Application Host Process");
                    Console.WriteLine("Rev 2.0 July 3 2021 / Radio-86RK");
                    Console.WriteLine();
                    Console.WriteLine("Booting server daemon...");
                    Start(args);
                    Console.WriteLine("...server started");
                    Console.WriteLine("Application: {0} / `{1}`".Args(s_Application.AppId, s_Application.Name));
                    Console.WriteLine("Boot daemon: {0} / `{1}`".Args(s_Server.GetType().Name, s_Server.Name));
                    Console.WriteLine("Environment: {0}".Args(s_Application.EnvironmentName));
                    Console.WriteLine("Services provided: ");
                    Console.WriteLine("  " + s_Server.ServiceDescription.Default("n/a"));
                    Console.WriteLine();
                    Console.WriteLine("To stop the process enter 'exit' or 'stop' commands or hit <CTL+C>");

                    using (var term = new AppRemoteTerminal())
                    {
                        s_Application.InjectInto(term);

                        #region Perform SYSTEM Implicit Grant
                        //Impersonate the current call flow as local SYSTEM user.
                        //Warning: this code here is a very special case: a root server console.
                        //Never use the similar code in business applications as it bypasses all security
                        //by injecting the caller with SYSTEM-level privilege which results in implicit grant
                        //of all permission checks.
                        //Note: The session is injected with blank SysAuthToken() so this session is only granted locally
                        //and not capable of impersonating system on remote hosts
                        ExecutionContext.__SetThreadLevelSessionContext(new BaseSession(Guid.NewGuid(), 123)
                        {
                            User = new User(BlankCredentials.Instance, new SysAuthToken(), UserStatus.System, "sys", "Implicit grant", Rights.None)
                        });
                        #endregion

                        var wasPrompt = false;
                        while (s_Application.Active)
                        {
                            if (!wasPrompt)
                            {
                                Console.Write("{0}@{1}\n$ ".Args(s_Application.AppId, Platform.Computer.HostName));
                                wasPrompt = true;
                            }

                            if (!Console.KeyAvailable)
                            {
                                if (s_Application.WaitForStopOrShutdown(50))
                                {
                                    break;
                                }
                                continue;
                            }

                            var cmd = Console.ReadLine();
                            wasPrompt = false;

                            if (!s_Application.Active)
                            {
                                break;
                            }
                            if (cmd.IsNullOrWhiteSpace())
                            {
                                continue;
                            }
                            if (cmd.IsOneOf("quit", "exit", "stop"))
                            {
                                break;
                            }

                            try
                            {
                                var response = term.Execute(cmd);

                                if (response.StartsWith(AppRemoteTerminal.MARKUP_PRAGMA))
                                {
                                    ConsoleUtils.WriteMarkupContent(response);
                                }
                                else
                                {
                                    Console.WriteLine(response);
                                }
                            }
                            catch (Exception eterm)
                            {
                                ConsoleUtils.Error("Terminal error: ");
                                var wrap = new WrappedExceptionData(eterm, true);
                                Console.WriteLine(wrap.ToJson(JsonWritingOptions.PrettyPrintRowsAsMap));
                            }
                        }
                    }
                    Console.WriteLine("...shutting down now");
                    Console.WriteLine();
                }
                finally
                {
                    Stop();
                }

                return(0);
            }
            catch (Exception error)
            {
                ConsoleUtils.Error("App Root exception, details: ");
                var wrap = new WrappedExceptionData(error, true);
                Console.WriteLine(wrap.ToJson(JsonWritingOptions.PrettyPrintRowsAsMap));

                return(-100);
            }
        }
Example #5
0
        /// <summary>
        /// Called for governed console process model which is NOT daemon but governed by governor process at the specified port
        /// </summary>
        public static int GovernedConsoleMain(BootArgs args)
        {
            var result = governedConsoleMainBody(args);

            return(result);
        }