Exemplo n.º 1
0
        public void Install()
        {
            RunAs(() =>
            {
                var state = new Hashtable();
                using (var installer = new ServiceProcessInstaller())
                    using (var serviceInstaller = new ServiceInstaller())
                    {
                        try
                        {
                            installer.Account = ServiceAccount.LocalSystem;
                            installer.Installers.Add(serviceInstaller);
                            Console.WriteLine("Install service " + serviceName);

                            installer.Context = serviceInstaller.Context = new InstallContext(null, null);
                            serviceInstaller.Context.Parameters.Add("assemblyPath", IsLowerDotnetFramework()
                            ? (typeof(ServiceManager).Assembly.Location + "\" \"" + serviceName)
                            : ("\"" + typeof(ServiceManager).Assembly.Location + "\" " + serviceName));

                            serviceInstaller.ServiceName = serviceName;
                            serviceInstaller.DisplayName = "Logrotate Daemon";
                            serviceInstaller.Description = "logrotate daemon service";

                            serviceInstaller.StartType = ServiceStartMode.Automatic;

                            installer.Install(state);
                            Console.Write("Success.");
                        }
                        catch (Exception e)
                        {
                            if (state.Count > 0)
                            {
                                installer.Rollback(state);
                            }
                            Console.Write("Failed: " + e.Message);
                        }
                    }
            });
        }
Exemplo n.º 2
0
        static void Invoke(InvokeAction action, object args)
        {
            const string PipeType = "controller";

            var user = WindowsIdentity.GetCurrent();

            if (!new WindowsPrincipal(user).IsInRole(WindowsBuiltInRole.Administrator))
            {
                ProcessStartInfo si = new ProcessStartInfo();
                si.WindowStyle      = ProcessWindowStyle.Hidden;
                si.UseShellExecute  = true;
                si.FileName         = typeof(ServiceManager).Assembly.Location;
                si.WorkingDirectory = Environment.CurrentDirectory;
                si.Arguments        = Environment.CommandLine.Substring(Environment.CommandLine.IndexOf(' ') + 1);
                si.Verb             = "runas";

                using (var server = ControlPipeFactory.CreateServer(PipeType))
                {
                    server.BeginWaitForConnection(OnPipeConnected, server);

                    try
                    {
                        Process.Start(si).WaitForExit();
                    }
                    catch
                    {
                        Console.WriteLine("Install/Uninstall service requires administrator priviledge.");
                        Environment.Exit(1);
                    }
                }
            }
            else
            {
                var        pipe = ControlPipeFactory.CreateClient(PipeType);
                TextWriter writer;
                try
                {
                    pipe.Connect(1);
                    writer = new StreamWriter(pipe)
                    {
                        AutoFlush = true
                    };
                }
                catch (System.TimeoutException)
                {
                    writer = Console.Out;
                }

                var state = new Hashtable();

                using (var installer = new ServiceProcessInstaller())
                    using (var serviceInstaller = new ServiceInstaller())
                    {
                        try
                        {
                            installer.Account = ServiceAccount.LocalSystem;
                            installer.Installers.Add(serviceInstaller);
                            action(writer, state, installer, serviceInstaller, args);
                            if (state.Count > 0)
                            {
                                installer.Commit(state);
                            }
                            writer.Write("Success.");
                        }
                        catch (Exception e)
                        {
                            if (state.Count > 0)
                            {
                                installer.Rollback(state);
                            }
                            writer.WriteLine("Error: " + e.Message);
                            Environment.Exit(1);
                        }
                    }
            }
            Environment.Exit(0);
        }