forked from open-horizon/edge-sync-service
-
Notifications
You must be signed in to change notification settings - Fork 0
/
standalone.go
149 lines (124 loc) · 4.59 KB
/
standalone.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
package base
import (
"flag"
"fmt"
"os"
"github.com/open-horizon/edge-sync-service/core/security"
"github.com/open-horizon/edge-sync-service/common"
"github.com/open-horizon/edge-utilities/logger"
"github.com/open-horizon/edge-utilities/logger/log"
"github.com/open-horizon/edge-utilities/logger/trace"
)
var help bool
// ConfigFile configuration file name loaded by command line argument
var ConfigFile string
var swaggerFile string
var ipAddress string
func init() {
if flag.CommandLine.Lookup("h") == nil {
flag.BoolVar(&help, "h", false, "Display usage information.")
}
flag.StringVar(&ConfigFile, "c", "/etc/edge-sync-service/sync.conf", "Specify the configuration file to use")
flag.StringVar(&swaggerFile, "swagger", "", "Name of the file with the generated swagger document for the Edge sync service APIs")
}
// ConfigStandaloneSyncService Configures a standalone Sync Service
func ConfigStandaloneSyncService() {
flag.Parse()
err := common.Load(ConfigFile)
if err != nil {
fmt.Printf("Failed to load the configuration file (%s). Error: %s\n", ConfigFile, err)
os.Exit(99)
}
if help {
fmt.Fprintln(os.Stderr,
"Usage: sync-service [-h | -c <config file name>]")
flag.PrintDefaults()
os.Exit(0)
}
if common.Configuration.NodeType == common.CSS {
// If we are a CSS set DestinationType and DestinationID to hard coded values
common.Configuration.DestinationType = "Cloud"
common.Configuration.DestinationID = "Cloud"
}
err = common.ValidateConfig()
if err != nil {
fmt.Fprintf(os.Stderr, "%s\n", err)
os.Exit(98)
}
}
// StandaloneSyncService runs a standalone Sync Service
func StandaloneSyncService(auth security.Authentication) {
destinations, entries := log.ParseDestinationsList(common.Configuration.LogTraceDestination)
if !entries {
destinations[logger.FILE] = true
}
var logFileSize = common.DefaultLogTraceFileSize
var err error
if destinations[logger.FILE] {
logFileSize, err =
logger.AdjustMaxLogfileSize(common.Configuration.LogTraceFileSizeKB, common.DefaultLogTraceFileSize,
common.Configuration.LogRootPath)
if err != nil {
fmt.Printf("WARNING: Unable to get disk statistics for the path %s. Error: %s\n",
common.Configuration.LogRootPath, err)
}
}
parameters := logger.Parameters{RootPath: common.Configuration.LogRootPath, FileName: common.Configuration.LogFileName,
MaxFileSize: logFileSize,
MaxCompressedFilesNumber: common.Configuration.MaxCompressedlLogTraceFilesNumber,
Destinations: common.Configuration.LogTraceDestination, Prefix: common.Configuration.NodeType + ": ",
Level: common.Configuration.LogLevel,
MaintenanceInterval: common.Configuration.LogTraceMaintenanceInterval}
if err = log.Init(parameters); err != nil {
fmt.Fprintf(os.Stderr, "Failed to initialize the log. Error: %s\n", err)
os.Exit(98)
}
defer log.Stop()
if destinations[logger.FILE] {
logFileSize, err =
logger.AdjustMaxLogfileSize(common.Configuration.LogTraceFileSizeKB, common.DefaultLogTraceFileSize,
common.Configuration.TraceRootPath)
if err != nil {
fmt.Printf("WARNING: Unable to get disk statistics for the path %s. Error: %s\n",
common.Configuration.TraceRootPath, err)
}
}
parameters.RootPath = common.Configuration.TraceRootPath
parameters.FileName = common.Configuration.TraceFileName
parameters.Level = common.Configuration.TraceLevel
parameters.MaxFileSize = logFileSize
if err = trace.Init(parameters); err != nil {
fmt.Fprintf(os.Stderr, "Failed to initialize the trace. Error: %s\n", err)
os.Exit(98)
}
defer trace.Stop()
censorAndDumpConfig()
security.SetAuthentication(auth)
err = Start(swaggerFile, true)
if err != nil {
if log.IsLogging(logger.FATAL) {
log.Fatal(err.Error())
}
} else {
log.Info("The Sync Service has started")
BlockUntilShutdown()
}
}
func censorAndDumpConfig() {
toBeCensored := []*string{&common.Configuration.ServerCertificate, &common.Configuration.ServerKey,
&common.Configuration.HTTPCSSCACertificate,
&common.Configuration.MQTTUserName, &common.Configuration.MQTTPassword,
&common.Configuration.MQTTCACertificate, &common.Configuration.MQTTSSLCert, &common.Configuration.MQTTSSLKey,
&common.Configuration.MongoUsername, &common.Configuration.MongoPassword, &common.Configuration.MongoCACertificate}
backups := make([]string, len(toBeCensored))
for index, fieldPointer := range toBeCensored {
backups[index] = *fieldPointer
if len(*fieldPointer) != 0 {
*fieldPointer = "<...>"
}
}
trace.Dump("Loaded configuration:", common.Configuration)
for index, fieldPointer := range toBeCensored {
*fieldPointer = backups[index]
}
}