-
Notifications
You must be signed in to change notification settings - Fork 138
/
sg-service.go
177 lines (149 loc) · 4.28 KB
/
sg-service.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
//go:build windows
// +build windows
/*
Copyright 2016-Present Couchbase, Inc.
Use of this software is governed by the Business Source License included in
the file licenses/BSL-Couchbase.txt. As of the Change Date specified in that
file, in accordance with the Business Source License, use of this software will
be governed by the Apache License, Version 2.0, included in the file
licenses/APL2.txt.
*/
package main
import (
"log"
"os/exec"
"os"
"github.com/kardianos/service"
)
const installLocation = "C:\\Program Files\\Couchbase\\Sync Gateway\\"
const defaultLogFilePath = installLocation + "var\\lib\\couchbase\\logs"
var logger service.Logger
var infoLogger systemLoggerError
type program struct {
ExePath string
ConfigPath string
SyncGateway *exec.Cmd
}
func (p *program) Start(s service.Service) error {
err := p.startup()
if err != nil {
return err
}
go p.wait()
return nil
}
func (p *program) startup() error {
logger.Infof("Starting Sync Gateway service using command: `%s --defaultLogFilePath %s %s`", p.ExePath, defaultLogFilePath, p.ConfigPath)
if p.ConfigPath != "" {
p.SyncGateway = exec.Command(p.ExePath, "--defaultLogFilePath", defaultLogFilePath, p.ConfigPath)
} else {
p.SyncGateway = exec.Command(p.ExePath, "--defaultLogFilePath", defaultLogFilePath)
}
p.SyncGateway.Stdout = infoLogger
p.SyncGateway.Stderr = infoLogger
err := p.SyncGateway.Start()
if err != nil {
logger.Errorf("Failed to start Sync Gateway due to error %v", err)
return err
}
return nil
}
func (p *program) wait() {
err := p.SyncGateway.Wait()
if err != nil {
logger.Errorf("Sync Gateway exiting with %v", err)
}
panic("Sync Gateway service exited.")
}
func (p *program) Stop(s service.Service) error {
logger.Infof("Stopping Sync Gateway service...")
p.SyncGateway.Process.Kill()
return nil
}
func main() {
svcConfig := &service.Config{
Name: "SyncGateway",
DisplayName: "Couchbase Sync Gateway",
Description: "Couchbase Sync Gateway mobile application REST gateway service.",
}
var exePath string
var configPath string
switch len(os.Args) {
case 2:
exePath = installLocation + "sync_gateway.exe" // Uses default binary image path
svcConfig.Arguments = []string{"start"} // Uses the default config
case 3:
exePath = installLocation + "sync_gateway.exe" // Uses default binary image path
configPath = os.Args[2] // Uses custom config
svcConfig.Arguments = []string{"start", configPath}
case 4:
exePath = os.Args[2] // Uses custom binary image path
configPath = os.Args[3] // Uses custom config
svcConfig.Arguments = []string{"start", exePath, configPath}
case 5:
exePath = os.Args[2] // Uses custom binary image path
configPath = os.Args[3] // Uses custom config
svcConfig.Arguments = []string{"start", exePath, configPath}
default:
panic("Valid parameters combinations are: COMMAND [none, custom config path, or custom exe path and custom config path].")
}
prg := &program{
ExePath: exePath,
ConfigPath: configPath,
}
s, err := service.New(prg, svcConfig)
if err != nil {
log.Fatal(err)
}
logger, err = s.Logger(nil)
if err != nil {
log.Fatal(err)
}
systemLogger, err := s.SystemLogger(nil)
if err != nil {
log.Fatalf("Unable to setup system logger: %v", err)
}
infoLogger = systemLoggerError{
systemLogger: systemLogger,
}
switch {
case os.Args[1] == "install":
logger.Info("Installing Sync Gateway")
err := s.Install()
if err != nil {
logger.Errorf("Failed to install Sync Gateway service: %s", err)
}
return
case os.Args[1] == "uninstall":
logger.Info("Uninstalling Sync Gateway")
err := s.Uninstall()
if err != nil {
logger.Errorf("Failed to uninstall Sync Gateway service: %s", err)
}
return
case os.Args[1] == "stop":
err := s.Stop()
if err != nil {
logger.Errorf("Failed to stop Sync Gateway service: %s", err)
}
return
case os.Args[1] == "restart":
err := s.Restart()
if err != nil {
logger.Errorf("Failed to restart Sync Gateway service: %s", err)
}
return
}
err = s.Run()
if err != nil {
logger.Error(err)
}
logger.Infof("Exiting Sync Gateway service.")
}
type systemLoggerError struct {
systemLogger service.Logger
}
func (n systemLoggerError) Write(b []byte) (int, error) {
n.systemLogger.Info(string(b))
return len(b), nil
}