-
Notifications
You must be signed in to change notification settings - Fork 2
/
main.go
119 lines (91 loc) · 2.35 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
package main
import (
"os"
"os/signal"
"strings"
"syscall"
"github.com/nettica-com/nettica-admin/model"
log "github.com/sirupsen/logrus"
)
var Version = "development"
func main() {
path := "nettica.log"
file, err := os.OpenFile(GetDataPath()+path, os.O_APPEND|os.O_WRONLY, 0644)
if err != nil {
log.Info("Create a file named nettica.log in the nettica directory if you want to capture logs to a file")
} else {
log.SetFormatter(&log.TextFormatter{})
log.SetOutput(file)
log.SetLevel(log.InfoLevel)
}
log.Infof("Nettica Client %s", Version)
// Ensure the data directory exists
err = os.MkdirAll(GetDataPath(), 0755)
if err != nil {
log.Errorf("Could not create data directory: %v", err)
}
err = loadConfig()
if err != nil && len(os.Args) < 2 {
log.Error("Could not load config, will load when it is ready. err= ", err)
}
log.SetLevel(log.InfoLevel)
if cfg.quiet {
log.SetLevel(log.ErrorLevel)
}
if cfg.debug {
log.SetLevel(log.DebugLevel)
}
log.SetLevel(log.InfoLevel)
// Migrate if needed
Migrate()
// If the config is set by environment variables, ignore
// the config file
if cfg.Server != "" && cfg.DeviceID != "" && cfg.ApiKey != "" {
log.Info("Using environment variables for configuration")
msg := model.Message{}
msg.Device = &model.Device{}
msg.Device.Version = Version
msg.Device.Server = cfg.Server
msg.Device.Id = cfg.DeviceID
msg.Device.ApiKey = cfg.ApiKey
msg.Device.UpdateKeys = cfg.UpdateKeys
msg.Device.Enable = true
msg.Device.CheckInterval = 10
if cfg.debug {
msg.Device.Logging = "debug"
} else if cfg.quiet {
msg.Device.Logging = "error"
} else {
msg.Device.Logging = "info"
}
// Not getting the return value server because it has
// been added to the Servers list and will be started shortly
_ = NewServer(msg.Device.Server, msg)
}
KeyInitialize()
KeyLoad()
const svcName = "nettica"
inService, _ := InService()
if inService {
RunService(svcName)
return
}
if len(os.Args) > 1 {
cmd := strings.ToLower(os.Args[1])
ServiceManager(svcName, cmd)
return
} else {
log.Infof("Nettica Control Plane Started")
DoWork()
sigs := make(chan os.Signal, 1)
done := make(chan bool, 1)
signal.Notify(sigs, syscall.SIGINT, syscall.SIGTERM)
go func() {
sig := <-sigs
log.Errorf("%v", sig)
done <- true
}()
<-done
log.Info("Exiting")
}
}