From 9cddf0fcdfbdcd976507afa8b8108671c137cdce Mon Sep 17 00:00:00 2001 From: Ivan Kozlovic Date: Mon, 17 Jul 2017 13:25:56 -0600 Subject: [PATCH] [FIXED] Windows Docker Image The use of the `svc` API prevented the NATS Server to run as a container on both nanoserver and windowsservercore Docker images. An attempt was made to replace svc.Debug with normal server.Start() if detected to be interactive, however, that did not work. The fact of detecting if interactive or not already requires connecting to the service controller apparently. This change looks up for an environment variable (NATS_DOCKERIZED) and if set to "1", will not make use of the `svc` package. This environment variable will be set in the Docker image (in nats-docker/windows/nanoserver/Dockerfile and windowsservercore/Dockerfile). Resolves #543 --- Dockerfile.win64 | 11 +++++++++++ logger/syslog_windows.go | 3 ++- logger/syslog_windows_test.go | 3 ++- server/const.go | 2 +- server/service_windows.go | 16 ++++++++++++++++ 5 files changed, 32 insertions(+), 3 deletions(-) create mode 100644 Dockerfile.win64 diff --git a/Dockerfile.win64 b/Dockerfile.win64 new file mode 100644 index 00000000..848db66a --- /dev/null +++ b/Dockerfile.win64 @@ -0,0 +1,11 @@ +FROM golang:1.7.6 + +MAINTAINER Ivan Kozlovic + +COPY . /go/src/github.com/nats-io/gnatsd +WORKDIR /go/src/github.com/nats-io/gnatsd + +RUN CGO_ENABLED=0 GOOS=windows GOARCH=amd64 go build -ldflags "-s -w" -o gnatsd.exe . + +ENTRYPOINT ["go"] +CMD ["version"] diff --git a/logger/syslog_windows.go b/logger/syslog_windows.go index 10eddc6b..f1d5644f 100644 --- a/logger/syslog_windows.go +++ b/logger/syslog_windows.go @@ -5,9 +5,10 @@ package logger import ( "fmt" - "golang.org/x/sys/windows/svc/eventlog" "os" "strings" + + "golang.org/x/sys/windows/svc/eventlog" ) const ( diff --git a/logger/syslog_windows_test.go b/logger/syslog_windows_test.go index c14c7b56..3c6b2a76 100755 --- a/logger/syslog_windows_test.go +++ b/logger/syslog_windows_test.go @@ -4,10 +4,11 @@ package logger import ( - "golang.org/x/sys/windows/svc/eventlog" "os/exec" "strings" "testing" + + "golang.org/x/sys/windows/svc/eventlog" ) // Skips testing if we do not have privledges to run this test. diff --git a/server/const.go b/server/const.go index 29a9214c..93636224 100644 --- a/server/const.go +++ b/server/const.go @@ -19,7 +19,7 @@ const ( const ( // VERSION is the current version for the server. - VERSION = "1.0.0" + VERSION = "1.0.1" // DEFAULT_PORT is the default port for client connections. DEFAULT_PORT = 4222 diff --git a/server/service_windows.go b/server/service_windows.go index aa840fe3..d9b3ea70 100644 --- a/server/service_windows.go +++ b/server/service_windows.go @@ -3,6 +3,7 @@ package server import ( + "os" "time" "golang.org/x/sys/windows/svc" @@ -22,6 +23,14 @@ type winServiceWrapper struct { server *Server } +var dockerized = false + +func init() { + if v, exists := os.LookupEnv("NATS_DOCKERIZED"); exists && v == "1" { + dockerized = true + } +} + // Execute will be called by the package code at the start of // the service, and the service will exit once Execute completes. // Inside Execute you must read service change requests from r and @@ -76,6 +85,10 @@ loop: // Run starts the NATS server as a Windows service. func Run(server *Server) error { + if dockerized { + server.Start() + return nil + } run := svc.Run isInteractive, err := svc.IsAnInteractiveSession() if err != nil { @@ -89,6 +102,9 @@ func Run(server *Server) error { // isWindowsService indicates if NATS is running as a Windows service. func isWindowsService() bool { + if dockerized { + return false + } isInteractive, _ := svc.IsAnInteractiveSession() return !isInteractive }