Example #1
0
        public void Run()
        {
            if (!WindowsServiceControlManager.IsInstalled(Description.GetServiceName()))
            {
                _log.ErrorFormat("The {0} service is not installed.", Description.GetServiceName());
                return;
            }

            if (!WindowsUserAccessControl.IsAdministrator)
            {
                if (Sudo)
                {
                    if (WindowsUserAccessControl.RerunAsAdministrator())
                    {
                        return;
                    }
                }

                _log.ErrorFormat("The {0} service can only be uninstalled as an administrator", Description.GetServiceName());
                return;
            }

            _log.DebugFormat("Attempting to uninstall '{0}'", Description.GetServiceName());

            WithInstaller(ti => ti.Uninstall(null));
        }
Example #2
0
        public void Run()
        {
            if (!WindowsServiceControlManager.IsInstalled(Description.GetServiceName()))
            {
                string message = string.Format("The {0} service is not installed.", Description.GetServiceName());
                _log.Error(message);

                return;
            }

            if (!WindowsUserAccessControl.IsAdministrator)
            {
                if (!WindowsUserAccessControl.RerunAsAdministrator())
                {
                    _log.ErrorFormat("The {0} service can only be stopped by an administrator", Description.GetServiceName());
                }

                return;
            }

            _log.DebugFormat("Attempting to stop '{0}'", Description.GetServiceName());

            WindowsServiceControlManager.Stop(Description.GetServiceName());

            _log.InfoFormat("The {0} service has been stopped.", Description.GetServiceName());
        }
Example #3
0
        public void PromptForPasswordForNonDefaultServiceUserAccount()
        {
            using (var tc = CreateTestContext())
            {
                var serviceControlManager = new WindowsServiceControlManager();
                serviceControlManager.Initialize(tc);
                var agentSettings = new AgentSettings {
                    ServerUrl = "http://server.name", AgentName = "myagent"
                };

                _reader.Setup(
                    x => x.ReadValue(
                        "windowslogonaccount",
                        It.IsAny <string>(),                       // description
                        It.IsAny <bool>(),                         // secret
                        It.IsAny <string>(),                       // defaultValue
                        It.IsAny <Func <string, bool> >(),         // validator
                        It.IsAny <Dictionary <string, string> >(), // args
                        It.IsAny <bool>())).Returns("domain\\randomuser");

                serviceControlManager.ConfigureService(agentSettings, null, true);

                _reader.Verify(
                    x =>
                    x.ReadValue(
                        "windowslogonpassword",
                        It.IsAny <string>(),
                        true,
                        It.IsAny <string>(),
                        It.IsAny <Func <string, bool> >(),
                        It.IsAny <Dictionary <string, string> >(),
                        It.IsAny <bool>()),
                    Times.Once);
            }
        }
Example #4
0
        public void Run()
        {
            if (WindowsServiceControlManager.IsInstalled(Description.GetServiceName()))
            {
                string message = string.Format("The {0} service is already installed.", Description.GetServiceName());
                _log.Error(message);

                return;
            }

            if (!WindowsUserAccessControl.IsAdministrator)
            {
                if (Sudo)
                {
                    if (WindowsUserAccessControl.RerunAsAdministrator())
                    {
                        return;
                    }
                }

                _log.ErrorFormat("The {0} service can only be installed as an administrator", Description.GetServiceName());
                return;
            }

            _log.DebugFormat("Attempting to install '{0}'", Description.GetServiceName());

            WithInstaller(ti => ti.Install(new Hashtable()));
        }
Example #5
0
        void SetRecoveryOptions(ServiceDescription description)
        {
            _log.DebugFormat("Setting service recovery options for {0}", description.GetServiceName());

            try
            {
                WindowsServiceControlManager.SetServiceRecoveryOptions(description.GetServiceName(), _options);
            }
            catch (Exception ex)
            {
                _log.Error("Failed to set service recovery options", ex);
            }
        }
Example #6
0
        public void CheckServiceExistsShouldWorkCorrectly()
        {
            using (var tc = CreateTestContext())
            {
                var serviceControlManager = new WindowsServiceControlManager();
                serviceControlManager.Initialize(tc);

                Assert.Equal(serviceControlManager.CheckServiceExists("NoService" + Guid.NewGuid()), false);
                // TODO: qvoid creating testable and write a wrapper for ServiceController as it can't be mocked
                _windowsServiceHelper.Setup(x => x.TryGetServiceController("test"))
                .Returns(new ServiceController("test"));

                Assert.Equal(serviceControlManager.CheckServiceExists("test"), true);
            }
        }
 public void WindowsServiceControlManagerShouldInstallService()
 {
     using (var tc = CreateTestContext())
     {
         var serviceControlManager = new WindowsServiceControlManager();
         serviceControlManager.Initialize(tc);
         var agentSettings = new AgentSettings { ServerUrl = "http://server.name", AgentName = "myagent" };
         var command = new CommandSettings(
             tc,
             new[]
             {
                 "--windowslogonaccount", _expectedLogonAccount,
                 "--windowslogonpassword", _expectedLogonPassword,
                 "--unattended"
             });
         serviceControlManager.ConfigureService(agentSettings, command);
         Assert.Equal("vstsagent.server.myagent", serviceControlManager.ServiceName);
         Assert.Equal("VSTS Agent (server.myagent)", serviceControlManager.ServiceDisplayName);
     }
 }
Example #8
0
        public void WindowsServiceControlManagerShouldInstallService()
        {
            using (var tc = CreateTestContext())
            {
                var serviceControlManager = new WindowsServiceControlManager();
                serviceControlManager.Initialize(tc);
                var agentSettings = new AgentSettings {
                    ServerUrl = "http://server.name", AgentName = "myagent"
                };
                serviceControlManager.ConfigureService(
                    agentSettings,
                    new Dictionary <string, string>
                {
                    { "windowslogonaccount", "NT AUTHORITY\\LOCAL SERVICE" },
                    { "windowslogonpassword", "test" }
                },
                    true);

                Assert.Equal("vstsagent.server.myagent", agentSettings.ServiceName);
                Assert.Equal("VSTS Agent (server.myagent)", agentSettings.ServiceDisplayName);
            }
        }