|
1 | 1 | package config
|
2 |
| - |
3 |
| -// |
4 |
| -//import ( |
5 |
| -// "encoding/json" |
6 |
| -// "errors" |
7 |
| -// log "github.com/cihub/seelog" |
8 |
| -// "infini.sh/agent/model" |
9 |
| -// "infini.sh/framework/core/env" |
10 |
| -// "infini.sh/framework/core/event" |
11 |
| -// "infini.sh/framework/core/global" |
12 |
| -// "infini.sh/framework/core/kv" |
13 |
| -// "infini.sh/framework/core/model" |
14 |
| -// "strconv" |
15 |
| -// "strings" |
16 |
| -// "sync" |
17 |
| -// "time" |
18 |
| -//) |
19 |
| -// |
20 |
| -//type ManagedConfig struct { |
21 |
| -// Manager *Manager `config:"manager"` |
22 |
| -//} |
23 |
| -// |
24 |
| -//type Manager struct { |
25 |
| -// Endpoint string `config:"endpoint"` |
26 |
| -//} |
27 |
| -// |
28 |
| -//var agentConfig *ManagedConfig |
29 |
| -//var hostInfo *model.Instance |
30 |
| -//var hostInfoObserver []func(newHostInfo *model.Instance) |
31 |
| -//var instanceLock sync.RWMutex |
32 |
| -// |
33 |
| -//const ( |
34 |
| -// UrlUploadInstanceInfo string = "/agent/instance" |
35 |
| -// UrlUpdateInstanceInfo = "/agent/instance/:instance_id" |
36 |
| -// UrlHearBeat = "/agent/instance/:instance_id/_heartbeat" |
37 |
| -// UrlGetInstanceInfo = "/agent/instance/:instance_id" |
38 |
| -//) |
39 |
| -// |
40 |
| -//func InitConfig() { |
41 |
| -// appConfig := &ManagedConfig{} |
42 |
| -// ok, err := env.ParseConfig("configs", appConfig) |
43 |
| -// if err != nil { |
44 |
| -// panic(err) |
45 |
| -// } |
46 |
| -// if !ok { |
47 |
| -// panic("config.InitConfig: can not find agent config") |
48 |
| -// } |
49 |
| -// agentConfig = appConfig |
50 |
| -// hostInfoObserver = make([]func(newHostInfo *model.Instance), 1) |
51 |
| -//} |
52 |
| -// |
53 |
| -//func RegisterHostInfoObserver(fn func(newHostInfo *model.Instance)) { |
54 |
| -// hostInfoObserver = append(hostInfoObserver, fn) |
55 |
| -//} |
56 |
| -// |
57 |
| -//func NotifyHostInfoObserver(newHostInfo *model.Instance) { |
58 |
| -// for i := 0; i < len(hostInfoObserver); i++ { |
59 |
| -// if hostInfoObserver[i] != nil { |
60 |
| -// hostInfoObserver[i](newHostInfo) |
61 |
| -// } |
62 |
| -// } |
63 |
| -//} |
64 |
| -// |
65 |
| -//func GetManagerEndpoint() string { |
66 |
| -// if agentConfig == nil || agentConfig.Manager == nil { |
67 |
| -// return "" |
68 |
| -// } |
69 |
| -// endPoint := agentConfig.Manager.Endpoint |
70 |
| -// if strings.HasSuffix(endPoint, "/") { |
71 |
| -// endPoint = endPoint[:len(endPoint)-1] |
72 |
| -// } |
73 |
| -// return agentConfig.Manager.Endpoint |
74 |
| -//} |
75 |
| -// |
76 |
| -//func GetListenPort() uint { |
77 |
| -// bindAddress := global.Env().SystemConfig.APIConfig.NetworkConfig.Binding |
78 |
| -// if strings.Contains(bindAddress, ":") { |
79 |
| -// temps := strings.Split(bindAddress, ":") |
80 |
| -// port, _ := strconv.Atoi(temps[1]) |
81 |
| -// return uint(port) |
82 |
| -// } |
83 |
| -// return 0 |
84 |
| -//} |
85 |
| -// |
86 |
| -//func IsHTTPS() bool { |
87 |
| -// return global.Env().SystemConfig.APIConfig.TLSConfig.TLSEnabled |
88 |
| -//} |
89 |
| -// |
90 |
| -//func IsAgentActivated() bool { |
91 |
| -// instanceInfo := GetInstanceInfo() |
92 |
| -// if instanceInfo == nil || !instanceInfo.IsRunning { |
93 |
| -// return false |
94 |
| -// } |
95 |
| -// return true |
96 |
| -//} |
97 |
| -// |
98 |
| -//func GetInstanceInfo() *model.Instance { |
99 |
| -// if hostInfo != nil { |
100 |
| -// return hostInfo |
101 |
| -// } |
102 |
| -// hostInfo = getInstanceInfoFromKV() |
103 |
| -// return hostInfo |
104 |
| -//} |
105 |
| -// |
106 |
| -////func GetOrInitInstanceInfo() *model.Instance { |
107 |
| -//// if hostInfo != nil { |
108 |
| -//// return hostInfo |
109 |
| -//// } |
110 |
| -//// hostInfo = getInstanceInfoFromKV() |
111 |
| -//// if hostInfo == nil { |
112 |
| -//// hostInfo = &model.Instance{ |
113 |
| -//// IPs: util.GetLocalIPs(), |
114 |
| -//// Host: agent.HostInfo{}, |
115 |
| -//// } |
116 |
| -//// _, majorIp, _, err := util.GetPublishNetworkDeviceInfo(agentConfig.MajorIpPattern) |
117 |
| -//// if err != nil { |
118 |
| -//// log.Error(err) |
119 |
| -//// } |
120 |
| -//// hostInfo.MajorIP = majorIp |
121 |
| -//// } |
122 |
| -//// SetInstanceInfo(hostInfo) |
123 |
| -//// return hostInfo |
124 |
| -////} |
125 |
| -// |
126 |
| -//func UpdateAgentBootTime() { |
127 |
| -// instanceInfo := GetInstanceInfo() |
128 |
| -// instanceInfo.BootTime = time.Now().UnixMilli() |
129 |
| -// SetInstanceInfo(instanceInfo) |
130 |
| -//} |
131 |
| -// |
132 |
| -//func SetInstanceInfo(host *model.Instance) error { |
133 |
| -// if host == nil { |
134 |
| -// return errors.New("host info can not be nil") |
135 |
| -// } |
136 |
| -// instanceLock.Lock() |
137 |
| -// defer instanceLock.Unlock() |
138 |
| -// hostInfo = host |
139 |
| -// event.UpdateAgentID(hostInfo.AgentID) |
140 |
| -// hostByte, _ := json.Marshal(host) |
141 |
| -// if host.IsRunning { |
142 |
| -// NotifyHostInfoObserver(hostInfo) |
143 |
| -// } |
144 |
| -// return kv.AddValue(model.KVInstanceBucket, []byte(model.KVInstanceInfo), hostByte) |
145 |
| -//} |
146 |
| -// |
147 |
| -//func SetInstanceInfoNoNotify(host *model.Instance) error { |
148 |
| -// if host == nil { |
149 |
| -// return errors.New("host info can not be nil") |
150 |
| -// } |
151 |
| -// |
152 |
| -// instanceLock.Lock() |
153 |
| -// defer instanceLock.Unlock() |
154 |
| -// hostInfo = host |
155 |
| -// event.UpdateAgentID(hostInfo.AgentID) |
156 |
| -// hostByte, _ := json.Marshal(host) |
157 |
| -// return kv.AddValue(model.KVInstanceBucket, []byte(model.KVInstanceInfo), hostByte) |
158 |
| -//} |
159 |
| -// |
160 |
| -//func DeleteInstanceInfo() error { |
161 |
| -// hostInfo = nil |
162 |
| -// return kv.DeleteKey(model.KVInstanceBucket, []byte(model.KVInstanceInfo)) |
163 |
| -//} |
164 |
| -// |
165 |
| -//func ReloadHostInfo() { |
166 |
| -// hostInf := getInstanceInfoFromKV() |
167 |
| -// if hostInf == nil { |
168 |
| -// return |
169 |
| -// } |
170 |
| -// ret, _ := json.Marshal(hostInf) |
171 |
| -// log.Debugf(string(ret)) |
172 |
| -//} |
173 |
| -// |
174 |
| -//var host *model.Instance |
175 |
| -// |
176 |
| -//func getInstanceInfoFromKV() *model.Instance { |
177 |
| -// hs, err := kv.GetValue(model.KVInstanceBucket, []byte(model.KVInstanceInfo)) |
178 |
| -// if err != nil { |
179 |
| -// log.Error(err) |
180 |
| -// return nil |
181 |
| -// } |
182 |
| -// if hs == nil { |
183 |
| -// return nil |
184 |
| -// } |
185 |
| -// err = json.Unmarshal(hs, &host) |
186 |
| -// if err != nil { |
187 |
| -// log.Errorf("config.getInstanceInfoFromKV: %v\n", err) |
188 |
| -// return nil |
189 |
| -// } |
190 |
| -// return host |
191 |
| -//} |
0 commit comments