/
getcvmlist.go
390 lines (373 loc) · 11.4 KB
/
getcvmlist.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
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
package main
import "fmt"
import "strconv"
import "net/http"
import "encoding/json"
import sjson "github.com/bitly/go-simplejson"
import log "github.com/kdar/factorlog"
import "io/ioutil"
import "os"
import "runtime"
import "sync"
import "time"
import "math/rand"
import "bytes"
import "cvmcode"
type Log struct {
Level int
File string
}
type CgwConfig struct {
Gz string // guangzhou
Sh string // shanghai
Hk string // hongkong
Ca string // north america
CgwTimeout int // cgw timeout
ListenPort int // listening port
Log Log // log
}
// cgw config
var cgw_conf CgwConfig
func init() {
// default config file
config_file := "getcvmlist.conf"
if len(os.Args) == 2 {
config_file = os.Args[1]
}
conf_data, err := ioutil.ReadFile(config_file)
if err != nil {
fmt.Printf("read config file fail : %s\n", err)
os.Exit(1)
}
if err := json.Unmarshal(conf_data, &cgw_conf); err != nil {
fmt.Printf("decode config file fail : %s\n", err)
os.Exit(1)
}
// use default port if needed
if cgw_conf.ListenPort <= 0 {
cgw_conf.ListenPort = 20487
}
fmt.Println(cgw_conf.Gz)
fmt.Println(cgw_conf.Sh)
fmt.Println(cgw_conf.Hk)
fmt.Println(cgw_conf.Ca)
fmt.Println("listen port : ", cgw_conf.ListenPort)
fmt.Println("log file : ", cgw_conf.Log.File)
fmt.Println("log level: ", cgw_conf.Log.Level)
logFile, err := os.OpenFile(cgw_conf.Log.File, os.O_CREATE|os.O_WRONLY|os.O_APPEND, 0666)
if err != nil {
fmt.Printf("open log file %s fail, err : %s\n", cgw_conf.Log.File, err)
os.Exit(1)
}
log.SetOutput(logFile)
log.SetFormatter(log.NewStdFormatter("%{Date} %{Time \"15:04:05.000000\"} %{severity}" + " %{File}:%{Line}:%{Function} %{Message}"))
log.SetMinMaxSeverity(log.Severity(cgw_conf.Log.Level), log.PANIC)
runtime.GOMAXPROCS(runtime.NumCPU())
}
func parseParams(r *http.Request) (int, string, string) {
r.ParseForm()
app_id, err := strconv.Atoi(r.FormValue("app_id"))
if err != nil {
return -1, "", ""
}
owner_uin := r.FormValue("owner_uin")
district := r.FormValue("district")
return app_id, owner_uin, district
}
type Resp0 struct {
code int
message string
data string
}
type Resp1 struct {
code int
message string
district string
data []interface{}
}
func preparePostData(start_idx, end_idx, app_id int, owner_uin string) (string, int) {
// params
params := sjson.New()
params.Set("appId", app_id)
params.Set("uin", owner_uin)
params.Set("startNum", start_idx)
params.Set("endNum", end_idx)
params.Set("simplify", 1)
params.Set("allType", 1)
// interface data
interface_data := sjson.New()
interface_data.Set("interfaceName", "qcloud.Qcvm.getCvmList")
interface_data.Set("para", params)
// post data in json format
post_data := sjson.New()
// unix timestamp
cur_time := time.Now().Unix()
// random number as event id
eventId := rand.New(rand.NewSource(cur_time)).Intn(6553500)
post_data.Set("version", "1.0")
post_data.Set("caller", "CGW")
post_data.Set("callee", "CGW")
post_data.Set("eventId", eventId)
post_data.Set("timestamp", cur_time)
post_data.Set("interface", interface_data)
post_data.Set("postOperation", make([]string, 0))
bytes, err := post_data.Encode()
if err != nil {
return "", 1
}
return string(bytes), 0
}
// http://gz.cgateway.tencentyun.com/interfaces/interface.php
// http://sh.cgateway.tencentyun.com/interfaces/interface.php
// http://hk.cgateway.tencentyun.com/interfaces/interface.php
// http://ca.cgateway.tencentyun.com/interfaces/interface.php
func getDistrictNameFromInterface(interface_name string) string {
if len(interface_name) < 9 {
return "invalid_interface_name"
}
return interface_name[7:9]
}
// pack request and send request to cgw
// in any case, there should be a resp written to response channel
// if cgw returns ok, raw data from cgw is stored in resp0.data
func requestInterface(start_idx, end_idx, app_id int, owner_uin, interface_name string, ch chan<- Resp0) {
resp := Resp0 {
code: cvmcode.OK,
message: "ok",
data: "",
}
// prepare request data
post_data, errcode := preparePostData(start_idx, end_idx, app_id, owner_uin)
if errcode != 0 {
resp.code = cvmcode.PREPARE_POST_DATA_FAIL
resp.message = "prepare post data fail"
ch <- resp
return
}
log.Infof("req %s\n%s\n", interface_name, post_data)
postBytesReader := bytes.NewReader([]byte(post_data))
// http client without timeout
client := &http.Client{}
// client := &http.Client{
// Timeout: cgw_conf.CgwTimeout}
request, err := http.NewRequest("POST", interface_name, postBytesReader)
if err != nil {
resp.code = cvmcode.NEWREQUEST_FAIL
resp.message = "http.NewRequest fail"
ch <- resp
return
}
response, err := client.Do(request)
if err != nil {
resp.code = cvmcode.DO_REQUEST_FAIL
resp.message = "http client: do request to cgw fail"
ch <- resp
return
}
defer response.Body.Close()
if response.StatusCode != 200 {
resp.code = cvmcode.CGW_HTTP_RETRUNCODE_NOT_200
resp.message = "http client: cgw http return code not 200"
ch <- resp
return
}
body, err := ioutil.ReadAll(response.Body)
if err != nil {
resp.code = cvmcode.READ_RESPONSE_FAIL
resp.message = "http: read response from cgw fail"
ch <- resp
return
}
resp.data = string(body);
log.Infof("resp from %s\n%s\n", interface_name, resp.data)
// response ok
// do not parse response here
// check detailed response info in collecting procedure
ch <- resp
}
// read from cgw response channel, deal with it
// return = 0 : done
// return > 0 : check if need get remaining cvms
func dealCgwResponse(ch <-chan Resp0, resp1 *Resp1) (int) {
var resp0 Resp0
select {
case resp0 = <-ch:
case <-time.After(time.Second * 2):
// timeout : 2 seconds
// set code & msg
resp1.code = cvmcode.CGW_TIMEOUT
resp1.message = "cgw timeout"
return 0
}
resp1.code = resp0.code
resp1.message = resp0.message
// directly return if any error occurs in http request
if resp1.code != cvmcode.OK {
return 0
}
// parse cgw response
cgw_jdata, err := sjson.NewJson([]byte(resp0.data))
if err != nil {
resp1.code = cvmcode.PARSE_CGW_RESPONSE__FAIL
resp1.message = "parse cgw response fail"
return 0
}
resp1.code = cgw_jdata.Get("returnCode").MustInt(cvmcode.CGW_NO_RETURNCODE)
// directly return if cgw returns error
if resp1.code != cvmcode.OK {
resp1.message = cgw_jdata.Get("returnMessage").MustString("on return msg")
return 0
}
// get cvm data
cvm_lists := cgw_jdata.GetPath("data", "deviceList").MustArray(make([]interface{},0));
// append to cvm list
for _, cvm := range cvm_lists {
resp1.data = append(resp1.data, cvm)
}
// return total number
return cgw_jdata.GetPath("data", "totalNum").MustInt(0)
}
// return 4 slices
// code message district devicelist
func processGetCvmList(app_id int, owner_uin, district string) ([]int, []string, []string, []interface{}) {
code_list := make([]int, 0)
message_list := make([]string, 0)
district_list := make([]string, 0)
device_list := make([]interface{}, 0)
// calling all the interfaces if district == "all"
var interface_list []string
switch district {
case "gz":
interface_list = []string{cgw_conf.Gz}
case "sh":
interface_list = []string{cgw_conf.Sh}
case "hk":
interface_list = []string{cgw_conf.Hk}
case "ca":
interface_list = []string{cgw_conf.Ca}
case "all":
interface_list = []string{cgw_conf.Gz, cgw_conf.Sh, cgw_conf.Hk, cgw_conf.Ca}
default:
log.Error("invalid district -> ", district)
// return empty list
return code_list, message_list, district_list, device_list
}
interface_num := len(interface_list)
// wait for all the interface to return
collect_ch := make(chan Resp1, interface_num)
var wait_group sync.WaitGroup
wait_group.Add(interface_num)
// calling multiple interfaces concurrently
for _, interface_name := range interface_list {
// each with a single goroutine
go func(interface_name string) {
defer wait_group.Done()
// get the first 1024 cvms
start_idx := 0
end_idx := 1023
// for timeout control and result retrive
ch := make(chan Resp0, 1)
// POST request in goroutine
// response should be written to ch
go requestInterface(start_idx, end_idx, app_id, owner_uin, interface_name, ch)
// no data
resp1 := Resp1 {
district: getDistrictNameFromInterface(interface_name),
data: make([]interface{}, 0),
}
// expect response from ch
total_cvm_num := dealCgwResponse(ch, &resp1)
start_idx = end_idx + 1
end_idx = total_cvm_num - 1
// no more cvms
if end_idx < start_idx {
collect_ch <- resp1
return
}
// get the ramaining cvms in another goroutine
go requestInterface(start_idx, end_idx, app_id, owner_uin, interface_name, ch)
dealCgwResponse(ch, &resp1)
// response anyway
collect_ch <- resp1
} (interface_name)
}
// wait for all the interface to return
wait_group.Wait()
collect_result_done := false
// iterate until all data are collected
for collect_result_done==false {
select {
case final_result := <-collect_ch:
log.Info("collect -> ", final_result)
// collect cvms in data
for _, cvm := range final_result.data {
device_list = append(device_list, cvm)
}
// collect code & message & district
code_list = append(code_list, final_result.code)
message_list = append(message_list, final_result.message)
district_list = append(district_list, final_result.district)
default:
// done
collect_result_done = true
}
}
return code_list, message_list, district_list, device_list
}
func packResponse(returnCode int, returnMessage string, code_list []int, message_list, district_list []string, data interface{}) []byte{
resp := sjson.New()
resp.Set("returnCode", returnCode)
resp.Set("returnMessage", returnMessage)
resp.Set("data", data)
resp.Set("codes", code_list)
resp.Set("msgs", message_list)
resp.Set("districts", district_list)
resp_str, err := resp.EncodePretty()
if err != nil {
log.Error("EncodePretty error")
resp_str = []byte("EncodePretty error")
}
return resp_str
}
func getCvmList(w http.ResponseWriter, req *http.Request) {
// parse parameters
app_id, owner_uin, district := parseParams(req)
var resp_str []byte
if app_id < 0 {
// fail
log.Error("invalid app_id : ", app_id)
resp_str = packResponse(cvmcode.PARAM_ERR, "param error", nil, nil, nil, nil);
w.Write(resp_str)
return
}
log.Infof("get req -> app_id : %d owner_uin : %s district : %s\n", app_id, owner_uin, district)
// expect cvm list
code_list, message_list, district_list, device_list := processGetCvmList(app_id, owner_uin, district)
log.Info("final code_list => ", code_list)
log.Info("final message_list => ", message_list)
log.Info("final district_list => ", district_list)
log.Info("final device_list => ", device_list)
final_return_code := cvmcode.CGW_INTERFACE_ALL_FAIL
final_return_msg := "all cgw interface failed"
// return ok if there are any interface returns ok
for _, rt_code := range code_list {
if rt_code == cvmcode.OK {
final_return_code = cvmcode.OK
final_return_msg = "ok"
break
}
}
device_num := len(device_list)
cvms := sjson.New()
cvms.Set("totalNum", device_num)
cvms.Set("deviceList", device_list)
// response
resp_str = packResponse(final_return_code, final_return_msg, code_list, message_list, district_list, cvms)
w.Write(resp_str)
}
func main() {
http.HandleFunc("/getcvmlist", getCvmList)
listening := fmt.Sprintf(":%d", cgw_conf.ListenPort)
fmt.Println("now listening on ", listening)
http.ListenAndServe(listening, nil)
}