forked from huawei-openlab/oct-engine
/
main.go
226 lines (198 loc) · 5.45 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
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
package main
import (
"bytes"
"encoding/json"
"fmt"
"io/ioutil"
"net/http"
"os"
"path"
"strings"
"github.com/Sirupsen/logrus"
"github.com/drone/routes"
"github.com/huawei-openlab/oct-engine/liboct"
)
type SchedulerConfig struct {
Port int
ServerListFile string
Debug bool
}
func GetTaskStatus(w http.ResponseWriter, r *http.Request) {
//TODO
}
func GetTaskReport(w http.ResponseWriter, r *http.Request) {
db := liboct.GetDefaultDB()
id := r.URL.Query().Get(":ID")
sInterface, err := db.Get(liboct.DBScheduler, id)
if err != nil {
liboct.RenderError(w, err)
return
}
s, _ := liboct.SchedulerFromString(sInterface.String())
//Send the collect command to the octd
if err := s.Command(liboct.TestActionCollect); err != nil {
db.Update(liboct.DBScheduler, id, s)
liboct.RenderError(w, err)
return
} else {
db.Update(liboct.DBScheduler, id, s)
}
//Tar the reports in the cacheDir
reportURL := path.Join(strings.TrimSuffix(s.Case.GetBundleURL(), ".tar.gz"), "source", "reports.tar.gz")
found := false
logrus.Debugf("Get reportURL %s", reportURL)
_, err = os.Stat(reportURL)
if err != nil {
logrus.Debugf("Regenerate the report")
var reports []string
for index := 0; index < len(s.Case.Units); index++ {
if len(s.Case.Units[index].ReportURL) > 0 {
reports = append(reports, s.Case.Units[index].ReportURL)
}
//Add log
reports = append(reports, fmt.Sprintf("%s.log", s.Case.Units[index].Name))
}
tarDir := path.Join(strings.TrimSuffix(s.Case.GetBundleURL(), ".tar.gz"), "source")
reportURL, found = liboct.TarFileList(reports, tarDir, "reports")
}
if !found {
liboct.RenderError(w, err)
return
}
file, err := os.Open(reportURL)
defer file.Close()
if err != nil {
liboct.RenderError(w, err)
} else {
buf := bytes.NewBufferString("")
buf.ReadFrom(file)
//Different write back, not json
w.Write([]byte(buf.String()))
}
}
func ReceiveTaskCommand(w http.ResponseWriter, r *http.Request) {
db := liboct.GetDefaultDB()
id := r.URL.Query().Get(":ID")
sInterface, err := db.Get(liboct.DBScheduler, id)
if err != nil {
liboct.RenderError(w, err)
return
}
s, _ := liboct.SchedulerFromString(sInterface.String())
result, _ := ioutil.ReadAll(r.Body)
logrus.Debugf("Receive task Command %s", string(result))
r.Body.Close()
/* Donnot use this now FIXME
var cmd liboct.TestActionCommand
json.Unmarshal([]byte(result), &cmd)
*/
action, err := liboct.TestActionFromString(string(result))
if err != nil {
liboct.RenderError(w, err)
return
}
err = s.Command(action)
db.Update(liboct.DBScheduler, id, s)
if err != nil {
liboct.RenderError(w, err)
} else {
liboct.RenderOK(w, "", nil)
}
}
// Since the sheduler ID is got after receiving the test files
// we need to move it to a better place
// /tmp/.../A.tar.gz --> /tmp/.../id/A.tar.gz
func updateSchedulerBundle(id string, oldURL string) {
db := liboct.GetDefaultDB()
sInterface, _ := db.Get(liboct.DBScheduler, id)
s, _ := liboct.SchedulerFromString(sInterface.String())
dir := path.Dir(path.Dir(oldURL))
newURL := fmt.Sprintf("%s/%s", path.Join(dir, id), path.Base(oldURL))
liboct.PreparePath(path.Join(dir, id), "")
logrus.Debugf("Old URL %s, New URL %s", oldURL, newURL)
os.Rename(strings.TrimSuffix(oldURL, ".tar.gz"), strings.TrimSuffix(newURL, ".tar.gz"))
os.Rename(oldURL, newURL)
//bundleURL is the directory of the bundle
s.Case.SetBundleURL(strings.TrimSuffix(newURL, ".tar.gz"))
db.Update(liboct.DBScheduler, id, s)
os.RemoveAll(path.Dir(oldURL))
}
func ReceiveTask(w http.ResponseWriter, r *http.Request) {
logrus.Debugf("ReceiveTask begin")
var tc liboct.TestCase
db := liboct.GetDefaultDB()
realURL, _ := liboct.ReceiveFile(w, r, liboct.SchedulerCacheDir)
tc, err := liboct.CaseFromTar(realURL, "")
if err != nil {
liboct.RenderError(w, err)
return
}
s, _ := liboct.SchedulerNew(tc)
err = s.Command(liboct.TestActionApply)
if err == nil {
if id, e := db.Add(liboct.DBScheduler, s); e == nil {
updateSchedulerBundle(id, realURL)
liboct.RenderOK(w, id, nil)
} else {
liboct.RenderError(w, e)
}
} else {
liboct.RenderError(w, err)
}
}
// Will use DB in the future, (mongodb for example)
func init() {
sf, err := os.Open("scheduler.conf")
if err != nil {
logrus.Fatal(err)
return
}
defer sf.Close()
if err = json.NewDecoder(sf).Decode(&pubConfig); err != nil {
logrus.Fatal(err)
return
}
if pubConfig.Debug {
logrus.SetLevel(logrus.DebugLevel)
} else {
logrus.SetLevel(logrus.InfoLevel)
}
db := liboct.GetDefaultDB()
db.RegistCollect(liboct.DBResource)
db.RegistCollect(liboct.DBScheduler)
if len(pubConfig.ServerListFile) == 0 {
return
}
slf, err := os.Open(pubConfig.ServerListFile)
if err != nil {
return
}
defer slf.Close()
var rl []liboct.Resource
if err = json.NewDecoder(slf).Decode(&rl); err != nil {
return
}
for index := 0; index < len(rl); index++ {
if _, e := db.Add(liboct.DBResource, rl[index]); e != nil {
logrus.Warn(e)
}
}
}
var pubConfig SchedulerConfig
func main() {
mux := routes.New()
mux.Get("/resource", GetResource)
mux.Post("/resource", PostResource)
mux.Get("/resource/:ID", GetResourceStatus)
mux.Post("/task", ReceiveTask)
mux.Get("/task/:ID", GetTaskStatus)
mux.Post("/task/:ID", ReceiveTaskCommand)
mux.Get("/task/:ID/report", GetTaskReport)
logrus.Infof("Start to listen :%d", pubConfig.Port)
http.Handle("/", mux)
port := fmt.Sprintf(":%d", pubConfig.Port)
err := http.ListenAndServe(port, nil)
if err != nil {
logrus.Fatal(err)
}
}