forked from chyeh/nqm-agent
/
cfg.go
184 lines (153 loc) · 4.24 KB
/
cfg.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
package main
import (
"encoding/json"
"log"
"os"
"os/exec"
"path/filepath"
"strings"
"sync"
"sync/atomic"
"time"
"github.com/Cepave/common/model"
"github.com/toolkits/file"
)
type AgentConfig struct {
PushURL string `json:"pushURL"`
}
type HbsConfig struct {
RPCServer string `json:"RPCServer"`
Interval time.Duration `json:"interval"`
}
type JSONConfigFile struct {
Agent *AgentConfig `json:"agent"`
Hbs *HbsConfig `json:"hbs"`
Hostname string `json:"hostname"`
IPAddress string `json:"ipAddress"`
ConnectionID string `json:"connectionID"`
}
type GeneralConfig struct {
JSONConfigFile
hbsResp atomic.Value // for receiving model.NqmTaskResponse
Hostname string
IPAddress string
ConnectionID string
}
var (
jsonConfig *JSONConfigFile
generalConfig *GeneralConfig
jsonCfgLock = new(sync.RWMutex)
)
func getBinAbsPath() string {
bin, err := filepath.Abs(os.Args[0])
if err != nil {
log.Fatalln(err)
}
return bin
}
func getWorkingDirAbsPath() string {
return filepath.Dir(getBinAbsPath())
}
func getCfgAbsPath(cfgPath string) string {
if cfgPath == "cfg.json" {
return filepath.Join(getWorkingDirAbsPath(), cfgPath)
}
wd, _ := os.Getwd()
cfgAbsPath := filepath.Join(wd, cfgPath)
return cfgAbsPath
}
func PublicIP() (string, error) {
output, err := exec.Command("dig", "+short", "myip.opendns.com", "@resolver1.opendns.com").Output()
if err != nil {
return "UNKNOWN", err
}
ipStr := strings.TrimSpace(string(output))
return ipStr, nil
}
func getJSONConfig() *JSONConfigFile {
jsonCfgLock.RLock()
defer jsonCfgLock.RUnlock()
return jsonConfig
}
func getHostname() string {
hostname := getJSONConfig().Hostname
if hostname != "" {
log.Println("Hostname set in config: [", hostname, "]")
return hostname
}
hostname, err := os.Hostname()
if err != nil {
log.Fatalln("os.Hostname() ERROR:", err)
}
// hostname -s
// -s, --short
// Display the short host name. This is the host name cut at the first dot.
hostname = strings.Split(hostname, ".")[0]
log.Println("Hostname not set in config, using system's hostname...succeeded: [", hostname, "]")
return hostname
}
func getIP() string {
ip := getJSONConfig().IPAddress
if ip != "" {
log.Println("IP set in config: [", ip, "]")
return ip
}
ip, err := PublicIP()
if err != nil {
log.Println("IP not set in config, getting public IP...failed:", err)
} else {
log.Println("IP not set in config, getting public IP...succeeded: [", ip, "]")
}
return ip
}
func getConnectionID() string {
connectionID := getJSONConfig().ConnectionID
if connectionID != "" {
log.Println("ConnectionID set in config: [", connectionID, "]")
return connectionID
}
// Logically it shouldn't happen because ConnectionID is alwasy generated
// after Hostname and IPAddress are set.
if GetGeneralConfig().Hostname == "" || GetGeneralConfig().IPAddress == "" {
log.Fatalln("ConnectionID not set in config, generating...failed!")
}
connectionID = GetGeneralConfig().Hostname + "@" + GetGeneralConfig().IPAddress
log.Println("ConnectionID not set in config, generating...succeeded: [", connectionID, "]")
return connectionID
}
func loadJSONConfig(cfgFile string) {
cfgFile = filepath.Clean(cfgFile)
cfgPath := getCfgAbsPath(cfgFile)
if !file.IsExist(cfgPath) {
log.Fatalln("Configuration file [", cfgFile, "] doesn't exist")
}
configContent, err := file.ToTrimString(cfgPath)
if err != nil {
log.Fatalln("Reading configuration file [", cfgFile, "] failed:", err)
}
var c JSONConfigFile
err = json.Unmarshal([]byte(configContent), &c)
if err != nil {
log.Fatalln("Parsing configuration file [", cfgFile, "] failed:", err)
}
jsonCfgLock.Lock()
defer jsonCfgLock.Unlock()
jsonConfig = &c
log.Println("Reading configuration file [", cfgFile, "] succeeded")
}
func GetGeneralConfig() *GeneralConfig {
return generalConfig
}
func InitGeneralConfig(cfgFilePath string) {
var cfg GeneralConfig
generalConfig = &cfg
loadJSONConfig(cfgFilePath)
cfg.Agent = getJSONConfig().Agent
cfg.Hbs = getJSONConfig().Hbs
cfg.hbsResp.Store(model.NqmTaskResponse{})
cfg.Hostname = getHostname()
if cfg.IPAddress = getIP(); cfg.IPAddress == "UNKNOWN" {
log.Fatalln("IP can't be \"UNKNOWN\"")
}
cfg.ConnectionID = getConnectionID()
}