forked from kubemq-io/kubemq-sources
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
163 lines (153 loc) · 3.92 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
package main
import (
"context"
"flag"
"fmt"
"github.com/ghodss/yaml"
"github.com/kubemq-hub/builder/connector/common"
connectorSources "github.com/kubemq-hub/builder/connector/sources"
"github.com/kubemq-hub/kubemq-sources/api"
"github.com/kubemq-hub/kubemq-sources/binding"
"github.com/kubemq-hub/kubemq-sources/config"
"github.com/kubemq-hub/kubemq-sources/pkg/logger"
"github.com/kubemq-hub/kubemq-sources/sources"
"github.com/kubemq-hub/kubemq-sources/targets"
"io/ioutil"
"os"
"os/signal"
"syscall"
)
var (
version = "dev"
commit = "none"
date = "unknown"
)
var (
log *logger.Logger
generateManifest = flag.Bool("manifest", false, "generate source connectors manifest")
build = flag.Bool("build", false, "build sources configuration")
configFile = flag.String("config", "config.yaml", "set config file name")
)
func saveManifest() error {
sourceConnectors := sources.Connectors()
if err := sourceConnectors.Validate(); err != nil {
return err
}
targetConnectors := targets.Connectors()
if err := targetConnectors.Validate(); err != nil {
return err
}
return common.NewManifest().
SetSchema("sources").
SetVersion(version).
SetSourceConnectors(sourceConnectors).
SetTargetConnectors(targetConnectors).
Save()
}
func loadCfgBindings() []*common.Binding {
file, err := ioutil.ReadFile("./config.yaml")
if err != nil {
return nil
}
list := &common.Bindings{}
err = yaml.Unmarshal(file, list)
if err != nil {
return nil
}
return list.Bindings
}
func buildConfig() error {
var err error
var bindingsYaml []byte
loadedOptions := common.NewDefaultOptions().Add("kubemq-address", []string{"localhost:50000", "Other"})
if bindingsYaml, err = connectorSources.NewSource("kubemq-sources", loadCfgBindings(), loadedOptions, nil).
SetManifestFile("./sources-manifest.json").
Render(); err != nil {
return err
}
return ioutil.WriteFile("./config.yaml", bindingsYaml, 0644)
}
func run() error {
var gracefulShutdown = make(chan os.Signal, 1)
signal.Notify(gracefulShutdown, syscall.SIGTERM)
signal.Notify(gracefulShutdown, syscall.SIGINT)
signal.Notify(gracefulShutdown, syscall.SIGQUIT)
configCh := make(chan *config.Config)
cfg, err := config.Load(configCh)
if err != nil {
return err
}
err = cfg.Validate()
if err != nil {
return err
}
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
bindingsService, err := binding.New()
if err != nil {
return err
}
err = bindingsService.Start(ctx, cfg)
if err != nil {
return err
}
apiServer, err := api.Start(ctx, cfg.ApiPort, bindingsService)
if err != nil {
return err
}
for {
select {
case newConfig := <-configCh:
err = newConfig.Validate()
if err != nil {
return fmt.Errorf("error on validation new config file: %s", err.Error())
}
bindingsService.Stop()
err = bindingsService.Start(ctx, newConfig)
if err != nil {
return fmt.Errorf("error on restarting service with new config file: %s", err.Error())
}
if apiServer != nil {
err = apiServer.Stop()
if err != nil {
return fmt.Errorf("error on shutdown api server: %s", err.Error())
}
}
apiServer, err = api.Start(ctx, newConfig.ApiPort, bindingsService)
if err != nil {
return fmt.Errorf("error on start api server: %s", err.Error())
}
case <-gracefulShutdown:
_ = apiServer.Stop()
bindingsService.Stop()
return nil
}
}
}
func main() {
log = logger.NewLogger("main")
flag.Parse()
if *generateManifest {
err := saveManifest()
if err != nil {
log.Error(err)
os.Exit(1)
}
log.Infof("generated manifest.json completed")
os.Exit(0)
}
if *build {
err := buildConfig()
if err != nil {
log.Error(err)
os.Exit(1)
}
}
config.SetConfigFile(*configFile)
log = logger.NewLogger("main")
log.Infof("starting kubemq sources connectors version: %s, commit: %s, date %s", version, commit, date)
if err := run(); err != nil {
log.Error(err)
os.Exit(1)
}
}