Exemple #1
0
        internal static void Main(string[] args)
        {
            IArguments argsDictionary    = new CommandArgs(args);
            var        backgroundService = new KonfDBH(argsDictionary);
            var        services          = new ServiceBase[] { backgroundService };

            if (argsDictionary.ContainsKey("install"))
            {
                ManagedInstallerClass.InstallHelper(new[]
                {
                    Assembly.GetExecutingAssembly().Location
                });
            }
            else if (argsDictionary.ContainsKey("uninstall"))
            {
                ManagedInstallerClass.InstallHelper(new[]
                {
                    "/u",
                    Assembly.GetExecutingAssembly().Location
                });
            }
            else if (argsDictionary.ContainsKey("console")) // console mode
            {
                #region Console

                var contextSettings = new ContextSettings
                {
                    CommandFactory = new CommandFactory()
                };
                HostContext.CreateFrom(argsDictionary.GetValue("configPath", "konfdb.json"), contextSettings);
                CurrentHostContext.Default.Log.Debug("Running in Console Mode");
                Console.SetWindowPosition(0, 0);
                Console.BackgroundColor = ConsoleColor.DarkGray;
                Console.ForegroundColor = ConsoleColor.White;
                Console.Clear();

                services.RunService();

                var shutdown = new ManualResetEvent(false);
                var thread   = new Thread(() =>
                {
                    while (!shutdown.WaitOne(0))
                    {
                        Thread.Sleep(1000);
                    }
                });
                thread.Start();

                bool   exitLoop          = false;
                var    commandService    = backgroundService.ServiceFacade;
                string internalSessionId = Guid.NewGuid().ToString();
                while (!exitLoop)
                {
                    Console.Write(">");
                    string line = Console.ReadLine();

                    if (string.IsNullOrEmpty(line))
                    {
                        continue;
                    }

                    var commandOutput = commandService.ExecuteCommand(new ServiceRequestContext
                    {
                        Command   = line,
                        Token     = backgroundService.AuthenticationToken,
                        SessionId = internalSessionId
                    });
                    if (commandOutput == null)
                    {
                        continue;
                    }

                    if (commandOutput.MessageType == CommandOutput.DisplayMessageType.Message)
                    {
                        Console.WriteLine(commandOutput.Data != null
                            ? commandOutput.Data.ToJson()
                            : commandOutput.DisplayMessage);
                    }
                    else if (commandOutput.MessageType == CommandOutput.DisplayMessageType.Error)
                    {
                        Console.WriteLine(commandOutput.DisplayMessage);
                    }

                    if (commandOutput.PostAction == CommandOutput.PostCommandAction.ExitApplication)
                    {
                        shutdown.Set();
                        thread.Join();

                        services.StopService();

                        CurrentHostContext.Default.Log.Info("Exiting...");

                        Thread.Sleep(500);

                        exitLoop = true;
                    }
                }

                #endregion
            }
            else
            {
                ServiceBase.Run(services);
            }
        }
Exemple #2
0
        protected override void OnStart(string[] args)
        {
            AppDomain.CurrentDomain.UnhandledException += CurrentDomain_UnhandledException;
            var contextSettings = new ContextSettings
            {
                CommandFactory = new CommandFactory()
            };

            HostContext.CreateFrom(_arguments.GetValue("configPath", "konfdb.json"), contextSettings);
            CurrentHostContext.Default.Log.Info("Agent Started: DataManagement");

            #region Run Command Service

            ServiceFacade = new ServiceCore();
            string internalSessionId = Guid.NewGuid().ToString();
            // Ensure that the super user admin exists
            ServiceFacade.ExecuteCommand(new ServiceRequestContext
            {
                Command = String.Format("NewUser /name:{0} /pwd:{1} /cpwd:{1} /role:admin /silent",
                                        CurrentHostContext.Default.Config.Runtime.SuperUser.Username,
                                        CurrentHostContext.Default.Config.Runtime.SuperUser.Password),
                SessionId = internalSessionId
            });

            // Ensure that the super user readonly exists
            ServiceFacade.ExecuteCommand(new ServiceRequestContext
            {
                Command = String.Format("NewUser /name:{0}_ro /pwd:{1} /cpwd:{1} /role:readonly /silent",
                                        CurrentHostContext.Default.Config.Runtime.SuperUser.Username,
                                        CurrentHostContext.Default.Config.Runtime.SuperUser.Password),
                SessionId = internalSessionId
            });

            var serviceConfig = CurrentHostContext.Default.Config.Runtime.Server;
            _serviceHostNative = new WcfService <ICommandService <object>, NativeCommandService>("localhost",
                                                                                                 "CommandService");
            _serviceHostJson = new WcfService <ICommandService <string>, JsonCommandService>("localhost", "CommandService");

            for (int i = 0; i < serviceConfig.Count; i++)
            {
                var configuration = new BindingConfiguration
                {
                    Port        = serviceConfig[i].Port.ToString(CultureInfo.InvariantCulture),
                    ServiceType = serviceConfig[i].GetWcfServiceType()
                };

                var binding = BindingFactory.Create(configuration);
                if (binding.DataTypes.IsSet(DataTypeSupport.Native))
                {
                    _serviceHostNative.AddBinding(binding);
                }
                else if (binding.DataTypes.IsSet(DataTypeSupport.Json))
                {
                    _serviceHostJson.AddBinding(binding);
                }
            }

            if (CurrentHostContext.Default.Config.Runtime.ServiceSecurity == ServiceSecurityMode.BasicSSL)
            {
                var serviceSecurity = new ServiceSecurity
                {
                    CertificateConfiguration = CurrentHostContext.Default.Config.Certificate.Default,
                    SecurityMode             = CurrentHostContext.Default.Config.Runtime.ServiceSecurity
                };

                _serviceHostJson.SetSecured(serviceSecurity);
                _serviceHostNative.SetSecured(serviceSecurity);
            }

            _serviceHostNative.Host();
            _serviceHostJson.Host();

            var authOutput = ServiceFacade.ExecuteCommand(new ServiceRequestContext
            {
                Command = String.Format("UserAuth /name:{0} /pwd:{1}",
                                        CurrentHostContext.Default.Config.Runtime.SuperUser.Username,
                                        CurrentHostContext.Default.Config.Runtime.SuperUser.Password),
                SessionId = internalSessionId
            });

            var authenticationOutput = authOutput.Data as AuthenticationOutput;
            if (authenticationOutput == null)
            {
                throw new InvalidOperationException(
                          "Could not authenticate server user: "******"GetSettings",
                SessionId = internalSessionId
            });
            if (settingsOutput != null && settingsOutput.Data != null)
            {
                var settings = (Dictionary <string, string>)settingsOutput.Data;
                foreach (var setting in settings)
                {
                    CurrentHostContext.Default.ApplicationParams.Add(setting.Key, setting.Value);
                }
            }

            //AppContext.Current.Log.Info("Agent Started: " + _serviceHostNative);
            //AppContext.Current.Log.Info("Agent Started: " + _serviceHostJson);

            #endregion

            _thread = new Thread(RunInBackground)
            {
                Name         = "ShellService",
                IsBackground = true
            };

            _shutdownEvent = new ManualResetEvent(false);
            _thread.Start();
        }