From 411b24d95df3e4ef2ad5dee072354f4685aa8e7c Mon Sep 17 00:00:00 2001 From: Siva Date: Mon, 18 Jun 2018 14:49:59 -0400 Subject: [PATCH 1/5] Added Hook for Windows Service --- service_os/service.go | 1 + service_os/service_windows.go | 43 +++++++++++++++++++++++++++++++++++ 2 files changed, 44 insertions(+) create mode 100644 service_os/service.go create mode 100644 service_os/service_windows.go diff --git a/service_os/service.go b/service_os/service.go new file mode 100644 index 0000000000..6db6263b6f --- /dev/null +++ b/service_os/service.go @@ -0,0 +1 @@ +package service_os diff --git a/service_os/service_windows.go b/service_os/service_windows.go new file mode 100644 index 0000000000..fa3e8b30c8 --- /dev/null +++ b/service_os/service_windows.go @@ -0,0 +1,43 @@ +//+build windows + +package service_os + +import ( + "os" + wsvc "golang.org/x/sys/windows/svc" +) + +type serviceWindows struct {} + +func init() { + interactive, err := wsvc.IsAnInteractiveSession() + if err != nil { + panic(err) + } + if interactive { + return + } + go func() { + _ = wsvc.Run("", serviceWindows{}) + os.Exit(0) + }() +} + +func (serviceWindows) Execute(args []string, r <-chan wsvc.ChangeRequest, s chan<- wsvc.Status) (svcSpecificEC bool, exitCode uint32) { + const accCommands = wsvc.AcceptStop | wsvc.AcceptShutdown + s <- wsvc.Status{State: wsvc.StartPending} + + s <- wsvc.Status{State: wsvc.Running, Accepts: accCommands} + for { + c := <-r + switch c.Cmd { + case wsvc.Interrogate: + s <- c.CurrentStatus + case wsvc.Stop, wsvc.Shutdown: + s <- wsvc.Status{State: wsvc.StopPending} + return false, 0 + } + } + + return false, 0 +} From e5ad1dd5765051b6e9a2e044ca01b5bc6c96a788 Mon Sep 17 00:00:00 2001 From: Siva Date: Mon, 18 Jun 2018 14:55:11 -0400 Subject: [PATCH 2/5] Added Side Effect import for Windows Service --- main.go | 1 + 1 file changed, 1 insertion(+) diff --git a/main.go b/main.go index 855967af47..9755775f2c 100644 --- a/main.go +++ b/main.go @@ -8,6 +8,7 @@ import ( "github.com/hashicorp/consul/command" "github.com/hashicorp/consul/lib" + _ "github.com/hashicorp/consul/service_os" "github.com/mitchellh/cli" ) From 319a0ae2bf7cada375b39a3055be81f0ec42d33f Mon Sep 17 00:00:00 2001 From: Siva Date: Wed, 20 Jun 2018 14:30:17 -0400 Subject: [PATCH 3/5] Graceful exits added --- command/agent/agent.go | 3 +++ service_os/service.go | 6 ++++++ service_os/service_windows.go | 7 +++++-- 3 files changed, 14 insertions(+), 2 deletions(-) diff --git a/command/agent/agent.go b/command/agent/agent.go index 90a040db80..a23b93860c 100644 --- a/command/agent/agent.go +++ b/command/agent/agent.go @@ -21,6 +21,7 @@ import ( "github.com/hashicorp/consul/command/flags" "github.com/hashicorp/consul/lib" "github.com/hashicorp/consul/logger" + "github.com/hashicorp/consul/service_os" "github.com/hashicorp/go-checkpoint" multierror "github.com/hashicorp/go-multierror" "github.com/hashicorp/logutils" @@ -411,6 +412,8 @@ func (c *cmd) run(args []string) int { case ch := <-agent.ReloadCh(): sig = syscall.SIGHUP reloadErrCh = ch + case <-service_os.Shutdown_Channel(): + sig = os.Interrupt case <-c.shutdownCh: sig = os.Interrupt case err := <-agent.RetryJoinCh(): diff --git a/service_os/service.go b/service_os/service.go index 6db6263b6f..432baaf2ad 100644 --- a/service_os/service.go +++ b/service_os/service.go @@ -1 +1,7 @@ package service_os + +var chanGraceExit = make(chan int) + +func Shutdown_Channel() <-chan int { + return chanGraceExit +} diff --git a/service_os/service_windows.go b/service_os/service_windows.go index fa3e8b30c8..a160d2bcae 100644 --- a/service_os/service_windows.go +++ b/service_os/service_windows.go @@ -3,11 +3,12 @@ package service_os import ( - "os" wsvc "golang.org/x/sys/windows/svc" + "os" + "time" ) -type serviceWindows struct {} +type serviceWindows struct{} func init() { interactive, err := wsvc.IsAnInteractiveSession() @@ -19,6 +20,7 @@ func init() { } go func() { _ = wsvc.Run("", serviceWindows{}) + time.Sleep(4 * time.Second) os.Exit(0) }() } @@ -34,6 +36,7 @@ func (serviceWindows) Execute(args []string, r <-chan wsvc.ChangeRequest, s chan case wsvc.Interrogate: s <- c.CurrentStatus case wsvc.Stop, wsvc.Shutdown: + chanGraceExit <- 1 s <- wsvc.Status{State: wsvc.StopPending} return false, 0 } From 7969d16e423d57632680c7808e0a174176687a7b Mon Sep 17 00:00:00 2001 From: Siva Date: Tue, 26 Jun 2018 18:23:55 -0400 Subject: [PATCH 4/5] Added guide for Windows Service --- website/source/docs/guides/index.html.md | 2 + .../source/docs/guides/windows-guide.html.md | 69 +++++++++++++++++++ website/source/layouts/docs.erb | 3 + 3 files changed, 74 insertions(+) create mode 100644 website/source/docs/guides/windows-guide.html.md diff --git a/website/source/docs/guides/index.html.md b/website/source/docs/guides/index.html.md index 096aa58066..946a96c7ea 100644 --- a/website/source/docs/guides/index.html.md +++ b/website/source/docs/guides/index.html.md @@ -45,3 +45,5 @@ The following guides are available: * [Sentinel](/docs/guides/sentinel.html) - This guide covers using Sentinel for policy enforcement in Consul. * [Server Performance](/docs/guides/performance.html) - This guide covers minimum requirements for Consul servers as well as guidelines for running Consul servers in production. + +* [Windows Service](/docs/guides/windows-guide.html) - This guide covers how to run Consul as a service on Windows. \ No newline at end of file diff --git a/website/source/docs/guides/windows-guide.html.md b/website/source/docs/guides/windows-guide.html.md new file mode 100644 index 0000000000..3f955e889e --- /dev/null +++ b/website/source/docs/guides/windows-guide.html.md @@ -0,0 +1,69 @@ +--- +layout: "docs" +page_title: "Windows Service" +sidebar_current: "docs-guides-windows-service" +description: |- + For our friends running Consul on Windows, we have good news. By using the _sc_ command either on Powershell or + the Windows command line, you can make Consul run as a service. For more details about the _sc_ command + the Windows page for [sc](https://msdn.microsoft.com/en-us/library/windows/desktop/ms682107(v=vs.85).aspx) + should help you get started. + +--- + +# Overview +For our friends running Consul on Windows, we have good news. By using the _sc_ command either on Powershell or +the Windows command line, you can make Consul run as a service. For more details about the _sc_ command +the Windows page for [sc](https://msdn.microsoft.com/en-us/library/windows/desktop/ms682107(v=vs.85).aspx) +should help you get started. + +Please remember to create a permanent directory for storing the configuration files, +as it would be handy, if you're starting Consul with the _-config-dir_ argument. + +The steps presented here assume, that the user has launched **Powershell** with _Adminstrator_ capabilities. + +If you come across bugs while using Consul for Windows, do not hesitate to open an issue [here](https://github.com/hashicorp/consul/issues). + +## Detailed steps involved in making Consul run as a service on Windows +Download the Consul binary for your architecture. + +Setup your environmental _path_ variable, so that Windows can find +the Consul binary. (Will be handy for quick Consul commands) +Use the _sc_ command to create a Service named **Consul**, which starts in the _dev_ mode. + + ```text + sc.exe create "Consul" binPath="Path to the Consul.exe arg1 arg2 ...argN" + [SC] CreateService SUCCESS + ``` + + + If you get an output that is similar to the one above, then your service is + registered with the Service manager. + + + If you get an error, please check that + you have specified the proper path to the binary and check if you've entered the arguments correctly for the Consul + service. + +After this step there are two ways to start the service: + +* Go to the Windows Service Manager, and look for **Consul** under the + service name. Click the _start_ button to start the service. +* Using the _sc_ command: + + ```text + sc.exe start "Consul" + + SERVICE_NAME: Consul + TYPE : 10 WIN32_OWN_PROCESS + STATE : 4 RUNNING + (STOPPABLE, NOT_PAUSABLE, ACCEPTS_SHUTDOWN) + WIN32_EXIT_CODE : 0 (0x0) + SERVICE_EXIT_CODE : 0 (0x0) + CHECKPOINT : 0x0 + WAIT_HINT : 0x0 + PID : 8008 + FLAGS : + ``` +If you followed the steps above, congratulations, you have successful made Consul +run as a service on Windows. The service automatically starts up during/after boot, so you don't need to +launch Consul from the command-line again. \ No newline at end of file diff --git a/website/source/layouts/docs.erb b/website/source/layouts/docs.erb index c42ddccf0b..635a11e9d7 100644 --- a/website/source/layouts/docs.erb +++ b/website/source/layouts/docs.erb @@ -359,6 +359,9 @@ Certificates > Server Performance + > + Windows (Service) + From e281a3f9253b93ce5a39df7b9a720abf62cc7e0e Mon Sep 17 00:00:00 2001 From: Siva Date: Thu, 28 Jun 2018 21:18:14 -0400 Subject: [PATCH 5/5] Changes made : 1. Website Changed some of the wordings and reorganized the content of the website. 2. Code: Removed sleep and exit lines from the code. --- agent/consul/client.go | 2 +- service_os/service_windows.go | 4 --- .../source/docs/guides/windows-guide.html.md | 28 ++++++++++--------- 3 files changed, 16 insertions(+), 18 deletions(-) diff --git a/agent/consul/client.go b/agent/consul/client.go index 000cb66f88..f6b123a315 100644 --- a/agent/consul/client.go +++ b/agent/consul/client.go @@ -134,7 +134,7 @@ func NewClientLogger(config *Config, logger *log.Logger) (*Client, error) { shutdownCh: make(chan struct{}), } - c.rpcLimiter.Store(rate.NewLimiter(config.RPCRate, config.RPCMaxBurst)) + c.rpcLimiter.Store(rate.NewLimiter(config.RPCRate, config.RPCMaxBurst)) if err := c.initEnterprise(); err != nil { c.Shutdown() diff --git a/service_os/service_windows.go b/service_os/service_windows.go index a160d2bcae..d7fc245f52 100644 --- a/service_os/service_windows.go +++ b/service_os/service_windows.go @@ -4,8 +4,6 @@ package service_os import ( wsvc "golang.org/x/sys/windows/svc" - "os" - "time" ) type serviceWindows struct{} @@ -20,8 +18,6 @@ func init() { } go func() { _ = wsvc.Run("", serviceWindows{}) - time.Sleep(4 * time.Second) - os.Exit(0) }() } diff --git a/website/source/docs/guides/windows-guide.html.md b/website/source/docs/guides/windows-guide.html.md index 3f955e889e..43d75347d8 100644 --- a/website/source/docs/guides/windows-guide.html.md +++ b/website/source/docs/guides/windows-guide.html.md @@ -3,7 +3,7 @@ layout: "docs" page_title: "Windows Service" sidebar_current: "docs-guides-windows-service" description: |- - For our friends running Consul on Windows, we have good news. By using the _sc_ command either on Powershell or + By using the _sc_ command either on Powershell or the Windows command line, you can make Consul run as a service. For more details about the _sc_ command the Windows page for [sc](https://msdn.microsoft.com/en-us/library/windows/desktop/ms682107(v=vs.85).aspx) should help you get started. @@ -11,24 +11,23 @@ description: |- --- # Overview -For our friends running Consul on Windows, we have good news. By using the _sc_ command either on Powershell or +By using the _sc_ command either on Powershell or the Windows command line, you can make Consul run as a service. For more details about the _sc_ command the Windows page for [sc](https://msdn.microsoft.com/en-us/library/windows/desktop/ms682107(v=vs.85).aspx) should help you get started. -Please remember to create a permanent directory for storing the configuration files, -as it would be handy, if you're starting Consul with the _-config-dir_ argument. +Please remember to create a permanent directory for storing the configuration files. It is always +advisable to start Consul with the _-config-dir_ option. -The steps presented here assume, that the user has launched **Powershell** with _Adminstrator_ capabilities. +The steps presented here , we assume that the user has launched Powershell with _Adminstrator_ capabilities. -If you come across bugs while using Consul for Windows, do not hesitate to open an issue [here](https://github.com/hashicorp/consul/issues). +## Running Consul run as a service on Windows + +### Installing Consul as a Service -## Detailed steps involved in making Consul run as a service on Windows Download the Consul binary for your architecture. -Setup your environmental _path_ variable, so that Windows can find -the Consul binary. (Will be handy for quick Consul commands) -Use the _sc_ command to create a Service named **Consul**, which starts in the _dev_ mode. + Use the _sc_ command to create a Service named **Consul**, which starts in the _dev_ mode. ```text sc.exe create "Consul" binPath="Path to the Consul.exe arg1 arg2 ...argN" @@ -44,7 +43,10 @@ Use the _sc_ command to create a Service named **Consul**, which starts in the _ you have specified the proper path to the binary and check if you've entered the arguments correctly for the Consul service. -After this step there are two ways to start the service: + +### Running Consul as a service + +You have two ways to start the service. * Go to the Windows Service Manager, and look for **Consul** under the service name. Click the _start_ button to start the service. @@ -64,6 +66,6 @@ After this step there are two ways to start the service: PID : 8008 FLAGS : ``` -If you followed the steps above, congratulations, you have successful made Consul -run as a service on Windows. The service automatically starts up during/after boot, so you don't need to + +The service automatically starts up during/after boot, so you don't need to launch Consul from the command-line again. \ No newline at end of file