/
main.go
257 lines (233 loc) · 7.15 KB
/
main.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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
// Copyright 2018 StreamSets Inc.
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package main
import (
"crypto/tls"
"flag"
"fmt"
"github.com/kardianos/service"
log "github.com/sirupsen/logrus"
"github.com/spf13/cast"
"github.com/streamsets/datacollector-edge/container/common"
"github.com/streamsets/datacollector-edge/container/controlhub"
"github.com/streamsets/datacollector-edge/container/edge"
_ "github.com/streamsets/datacollector-edge/stages/destinations"
_ "github.com/streamsets/datacollector-edge/stages/origins"
_ "github.com/streamsets/datacollector-edge/stages/processors"
_ "github.com/streamsets/datacollector-edge/stages/services"
"net/http"
"os"
"os/signal"
"path/filepath"
"runtime"
"strings"
"syscall"
)
const (
ServiceStatus = "status"
)
var debugFlag = flag.Bool("debug", false, "Debug flag")
var logToConsoleFlag = flag.Bool("logToConsole", false, "Log to console flag")
var startFlag = flag.String("start", "", "Start Pipeline ID")
var runtimeParametersArg = flag.String("runtimeParameters", "", "Runtime Parameters")
var insecureSkipVerifyArg = flag.Bool(
"insecureSkipVerify",
false,
"InsecureSkipVerify controls whether a client verifies the server's certificate chain and host name",
)
var serviceArg = flag.String(
"service", "",
"Manage service commands - install, uninstall, start, stop and restart",
)
var enableControlHubArg = flag.Bool(
"enableControlHub",
false,
"Enable Control Hub",
)
var disableControlHubArg = flag.Bool(
"disableControlHub",
false,
"Disable Control Hub",
)
var controlHubUrlArg = flag.String(
"controlHubUrl",
"https://cloud.streamsets.com",
"Control Hub URl, For Control Hub cloud, use https://cloud.streamsets.com. "+
"For Control Hub on-premises, use the URL provided by your system administrator. "+
"For example, https://<hostname>:18631",
)
var controlHubUserTokenArg = flag.String(
"controlHubUserToken",
"",
"Enter your Control Hub user auth token",
)
var controlHubUserArg = flag.String(
"controlHubUser",
"",
"Enter your Control Hub user ID using the following format: <ID>@<organization ID>",
)
var controlHubPasswordArg = flag.String(
"controlHubPassword",
"",
"Enter the password for your Control Hub user account",
)
var controlHubLabelsArg = flag.String(
"controlHubLabels",
"",
"Enter labels to report to Control Hub",
)
type program struct {
dataCollectorEdge *edge.DataCollectorEdgeMain
}
func (p *program) Start(s service.Service) error {
go p.run()
return nil
}
func (p *program) run() {
fmt.Println("StreamSets Data Collector Edge (SDC Edge): ")
fmt.Printf("OS: %s\nArchitecture: %s\n", runtime.GOOS, runtime.GOARCH)
if *insecureSkipVerifyArg {
tr := http.DefaultTransport.(*http.Transport)
tr.TLSClientConfig = &tls.Config{
InsecureSkipVerify: true,
}
log.Warn("TLS accepts any certificate presented by the server and any host name in that certificate. " +
"In this mode, TLS is susceptible to man-in-the-middle attacks. This should be used only for testing")
}
p.dataCollectorEdge, _ = edge.DoMain(
getBaseDir(),
*debugFlag,
*logToConsoleFlag,
*startFlag,
*runtimeParametersArg,
)
go shutdownHook(p.dataCollectorEdge)
p.dataCollectorEdge.WebServerTask.Run()
}
func (p *program) Stop(s service.Service) error {
return nil
}
func main() {
flag.Parse()
svcConfig := &service.Config{
Name: "datacollector-edge",
DisplayName: "StreamSets Data Collector Edge Service",
Description: "Streams data such as logs and files for analytics",
}
prg := &program{}
newService, err := service.New(prg, svcConfig)
if err != nil {
log.Fatal(err)
}
if *serviceArg != "" {
if *serviceArg == ServiceStatus {
status, err := newService.Status()
if err != nil {
fmt.Println(err)
} else {
switch status {
case service.StatusRunning:
fmt.Println("Data Collector Edge service is running")
case service.StatusStopped:
fmt.Println("Data Collector Edge service is stopped")
default:
fmt.Println("Data Collector Edge service is not installed")
}
}
} else {
err := service.Control(newService, *serviceArg)
if err != nil {
fmt.Println(err.Error())
} else {
fmt.Printf("Action '%s' for service 'Data Collector Edge' ran successfully. \n", *serviceArg)
}
}
} else if *enableControlHubArg {
if fullAuthToken, err := controlhub.EnableControlHub(
*controlHubUrlArg,
*controlHubUserArg,
*controlHubPasswordArg,
*controlHubUserTokenArg,
); err != nil {
fmt.Println(err.Error())
} else {
edgeConfigFile := getBaseDir() + edge.DefaultConfigFilePath
config := edge.NewConfig()
err = config.FromTomlFile(edgeConfigFile)
if err != nil {
panic(err)
}
config.SCH.Enabled = true
config.SCH.BaseUrl = *controlHubUrlArg
config.SCH.AppAuthToken = cast.ToString(fullAuthToken)
if len(*controlHubLabelsArg) > 0 {
labels := strings.Split(*controlHubLabelsArg, ",")
if len(labels) > 0 {
config.SCH.JobLabels = labels
}
}
if err = config.ToTomlFile(edgeConfigFile); err != nil {
panic(err)
}
fmt.Println("Control Hub enabled successfully")
}
} else if *disableControlHubArg {
edgeConfigFile := getBaseDir() + edge.DefaultConfigFilePath
config := edge.NewConfig()
err = config.FromTomlFile(edgeConfigFile)
if err != nil {
panic(err)
}
config.SCH.Enabled = false
if err = config.ToTomlFile(edgeConfigFile); err != nil {
panic(err)
}
fmt.Println("Control Hub disabled successfully")
} else {
err = newService.Run()
if err != nil {
panic(err)
}
}
}
func shutdownHook(dataCollectorEdge *edge.DataCollectorEdgeMain) {
c := make(chan os.Signal, 1)
signal.Notify(c, syscall.SIGINT, syscall.SIGTERM)
log.Infof("Program got a system signal %v", <-c)
if pipelineInfos, er := dataCollectorEdge.PipelineStoreTask.GetPipelines(); er == nil {
for _, pipelineInfo := range pipelineInfos {
runner := dataCollectorEdge.Manager.GetRunner(pipelineInfo.PipelineId)
if pipelineState, er := runner.GetStatus(); er == nil &&
(pipelineState.Status == common.RUNNING || pipelineState.Status == common.STARTING) {
log.WithField("id", pipelineInfo.PipelineId).Info("Stopping pipeline")
if _, err := runner.StopPipeline(); err != nil {
log.WithField("id", pipelineInfo.PipelineId).Error("Error stopping pipeline")
}
}
}
}
dataCollectorEdge.WebServerTask.Shutdown()
if dataCollectorEdge.RuntimeInfo.DPMEnabled {
dataCollectorEdge.DPMMessageEventHandler.Shutdown()
}
log.Info("Data Collector Edge shutting down")
}
func getBaseDir() string {
ex, err := os.Executable()
if err != nil {
panic(err)
}
baseDir := strings.TrimSuffix(filepath.Dir(ex), "/bin")
baseDir = strings.TrimSuffix(baseDir, "\\bin") // for windows
return baseDir
}